fix(opentsdb): fmt
This commit is contained in:
@@ -1,23 +1,23 @@
|
|||||||
package opentsdb
|
package opentsdb
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"context"
|
||||||
"path"
|
"fmt"
|
||||||
"strings"
|
"path"
|
||||||
"context"
|
"strconv"
|
||||||
"strconv"
|
"strings"
|
||||||
|
|
||||||
"net/url"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
"io/ioutil"
|
"net/url"
|
||||||
//"net/http/httputil"
|
//"net/http/httputil"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
|
||||||
"gopkg.in/guregu/null.v3"
|
"gopkg.in/guregu/null.v3"
|
||||||
|
|
||||||
"github.com/grafana/grafana/pkg/log"
|
"github.com/grafana/grafana/pkg/log"
|
||||||
"github.com/grafana/grafana/pkg/tsdb"
|
"github.com/grafana/grafana/pkg/setting"
|
||||||
"github.com/grafana/grafana/pkg/setting"
|
"github.com/grafana/grafana/pkg/tsdb"
|
||||||
)
|
)
|
||||||
|
|
||||||
type OpenTsdbExecutor struct {
|
type OpenTsdbExecutor struct {
|
||||||
@@ -41,112 +41,112 @@ func init() {
|
|||||||
func (e *OpenTsdbExecutor) Execute(ctx context.Context, queries tsdb.QuerySlice, queryContext *tsdb.QueryContext) *tsdb.BatchResult {
|
func (e *OpenTsdbExecutor) Execute(ctx context.Context, queries tsdb.QuerySlice, queryContext *tsdb.QueryContext) *tsdb.BatchResult {
|
||||||
result := &tsdb.BatchResult{}
|
result := &tsdb.BatchResult{}
|
||||||
|
|
||||||
var tsdbQuery OpenTsdbQuery
|
var tsdbQuery OpenTsdbQuery
|
||||||
|
|
||||||
tsdbQuery.Start = queryContext.TimeRange.GetFromAsMsEpoch()
|
tsdbQuery.Start = queryContext.TimeRange.GetFromAsMsEpoch()
|
||||||
tsdbQuery.End = queryContext.TimeRange.GetToAsMsEpoch()
|
tsdbQuery.End = queryContext.TimeRange.GetToAsMsEpoch()
|
||||||
|
|
||||||
for _, query := range queries {
|
for _, query := range queries {
|
||||||
tsdbQuery.Queries = []OpenTsdbMetric {
|
tsdbQuery.Queries = []OpenTsdbMetric{
|
||||||
OpenTsdbMetric{
|
OpenTsdbMetric{
|
||||||
Metric: query.Model.Get("metric").MustString(),
|
Metric: query.Model.Get("metric").MustString(),
|
||||||
Aggregator: query.Model.Get("aggregator").MustString(),
|
Aggregator: query.Model.Get("aggregator").MustString(),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if setting.Env == setting.DEV {
|
if setting.Env == setting.DEV {
|
||||||
plog.Debug("OpenTsdb request", "params", tsdbQuery)
|
plog.Debug("OpenTsdb request", "params", tsdbQuery)
|
||||||
}
|
}
|
||||||
|
|
||||||
req, err := e.createRequest(tsdbQuery)
|
req, err := e.createRequest(tsdbQuery)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
result.Error = err
|
result.Error = err
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
res, err := HttpClient.Do(req)
|
res, err := HttpClient.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
result.Error = err
|
result.Error = err
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
queryResult, err := e.parseResponse(tsdbQuery, res)
|
queryResult, err := e.parseResponse(tsdbQuery, res)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return result.WithError(err)
|
return result.WithError(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
result.QueryResults = queryResult
|
result.QueryResults = queryResult
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *OpenTsdbExecutor) createRequest(data OpenTsdbQuery) (*http.Request, error) {
|
func (e *OpenTsdbExecutor) createRequest(data OpenTsdbQuery) (*http.Request, error) {
|
||||||
u, _ := url.Parse(e.Url)
|
u, _ := url.Parse(e.Url)
|
||||||
u.Path = path.Join(u.Path, "api/query")
|
u.Path = path.Join(u.Path, "api/query")
|
||||||
|
|
||||||
postData, err := json.Marshal(data)
|
postData, err := json.Marshal(data)
|
||||||
|
|
||||||
req, err := http.NewRequest(http.MethodPost, u.String(), strings.NewReader(string(postData)))
|
req, err := http.NewRequest(http.MethodPost, u.String(), strings.NewReader(string(postData)))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
plog.Info("Failed to create request", "error", err)
|
plog.Info("Failed to create request", "error", err)
|
||||||
return nil, fmt.Errorf("Failed to create request. error: %v", err)
|
return nil, fmt.Errorf("Failed to create request. error: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
req.Header.Set("Content-Type", "application/json")
|
req.Header.Set("Content-Type", "application/json")
|
||||||
if e.BasicAuth {
|
if e.BasicAuth {
|
||||||
req.SetBasicAuth(e.BasicAuthUser, e.BasicAuthPassword)
|
req.SetBasicAuth(e.BasicAuthUser, e.BasicAuthPassword)
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
requestDump, err := httputil.DumpRequest(req, true)
|
requestDump, err := httputil.DumpRequest(req, true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
}
|
}
|
||||||
fmt.Println(string(requestDump))
|
fmt.Println(string(requestDump))
|
||||||
*/
|
*/
|
||||||
return req, err
|
return req, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *OpenTsdbExecutor) parseResponse(query OpenTsdbQuery, res *http.Response) (map[string]*tsdb.QueryResult, error) {
|
func (e *OpenTsdbExecutor) parseResponse(query OpenTsdbQuery, res *http.Response) (map[string]*tsdb.QueryResult, error) {
|
||||||
|
|
||||||
queryResults := make(map[string]*tsdb.QueryResult)
|
|
||||||
queryRes := tsdb.NewQueryResult()
|
|
||||||
|
|
||||||
body, err := ioutil.ReadAll(res.Body)
|
queryResults := make(map[string]*tsdb.QueryResult)
|
||||||
defer res.Body.Close()
|
queryRes := tsdb.NewQueryResult()
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
if res.StatusCode/100 != 2 {
|
body, err := ioutil.ReadAll(res.Body)
|
||||||
plog.Info("Request failed", "status", res.Status, "body", string(body))
|
defer res.Body.Close()
|
||||||
return nil, fmt.Errorf("Request failed status: %v", res.Status)
|
if err != nil {
|
||||||
}
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
var data []OpenTsdbResponse
|
if res.StatusCode/100 != 2 {
|
||||||
err = json.Unmarshal(body, &data)
|
plog.Info("Request failed", "status", res.Status, "body", string(body))
|
||||||
if err != nil {
|
return nil, fmt.Errorf("Request failed status: %v", res.Status)
|
||||||
plog.Info("Failed to unmarshal opentsdb response", "error", err, "status", res.Status, "body", string(body))
|
}
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, val := range data {
|
var data []OpenTsdbResponse
|
||||||
series := tsdb.TimeSeries{
|
err = json.Unmarshal(body, &data)
|
||||||
Name: val.Metric,
|
if err != nil {
|
||||||
}
|
plog.Info("Failed to unmarshal opentsdb response", "error", err, "status", res.Status, "body", string(body))
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
for timeString, value := range val.DataPoints {
|
for _, val := range data {
|
||||||
timestamp, err := strconv.ParseFloat(timeString, 64)
|
series := tsdb.TimeSeries{
|
||||||
if err != nil {
|
Name: val.Metric,
|
||||||
plog.Info("Failed to unmarshal opentsdb timestamp", "timestamp", timeString)
|
}
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
series.Points = append(series.Points, tsdb.NewTimePoint(null.FloatFrom(value), timestamp))
|
|
||||||
}
|
|
||||||
|
|
||||||
queryRes.Series = append(queryRes.Series, &series)
|
for timeString, value := range val.DataPoints {
|
||||||
}
|
timestamp, err := strconv.ParseFloat(timeString, 64)
|
||||||
|
if err != nil {
|
||||||
|
plog.Info("Failed to unmarshal opentsdb timestamp", "timestamp", timeString)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
series.Points = append(series.Points, tsdb.NewTimePoint(null.FloatFrom(value), timestamp))
|
||||||
|
}
|
||||||
|
|
||||||
queryResults["A"] = queryRes
|
queryRes.Series = append(queryRes.Series, &series)
|
||||||
return queryResults, nil
|
}
|
||||||
|
|
||||||
|
queryResults["A"] = queryRes
|
||||||
|
return queryResults, nil
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user