-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraph.go
More file actions
280 lines (255 loc) · 7.19 KB
/
Copy pathgraph.go
File metadata and controls
280 lines (255 loc) · 7.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
package main
import (
"fmt"
"regexp"
"strings"
services "github.com/CHESSComputing/golib/services"
"github.com/CHESSComputing/golib/utils"
)
// ---- cytoscape.js element shapes ----
type NodeData struct {
ID string `json:"id"`
Label string `json:"label"`
Group string `json:"group"` // drives color-coding (schema name)
Details map[string]any `json:"details"` // full record, shown on click
}
type GraphNode struct {
Data NodeData `json:"data"`
}
type EdgeData struct {
ID string `json:"id"`
Source string `json:"source"`
Target string `json:"target"`
}
type GraphEdge struct {
Data EdgeData `json:"data"`
}
type GraphElements struct {
Nodes []GraphNode `json:"nodes"`
Edges []GraphEdge `json:"edges"`
}
// fetchGraphRecords is a placeholder data source. Replace this with a real
// call to your FOXDEN MetaData/DataDiscovery API — the rest of the
// pipeline (buildGraph, template rendering) doesn't need to change,
// as long as each returned record has "did", "parent_dids" and
// (ideally) "schema".
func fetchGraphRecords(did string) []map[string]any {
// use did:/beamline=3a/btr=test-demo/cycle=2026-1/sample_name=test-demo/test=Composite
// for testing
var records []map[string]any
if mrec, err := findMetadataRecord(did); err == nil {
records = append(records, mrec)
mdid, _ := mrec["did"].(string)
if provRecords, err := getData("provenance", mdid); err == nil {
records = append(records, provRecords...)
}
}
// loop over parents records and append them to our list of records
for _, r := range utils.List2Set(getParents(did)) {
if mrec, err := findMetadataRecord(r); err == nil {
records = append(records, mrec)
mdid, _ := mrec["did"].(string)
if provRecords, err := getData("provenance", mdid); err == nil {
records = append(records, provRecords...)
}
}
}
// get provenance record for our did
if provRecords, err := getData("provenance", did); err == nil {
records = append(records, provRecords...)
}
// get children from provenance service
var parents []string
provParents, _ := getData("parents", did)
for _, r := range provParents {
if f, ok := r["parent_did"]; ok {
if f != nil {
v := f.(string)
parents = append(parents, v)
}
}
}
// ensure that parents is unique list
parents = utils.List2Set(parents)
for _, r := range parents {
if mrec, err := findMetadataRecord(r); err == nil {
records = append(records, mrec)
provRecords, _ := getData("provenance", did)
records = append(records, provRecords...)
}
}
// get spec scan records
spec := make(map[string]any)
spec["did"] = did
specRecords, err := fetchSpecScans(services.ServiceRequest{
Client: "frontend",
ServiceQuery: services.ServiceQuery{
Spec: spec,
},
})
if err == nil {
for _, r := range specRecords {
if _, ok := r["schema"]; !ok {
r["schema"] = "specscans"
}
records = append(records, r)
}
}
return records
}
// buildGraph turns a flat list of records into cytoscape.js elements.
// Each record's "did" becomes a node id; each entry in "parent_dids"
// becomes a directed edge parent -> child.
func buildGraph(records []map[string]any) GraphElements {
var re1 = regexp.MustCompile(`^/provenance-[^/]+`)
var re2 = regexp.MustCompile(`^/specscans-[^/]+`)
var els GraphElements
var provIdx, specIdx int
var nodeids []string
for _, r := range records {
schema, _ := r["schema"].(string)
if schema == "specscans" {
specIdx += 1
}
}
for _, r := range records {
did, _ := r["did"].(string)
if did == "" {
continue // skip malformed records
}
schema, _ := r["schema"].(string)
group := schema
nodeID := fmt.Sprintf("/record%s", did)
if schema == "specscans" {
//group = fmt.Sprintf("specscans-%d", specIdx)
//specIdx += 1
group = "specscans"
nodeID = fmt.Sprintf("/specscans-%d%s", specIdx, did)
} else if schema == "" || schema == "provenance" {
group = fmt.Sprintf("provenance-%d", provIdx)
nodeID = fmt.Sprintf("/provenance-%d%s", provIdx, did)
provIdx += 1
}
label := shortLabel(r, group)
if !utils.InList(nodeID, nodeids) {
els.Nodes = append(els.Nodes, GraphNode{Data: NodeData{
ID: nodeID,
Label: label,
Group: group,
Details: r,
}})
}
nodeids = append(nodeids, nodeID)
}
provIdx = 0
for _, r := range records {
did, _ := r["did"].(string)
if did == "" {
continue // skip malformed records
}
// create nodeID from record did
schema, _ := r["schema"].(string)
group := schema
nodeID := fmt.Sprintf("/record%s", did)
if group == "" {
nodeID = fmt.Sprintf("/provenance-%d%s", provIdx, did)
provIdx += 1
}
for _, parentDid := range toStringSlice(r["parent_dids"]) {
if parentDid == "" {
continue
}
var parentNodeID string
schema, _ := r["schema"].(string)
if schema != "" {
parentNodeID = fmt.Sprintf("/record%s", parentDid)
if utils.InList(parentNodeID, nodeids) {
els.Edges = append(els.Edges, GraphEdge{Data: EdgeData{
ID: parentDid + "->" + nodeID,
Source: parentNodeID,
Target: nodeID,
}})
}
}
}
// if record only contain parent_did (provenance records)
if parentVal, ok := r["parent_did"]; ok {
parentDid := parentVal.(string)
var parentNodeID string
schema, _ := r["schema"].(string)
if schema != "" {
parentNodeID = fmt.Sprintf("/record%s", parentDid)
if parentDid != "" {
els.Edges = append(els.Edges, GraphEdge{Data: EdgeData{
ID: parentDid + "->" + nodeID,
Source: parentNodeID,
Target: nodeID,
}})
}
}
}
}
// add provenance and specscans edges
for _, nodeID := range nodeids {
// if !strings.HasPrefix(nodeID, "/provenance") {
if strings.HasPrefix(nodeID, "/record") {
continue
}
// check out provenance edges
if strings.HasPrefix(nodeID, "/provenance") {
recordID := re1.ReplaceAllString(nodeID, "/record")
els.Edges = append(els.Edges, GraphEdge{Data: EdgeData{
ID: nodeID + "->" + recordID,
Source: nodeID,
Target: recordID,
}})
}
// check out specscans edges
if strings.HasPrefix(nodeID, "/specscans") {
recordID := re2.ReplaceAllString(nodeID, "/record")
els.Edges = append(els.Edges, GraphEdge{Data: EdgeData{
ID: nodeID + "->" + recordID,
Source: nodeID,
Target: recordID,
}})
}
}
return els
}
// toStringSlice safely reads a []any or []string field from
// a decoded JSON map, tolerating records where the field is missing,
// null, or an empty string (as in the ID1A3 example's doi_* fields).
func toStringSlice(v any) []string {
var out []string
switch t := v.(type) {
case []any:
for _, x := range t {
if s, ok := x.(string); ok && s != "" {
out = append(out, s)
}
}
case []string:
out = t
}
return out
}
// shortLabel picks a compact, human-readable node label instead of
// the full did path.
func shortLabel(r map[string]any, group string) string {
doi, _ := r["doi"].(string)
if doi != "" {
return fmt.Sprintf("doi (%s)", doi)
}
app, _ := r["application"].(string)
if app != "" {
return fmt.Sprintf("metadata (%s)", app)
}
schema, _ := r["schema"].(string)
if schema == "specscans" {
return group
}
if schema != "" {
return fmt.Sprintf("metadata (%s)", schema)
}
return group
}