Skip to content

Commit

Permalink
Merge pull request #15736 from influxdata/4974p/pkger_dash_single_sta…
Browse files Browse the repository at this point in the history
…t_plus_line

feat(pkger): add single stat plus line view support to pkger
  • Loading branch information
jsteenb2 authored Nov 4, 2019
2 parents a66d0c9 + a76ca6f commit a78c53d
Show file tree
Hide file tree
Showing 5 changed files with 701 additions and 24 deletions.
131 changes: 113 additions & 18 deletions pkger/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,13 +167,14 @@ type ChartKind string

// available chart kinds
const (
ChartKindUnknown ChartKind = ""
ChartKindSingleStat ChartKind = "single_stat"
ChartKindUnknown ChartKind = ""
ChartKindSingleStat ChartKind = "single_stat"
ChartKindSingleStatPlusLine ChartKind = "single_stat_plus_line"
)

func (c ChartKind) ok() bool {
switch c {
case ChartKindSingleStat:
case ChartKindSingleStat, ChartKindSingleStatPlusLine:
return true
default:
return false
Expand Down Expand Up @@ -457,8 +458,10 @@ type chart struct {
DecimalPlaces int
EnforceDecimals bool
Shade bool
Colors []*color
Queries []query
Legend legend
Colors colors
Queries queries
Axes axes

XCol, YCol string
XPos, YPos int
Expand All @@ -478,8 +481,27 @@ func (c chart) properties() influxdb.ViewProperties {
},
Note: c.Note,
ShowNoteWhenEmpty: c.NoteOnEmpty,
Queries: queries(c.Queries).influxDashQueries(),
ViewColors: colors(c.Colors).influxViewColors(),
Queries: c.Queries.influxDashQueries(),
ViewColors: c.Colors.influxViewColors(),
}
case ChartKindSingleStatPlusLine:
return influxdb.LinePlusSingleStatProperties{
Type: "line-plus-single-stat",
Prefix: c.Prefix,
Suffix: c.Suffix,
DecimalPlaces: influxdb.DecimalPlaces{
IsEnforced: c.EnforceDecimals,
Digits: int32(c.DecimalPlaces),
},
Note: c.Note,
ShowNoteWhenEmpty: c.NoteOnEmpty,
XColumn: c.XCol,
YColumn: c.YCol,
ShadeBelow: c.Shade,
Legend: c.Legend.influxLegend(),
Queries: c.Queries.influxDashQueries(),
ViewColors: c.Colors.influxViewColors(),
Axes: c.Axes.influxAxes(),
}
default:
return nil
Expand All @@ -491,8 +513,8 @@ func (c chart) validProperties() []failure {

validatorFns := []func() []failure{
c.validBaseProps,
queries(c.Queries).valid,
colors(c.Colors).valid,
c.Queries.valid,
c.Colors.valid,
}
for _, validatorFn := range validatorFns {
fails = append(fails, validatorFn()...)
Expand All @@ -501,14 +523,10 @@ func (c chart) validProperties() []failure {
// chart kind specific validations
switch c.Kind {
case ChartKindSingleStat:
for i, clr := range c.Colors {
if clr.Type != colorTypeText {
fails = append(fails, failure{
Field: fmt.Sprintf("colors[%d].type", i),
Msg: "single stat charts must have color type of \"text\"",
})
}
}
fails = append(fails, c.Colors.hasTypes(colorTypeText)...)
case ChartKindSingleStatPlusLine:
fails = append(fails, c.Colors.hasTypes(colorTypeText, colorTypeScale)...)
fails = append(fails, c.Axes.hasAxes("x", "y")...)
}

return fails
Expand All @@ -533,7 +551,8 @@ func (c chart) validBaseProps() []failure {
}

const (
colorTypeText = "text"
colorTypeText = "text"
colorTypeScale = "scale"
)

type color struct {
Expand Down Expand Up @@ -565,6 +584,25 @@ func (c colors) influxViewColors() []influxdb.ViewColor {
return iColors
}

func (c colors) hasTypes(types ...string) []failure {
tMap := make(map[string]bool)
for _, cc := range c {
tMap[cc.Type] = true
}

var failures []failure
for _, t := range types {
if !tMap[t] {
failures = append(failures, failure{
Field: "colors",
Msg: fmt.Sprintf("type not found: %q", t),
})
}
}

return failures
}

func (c colors) valid() []failure {
var fails []failure
if len(c) == 0 {
Expand Down Expand Up @@ -626,3 +664,60 @@ func (q queries) valid() []failure {

return fails
}

type axis struct {
Base string
Label string
Name string
Prefix string
Scale string
Suffix string
}

type axes []axis

func (a axes) influxAxes() map[string]influxdb.Axis {
m := make(map[string]influxdb.Axis)
for _, ax := range a {
m[ax.Name] = influxdb.Axis{
Bounds: []string{},
Label: ax.Label,
Prefix: ax.Prefix,
Suffix: ax.Suffix,
Base: ax.Base,
Scale: ax.Scale,
}
}
return m
}

func (a axes) hasAxes(expectedAxes ...string) []failure {
mAxes := make(map[string]bool)
for _, ax := range a {
mAxes[ax.Name] = true
}

var failures []failure
for _, expected := range expectedAxes {
if !mAxes[expected] {
failures = append(failures, failure{
Field: "axes",
Msg: fmt.Sprintf("axis not found: %q", expected),
})
}
}

return failures
}

type legend struct {
Orientation string
Type string
}

func (l legend) influxLegend() influxdb.Legend {
return influxdb.Legend{
Type: l.Type,
Orientation: l.Orientation,
}
}
29 changes: 26 additions & 3 deletions pkger/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,11 @@ func parseChart(r Resource) (chart, []failure) {
Width: r.intShort("width"),
}

if leg, ok := ifaceMapToResource(r["legend"]); ok {
c.Legend.Type = leg.stringShort("type")
c.Legend.Orientation = leg.stringShort("orientation")
}

if dp, ok := r.int("decimalPlaces"); ok {
c.EnforceDecimals = true
c.DecimalPlaces = dp
Expand All @@ -585,7 +590,7 @@ func parseChart(r Resource) (chart, []failure) {
var failures []failure
for _, rq := range r.slcResource("queries") {
c.Queries = append(c.Queries, query{
Query: rq.stringShort("query"),
Query: strings.TrimSpace(rq.stringShort("query")),
})
}

Expand All @@ -599,6 +604,17 @@ func parseChart(r Resource) (chart, []failure) {
})
}

for _, ra := range r.slcResource("axes") {
c.Axes = append(c.Axes, axis{
Base: ra.stringShort("base"),
Label: ra.stringShort("label"),
Name: ra.Name(),
Prefix: ra.stringShort("prefix"),
Scale: ra.stringShort("scale"),
Suffix: ra.stringShort("suffix"),
})
}

if fails := c.validProperties(); len(fails) > 0 {
failures = append(failures, fails...)
}
Expand Down Expand Up @@ -716,8 +732,15 @@ func (r Resource) intShort(key string) int {
}

func (r Resource) string(key string) (string, bool) {
s, ok := r[key].(string)
return s, ok
if s, ok := r[key].(string); ok {
return s, true
}

if i, ok := r[key].(int); ok {
return strconv.Itoa(i), true
}

return "", false
}

func (r Resource) stringShort(key string) string {
Expand Down
Loading

0 comments on commit a78c53d

Please sign in to comment.