示例#1
0
文件: tsdbexec.go 项目: keep94/scotty
func query(
	request *tsdbjson.QueryRequest,
	endpoints *datastructs.ApplicationStatuses,
	minDownSampleTime time.Duration) (
	result []tsdbjson.TimeSeries, err error) {
	parsedQueries, err := tsdbjson.ParseQueryRequest(request)
	if err != nil {
		return
	}
	var allSeries []tsdbjson.TimeSeries
	for i := range parsedQueries {
		var options tsdbimpl.QueryOptions
		options.HostNameFilter, err = newTagFilter(
			parsedQueries[i].Options.HostNameFilter)
		if err != nil {
			return
		}
		options.AppNameFilter, err = newTagFilter(
			parsedQueries[i].Options.AppNameFilter)
		if err != nil {
			return
		}
		options.GroupByAppName = parsedQueries[i].Options.GroupByAppName
		options.GroupByHostName = parsedQueries[i].Options.GroupByHostName
		if parsedQueries[i].Aggregator.DownSample == nil {
			return nil, tsdbjson.ErrUnsupportedAggregator
		}
		ensureDurationAtLeast(
			duration.ToFloat(minDownSampleTime),
			&parsedQueries[i].Aggregator.DownSample)
		var aggregatorGen tsdb.AggregatorGenerator
		aggregatorGen, err = tsdbjson.NewAggregatorGenerator(
			parsedQueries[i].Aggregator.Type,
			parsedQueries[i].Aggregator.DownSample,
			parsedQueries[i].Aggregator.RateOptions,
		)
		if err != nil {
			return
		}
		var series *tsdb.TaggedTimeSeriesSet
		series, err = tsdbimpl.Query(
			endpoints,
			parsedQueries[i].Metric,
			aggregatorGen,
			parsedQueries[i].Start,
			parsedQueries[i].End,
			&options)
		if err != nil {
			return
		}
		allSeries = append(allSeries, tsdbjson.NewTimeSeriesSlice(series)...)
	}
	if allSeries == nil {
		return make([]tsdbjson.TimeSeries, 0), nil
	}
	return allSeries, nil
}
示例#2
0
func TestParseQueryBadRequest(t *testing.T) {
	request := &tsdbjson.QueryRequest{
		StartInMillis: 1456789123125,
		EndInMillis:   1511789123125,
		Queries: []*tsdbjson.Query{
			{
				Metric:     "Hi_20there",
				Aggregator: "avg",
				DownSample: "abadvalue",
			},
		},
	}
	_, err := tsdbjson.ParseQueryRequest(request)
	assertValueEquals(t, tsdbjson.ErrBadValue, err)
}
示例#3
0
func unescape(orig string) string {
	request := &tsdbjson.QueryRequest{
		StartInMillis: 1456789123125,
		EndInMillis:   1511789123125,
		Queries: []*tsdbjson.Query{
			{
				Metric:     orig,
				Aggregator: "avg",
				DownSample: "15m-avg-zero",
			},
		},
	}
	parsedRequests, err := tsdbjson.ParseQueryRequest(request)
	if err != nil {
		panic(err)
	}
	return parsedRequests[0].Metric
}
示例#4
0
func TestParseQueryRequest(t *testing.T) {
	request := &tsdbjson.QueryRequest{
		StartInMillis: 1456789123125,
		EndInMillis:   1511789123125,
		Queries: []*tsdbjson.Query{
			{
				Metric:     "Hi_20there",
				Aggregator: "avg",
				DownSample: "15m-avg-zero",
			},
			{
				Metric:     "What_27s_20up",
				Aggregator: "sum",
				DownSample: "10m-avg",
				Filters: []*tsdbjson.Filter{
					{
						Type:    "literal_or",
						Tagk:    "HostName",
						Filter:  "some_20Host",
						GroupBy: true,
					},
					{
						Type:    "wildcard",
						Tagk:    "appname",
						Filter:  "some_20App",
						GroupBy: false,
					},
				},
			},
			{
				Metric:     "AThird",
				Aggregator: "prod",
			},
		},
	}
	parsedRequests, err := tsdbjson.ParseQueryRequest(request)
	if err != nil {
		t.Fatal(err)
	}
	expected := []tsdbjson.ParsedQuery{
		{
			Metric: "Hi there",
			Aggregator: tsdbjson.AggregatorSpec{
				Type: "avg",
				DownSample: &tsdbjson.DownSampleSpec{
					DurationInSeconds: 900.0,
					Type:              "avg",
					Fill:              "zero",
				},
			},
			Start: 1456789123.125,
			End:   1511789123.125,
		},
		{
			Metric: "What's up",
			Aggregator: tsdbjson.AggregatorSpec{
				Type: "sum",
				DownSample: &tsdbjson.DownSampleSpec{
					DurationInSeconds: 600.0,
					Type:              "avg",
				},
			},
			Start: 1456789123.125,
			End:   1511789123.125,
			Options: tsdbjson.ParsedQueryOptions{
				HostNameFilter: &tsdbjson.FilterSpec{
					Type:  "literal_or",
					Value: "some_20Host",
				},
				AppNameFilter: &tsdbjson.FilterSpec{
					Type:  "wildcard",
					Value: "some_20App",
				},
				GroupByHostName: true,
			},
		},
		{
			Metric: "AThird",
			Aggregator: tsdbjson.AggregatorSpec{
				Type: "prod",
			},
			Start: 1456789123.125,
			End:   1511789123.125,
		},
	}
	assertValueDeepEquals(t, expected, parsedRequests)
}