Skip to content

Commit

Permalink
Kindsys: Include @grafanamaturity counts to Kinds report (grafana#61911)
Browse files Browse the repository at this point in the history
* Kindsys: Include @grafanamaturity counts to Kinds report

* Replace reflect.DeepEqual with regular (in)equality op

* Move reference check to its use (inline)

* Fix linter complains
  • Loading branch information
joanlopez authored Jan 24, 2023
1 parent 7c786c1 commit 479da46
Show file tree
Hide file tree
Showing 3 changed files with 170 additions and 9 deletions.
74 changes: 74 additions & 0 deletions pkg/kindsys/kindsysreport/kindsysreport.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package kindsysreport

import (
"cuelang.org/go/cue"
)

type AttributeWalker struct {
seen map[cue.Value]bool
count map[string]int
}

func (w *AttributeWalker) Count(sch cue.Value, attrs ...string) map[string]int {
w.seen = make(map[cue.Value]bool)
w.count = make(map[string]int)

for _, attr := range attrs {
w.count[attr] = 0
}

w.walk(cue.MakePath(), sch)
return w.count
}

func (w *AttributeWalker) walk(p cue.Path, v cue.Value) {
if w.seen[v] {
return
}

w.seen[v] = true

for attr := range w.count {
if found := v.Attribute(attr); found.Err() == nil {
w.count[attr]++
}
}

// nolint: exhaustive
switch v.Kind() {
case cue.StructKind:
// If current cue.Value is a reference to another
// definition, we don't want to traverse its fields
// individually, because we'll do so for the actual def.
if v != cue.Dereference(v) {
return
}

iter, err := v.Fields(cue.All())
if err != nil {
panic(err)
}

for iter.Next() {
w.walk(appendPath(p, iter.Selector()), iter.Value())
}
if lv := v.LookupPath(cue.MakePath(cue.AnyString)); lv.Exists() {
w.walk(appendPath(p, cue.AnyString), lv)
}
case cue.ListKind:
list, err := v.List()
if err != nil {
panic(err)
}
for i := 0; list.Next(); i++ {
w.walk(appendPath(p, cue.Index(i)), list.Value())
}
if lv := v.LookupPath(cue.MakePath(cue.AnyIndex)); lv.Exists() {
w.walk(appendPath(p, cue.AnyString), lv)
}
}
}

func appendPath(p cue.Path, sel cue.Selector) cue.Path {
return cue.MakePath(append(p.Selectors(), sel)...)
}
31 changes: 22 additions & 9 deletions pkg/kindsys/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@ import (
"sort"
"strings"

"cuelang.org/go/cue"

"github.com/grafana/codejen"
"github.com/grafana/grafana/pkg/kindsys"
"github.com/grafana/grafana/pkg/kindsys/kindsysreport"
"github.com/grafana/grafana/pkg/plugins/pfs/corelist"
"github.com/grafana/grafana/pkg/plugins/plugindef"
"github.com/grafana/grafana/pkg/registry/corekind"
Expand Down Expand Up @@ -82,8 +85,9 @@ type KindLinks struct {

type Kind struct {
kindsys.SomeKindProperties
Category string
Links KindLinks
Category string
Links KindLinks
GrafanaMaturityCount int
}

// MarshalJSON is overwritten to marshal
Expand All @@ -100,6 +104,7 @@ func (k Kind) MarshalJSON() ([]byte, error) {
}

m["category"] = k.Category
m["grafanaMaturityCount"] = k.GrafanaMaturityCount

m["links"] = map[string]string{}
for _, ref := range []string{"Schema", "Go", "Ts", "Docs"} {
Expand Down Expand Up @@ -177,13 +182,14 @@ func buildKindStateReport() *KindStateReport {
seen := make(map[string]bool)
for _, k := range b.All() {
seen[k.Props().Common().Name] = true
k.Lineage()
lin := k.Lineage()
switch k.Props().(type) {
case kindsys.CoreProperties:
r.add(Kind{
SomeKindProperties: k.Props(),
Category: "core",
Links: buildCoreLinks(k.Lineage(), k.Decl().Properties),
SomeKindProperties: k.Props(),
Category: "core",
Links: buildCoreLinks(lin, k.Decl().Properties),
GrafanaMaturityCount: grafanaMaturityAttrCount(lin.Latest().Underlying()),
})
}
}
Expand Down Expand Up @@ -212,9 +218,10 @@ func buildKindStateReport() *KindStateReport {
for _, si := range all {
if ck, has := pp.ComposableKinds[si.Name()]; has {
r.add(Kind{
SomeKindProperties: ck.Props(),
Category: "composable",
Links: buildComposableLinks(pp.Properties, ck.Decl().Properties),
SomeKindProperties: ck.Props(),
Category: "composable",
Links: buildComposableLinks(pp.Properties, ck.Decl().Properties),
GrafanaMaturityCount: grafanaMaturityAttrCount(ck.Lineage().Latest().Underlying()),
})
} else if may := si.Should(string(pp.Properties.Type)); may {
n := plugindef.DerivePascalName(pp.Properties) + si.Name()
Expand Down Expand Up @@ -306,6 +313,12 @@ func buildComposableLinks(pp plugindef.PluginDef, cp kindsys.ComposablePropertie
}
}

func grafanaMaturityAttrCount(sch cue.Value) int {
const attr = "grafanamaturity"
aw := new(kindsysreport.AttributeWalker)
return aw.Count(sch, attr)[attr]
}

func machinize(s string) string {
return strings.Map(func(r rune) rune {
switch {
Expand Down
Loading

0 comments on commit 479da46

Please sign in to comment.