Skip to content

Commit

Permalink
Simplify stat options
Browse files Browse the repository at this point in the history
Cannot use UnmarshallJsonStrict() of stat.Options, some fields may be missing.
  • Loading branch information
v-zhuravlev committed Nov 5, 2024
1 parent 4b54416 commit 985c733
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 54 deletions.
30 changes: 17 additions & 13 deletions lint/lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"strings"

"github.com/grafana/grafana-foundation-sdk/go/dashboard"
"github.com/grafana/grafana-foundation-sdk/go/stat"
)

type Severity int
Expand Down Expand Up @@ -198,20 +197,25 @@ func (a *Annotation) GetDataSource() (Datasource, error) {
// Panel is a deliberately incomplete representation of the Dashboard -> Panel type in grafana.
// The properties which are extracted from JSON are only those used for linting purposes.
type Panel struct {
Id int `json:"id"`
Title string `json:"title"`
Description string `json:"description,omitempty"`
Targets []Target `json:"targets,omitempty"`
Datasource interface{} `json:"datasource,omitempty"`
Type string `json:"type"`
Panels []Panel `json:"panels,omitempty"`
FieldConfig *FieldConfig `json:"fieldConfig,omitempty"`
Options json.RawMessage `json:"options,omitempty"`
Id int `json:"id"`
Title string `json:"title"`
Description string `json:"description,omitempty"`
Targets []Target `json:"targets,omitempty"`
Datasource interface{} `json:"datasource,omitempty"`
Type string `json:"type"`
Panels []Panel `json:"panels,omitempty"`
FieldConfig *FieldConfig `json:"fieldConfig,omitempty"`
Options []byte `json:"options,omitempty"`
}

type statPanel struct {
options stat.Options
Panel
// oversimplified stat panel options
type StatOptions struct {
ReduceOptions ReduceOptions
}

// oversimplified Reduce options
type ReduceOptions struct {
Fields string `json:"fields,omitempty"`
}

type FieldConfig struct {
Expand Down
23 changes: 8 additions & 15 deletions lint/rule_panel_units.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package lint

import (
"encoding/json"
"fmt"

"github.com/grafana/grafana-foundation-sdk/go/dashboard"
"github.com/grafana/grafana-foundation-sdk/go/stat"
)

func NewPanelUnitsRule() *PanelRuleFunc {
Expand Down Expand Up @@ -86,21 +86,12 @@ func NewPanelUnitsRule() *PanelRuleFunc {

// ignore if has reduceOptions fields (for stat panels only):
if p.Type == "stat" {
var opts stat.Options
optsByte, err := p.Options.MarshalJSON()
var opts StatOptions
err := json.Unmarshal(p.Options, &opts)
if err != nil {
r.AddError(d, p, err.Error())
}
err = (*stat.Options).UnmarshalJSONStrict(&opts, optsByte)
if err != nil {
r.AddError(d, p, err.Error())
}
statPanel := &statPanel{
Panel: p,
options: opts,
}

if hasReduceOptionsNonNumericFields(*statPanel) {
if hasReduceOptionsNonNumericFields(&opts.ReduceOptions) {
return r
}
}
Expand Down Expand Up @@ -152,10 +143,12 @@ func getValueMappings(p Panel) []dashboard.ValueMapping {
return valueMappings
}

func hasReduceOptionsNonNumericFields(p statPanel) bool {
// Numeric fields are set as empty string "". Any other value means nonnumeric on grafana stat panel.
func hasReduceOptionsNonNumericFields(reduceOpts *ReduceOptions) bool {

if p.options.ReduceOptions.Fields != nil && *p.options.ReduceOptions.Fields != "" {
if reduceOpts.Fields != "" {

Check failure on line 149 in lint/rule_panel_units.go

View workflow job for this annotation

GitHub Actions / lint

S1008: should use 'return reduceOpts.Fields != ""' instead of 'if reduceOpts.Fields != "" { return true }; return false' (gosimple)
return true
}

return false
}
28 changes: 2 additions & 26 deletions lint/rule_panel_units_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,20 +109,8 @@ func TestPanelUnits(t *testing.T) {
Options: []byte(`
{
"reduceOptions": {
"values": false,
"calcs": [
"lastNotNull"
],
"fields": "/^version$/"
},
"orientation": "auto",
"textMode": "auto",
"wideLayout": true,
"colorMode": "fixed",
"graphMode": "none",
"justifyMode": "auto",
"showPercentChange": false,
"percentChangeColorMode": "standard"
}
}
`),
Expand All @@ -141,20 +129,8 @@ func TestPanelUnits(t *testing.T) {
Options: []byte(`
{
"reduceOptions": {
"values": false,
"calcs": [
"lastNotNull"
],
"fields": ""
},
"orientation": "auto",
"textMode": "auto",
"wideLayout": true,
"colorMode": "fixed",
"graphMode": "none",
"justifyMode": "auto",
"showPercentChange": false,
"percentChangeColorMode": "standard"
}
}
`),
Expand Down

0 comments on commit 985c733

Please sign in to comment.