grafana/pkg/tsdb/testdatasource/usa_stats.go
Will Browne b80fbe03f0
Plugins: Refactor Plugin Management (#40477)
* add core plugin flow

* add instrumentation

* move func

* remove cruft

* support external backend plugins

* refactor + clean up

* remove comments

* refactor loader

* simplify core plugin path arg

* cleanup loggers

* move signature validator to plugins package

* fix sig packaging

* cleanup plugin model

* remove unnecessary plugin field

* add start+stop for pm

* fix failures

* add decommissioned state

* export fields just to get things flowing

* fix comments

* set static routes

* make image loading idempotent

* merge with backend plugin manager

* re-use funcs

* reorder imports + remove unnecessary interface

* add some TODOs + remove unused func

* remove unused instrumentation func

* simplify client usage

* remove import alias

* re-use backendplugin.Plugin interface

* re order funcs

* improve var name

* fix log statements

* refactor data model

* add logic for dupe check during loading

* cleanup state setting

* refactor loader

* cleanup manager interface

* add rendering flow

* refactor loading + init

* add renderer support

* fix renderer plugin

* reformat imports

* track errors

* fix plugin signature inheritance

* name param in interface

* update func comment

* fix func arg name

* introduce class concept

* remove func

* fix external plugin check

* apply changes from pm-experiment

* fix core plugins

* fix imports

* rename interface

* comment API interface

* add support for testdata plugin

* enable alerting + use correct core plugin contracts

* slim manager API

* fix param name

* fix filter

* support static routes

* fix rendering

* tidy rendering

* get tests compiling

* fix install+uninstall

* start finder test

* add finder test coverage

* start loader tests

* add test for core plugins

* load core + bundled test

* add test for nested plugin loading

* add test files

* clean interface + fix registering some core plugins

* refactoring

* reformat and create sub packages

* simplify core plugin init

* fix ctx cancel scenario

* migrate initializer

* remove Init() funcs

* add test starter

* new logger

* flesh out initializer tests

* refactoring

* remove unused svc

* refactor rendering flow

* fixup loader tests

* add enabled helper func

* fix logger name

* fix data fetchers

* fix case where plugin dir doesn't exist

* improve coverage + move dupe checking to loader

* remove noisy debug logs

* register core plugins automagically

* add support for renderer in catalog

* make private func + fix req validation

* use interface

* re-add check for renderer in catalog

* tidy up from moving to auto reg core plugins

* core plugin registrar

* guards

* copy over core plugins for test infra

* all tests green

* renames

* propagate new interfaces

* kill old manager

* get compiling

* tidy up

* update naming

* refactor manager test + cleanup

* add more cases to finder test

* migrate validator to field

* more coverage

* refactor dupe checking

* add test for plugin class

* add coverage for initializer

* split out rendering

* move

* fixup tests

* fix uss test

* fix frontend settings

* fix grafanads test

* add check when checking sig errors

* fix enabled map

* fixup

* allow manual setup of CM

* rename to cloud-monitoring

* remove TODO

* add installer interface for testing

* loader interface returns

* tests passing

* refactor + add more coverage

* support 'stackdriver'

* fix frontend settings loading

* improve naming based on package name

* small tidy

* refactor test

* fix renderer start

* make cloud-monitoring plugin ID clearer

* add plugin update test

* add integration tests

* don't break all if sig can't be calculated

* add root URL check test

* add more signature verification tests

* update DTO name

* update enabled plugins comment

* update comments

* fix linter

* revert fe naming change

* fix errors endpoint

* reset error code field name

* re-order test to help verify

* assert -> require

* pm check

* add missing entry + re-order

* re-check

* dump icon log

* verify manager contents first

* reformat

* apply PR feedback

* apply style changes

* fix one vs all loading err

* improve log output

* only start when no signature error

* move log

* rework plugin update check

* fix test

* fix multi loading from cfg.PluginSettings

* improve log output #2

* add error abstraction to capture errors without registering a plugin

* add debug log

* add unsigned warning

* e2e test attempt

* fix logger

* set home path

* prevent panic

* alternate

* ugh.. fix home path

* return renderer even if not started

* make renderer plugin managed

* add fallback renderer icon, update renderer badge + prevent changes when renderer is installed

* fix icon loading

* rollback renderer changes

* use correct field

* remove unneccessary block

* remove newline

* remove unused func

* fix bundled plugins base + module fields

* remove unused field since refactor

* add authorizer abstraction

* loader only returns plugins expected to run

* fix multi log output
2021-11-01 10:53:33 +01:00

202 lines
5.5 KiB
Go

package testdatasource
import (
"context"
"encoding/json"
"fmt"
"math"
"time"
"github.com/grafana/grafana-plugin-sdk-go/backend"
"github.com/grafana/grafana-plugin-sdk-go/data"
)
var modeValueAsRow = "values-as-rows"
var modeValueAsFields = "values-as-fields"
var modeValueAsLabeledFields = "values-as-labeled-fields"
var modeTimeseries = "timeseries"
var modeTimeseriesWide = "timeseries-wide"
var allStateCodes = []string{"AL", "AK", "AZ", "AR", "CA", "CO", "CT", "DE", "DC", "FL", "GA", "HI", "ID", "IL", "IN", "IA", "KS", "KY", "LA", "ME", "MT", "NE", "NV", "NH", "NJ", "NM", "NY", "NC", "ND", "OH", "OK", "OR", "MD", "MA", "MI", "MN", "MS", "MO", "PA", "RI", "SC", "SD", "TN", "TX", "UT", "VT", "VA", "WA", "WV", "WI", "WY"}
type usaQueryWrapper struct {
USA usaQuery `json:"usa"`
}
type usaQuery struct {
Mode string `json:"mode"`
Period string `json:"period"`
Fields []string `json:"fields"`
States []string `json:"states"`
// From the main query
maxDataPoints int64
timeRange backend.TimeRange
interval time.Duration
period time.Duration
}
func (s *Service) handleUSAScenario(ctx context.Context, req *backend.QueryDataRequest) (*backend.QueryDataResponse, error) {
resp := backend.NewQueryDataResponse()
for _, q := range req.Queries {
wrapper := &usaQueryWrapper{}
err := json.Unmarshal(q.JSON, wrapper)
if err != nil {
return nil, fmt.Errorf("failed to parse query json: %v", err)
}
usa := wrapper.USA
periodString := usa.Period
if periodString == "" {
periodString = "30m"
}
period, err := time.ParseDuration(periodString)
if err != nil {
return nil, fmt.Errorf("failed to parse query json: %v", err)
}
if len(usa.Fields) == 0 {
usa.Fields = []string{"foo", "bar", "baz"}
}
if len(usa.States) == 0 {
usa.States = allStateCodes
}
usa.period = period
usa.maxDataPoints = q.MaxDataPoints * 2
usa.timeRange = q.TimeRange
usa.interval = q.Interval
resp.Responses[q.RefID] = doStateQuery(usa)
}
return resp, nil
}
func doStateQuery(query usaQuery) backend.DataResponse {
switch query.Mode {
default:
case modeTimeseries:
return getStateValueAsTimeseries(query, false)
case modeTimeseriesWide:
return getStateValueAsTimeseries(query, true)
case modeValueAsFields:
return getStateValueAsFrame(query.timeRange.To, query, false)
case modeValueAsLabeledFields:
return getStateValueAsFrame(query.timeRange.To, query, true)
}
return getStateValueAsRow(query.timeRange.To, query)
}
func getStateValueAsFrame(t time.Time, query usaQuery, asLabel bool) backend.DataResponse {
dr := backend.DataResponse{}
var labels data.Labels
for _, fname := range query.Fields {
frame := data.NewFrame(fname)
vals := getStateValues(t, fname, query)
for idx, state := range query.States {
name := state
if asLabel {
labels = data.Labels{"state": state}
name = ""
}
field := data.NewField(name, labels, []float64{vals[idx]})
frame.Fields = append(frame.Fields, field)
}
dr.Frames = append(dr.Frames, frame)
}
return dr
}
// One frame for each time+value
func getStateValueAsTimeseries(query usaQuery, wide bool) backend.DataResponse {
dr := backend.DataResponse{}
tr := query.timeRange
var labels data.Labels
for _, fname := range query.Fields {
timeWalkerMs := tr.From.UnixNano() / int64(time.Millisecond)
to := tr.To.UnixNano() / int64(time.Millisecond)
stepMillis := query.interval.Milliseconds()
// Calculate all values
timeVals := make([]time.Time, 0)
stateVals := make([][]float64, 0)
for i := int64(0); i < query.maxDataPoints && timeWalkerMs < to; i++ {
t := time.Unix(timeWalkerMs/int64(1e+3), (timeWalkerMs%int64(1e+3))*int64(1e+6)).UTC()
vals := getStateValues(t, fname, query)
timeVals = append(timeVals, t)
stateVals = append(stateVals, vals)
timeWalkerMs += stepMillis
}
values := make([]float64, len(timeVals))
for idx, state := range query.States {
for i := 0; i < len(timeVals); i++ {
values[i] = stateVals[i][idx]
}
labels = data.Labels{"state": state}
frame := data.NewFrame(fname,
data.NewField(data.TimeSeriesTimeFieldName, nil, timeVals),
data.NewField(data.TimeSeriesValueFieldName, labels, values),
)
dr.Frames = append(dr.Frames, frame)
}
}
// Stick them next to eachother
if wide {
wideFrame := data.NewFrame("", dr.Frames[0].Fields[0])
for _, frame := range dr.Frames {
field := frame.Fields[1]
field.Name = frame.Name
wideFrame.Fields = append(wideFrame.Fields, field)
}
dr.Frames = data.Frames{wideFrame}
}
return dr
}
func getStateValueAsRow(t time.Time, query usaQuery) backend.DataResponse {
frame := data.NewFrame("", data.NewField("state", nil, query.States))
for _, f := range query.Fields {
frame.Fields = append(frame.Fields, data.NewField(f, nil, getStateValues(t, f, query)))
}
return backend.DataResponse{
Frames: data.Frames{frame},
}
}
func getStateValues(t time.Time, field string, query usaQuery) []float64 {
tv := float64(t.UnixNano())
pn := float64(query.period.Nanoseconds())
incr := pn / float64(len(query.States))
fn := math.Sin
// period offsets
switch field {
case "bar":
fn = math.Cos
case "baz":
fn = math.Tan
}
values := make([]float64, len(query.States))
for i := range query.States {
tv += incr
value := fn(float64(int64(tv) % int64(pn)))
// We shall truncate float64 to a certain precision, because otherwise we might
// get different cos/tan results across different CPU architectures and compilers.
values[i] = float64(int(value*1e10)) / 1e10
}
return values
}