This repository has been archived by the owner on May 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
macro.go
109 lines (93 loc) · 3.18 KB
/
macro.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package flightsql
import (
"fmt"
"time"
"github.com/grafana/grafana-plugin-sdk-go/data/sqlutil"
)
var macros = sqlutil.Macros{
"dateBin": macroDateBin(""),
"dateBinAlias": macroDateBin("_binned"),
"interval": macroInterval,
"timeGroup": macroTimeGroup,
"timeGroupAlias": macroTimeGroupAlias,
// The behaviors of timeFrom and timeTo as defined in the SDK are different
// from all other Grafana SQL plugins. Instead we'll take their
// implementations, rename them and define timeFrom and timeTo ourselves.
"timeRangeFrom": sqlutil.DefaultMacros["timeFrom"],
"timeRangeTo": sqlutil.DefaultMacros["timeTo"],
"timeRange": sqlutil.DefaultMacros["timeFilter"],
"timeTo": macroTo,
"timeFrom": macroFrom,
}
func macroTimeGroup(query *sqlutil.Query, args []string) (string, error) {
if len(args) != 2 {
return "", fmt.Errorf("%w: expected 1 argument, received %d", sqlutil.ErrorBadArgumentCount, len(args))
}
column := args[0]
res := ""
switch args[1] {
case "minute":
res += fmt.Sprintf("datepart('minute', %s),", column)
fallthrough
case "hour":
res += fmt.Sprintf("datepart('hour', %s),", column)
fallthrough
case "day":
res += fmt.Sprintf("datepart('day', %s),", column)
fallthrough
case "month":
res += fmt.Sprintf("datepart('month', %s),", column)
fallthrough
case "year":
res += fmt.Sprintf("datepart('year', %s)", column)
}
return res, nil
}
func macroTimeGroupAlias(query *sqlutil.Query, args []string) (string, error) {
if len(args) != 2 {
return "", fmt.Errorf("%w: expected 1 argument, received %d", sqlutil.ErrorBadArgumentCount, len(args))
}
column := args[0]
res := ""
switch args[1] {
case "minute":
res += fmt.Sprintf("datepart('minute', %s) as %s_minute,", column, column)
fallthrough
case "hour":
res += fmt.Sprintf("datepart('hour', %s) as %s_hour,", column, column)
fallthrough
case "day":
res += fmt.Sprintf("datepart('day', %s) as %s_day,", column, column)
fallthrough
case "month":
res += fmt.Sprintf("datepart('month', %s) as %s_month,", column, column)
fallthrough
case "year":
res += fmt.Sprintf("datepart('year', %s) as %s_year", column, column)
}
return res, nil
}
func macroInterval(query *sqlutil.Query, _ []string) (string, error) {
return fmt.Sprintf("interval '%d second'", int64(query.Interval.Seconds())), nil
}
func macroFrom(query *sqlutil.Query, _ []string) (string, error) {
return fmt.Sprintf("cast('%s' as timestamp)", query.TimeRange.From.Format(time.RFC3339)), nil
}
func macroTo(query *sqlutil.Query, _ []string) (string, error) {
return fmt.Sprintf("cast('%s' as timestamp)", query.TimeRange.To.Format(time.RFC3339)), nil
}
func macroDateBin(suffix string) sqlutil.MacroFunc {
return func(query *sqlutil.Query, args []string) (string, error) {
if len(args) != 1 {
return "", fmt.Errorf("%w: expected 1 argument, received %d", sqlutil.ErrorBadArgumentCount, len(args))
}
column := args[0]
aliasing := func() string {
if suffix == "" {
return ""
}
return fmt.Sprintf(" as %s%s", column, suffix)
}()
return fmt.Sprintf("date_bin(interval '%d second', %s, timestamp '1970-01-01T00:00:00Z')%s", int64(query.Interval.Seconds()), column, aliasing), nil
}
}