influxdb/monitor/statement_executor.go

70 lines
1.8 KiB
Go
Raw Normal View History

2015-09-02 22:34:15 +00:00
package monitor
import (
"fmt"
"github.com/influxdb/influxdb/influxql"
"github.com/influxdb/influxdb/models"
2015-09-02 22:34:15 +00:00
)
// StatementExecutor translates InfluxQL queries to Monitor methods.
type StatementExecutor struct {
Monitor interface {
Statistics(map[string]string) ([]*statistic, error)
Diagnostics() (map[string]*Diagnostic, error)
2015-09-02 22:34:15 +00:00
}
}
// ExecuteStatement executes monitor-related query statements.
func (s *StatementExecutor) ExecuteStatement(stmt influxql.Statement) *influxql.Result {
switch stmt := stmt.(type) {
case *influxql.ShowStatsStatement:
2015-09-22 23:28:24 +00:00
return s.executeShowStatistics(stmt.Module)
2015-09-02 22:34:15 +00:00
case *influxql.ShowDiagnosticsStatement:
return s.executeShowDiagnostics()
default:
panic(fmt.Sprintf("unsupported statement type: %T", stmt))
}
}
2015-09-22 23:28:24 +00:00
func (s *StatementExecutor) executeShowStatistics(module string) *influxql.Result {
stats, err := s.Monitor.Statistics(nil)
if err != nil {
return &influxql.Result{Err: err}
}
2015-09-22 23:28:24 +00:00
rows := make([]*models.Row, 0)
2015-09-02 22:34:15 +00:00
2015-09-22 23:28:24 +00:00
for _, stat := range stats {
if module != "" && stat.Name != module {
continue
}
row := &models.Row{Name: stat.Name, Tags: stat.Tags}
2015-09-02 22:34:15 +00:00
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}
2015-09-22 23:28:24 +00:00
rows = append(rows, row)
2015-09-02 22:34:15 +00:00
}
return &influxql.Result{Series: rows}
}
func (s *StatementExecutor) executeShowDiagnostics() *influxql.Result {
diags, err := s.Monitor.Diagnostics()
if err != nil {
return &influxql.Result{Err: err}
}
rows := make([]*models.Row, 0, len(diags))
for k, v := range diags {
row := &models.Row{Name: k}
row.Columns = v.Columns
row.Values = v.Rows
rows = append(rows, row)
}
return &influxql.Result{Series: rows}
2015-09-02 22:34:15 +00:00
}