2016-10-06 07:16:26 -05:00
|
|
|
package influxdb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2016-10-06 08:30:09 -05:00
|
|
|
"fmt"
|
2016-10-06 07:16:26 -05:00
|
|
|
|
|
|
|
"github.com/grafana/grafana/pkg/tsdb"
|
|
|
|
"gopkg.in/guregu/null.v3"
|
|
|
|
)
|
|
|
|
|
2016-10-06 08:30:09 -05:00
|
|
|
type ResponseParser struct{}
|
|
|
|
|
|
|
|
func (rp *ResponseParser) Parse(response *Response) *tsdb.QueryResult {
|
2016-10-06 07:16:26 -05:00
|
|
|
queryRes := tsdb.NewQueryResult()
|
|
|
|
|
2016-10-06 08:30:09 -05:00
|
|
|
for _, result := range response.Results {
|
|
|
|
rp.parseResult(result.Series, queryRes)
|
|
|
|
}
|
2016-10-06 07:16:26 -05:00
|
|
|
|
2016-10-06 08:30:09 -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 {
|
2016-10-06 08:30:09 -05:00
|
|
|
points = append(points, rp.parseTimepoint(k, columnIndex))
|
2016-10-06 07:16:26 -05:00
|
|
|
}
|
2016-10-06 08:30:09 -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 08:30:09 -05:00
|
|
|
}
|
2016-10-06 07:16:26 -05:00
|
|
|
|
2016-10-06 08:30:09 -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
|
|
|
}
|
|
|
|
|
2016-10-06 08:30:09 -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
|
|
|
}
|