Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Atlas/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## 0.4.0 (2026-12-01)

- Cypher tables now show enumeration labels. A column that returns an enumeration-bound property of a typed variable (`MATCH (p:CNCProduction) RETURN p.AlarmCode.Value AS Alarm`, with or without `.Value` and `AS`) gets the same value mappings as the Data view, so dashboards no longer need hand-made mappings for Atlas codes. Expressions, functions and untyped variables are left as returned.
- Data > Current: a record property that holds several rows (MaintainX `LiveOpenWorkOrders`: one row per open work order) now gives one table row per record row in the Columns layout, ordered by the row's sequential number, and numbered parts in the Rows layout. It used to show one work order per machine, because Atlas's plain node read returns only one row; the plugin now reads records with a typed `UNWIND`.
- Data > Current: static properties (plain values with no history, such as a person's `Specialty` or a machine's `SerialNumber`) can be picked in the selection tree, marked "fixed value", and type or nested picks of them are read. Trend still offers only properties with history.

## 0.3.0 (2026-11-24)

Breaking: the plugin ID is now `flowsoftware-timebaseatlas-datasource` (was `flowsoftware-atlas-datasource`), so both Timebase plugins read as Timebase products (the Historian's is `flowsoftware-timebasehistorian-datasource`). It installs as a new plugin: data sources and dashboards made with 0.2.0 have to be recreated or pointed at the new type, and the `allow_loading_unsigned_plugins` setting needs the new ID.
Expand Down
4 changes: 2 additions & 2 deletions Atlas/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Atlas/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "timebaseatlas-datasource",
"version": "0.3.0",
"version": "0.4.0",
"scripts": {
"build": "webpack -c ./.config/webpack/webpack.config.ts --env production",
"dev": "webpack -w -c ./.config/webpack/webpack.config.ts --env development",
Expand Down
149 changes: 149 additions & 0 deletions Atlas/pkg/plugin/cypherenums.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
package plugin

import (
"regexp"
"strings"

"github.com/grafana/grafana-plugin-sdk-go/data"
)

// Enumerations on Cypher tables.
//
// A Cypher table column is just a name ("Alarm"), so the plugin cannot see
// that it holds an enumeration-bound property. It reads that from the query
// text instead: the node patterns give each variable its type
// ("(p:CNCProduction)"), and each RETURN item that is a plain property read
// ("p.AlarmCode", "p.AlarmCode.Value", optionally "AS Alarm") names the
// property. When the property is bound to an enumeration on that type, the
// column gets the same value mappings the Values and History views attach.
// Anything else (expressions, functions, CASE, untyped variables) is left as
// it is, so a query the parser does not understand behaves as before.

var (
cypherNodePattern = regexp.MustCompile(`\(\s*([A-Za-z_]\w*)\s*:\s*` + "`?" + `([A-Za-z_][\w-]*)`)
cypherReturnWord = regexp.MustCompile(`(?i)\bRETURN\b`)
cypherReturn = regexp.MustCompile(`(?is)^RETURN\s+(?:DISTINCT\s+)?(.*)$`)
cypherReturnEnd = regexp.MustCompile(`(?is)\s+(?:ORDER\s+BY|SKIP|LIMIT|UNION)\b`)
cypherReturnItem = regexp.MustCompile("(?is)^([A-Za-z_]\\w*)\\.([A-Za-z_]\\w*)(?:\\.Value)?(?:\\s+AS\\s+(`[^`]+`|[A-Za-z_]\\w*))?$")
)

// cypherColumn is one RETURN item that reads a property of a typed variable.
type cypherColumn struct {
Column string // the column name the result will carry
Variable string
Type string // local type name from the node pattern
Property string // local property name
}

// cypherPropertyColumns lists the RETURN items of the last RETURN clause that
// are plain property reads of a variable with a type label.
func cypherPropertyColumns(cypher string) []cypherColumn {
types := map[string]string{}
for _, m := range cypherNodePattern.FindAllStringSubmatch(cypher, -1) {
if _, seen := types[m[1]]; !seen {
types[m[1]] = m[2]
}
}
if len(types) == 0 {
return nil
}
// The last RETURN is the one that shapes the result.
idx := cypherReturnWord.FindAllStringIndex(cypher, -1)
if len(idx) == 0 {
return nil
}
m := cypherReturn.FindStringSubmatch(cypher[idx[len(idx)-1][0]:])
if m == nil {
return nil
}
clause := m[1]
if end := cypherReturnEnd.FindStringIndex(clause); end != nil {
clause = clause[:end[0]]
}
var out []cypherColumn
for _, item := range splitTopLevel(clause) {
item = strings.TrimSpace(item)
m := cypherReturnItem.FindStringSubmatch(item)
if m == nil {
continue
}
t, ok := types[m[1]]
if !ok {
continue
}
col := strings.Trim(m[3], "`")
if col == "" {
col = item
}
out = append(out, cypherColumn{Column: col, Variable: m[1], Type: t, Property: m[2]})
}
return out
}

// splitTopLevel splits a RETURN clause on commas that are not inside
// brackets, braces, parentheses or quotes.
func splitTopLevel(s string) []string {
var parts []string
depth, start := 0, 0
var quote rune
for i, r := range s {
switch {
case quote != 0:
if r == quote {
quote = 0
}
case r == '"' || r == '\'' || r == '`':
quote = r
case r == '(' || r == '[' || r == '{':
depth++
case r == ')' || r == ']' || r == '}':
depth--
case r == ',' && depth == 0:
parts = append(parts, s[start:i])
start = i + 1
}
}
return append(parts, s[start:])
}

// typeURIByLocal finds the entity type whose local name is name.
func (r *kindResolver) typeURIByLocal(name string) string {
if r == nil {
return ""
}
for uri := range r.types {
if localName(uri) == name {
return uri
}
}
return ""
}

// applyCypherTableEnums puts enumeration value mappings on the columns of a
// Cypher table that return an enumeration-bound property.
func applyCypherTableEnums(frame *data.Frame, cypher string, r *kindResolver) {
if frame == nil || r == nil || len(r.enums) == 0 {
return
}
cols := cypherPropertyColumns(cypher)
if len(cols) == 0 {
return
}
byName := map[string]cypherColumn{}
for _, c := range cols {
byName[c.Column] = c
byName[strings.Join(strings.Fields(c.Column), "")] = c // Atlas may echo an unaliased item without spaces
}
for _, fl := range frame.Fields {
c, ok := byName[fl.Name]
if !ok {
c, ok = byName[strings.Join(strings.Fields(fl.Name), "")]
}
if !ok {
continue
}
if e := r.enumInfoOf(r.typeURIByLocal(c.Type), c.Property); e != nil {
setMappings(fl, e)
}
}
}
68 changes: 68 additions & 0 deletions Atlas/pkg/plugin/cypherenums_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package plugin

import (
"testing"

"github.com/grafana/grafana-plugin-sdk-go/data"

"github.com/flow-software/Timebase-Grafana/Atlas/pkg/atlas"
)

func TestCypherPropertyColumns(t *testing.T) {
q := "MATCH (m:CNCMachine)-[]->(p:CNCProduction)\n" +
"RETURN m.entityId AS Machine, p.AlarmCode.Value AS Alarm, p.Program AS `Part program`,\n" +
" p.MTBF.Value * 2 AS Double, count(p) AS n, p.State, CASE WHEN p.X = 1 THEN 'a' ELSE 'b' END AS c\n" +
"ORDER BY Machine LIMIT 5"
got := map[string]cypherColumn{}
for _, c := range cypherPropertyColumns(q) {
got[c.Column] = c
}
for col, want := range map[string]cypherColumn{
"Machine": {Variable: "m", Type: "CNCMachine", Property: "entityId"},
"Alarm": {Variable: "p", Type: "CNCProduction", Property: "AlarmCode"},
"Part program": {Variable: "p", Type: "CNCProduction", Property: "Program"},
"p.State": {Variable: "p", Type: "CNCProduction", Property: "State"},
} {
g, ok := got[col]
if !ok || g.Variable != want.Variable || g.Type != want.Type || g.Property != want.Property {
t.Errorf("column %q: got %+v (found %v), want %+v", col, g, ok, want)
}
}
for _, skipped := range []string{"Double", "n", "c"} {
if _, ok := got[skipped]; ok {
t.Errorf("expression column %q should not be treated as a property read", skipped)
}
}
if len(cypherPropertyColumns("MATCH (n) RETURN n.State AS s")) != 0 {
t.Error("an untyped variable has no type to look the enumeration up on")
}
// Only the last RETURN shapes the result.
cols := cypherPropertyColumns("CALL { MATCH (a:Machine) RETURN a.State AS x } MATCH (b:Machine) RETURN b.Mode AS y")
if len(cols) != 1 || cols[0].Column != "y" {
t.Errorf("last RETURN: %+v", cols)
}
}

func TestApplyCypherTableEnums(t *testing.T) {
tvq := &atlas.CompoundRange{BaseCompoundURI: tvqURI, FieldEnumOverrides: map[string]string{"Value": stateEnumURI}}
r := newKindResolver([]atlas.EntityType{
{URI: "urn:x:Machine", Properties: []atlas.EntityProperty{{URI: "urn:x:State", CompoundRange: tvq}, {URI: "urn:x:RPM"}}},
}, []atlas.EnumType{stateEnum()})
frame := data.NewFrame("result",
data.NewField("Machine", nil, []*string{}),
data.NewField("Now", nil, []*float64{}),
data.NewField("m.State", nil, []*float64{}),
data.NewField("Speed", nil, []*float64{}),
)
applyCypherTableEnums(frame, "MATCH (m:Machine) RETURN m.entityId AS Machine, m.State.Value AS Now, m.State, m.RPM.Value AS Speed", r)
for _, f := range frame.Fields {
has := f.Config != nil && len(f.Config.Mappings) > 0
want := f.Name == "Now" || f.Name == "m.State"
if has != want {
t.Errorf("%s: mappings %v, want %v", f.Name, has, want)
}
}
// A resolver without enumerations, or a nil one, leaves the frame alone.
applyCypherTableEnums(frame, "MATCH (m:Machine) RETURN m.State AS s", nil)
applyCypherTableEnums(nil, "MATCH (m:Machine) RETURN m.State AS s", r)
}
3 changes: 3 additions & 0 deletions Atlas/pkg/plugin/datasource.go
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,9 @@ func (d *Datasource) cypherQuery(ctx context.Context, branch, domain string, qm
}
} else {
frames = data.Frames{cypherTable("result", res.Columns, rows)}
if format != formatSeries {
applyCypherTableEnums(frames[0], text, d.kinds(ctx, branch, domain, false))
}
if format == formatSeries {
if frames, err = splitSeries(frames[0]); err != nil {
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion Atlas/pkg/plugin/datasource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -804,7 +804,7 @@ func TestExpandPicksNestedAndType(t *testing.T) {
{Instance: "CNC01", Scope: "nested", Property: "RPM"},
{Instance: "Plain", Property: "X"},
{Instance: "CNC02", Scope: "nested", NestedType: "urn:x:CNCProduction", Property: "FeedRate"},
})
}, false)
if err != nil {
t.Fatal(err)
}
Expand Down
14 changes: 11 additions & 3 deletions Atlas/pkg/plugin/history.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,9 @@ const (
// expandPicks resolves nested and type picks into plain instance picks, in
// tree order, using the cached instance tree. With a property, only
// instances that have it as a live value are kept, so a scope does not
// report every instance that lacks it.
func (d *Datasource) expandPicks(ctx context.Context, branch, domain string, picks []historyPick) ([]historyPick, error) {
// report every instance that lacks it. withStatic (Current selections) also
// accepts static properties, which have a value but no history.
func (d *Datasource) expandPicks(ctx context.Context, branch, domain string, picks []historyPick, withStatic bool) ([]historyPick, error) {
dynamic := false
for _, p := range picks {
if p.Scope == pickNested || p.Scope == pickType {
Expand All @@ -129,6 +130,13 @@ func (d *Datasource) expandPicks(ctx context.Context, branch, domain string, pic
return true
}
}
if withStatic {
for _, l := range byID[id].Static {
if l.Name == prop {
return true
}
}
}
return false
}
var out []historyPick
Expand Down Expand Up @@ -331,7 +339,7 @@ func (d *Datasource) scopeNodes(ctx context.Context, branch, domain string, qm q
// targets. A pick without a property takes every live property of the
// instance. The notice names picks that give no series.
func (d *Datasource) selectionTargets(ctx context.Context, branch, domain string, picks []historyPick) ([]historyTarget, map[string]string, string, error) {
picks, err := d.expandPicks(ctx, branch, domain, picks)
picks, err := d.expandPicks(ctx, branch, domain, picks, false)
if err != nil {
return nil, nil, "", err
}
Expand Down
Loading