forked from influxdata/influxdb
/
statement_executor.go
81 lines (69 loc) · 2.03 KB
/
statement_executor.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
package monitor
import (
"fmt"
"sort"
"github.com/influxdb/influxdb/influxql"
"github.com/influxdb/influxdb/models"
)
// StatementExecutor translates InfluxQL queries to Monitor methods.
type StatementExecutor struct {
Monitor interface {
Statistics(map[string]string) ([]*Statistic, error)
Diagnostics() (map[string]*Diagnostic, error)
}
}
// ExecuteStatement executes monitor-related query statements.
func (s *StatementExecutor) ExecuteStatement(stmt influxql.Statement) *influxql.Result {
switch stmt := stmt.(type) {
case *influxql.ShowStatsStatement:
return s.executeShowStatistics(stmt.Module)
case *influxql.ShowDiagnosticsStatement:
return s.executeShowDiagnostics(stmt.Module)
default:
panic(fmt.Sprintf("unsupported statement type: %T", stmt))
}
}
func (s *StatementExecutor) executeShowStatistics(module string) *influxql.Result {
stats, err := s.Monitor.Statistics(nil)
if err != nil {
return &influxql.Result{Err: err}
}
var rows []*models.Row
for _, stat := range stats {
if module != "" && stat.Name != module {
continue
}
row := &models.Row{Name: stat.Name, Tags: stat.Tags}
values := make([]interface{}, 0, len(stat.Values))
for _, k := range stat.valueNames() {
row.Columns = append(row.Columns, k)
values = append(values, stat.Values[k])
}
row.Values = [][]interface{}{values}
rows = append(rows, row)
}
return &influxql.Result{Series: rows}
}
func (s *StatementExecutor) executeShowDiagnostics(module string) *influxql.Result {
diags, err := s.Monitor.Diagnostics()
if err != nil {
return &influxql.Result{Err: err}
}
rows := make([]*models.Row, 0, len(diags))
// Get a sorted list of diagnostics keys.
sortedKeys := make([]string, 0, len(diags))
for k := range diags {
sortedKeys = append(sortedKeys, k)
}
sort.Strings(sortedKeys)
for _, k := range sortedKeys {
if module != "" && k != module {
continue
}
row := &models.Row{Name: k}
row.Columns = diags[k].Columns
row.Values = diags[k].Rows
rows = append(rows, row)
}
return &influxql.Result{Series: rows}
}