func NewPerfDashApi(index string) *PerfDashApi { return &PerfDashApi{ es: elasticsearch.NewElasticsearch(), Index: index, metrics: metrics.NewMetrics(), } }
func InsertTestData(index string) error { es := elasticsearch.NewElasticsearch() gen := TestTransactionsGenerator{ From: time.Now().Add(-10 * time.Millisecond), To: time.Now().Add(-1 * time.Microsecond), NrServices: 60, NrHosts: 10, RtMin: 0, RtMax: 1000, CountMin: 1, CountMax: 10, ErrorProb: 0.1, } transactions := gen.Generate() // make sure we start fresh _, err := es.DeleteIndex(index) if err != nil { return err } es.Refresh(index) err = InsertInto(es, index, transactions) if err != nil { return err } return nil }
"field": "responsetime", "percents": []float32{50}, }, }, })) }) }) }) Context("With test ES", func() { var es *elasticsearch.Elasticsearch var index_name string var api *PerfDashApi BeforeEach(func() { index_name = fmt.Sprintf("packetbeat-unittest-%v", os.Getpid()) es = elasticsearch.NewElasticsearch() _, err := es.DeleteIndex(index_name) Expect(err).To(BeNil()) es.Refresh(index_name) ts1, _ := elasticsearch.TimeParse("2015-01-02T15:04:05.000Z") ts2, _ := elasticsearch.TimeParse("2015-01-02T15:04:05.001Z") transactions := []testdata.TestTransaction{ testdata.TestTransaction{ Timestamp: ts1, Service: "service1", Host: "Host0", Count: 2, Responsetime: 2000,
func main() { to := time.Now() from := to.Add(-1 * time.Hour) samples := 100 options := gengenOptionsForServices(samples, to, from) gen, err := testdata.NewGenGen(*options) if err != nil { fmt.Println(err) return } transChan := make(chan testdata.TestTransaction, 100) index_name := "packetbeat-test" es := elasticsearch.NewElasticsearch() // make sure we start fresh _, err = es.DeleteIndex(index_name) if err != nil { fmt.Println("Error: ", err) return } // create index with custom settings resp, err := es.Request("PUT", index_name, "", bytes.NewBufferString( `{"settings": {"index": {"refresh_interval": "-1"}}}`, )) if err != nil { fmt.Println(elasticsearch.ResponseBody(resp)) fmt.Println("Error: ", err) return } // generate in channel go func() { for i := 0; i < samples; i++ { for _, trans := range gen.Generate(i) { transChan <- testdata.TestTransaction{ Timestamp: elasticsearch.Time(trans["timestamp"].(time.Time)), Service: trans["service"].(string), Host: trans["host"].(string), Count: int(trans["count"].(float64)), Responsetime: trans["responsetime"].(int), Status: trans["status"].(string), } } } close(transChan) }() inserted, err := testdata.InsertInEsFromChan(es, index_name, transChan) if err != nil { fmt.Println("Error: ", err) } fmt.Printf("%d transactions inserted into %s\n", inserted, index_name) // set back the refreshing interval _, err = es.Request("PUT", index_name, "_settings", bytes.NewBufferString( `{"index": {"refresh_interval": "1s"}}`, )) if err != nil { fmt.Println("Error: ", err) } }
func DeleteTestData(index string) error { es := elasticsearch.NewElasticsearch() _, err := es.DeleteIndex(index) es.Refresh(index) return err }
func (api *ByDimensionApi) Query(req *ByDimensionRequest) (*ByDimensionResponse, int, error) { var esreq EsByDimensionReq es := elasticsearch.NewElasticsearch() api.setRequestDefaults(req) req.interval_seconds = float32(time.Time(req.Timerange.To).Sub(time.Time(req.Timerange.From))) / 1e9 primary := &esreq.Aggs.Primary primary.Terms.Field = req.Config.Primary_dimension // set timestamp filter esreq.Query.Filtered = MapStr{} esreq.Query.Filtered["filter"] = esBuildTimeFilter( req.Config.Timestamp_field, req.Timerange) aggs, err := api.buildRequestAggs(req) if err != nil { return nil, 400, err } primary.Aggs = *aggs aggs, err = api.buildRequestHistogramAggs(req) if err != nil { return nil, 400, err } for k, v := range *aggs { primary.Aggs[k] = v } // up to here we assume there are client errors, from here on // it's on us. objreq, err := json.Marshal(&esreq) if err != nil { return nil, 500, err } //fmt.Println("Objreq=", string(objreq)) resp, err := es.Search(api.Index, "?search_type=count", string(objreq)) if err != nil { return nil, 500, err } defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) if err != nil { return nil, 500, err } var answ struct { Aggregations struct { Primary struct { Buckets []map[string]json.RawMessage } } } err = json.Unmarshal(body, &answ) if err != nil { return nil, 500, err } var response ByDimensionResponse response.Primary = []PrimaryDimension{} for _, bucket := range answ.Aggregations.Primary.Buckets { primary, err := api.bucketToPrimary(req, bucket) if err != nil { return nil, 500, err } response.Primary = append(response.Primary, *primary) } // if we got so far, we're successful response.Status = "ok" return &response, 200, nil }
func NewByDimensionApi(index string) *ByDimensionApi { return &ByDimensionApi{ es: elasticsearch.NewElasticsearch(), Index: index, } }