2016-10-06 07:16:26 -05:00
|
|
|
package influxdb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2016-10-06 08:30:09 -05:00
|
|
|
"fmt"
|
2016-11-08 12:56:57 -06:00
|
|
|
"regexp"
|
2016-11-09 05:45:58 -06:00
|
|
|
"strconv"
|
2016-10-06 11:51:17 -05:00
|
|
|
"strings"
|
2016-10-06 07:16:26 -05:00
|
|
|
|
2017-01-13 05:32:30 -06:00
|
|
|
"github.com/grafana/grafana/pkg/components/null"
|
2016-10-06 07:16:26 -05:00
|
|
|
"github.com/grafana/grafana/pkg/tsdb"
|
|
|
|
)
|
|
|
|
|
2016-10-06 08:30:09 -05:00
|
|
|
type ResponseParser struct{}
|
|
|
|
|
2016-11-08 12:56:57 -06:00
|
|
|
var (
|
|
|
|
legendFormat *regexp.Regexp
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
legendFormat = regexp.MustCompile(`\[\[(\w+?)*\]\]*|\$\s*(\w+?)*`)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rp *ResponseParser) Parse(response *Response, query *Query) *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 {
|
2016-11-08 12:56:57 -06:00
|
|
|
queryRes.Series = append(queryRes.Series, rp.transformRows(result.Series, queryRes, query)...)
|
2016-10-06 08:30:09 -05:00
|
|
|
}
|
2016-10-06 07:16:26 -05:00
|
|
|
|
2016-10-06 08:30:09 -05:00
|
|
|
return queryRes
|
|
|
|
}
|
|
|
|
|
2016-11-08 12:56:57 -06:00
|
|
|
func (rp *ResponseParser) transformRows(rows []Row, queryResult *tsdb.QueryResult, query *Query) tsdb.TimeSeriesSlice {
|
2016-10-10 04:58:06 -05:00
|
|
|
var result tsdb.TimeSeriesSlice
|
|
|
|
|
|
|
|
for _, row := range rows {
|
|
|
|
for columnIndex, column := range row.Columns {
|
2016-10-06 08:30:09 -05:00
|
|
|
if column == "time" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
var points tsdb.TimeSeriesPoints
|
2016-10-10 04:58:06 -05:00
|
|
|
for _, valuePair := range row.Values {
|
|
|
|
point, err := rp.parseTimepoint(valuePair, columnIndex)
|
|
|
|
if err == nil {
|
|
|
|
points = append(points, point)
|
|
|
|
}
|
2016-10-06 07:16:26 -05:00
|
|
|
}
|
2016-10-10 04:58:06 -05:00
|
|
|
result = append(result, &tsdb.TimeSeries{
|
2016-11-08 12:56:57 -06:00
|
|
|
Name: rp.formatSerieName(row, column, query),
|
2016-10-06 08:30:09 -05:00
|
|
|
Points: points,
|
2017-12-01 09:16:49 -06:00
|
|
|
Tags: row.Tags,
|
2016-10-06 08:30:09 -05:00
|
|
|
})
|
2016-10-06 07:16:26 -05:00
|
|
|
}
|
|
|
|
}
|
2016-10-10 04:58:06 -05:00
|
|
|
|
|
|
|
return result
|
2016-10-06 08:30:09 -05:00
|
|
|
}
|
2016-10-06 07:16:26 -05:00
|
|
|
|
2016-11-08 12:56:57 -06:00
|
|
|
func (rp *ResponseParser) formatSerieName(row Row, column string, query *Query) string {
|
|
|
|
if query.Alias == "" {
|
|
|
|
return rp.buildSerieNameFromQuery(row, column)
|
|
|
|
}
|
|
|
|
|
2016-11-09 05:45:58 -06:00
|
|
|
nameSegment := strings.Split(row.Name, ".")
|
|
|
|
|
2016-11-08 12:56:57 -06:00
|
|
|
result := legendFormat.ReplaceAllFunc([]byte(query.Alias), func(in []byte) []byte {
|
|
|
|
aliasFormat := string(in)
|
|
|
|
aliasFormat = strings.Replace(aliasFormat, "[[", "", 1)
|
|
|
|
aliasFormat = strings.Replace(aliasFormat, "]]", "", 1)
|
|
|
|
aliasFormat = strings.Replace(aliasFormat, "$", "", 1)
|
|
|
|
|
|
|
|
if aliasFormat == "m" || aliasFormat == "measurement" {
|
|
|
|
return []byte(query.Measurement)
|
|
|
|
}
|
|
|
|
if aliasFormat == "col" {
|
|
|
|
return []byte(column)
|
|
|
|
}
|
|
|
|
|
2016-11-09 05:45:58 -06:00
|
|
|
pos, err := strconv.Atoi(aliasFormat)
|
|
|
|
if err == nil && len(nameSegment) >= pos {
|
|
|
|
return []byte(nameSegment[pos])
|
|
|
|
}
|
|
|
|
|
2016-11-08 12:56:57 -06:00
|
|
|
if !strings.HasPrefix(aliasFormat, "tag_") {
|
|
|
|
return in
|
|
|
|
}
|
|
|
|
|
|
|
|
tagKey := strings.Replace(aliasFormat, "tag_", "", 1)
|
|
|
|
tagValue, exist := row.Tags[tagKey]
|
|
|
|
if exist {
|
|
|
|
return []byte(tagValue)
|
|
|
|
}
|
|
|
|
|
|
|
|
return in
|
|
|
|
})
|
|
|
|
|
|
|
|
return string(result)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rp *ResponseParser) buildSerieNameFromQuery(row Row, column string) string {
|
2016-10-06 11:51:17 -05:00
|
|
|
var tags []string
|
2016-10-06 11:47:59 -05:00
|
|
|
|
|
|
|
for k, v := range row.Tags {
|
2016-10-06 11:51:17 -05:00
|
|
|
tags = append(tags, fmt.Sprintf("%s: %s", k, v))
|
2016-10-06 11:47:59 -05:00
|
|
|
}
|
|
|
|
|
2016-10-06 11:51:17 -05:00
|
|
|
tagText := ""
|
|
|
|
if len(tags) > 0 {
|
|
|
|
tagText = fmt.Sprintf(" { %s }", strings.Join(tags, " "))
|
2016-10-06 11:47:59 -05:00
|
|
|
}
|
|
|
|
|
2016-10-06 11:51:17 -05:00
|
|
|
return fmt.Sprintf("%s.%s%s", row.Name, column, tagText)
|
2016-10-06 08:30:09 -05:00
|
|
|
}
|
|
|
|
|
2016-10-10 04:58:06 -05:00
|
|
|
func (rp *ResponseParser) parseTimepoint(valuePair []interface{}, valuePosition int) (tsdb.TimePoint, error) {
|
|
|
|
var value null.Float = rp.parseValue(valuePair[valuePosition])
|
2016-10-06 08:30:09 -05:00
|
|
|
|
2016-10-10 04:58:06 -05:00
|
|
|
timestampNumber, _ := valuePair[0].(json.Number)
|
2016-10-06 08:30:09 -05:00
|
|
|
timestamp, err := timestampNumber.Float64()
|
|
|
|
if err != nil {
|
2016-10-10 04:58:06 -05:00
|
|
|
return tsdb.TimePoint{}, err
|
2016-10-06 07:16:26 -05:00
|
|
|
}
|
|
|
|
|
2016-10-10 04:58:06 -05:00
|
|
|
return tsdb.NewTimePoint(value, timestamp), nil
|
2016-10-06 08:30:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (rp *ResponseParser) parseValue(value interface{}) null.Float {
|
2016-10-10 04:58:06 -05:00
|
|
|
number, ok := value.(json.Number)
|
2016-10-06 08:30:09 -05:00
|
|
|
if !ok {
|
|
|
|
return null.FloatFromPtr(nil)
|
|
|
|
}
|
|
|
|
|
2016-10-10 04:58:06 -05:00
|
|
|
fvalue, err := number.Float64()
|
2016-10-06 08:30:09 -05:00
|
|
|
if err == nil {
|
|
|
|
return null.FloatFrom(fvalue)
|
|
|
|
}
|
|
|
|
|
2016-10-10 04:58:06 -05:00
|
|
|
ivalue, err := number.Int64()
|
2016-10-06 08:30:09 -05:00
|
|
|
if err == nil {
|
|
|
|
return null.FloatFrom(float64(ivalue))
|
|
|
|
}
|
|
|
|
|
|
|
|
return null.FloatFromPtr(nil)
|
2016-10-06 07:16:26 -05:00
|
|
|
}
|