Skip to content

Commit a76ca6f

Browse files
committed
feat(pkger): add single stat plus line view support to pkger
1 parent a66d0c9 commit a76ca6f

5 files changed

+701
-24
lines changed

pkger/models.go

+113-18
Original file line numberDiff line numberDiff line change
@@ -167,13 +167,14 @@ type ChartKind string
167167

168168
// available chart kinds
169169
const (
170-
ChartKindUnknown ChartKind = ""
171-
ChartKindSingleStat ChartKind = "single_stat"
170+
ChartKindUnknown ChartKind = ""
171+
ChartKindSingleStat ChartKind = "single_stat"
172+
ChartKindSingleStatPlusLine ChartKind = "single_stat_plus_line"
172173
)
173174

174175
func (c ChartKind) ok() bool {
175176
switch c {
176-
case ChartKindSingleStat:
177+
case ChartKindSingleStat, ChartKindSingleStatPlusLine:
177178
return true
178179
default:
179180
return false
@@ -457,8 +458,10 @@ type chart struct {
457458
DecimalPlaces int
458459
EnforceDecimals bool
459460
Shade bool
460-
Colors []*color
461-
Queries []query
461+
Legend legend
462+
Colors colors
463+
Queries queries
464+
Axes axes
462465

463466
XCol, YCol string
464467
XPos, YPos int
@@ -478,8 +481,27 @@ func (c chart) properties() influxdb.ViewProperties {
478481
},
479482
Note: c.Note,
480483
ShowNoteWhenEmpty: c.NoteOnEmpty,
481-
Queries: queries(c.Queries).influxDashQueries(),
482-
ViewColors: colors(c.Colors).influxViewColors(),
484+
Queries: c.Queries.influxDashQueries(),
485+
ViewColors: c.Colors.influxViewColors(),
486+
}
487+
case ChartKindSingleStatPlusLine:
488+
return influxdb.LinePlusSingleStatProperties{
489+
Type: "line-plus-single-stat",
490+
Prefix: c.Prefix,
491+
Suffix: c.Suffix,
492+
DecimalPlaces: influxdb.DecimalPlaces{
493+
IsEnforced: c.EnforceDecimals,
494+
Digits: int32(c.DecimalPlaces),
495+
},
496+
Note: c.Note,
497+
ShowNoteWhenEmpty: c.NoteOnEmpty,
498+
XColumn: c.XCol,
499+
YColumn: c.YCol,
500+
ShadeBelow: c.Shade,
501+
Legend: c.Legend.influxLegend(),
502+
Queries: c.Queries.influxDashQueries(),
503+
ViewColors: c.Colors.influxViewColors(),
504+
Axes: c.Axes.influxAxes(),
483505
}
484506
default:
485507
return nil
@@ -491,8 +513,8 @@ func (c chart) validProperties() []failure {
491513

492514
validatorFns := []func() []failure{
493515
c.validBaseProps,
494-
queries(c.Queries).valid,
495-
colors(c.Colors).valid,
516+
c.Queries.valid,
517+
c.Colors.valid,
496518
}
497519
for _, validatorFn := range validatorFns {
498520
fails = append(fails, validatorFn()...)
@@ -501,14 +523,10 @@ func (c chart) validProperties() []failure {
501523
// chart kind specific validations
502524
switch c.Kind {
503525
case ChartKindSingleStat:
504-
for i, clr := range c.Colors {
505-
if clr.Type != colorTypeText {
506-
fails = append(fails, failure{
507-
Field: fmt.Sprintf("colors[%d].type", i),
508-
Msg: "single stat charts must have color type of \"text\"",
509-
})
510-
}
511-
}
526+
fails = append(fails, c.Colors.hasTypes(colorTypeText)...)
527+
case ChartKindSingleStatPlusLine:
528+
fails = append(fails, c.Colors.hasTypes(colorTypeText, colorTypeScale)...)
529+
fails = append(fails, c.Axes.hasAxes("x", "y")...)
512530
}
513531

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

535553
const (
536-
colorTypeText = "text"
554+
colorTypeText = "text"
555+
colorTypeScale = "scale"
537556
)
538557

539558
type color struct {
@@ -565,6 +584,25 @@ func (c colors) influxViewColors() []influxdb.ViewColor {
565584
return iColors
566585
}
567586

587+
func (c colors) hasTypes(types ...string) []failure {
588+
tMap := make(map[string]bool)
589+
for _, cc := range c {
590+
tMap[cc.Type] = true
591+
}
592+
593+
var failures []failure
594+
for _, t := range types {
595+
if !tMap[t] {
596+
failures = append(failures, failure{
597+
Field: "colors",
598+
Msg: fmt.Sprintf("type not found: %q", t),
599+
})
600+
}
601+
}
602+
603+
return failures
604+
}
605+
568606
func (c colors) valid() []failure {
569607
var fails []failure
570608
if len(c) == 0 {
@@ -626,3 +664,60 @@ func (q queries) valid() []failure {
626664

627665
return fails
628666
}
667+
668+
type axis struct {
669+
Base string
670+
Label string
671+
Name string
672+
Prefix string
673+
Scale string
674+
Suffix string
675+
}
676+
677+
type axes []axis
678+
679+
func (a axes) influxAxes() map[string]influxdb.Axis {
680+
m := make(map[string]influxdb.Axis)
681+
for _, ax := range a {
682+
m[ax.Name] = influxdb.Axis{
683+
Bounds: []string{},
684+
Label: ax.Label,
685+
Prefix: ax.Prefix,
686+
Suffix: ax.Suffix,
687+
Base: ax.Base,
688+
Scale: ax.Scale,
689+
}
690+
}
691+
return m
692+
}
693+
694+
func (a axes) hasAxes(expectedAxes ...string) []failure {
695+
mAxes := make(map[string]bool)
696+
for _, ax := range a {
697+
mAxes[ax.Name] = true
698+
}
699+
700+
var failures []failure
701+
for _, expected := range expectedAxes {
702+
if !mAxes[expected] {
703+
failures = append(failures, failure{
704+
Field: "axes",
705+
Msg: fmt.Sprintf("axis not found: %q", expected),
706+
})
707+
}
708+
}
709+
710+
return failures
711+
}
712+
713+
type legend struct {
714+
Orientation string
715+
Type string
716+
}
717+
718+
func (l legend) influxLegend() influxdb.Legend {
719+
return influxdb.Legend{
720+
Type: l.Type,
721+
Orientation: l.Orientation,
722+
}
723+
}

pkger/parser.go

+26-3
Original file line numberDiff line numberDiff line change
@@ -577,6 +577,11 @@ func parseChart(r Resource) (chart, []failure) {
577577
Width: r.intShort("width"),
578578
}
579579

580+
if leg, ok := ifaceMapToResource(r["legend"]); ok {
581+
c.Legend.Type = leg.stringShort("type")
582+
c.Legend.Orientation = leg.stringShort("orientation")
583+
}
584+
580585
if dp, ok := r.int("decimalPlaces"); ok {
581586
c.EnforceDecimals = true
582587
c.DecimalPlaces = dp
@@ -585,7 +590,7 @@ func parseChart(r Resource) (chart, []failure) {
585590
var failures []failure
586591
for _, rq := range r.slcResource("queries") {
587592
c.Queries = append(c.Queries, query{
588-
Query: rq.stringShort("query"),
593+
Query: strings.TrimSpace(rq.stringShort("query")),
589594
})
590595
}
591596

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

607+
for _, ra := range r.slcResource("axes") {
608+
c.Axes = append(c.Axes, axis{
609+
Base: ra.stringShort("base"),
610+
Label: ra.stringShort("label"),
611+
Name: ra.Name(),
612+
Prefix: ra.stringShort("prefix"),
613+
Scale: ra.stringShort("scale"),
614+
Suffix: ra.stringShort("suffix"),
615+
})
616+
}
617+
602618
if fails := c.validProperties(); len(fails) > 0 {
603619
failures = append(failures, fails...)
604620
}
@@ -716,8 +732,15 @@ func (r Resource) intShort(key string) int {
716732
}
717733

718734
func (r Resource) string(key string) (string, bool) {
719-
s, ok := r[key].(string)
720-
return s, ok
735+
if s, ok := r[key].(string); ok {
736+
return s, true
737+
}
738+
739+
if i, ok := r[key].(int); ok {
740+
return strconv.Itoa(i), true
741+
}
742+
743+
return "", false
721744
}
722745

723746
func (r Resource) stringShort(key string) string {

0 commit comments

Comments
 (0)