1package graphite
2
3import (
4	"encoding/json"
5	"io/ioutil"
6	"net/http"
7	"reflect"
8	"strings"
9	"testing"
10	"time"
11
12	"github.com/grafana/grafana-plugin-sdk-go/data"
13	"github.com/grafana/grafana/pkg/infra/log"
14	"github.com/stretchr/testify/assert"
15	"github.com/stretchr/testify/require"
16)
17
18func TestFixIntervalFormat(t *testing.T) {
19	testCases := []struct {
20		name     string
21		target   string
22		expected string
23	}{
24		{
25			name:     "should transform 1m to graphite unit (1min) when used as interval string",
26			target:   "aliasByNode(hitcount(averageSeries(app.grafana.*.dashboards.views.count), '1m'), 4)",
27			expected: "aliasByNode(hitcount(averageSeries(app.grafana.*.dashboards.views.count), '1min'), 4)",
28		},
29		{
30			name:     "should transform 1M to graphite unit (1mon) when used as interval string",
31			target:   "aliasByNode(hitcount(averageSeries(app.grafana.*.dashboards.views.count), '1M'), 4)",
32			expected: "aliasByNode(hitcount(averageSeries(app.grafana.*.dashboards.views.count), '1mon'), 4)",
33		},
34		{
35			name:     "should not transform 1m when not used as interval string",
36			target:   "app.grafana.*.dashboards.views.1m.count",
37			expected: "app.grafana.*.dashboards.views.1m.count",
38		},
39		{
40			name:     "should not transform 1M when not used as interval string",
41			target:   "app.grafana.*.dashboards.views.1M.count",
42			expected: "app.grafana.*.dashboards.views.1M.count",
43		},
44	}
45
46	for _, tc := range testCases {
47		t.Run(tc.name, func(t *testing.T) {
48			tr := fixIntervalFormat(tc.target)
49			assert.Equal(t, tc.expected, tr)
50		})
51	}
52
53	service := &Service{logger: log.New("tsdb.graphite")}
54
55	t.Run("Converts response without tags to data frames", func(*testing.T) {
56		body := `
57		[
58			{
59				"target": "target",
60				"datapoints": [[50, 1], [null, 2], [100, 3]]
61			}
62		]`
63		a := 50.0
64		b := 100.0
65		expectedFrame := data.NewFrame("target",
66			data.NewField("time", nil, []time.Time{time.Unix(1, 0).UTC(), time.Unix(2, 0).UTC(), time.Unix(3, 0).UTC()}),
67			data.NewField("value", data.Labels{}, []*float64{&a, nil, &b}).SetConfig(&data.FieldConfig{DisplayNameFromDS: "target"}),
68		)
69		expectedFrames := data.Frames{expectedFrame}
70
71		httpResponse := &http.Response{StatusCode: 200, Body: ioutil.NopCloser(strings.NewReader(body))}
72		dataFrames, err := service.toDataFrames(httpResponse)
73
74		require.NoError(t, err)
75		if !reflect.DeepEqual(expectedFrames, dataFrames) {
76			expectedFramesJSON, _ := json.Marshal(expectedFrames)
77			dataFramesJSON, _ := json.Marshal(dataFrames)
78			t.Errorf("Data frames should have been equal but was, expected:\n%s\nactual:\n%s", expectedFramesJSON, dataFramesJSON)
79		}
80	})
81
82	t.Run("Converts response with tags to data frames", func(*testing.T) {
83		body := `
84		[
85			{
86				"target": "target",
87				"tags": { "fooTag": "fooValue", "barTag": "barValue", "int": 100, "float": 3.14 },
88				"datapoints": [[50, 1], [null, 2], [100, 3]]
89			}
90		]`
91		a := 50.0
92		b := 100.0
93		expectedFrame := data.NewFrame("target",
94			data.NewField("time", nil, []time.Time{time.Unix(1, 0).UTC(), time.Unix(2, 0).UTC(), time.Unix(3, 0).UTC()}),
95			data.NewField("value", data.Labels{
96				"fooTag": "fooValue",
97				"barTag": "barValue",
98				"int":    "100",
99				"float":  "3.14",
100			}, []*float64{&a, nil, &b}).SetConfig(&data.FieldConfig{DisplayNameFromDS: "target"}),
101		)
102		expectedFrames := data.Frames{expectedFrame}
103
104		httpResponse := &http.Response{StatusCode: 200, Body: ioutil.NopCloser(strings.NewReader(body))}
105		dataFrames, err := service.toDataFrames(httpResponse)
106
107		require.NoError(t, err)
108		if !reflect.DeepEqual(expectedFrames, dataFrames) {
109			expectedFramesJSON, _ := json.Marshal(expectedFrames)
110			dataFramesJSON, _ := json.Marshal(dataFrames)
111			t.Errorf("Data frames should have been equal but was, expected:\n%s\nactual:\n%s", expectedFramesJSON, dataFramesJSON)
112		}
113	})
114}
115