grafana/pkg/tsdb/influxdb/response_parser.go

81 lines
1.8 KiB
Go
Raw Normal View History

2016-10-06 07:16:26 -05:00
package influxdb
import (
"encoding/json"
"fmt"
2016-10-06 07:16:26 -05:00
"github.com/grafana/grafana/pkg/tsdb"
"gopkg.in/guregu/null.v3"
)
type ResponseParser struct{}
func (rp *ResponseParser) Parse(response *Response) *tsdb.QueryResult {
2016-10-06 07:16:26 -05:00
queryRes := tsdb.NewQueryResult()
for _, result := range response.Results {
rp.parseResult(result.Series, queryRes)
}
2016-10-06 07:16:26 -05:00
for _, serie := range queryRes.Series {
glog.Debug("result", "name", serie.Name, "points", serie.Points)
}
return queryRes
}
func (rp *ResponseParser) parseResult(result []Row, queryResult *tsdb.QueryResult) {
for _, r := range result {
for columnIndex, column := range r.Columns {
if column == "time" {
continue
}
var points tsdb.TimeSeriesPoints
2016-10-06 07:16:26 -05:00
for _, k := range r.Values {
points = append(points, rp.parseTimepoint(k, columnIndex))
2016-10-06 07:16:26 -05:00
}
queryResult.Series = append(queryResult.Series, &tsdb.TimeSeries{
Name: rp.formatName(r, column),
Points: points,
})
2016-10-06 07:16:26 -05:00
}
}
}
2016-10-06 07:16:26 -05:00
func (rp *ResponseParser) formatName(row Row, column string) string {
return fmt.Sprintf("%s.%s", row.Name, column)
}
func (rp *ResponseParser) parseTimepoint(k []interface{}, valuePosition int) tsdb.TimePoint {
var value null.Float = rp.parseValue(k[valuePosition])
timestampNumber, _ := k[0].(json.Number)
timestamp, err := timestampNumber.Float64()
if err != nil {
glog.Error("Invalid timestamp format. This should never happen!")
2016-10-06 07:16:26 -05:00
}
return tsdb.NewTimePoint(value, timestamp)
}
func (rp *ResponseParser) parseValue(value interface{}) null.Float {
num, ok := value.(json.Number)
if !ok {
return null.FloatFromPtr(nil)
}
fvalue, err := num.Float64()
if err == nil {
return null.FloatFrom(fvalue)
}
ivalue, err := num.Int64()
if err == nil {
return null.FloatFrom(float64(ivalue))
}
return null.FloatFromPtr(nil)
2016-10-06 07:16:26 -05:00
}