示例#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 escape(orig string) string {
	timeSeriesSet := &tsdb.TaggedTimeSeriesSet{
		MetricName: orig,
		Data: []tsdb.TaggedTimeSeries{
			{
				Values: tsdb.TimeSeries{
					{2.0, 3.0},
				},
			},
		},
	}
	result := tsdbjson.NewTimeSeriesSlice(timeSeriesSet)
	return result[0].Metric
}
示例#3
0
func TestJson(t *testing.T) {
	timeSeriesSet := &tsdb.TaggedTimeSeriesSet{
		MetricName: "someMetric",
		Data: []tsdb.TaggedTimeSeries{
			{
				Values: tsdb.TimeSeries{
					{48.0, 72.0},
					{55.0, 93.0},
				},
			},
		},
	}
	expected := []tsdbjson.TimeSeries{
		{
			Metric:        "someMetric",
			Tags:          map[string]string{},
			AggregateTags: []string{"HostName", "appname"},
			Dps: tsdb.TimeSeries{
				{48.0, 72.0},
				{55.0, 93.0},
			},
		},
	}
	assertTimeSeriesSliceEquals(
		t, expected, tsdbjson.NewTimeSeriesSlice(timeSeriesSet))

	timeSeriesSet = &tsdb.TaggedTimeSeriesSet{
		MetricName: "someMetric",
		Data: []tsdb.TaggedTimeSeries{
			{
				Tags: tsdb.TagSet{
					HostName: "host1",
				},
				Values: tsdb.TimeSeries{
					{48.0, 72.0},
					{55.0, 93.0},
				},
			},
			{
				Tags: tsdb.TagSet{
					HostName: "host2",
				},
				Values: tsdb.TimeSeries{
					{21.0, 29.0},
				},
			},
		},
		GroupedByHostName: true,
	}
	expected = []tsdbjson.TimeSeries{
		{
			Metric:        "someMetric",
			Tags:          map[string]string{"HostName": "host1"},
			AggregateTags: []string{"appname"},
			Dps: tsdb.TimeSeries{
				{48.0, 72.0},
				{55.0, 93.0},
			},
		},
		{
			Metric:        "someMetric",
			Tags:          map[string]string{"HostName": "host2"},
			AggregateTags: []string{"appname"},
			Dps: tsdb.TimeSeries{
				{21.0, 29.0},
			},
		},
	}
	assertTimeSeriesSliceEquals(
		t, expected, tsdbjson.NewTimeSeriesSlice(timeSeriesSet))

	timeSeriesSet = &tsdb.TaggedTimeSeriesSet{
		MetricName: "someMetric",
		Data: []tsdb.TaggedTimeSeries{
			{
				Tags: tsdb.TagSet{
					AppName: "app1",
				},
				Values: tsdb.TimeSeries{
					{2.0, 3.0},
					{5.0, 8.0},
				},
			},
			{
				Tags: tsdb.TagSet{
					AppName: "app2",
				},
				Values: tsdb.TimeSeries{
					{13.0, 21.0},
				},
			},
		},
		GroupedByAppName: true,
	}
	expected = []tsdbjson.TimeSeries{
		{
			Metric:        "someMetric",
			Tags:          map[string]string{"appname": "app1"},
			AggregateTags: []string{"HostName"},
			Dps: tsdb.TimeSeries{
				{2.0, 3.0},
				{5.0, 8.0},
			},
		},
		{
			Metric:        "someMetric",
			Tags:          map[string]string{"appname": "app2"},
			AggregateTags: []string{"HostName"},
			Dps: tsdb.TimeSeries{
				{13.0, 21.0},
			},
		},
	}
	assertTimeSeriesSliceEquals(
		t, expected, tsdbjson.NewTimeSeriesSlice(timeSeriesSet))

	timeSeriesSet = &tsdb.TaggedTimeSeriesSet{
		MetricName: "someMetric",
		Data: []tsdb.TaggedTimeSeries{
			{
				Tags: tsdb.TagSet{
					HostName: "host1",
					AppName:  "app1",
				},
				Values: tsdb.TimeSeries{
					{101.0, 202.0},
				},
			},
			{
				Tags: tsdb.TagSet{
					HostName: "host2",
					AppName:  "app2",
				},
				Values: tsdb.TimeSeries{
					{303.0, 404.0},
				},
			},
		},
		GroupedByHostName: true,
		GroupedByAppName:  true,
	}
	expected = []tsdbjson.TimeSeries{
		{
			Metric: "someMetric",
			Tags: map[string]string{
				"HostName": "host1",
				"appname":  "app1",
			},
			AggregateTags: []string{},
			Dps: tsdb.TimeSeries{
				{101.0, 202.0},
			},
		},
		{
			Metric: "someMetric",
			Tags: map[string]string{
				"HostName": "host2",
				"appname":  "app2",
			},
			AggregateTags: []string{},
			Dps: tsdb.TimeSeries{
				{303.0, 404.0},
			},
		},
	}
	assertTimeSeriesSliceEquals(
		t, expected, tsdbjson.NewTimeSeriesSlice(timeSeriesSet))
}