CloudMonitoring: Migrate to use backend plugin SDK contracts (#38650)
* Use SDK contracts for cloudmonitoring * Get build running, tests passing and do some refactoring (#38754) * fix build+tests and refactor * remove alerting stuff * remove unused field * fix plugin fetch * end to end * resp rename * tidy annotations * reformatting * update refID * reformat imports * fix styling * clean up unmarshalling * uncomment + fix tests * appease linter * remove spaces * remove old cruft * add check for empty queries * update tests * remove pm as dep * adjust proxy route contract * fix service loading * use UNIX val * fix endpoint + resp * h@ckz for frontend * fix resp * fix interval * always set custom meta * remove unused param * fix labels fetch * fix linter * fix test + remove unused field * apply pr feedback * fix grafana-auto intervals * fix tests * resolve conflicts * fix bad merge * fix conflicts * remove bad logger import Co-authored-by: Will Browne <wbrowne@users.noreply.github.com> Co-authored-by: Will Browne <will.browne@grafana.com>
This commit is contained in:
co-authored by
Will Browne
Will Browne
parent
d13c799aa6
commit
e822c8a24d
@@ -2,63 +2,65 @@ package cloudmonitoring
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"strings"
|
||||
|
||||
"github.com/grafana/grafana/pkg/plugins"
|
||||
"github.com/grafana/grafana-plugin-sdk-go/backend"
|
||||
"github.com/grafana/grafana-plugin-sdk-go/data"
|
||||
)
|
||||
|
||||
//nolint: staticcheck // plugins.DataPlugin deprecated
|
||||
func (e *Executor) executeAnnotationQuery(ctx context.Context, tsdbQuery plugins.DataQuery) (
|
||||
plugins.DataResponse, error) {
|
||||
result := plugins.DataResponse{
|
||||
Results: make(map[string]plugins.DataQueryResult),
|
||||
}
|
||||
func (s *Service) executeAnnotationQuery(ctx context.Context, req *backend.QueryDataRequest, dsInfo datasourceInfo) (
|
||||
*backend.QueryDataResponse, error) {
|
||||
resp := backend.NewQueryDataResponse()
|
||||
|
||||
firstQuery := tsdbQuery.Queries[0]
|
||||
|
||||
queries, err := e.buildQueryExecutors(tsdbQuery)
|
||||
queries, err := s.buildQueryExecutors(req)
|
||||
if err != nil {
|
||||
return plugins.DataResponse{}, err
|
||||
return resp, err
|
||||
}
|
||||
|
||||
queryRes, resp, _, err := queries[0].run(ctx, tsdbQuery, e)
|
||||
queryRes, dr, _, err := queries[0].run(ctx, req, s, dsInfo)
|
||||
if err != nil {
|
||||
return plugins.DataResponse{}, err
|
||||
return resp, err
|
||||
}
|
||||
|
||||
metricQuery := firstQuery.Model.Get("metricQuery")
|
||||
title := metricQuery.Get("title").MustString()
|
||||
text := metricQuery.Get("text").MustString()
|
||||
tags := metricQuery.Get("tags").MustString()
|
||||
mq := struct {
|
||||
Title string `json:"title"`
|
||||
Text string `json:"text"`
|
||||
Tags string `json:"tags"`
|
||||
}{}
|
||||
|
||||
err = queries[0].parseToAnnotations(&queryRes, resp, title, text, tags)
|
||||
result.Results[firstQuery.RefID] = queryRes
|
||||
firstQuery := req.Queries[0]
|
||||
err = json.Unmarshal(firstQuery.JSON, &mq)
|
||||
if err != nil {
|
||||
return resp, nil
|
||||
}
|
||||
err = queries[0].parseToAnnotations(queryRes, dr, mq.Title, mq.Text, mq.Tags)
|
||||
resp.Responses[firstQuery.RefID] = *queryRes
|
||||
|
||||
return result, err
|
||||
return resp, err
|
||||
}
|
||||
|
||||
//nolint: staticcheck // plugins.DataPlugin deprecated
|
||||
func transformAnnotationToTable(data []map[string]string, result *plugins.DataQueryResult) {
|
||||
table := plugins.DataTable{
|
||||
Columns: make([]plugins.DataTableColumn, 4),
|
||||
Rows: make([]plugins.DataRowValues, 0),
|
||||
func (timeSeriesQuery cloudMonitoringTimeSeriesQuery) transformAnnotationToFrame(annotations []map[string]string, result *backend.DataResponse) {
|
||||
frames := data.Frames{}
|
||||
for _, a := range annotations {
|
||||
frame := &data.Frame{
|
||||
RefID: timeSeriesQuery.getRefID(),
|
||||
Fields: []*data.Field{
|
||||
data.NewField("time", nil, a["time"]),
|
||||
data.NewField("title", nil, a["title"]),
|
||||
data.NewField("tags", nil, a["tags"]),
|
||||
data.NewField("text", nil, a["text"]),
|
||||
},
|
||||
Meta: &data.FrameMeta{
|
||||
Custom: map[string]interface{}{
|
||||
"rowCount": len(a),
|
||||
},
|
||||
},
|
||||
}
|
||||
frames = append(frames, frame)
|
||||
}
|
||||
table.Columns[0].Text = "time"
|
||||
table.Columns[1].Text = "title"
|
||||
table.Columns[2].Text = "tags"
|
||||
table.Columns[3].Text = "text"
|
||||
|
||||
for _, r := range data {
|
||||
values := make([]interface{}, 4)
|
||||
values[0] = r["time"]
|
||||
values[1] = r["title"]
|
||||
values[2] = r["tags"]
|
||||
values[3] = r["text"]
|
||||
table.Rows = append(table.Rows, values)
|
||||
}
|
||||
result.Tables = append(result.Tables, table)
|
||||
result.Meta.Set("rowCount", len(data))
|
||||
slog.Info("anno", "len", len(data))
|
||||
result.Frames = frames
|
||||
slog.Info("anno", "len", len(annotations))
|
||||
}
|
||||
|
||||
func formatAnnotationText(annotationText string, pointValue string, metricType string, metricLabels map[string]string, resourceLabels map[string]string) string {
|
||||
|
||||
Reference in New Issue
Block a user