func GetEventsQuery(query *m.GetEventsQuery) error { query.Result = make([]*schema.ProbeEvent, 0) esQuery := map[string]interface{}{ "query": map[string]interface{}{ "filtered": map[string]interface{}{ "filter": map[string]interface{}{ "and": []map[string]interface{}{ { "range": map[string]interface{}{ "timestamp": map[string]interface{}{ "gte": query.Start, "lte": query.End, }, }, }, { "term": map[string]int64{ "org_id": query.OrgId, }, }, }, }, "query": map[string]interface{}{ "query_string": map[string]string{ "query": query.Query, }, }, }, }, } out, err := es.Search("events", "", map[string]interface{}{"size": query.Size, "sort": "timestamp:desc"}, esQuery) if err != nil { return err } for _, hit := range out.Hits.Hits { var source schema.ProbeEvent err = json.Unmarshal(*hit.Source, &source) if err != nil { return err } query.Result = append(query.Result, &source) } return nil }
func GetEvents(c *middleware.Context, query m.GetEventsQuery) Response { query.OrgId = c.OrgId if query.End == 0 { query.End = time.Now().Unix() * 1000 } if query.Start == 0 { query.Start = query.End - (60 * 60 * 1000) //1hour } if query.Size == 0 { query.Size = 10 } if err := bus.Dispatch(&query); err != nil { return ApiError(500, "Failed to query events", err) } return Json(200, query.Result) }