-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathframes.go
664 lines (627 loc) · 18.1 KB
/
frames.go
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
// Package dataset includes the operations needed for processing collections of JSON documents and their attachments.
//
// Authors R. S. Doiel, <[email protected]> and Tom Morrel, <[email protected]>
//
// Copyright (c) 2022, Caltech
// All rights not granted herein are expressly reserved by Caltech.
//
// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
package dataset
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"os"
"path"
"sort"
"strings"
"time"
// Caltech Library packages
"github.com/caltechlibrary/dotpath"
)
//
// NOTE: frames.go presents an Object as the native go type map[string]interface{} and DataFrame is intended to let you work with an ordered list of objects.
//
// DataFrame is the basic structure holding a list of objects as well as the definition
// of the list (so you can regenerate an updated list from a changed collection).
// It persists with the collection.
type DataFrame struct {
// Explicit at creation
Name string `json:"frame_name"`
// CollectionName holds the name of the collection the frame was generated from. In theory you could
// define a frame in one collection and use its results in another. A DataFrame can be rendered as a JSON
// document.
CollectionName string `json:"collection_name"`
// DotPaths is a slice holding the definitions of what each Object attribute's data source is.
DotPaths []string `json:"dot_paths"`
// Labels are new attribute names for fields create from the provided
// DotPaths. Typically this is used to surface a deeper dotpath's
// value as something more useful in the frame's context (e.g.
// first_title from an array of titles might be labeled "title")
Labels []string `json:"labels"`
// NOTE: Keys is an orded list of object keys in the frame.
Keys []string `json:"keys"`
// NOTE: Object map privides a quick index by key to object index.
ObjectMap map[string]interface{} `json:"object_map"`
// Created is the date the frame is originally generated and defined
Created time.Time `json:"created"`
// Updated is the date the frame is updated (e.g. reframed)
Updated time.Time `json:"updated"`
}
// frameObject takes a list of dot paths, labels and object key
// then generates a new object based on that.
func (c *Collection) frameObject(key string, dotPaths []string, labels []string) (map[string]interface{}, error) {
errors := []string{}
obj := map[string]interface{}{}
err := c.Read(key, obj)
if err != nil {
return nil, err
}
o := map[string]interface{}{}
for j, dpath := range dotPaths {
value, err := dotpath.Eval(dpath, obj)
if err == nil {
key := labels[j]
o[key] = value
} else {
errors = append(errors, fmt.Sprintf("path %q not found, %q in %q for %+v", dpath, key, c.Name, obj))
}
}
if len(errors) > 0 {
return o, fmt.Errorf("%s", strings.Join(errors, ", "))
}
return o, nil
}
// ObjectList (on a collection) takes a set of collection keys and builds
// an ordered array of objects from the array of keys, dot paths and
// labels provided.
//
// ```
//
// var mapList []map[string]interface{}
//
// keys := []string{ "123", "124", "125" }
// dotPaths := []string{ ".title", ".description" }
// labels := []string{ "Title", "Description" }
// verbose := true
// mapList, err = c.ObjectList(keys, dotPaths, labels, verbose)
//
// ```
func (c *Collection) ObjectList(keys []string, dotPaths []string, labels []string, verbose bool) ([]map[string]interface{}, error) {
if len(dotPaths) != len(labels) {
return nil, fmt.Errorf("dot paths and labels do not match")
}
pid := os.Getpid()
objectList := make([]map[string]interface{}, len(keys))
for i, key := range keys {
obj, err := c.frameObject(key, dotPaths, labels)
if verbose == true {
if err != nil {
log.Printf("(pid: %d) WARNING: framing error, %s", pid, err)
}
if obj == nil {
log.Printf("(pid: %d) WARNING: skipping object %q (%d) in %q, object is nil", pid, key, i, c.Name)
}
}
if obj != nil {
objectList = append(objectList, obj)
}
if verbose && (i > 0) && ((i % 1000) == 0) {
log.Printf("(pid: %d) %d keys processed", pid, i)
}
}
return objectList, nil
}
// HasFrame checks if a frame is defined already. Collection needs
// to previously been opened.
//
// ```
//
// frameName := "journals"
// if c.HasFrame(frameName) {
// ...
// }
//
// ```
func (c *Collection) HasFrame(frameName string) bool {
framePath := path.Join(c.workPath, "_frames",
path.Base(frameName)+".json")
if _, err := os.Stat(framePath); os.IsNotExist(err) {
return false
}
return true
}
// getFrame retrieves a frame by frame name from a collection.
func (c *Collection) getFrame(key string) (*DataFrame, error) {
framePath := path.Join(c.workPath, "_frames", key+".json")
src, err := ioutil.ReadFile(framePath)
if err != nil {
return nil, err
}
f := new(DataFrame)
if err := json.Unmarshal(src, &f); err != nil {
return nil, err
}
// Double check if we have a bad object_map?
if f.ObjectMap == nil {
f.ObjectMap = map[string]interface{}{}
}
// return frame and error
return f, err
}
// setFrame writes a DataFrame struct to the collection
func (c *Collection) setFrame(key string, f *DataFrame) error {
// Check to see if we have a _frames directory to store our frames in
if _, err := os.Stat(path.Join(c.workPath, "_frames")); err != nil {
if err := os.MkdirAll(path.Join(c.workPath, "_frames"), 0775); err != nil {
return err
}
}
// Sanity check on frameName and collectionName
f.CollectionName = c.Name
f.Name = key
// render DataFrame to JSON for storage
src, err := JSONMarshal(f)
if err != nil {
return err
}
// calculate the path to store the frame
fName := key
if strings.HasSuffix(fName, ".json") == false {
fName = key + ".json"
}
savedPath := path.Join("_frames", fName)
// update c.FrameMap with rel path to frame
// save metadata and return
err = os.WriteFile(path.Join(c.workPath, savedPath), src, 0664)
if err != nil {
return err
}
return nil
}
// rmFrame removes a frame from storage as well as from frames.json
func (c *Collection) rmFrame(key string) error {
framePath := path.Join(c.workPath, "_frames", key+".json")
if _, err := os.Stat(framePath); os.IsNotExist(err) {
return fmt.Errorf("frame %q does not exist", key)
}
return os.RemoveAll(framePath)
}
// FrameCreate takes a set of collection keys, dot paths and labels
// builds an ObjectList and assembles additional metadata returning
// a new Frame associated with the collection as well as an error value.
// If there is a mis-match in number of labels and dot paths an an error
// will be returned. If the frame already exists an error will be returned.
//
// Conceptually a frame is an ordered list of objects. Frames are
// associated with a collection and the objects in a frame can
// easily be refreshed. Frames also serve as the basis for indexing
// a dataset collection and provide the data paths (expressed
// as a list of "dot paths"), labels (aka attribute names),
// and type information needed for indexing and search.
//
// If you need to update a frame's objects use FrameRefresh(). If
// you need to change a frame's objects or ordering use FrameReframe().
//
// ```
//
// frameName := "journals"
// keys := []string{ "123", "124", "125" }
// dotPaths := []string{ ".title", ".description" }
// labels := []string{ "Title", "Description" }
// verbose := true
// frame, err := c.FrameCreate(frameName, keys, dotPaths, labels, verbose)
// if err != nil {
// ...
// }
//
// ```
func (c *Collection) FrameCreate(name string, keys []string, dotPaths []string, labels []string, verbose bool) (*DataFrame, error) {
frameDir := path.Join(c.workPath, "_frames")
if _, err := os.Stat(frameDir); os.IsNotExist(err) {
os.MkdirAll(frameDir, 0775)
}
// If frame exists return the existing frame
if c.HasFrame(name) {
return nil, fmt.Errorf("frame %q exists in %q", name, c.workPath)
}
// Case of new Frame and with ObjectList
if labels != nil && dotPaths != nil &&
len(labels) != len(dotPaths) {
return nil, fmt.Errorf("Mismatched dot paths and labels")
}
f := new(DataFrame)
f.Name = name
f.CollectionName = c.Name
f.DotPaths = dotPaths[:]
f.Labels = labels[:]
f.Keys = []string{}
f.ObjectMap = make(map[string]interface{})
f.Created = time.Now()
f.Updated = time.Now()
// Populate our Object List
pid := os.Getpid()
for i, key := range keys {
obj, err := c.frameObject(key, f.DotPaths, f.Labels)
if verbose == true {
if err != nil {
log.Printf("(pid: %d) WARNING: framing error for key %q (%d), %s", pid, key, i, err)
}
if obj == nil {
log.Printf("(pid: %d) WARNING: skipping object for key %q (%d), object is nil", pid, key, i)
}
}
if obj != nil {
f.ObjectMap[key] = obj
f.Keys = append(f.Keys, key)
}
}
framePath := path.Join(c.workPath, "_frames", name+".json")
src, err := JSONMarshal(f)
if err != nil {
return f, fmt.Errorf("failed to encode fame %q, %s", name, err)
}
if err := ioutil.WriteFile(framePath, src, 0666); err != nil {
return f, fmt.Errorf("failed to write frame %q, %s", name, err)
}
return f, nil
}
// Frames retrieves a list of available frame names associated with a
// collection.
//
// ```
//
// frameNames := c.FrameNames()
// for _, name := range frames {
// // do something with each frame name
// objects, err := c.FrameObjects(name)
// ...
// }
//
// ```
func (c *Collection) FrameNames() []string {
framesDir := path.Join(c.workPath, "_frames")
files, err := os.ReadDir(framesDir)
if err != nil {
return []string{}
}
keys := []string{}
for _, file := range files {
keys = append(keys, strings.TrimSuffix(path.Base(file.Name()), ".json"))
}
sort.Strings(keys)
return keys
}
// FrameKeys retrieves a list of keys assocaited with a data frame
//
// ```
//
// frameName := "journals"
// keys := c.FrameKeys(frameName)
//
// ```
func (c *Collection) FrameKeys(name string) []string {
frame, err := c.FrameRead(name)
if err != nil {
return []string{}
}
return frame.Keys
}
// FrameRead retrieves a frame from a collection.
// Returns the DataFrame and an error value
//
// ```
//
// frameName := "journals"
// data, err := c.FrameRead(frameName)
// if err != nil {
// ..
// }
//
// ```
func (c *Collection) FrameRead(name string) (*DataFrame, error) {
return c.getFrame(name)
}
// FrameDef retrieves the frame definition returns a
// a map string interface.
//
// ```
//
// definition := map[string]interface{}{}
// frameName := "journals"
// definition, err := c.FrameDef(frameName)
// if err != nil {
// ..
// }
//
// ```
func (c *Collection) FrameDef(name string) (map[string]interface{}, error) {
frame, err := c.FrameRead(name)
if err != nil {
return nil, err
}
m := map[string]interface{}{
"name": frame.Name,
"dot_paths": frame.DotPaths,
"labels": frame.Labels,
}
return m, nil
}
// FrameRefresh updates a DataFrames' object list based on the
// existing keys in the frame. It doesn't change the order of objects.
// It is used when objects in a collection that are included in the
// frame have been updated. It uses the frame's existing definition.
//
// NOTE: If an object is missing in the collection it gets pruned from
// the object list.
//
// ```
//
// frameName, verbose := "journals", true
// err := c.FrameRefresh(frameName, verbose)
// if err != nil {
// ...
// }
//
// ```
func (c *Collection) FrameRefresh(name string, verbose bool) error {
f, err := c.getFrame(name)
if err != nil {
return err
}
for i, key := range f.Keys {
obj, err := c.frameObject(key, f.DotPaths, f.Labels)
if verbose == true {
if err != nil {
log.Printf("key %q (%d) frame error %s", key, i, err)
}
if obj == nil {
log.Printf("key %q (%d) frame object is nil", key, i)
}
}
if err != nil {
if verbose {
log.Printf("WARNING could not read %q from %q", key, c.workPath)
}
continue
}
if obj != nil {
if _, ok := f.ObjectMap[key]; ok == false {
f.Keys = append(f.Keys, key)
}
f.ObjectMap[key] = obj
} else {
// Remove the stale object
delete(f.ObjectMap, key)
for i, fkey := range f.Keys {
if fkey == key {
// Remove the stale key
f.Keys = append(f.Keys[:i], f.Keys[i+1:]...)
break
}
}
}
}
return c.setFrame(name, f)
}
// FrameReframe **replaces** a frame's object list based on the
// keys provided. It uses the frame's existing definition.
//
// ```
//
// frameName, verbose := "journals", false
// keys := ...
// err := c.FrameReframe(frameName, keys, verbose)
// if err != nil {
// ...
// }
//
// ```
func (c *Collection) FrameReframe(name string, keys []string, verbose bool) error {
f, err := c.getFrame(name)
if err != nil {
return err
}
// GC the stale objects
f.Keys = []string{}
f.ObjectMap = make(map[string]interface{})
// New Keys that will replace the values in f.Keys which are stale.
for _, key := range keys {
obj, err := c.frameObject(key, f.DotPaths, f.Labels)
if verbose == true {
if err != nil {
log.Printf("key %q frame error %s", key, err)
}
if obj == nil {
log.Printf("key %q framed as nil object", key)
}
}
if obj != nil {
f.ObjectMap[key] = obj
f.Keys = append(f.Keys, key)
}
}
// Now update the Keys list with the new keys
f.Updated = time.Now()
return c.setFrame(name, f)
}
// SaveFrame saves a frame in a collection or returns an error
//
// ```
//
// frameName := "journals"
// data, err := c.FrameRead(frameName)
// if err != nil {
// ...
// }
// // do stuff with the frame's data
// ...
// // Save the changed frame data
// err = c.SaveFrame(frameName, data)
//
// ```
func (c *Collection) SaveFrame(name string, f *DataFrame) error {
return c.setFrame(name, f)
}
// FrameClear empties the frame's object and key lists but
// leaves in place the Frame definition. Use Reframe()
// to re-populate a frame based on a new key list.
//
// ```
//
// frameName := "journals"
// err := c.FrameClear(frameName)
// if err != nil {
// ...
// }
func (c *Collection) FrameClear(name string) error {
f, err := c.getFrame(name)
if err != nil {
return err
}
// Emtpy the key and Object list.
f.Keys = []string{}
f.ObjectMap = make(map[string]interface{})
return c.setFrame(name, f)
}
// FrameDelete removes a frame from a collection, returns an error
// if frame can't be deleted.
//
// ```
//
// frameName := "journals"
// err := c.FrameDelete(frameName)
// if err != nil {
// ...
// }
//
// ```
func (c *Collection) FrameDelete(name string) error {
return c.rmFrame(name)
}
// FrameObjects returns a copy of a DataFrame's object list given a
// collection's frame name.
//
// ```
//
// var (
// err error
// objects []map[string]interface{}
// )
// frameName := "journals"
// objects, err = c.FrameObjects(frameName)
// if err != nil {
// ...
// }
//
// ```
func (c *Collection) FrameObjects(fName string) ([]map[string]interface{}, error) {
f, err := c.FrameRead(fName)
if err != nil {
return nil, err
}
ol := f.Objects()
return ol, nil
}
//
// The follow funcs define operations on the Frame struct.
//
// String renders the data structure DataFrame as JSON to a string
//
// ```
// frameName := "journals"
// data, err := c.FrameRead(frameName)
// if err != nil {
// ...
// }
// fmt.Printf("\n%s\n", data.String())
// ```
func (f *DataFrame) String() string {
src, _ := JSONMarshalIndent(f, "", " ")
return fmt.Sprintf("%s", src)
}
// Grid returns a table representaiton of a DataFrame's ObjectList
//
// ```
//
// frameName, includeHeader := "journals", true
// data, err := c.FrameRead(frameName)
// if err != nil {
// ...
// }
// rows, err := data.Grid(includeHeader)
// if err != nil {
// ...
// }
// ... /* now do something with the rows */ ...
//
// ```
func (f *DataFrame) Grid(includeHeaderRow bool) [][]interface{} {
rowCnt := len(f.Keys)
colCnt := len(f.Labels)
if includeHeaderRow == true {
rowCnt++
}
rows := [][]interface{}{}
if includeHeaderRow {
header := make([]interface{}, colCnt)
for i, val := range f.Labels {
header[i] = val
}
rows = append(rows, header)
}
// Now make reset of grid
objectList := f.Objects()
for i, obj := range objectList {
rowNo := i
if includeHeaderRow == true {
rowNo++
}
row := make([]interface{}, colCnt)
for colNo, label := range f.Labels {
if val, OK := obj[label]; OK == true {
row[colNo] = val
} else {
row[colNo] = ""
}
}
if len(row) > 0 {
rows = append(rows, row)
}
}
return rows
}
// Objects returns a copy of DataFrame's object list (an array of map[string]interface{})
//
// ```
//
// frameName := "journals"
// data, err := c.FrameRead(frameName)
// if err != nil {
// ...
// }
// objectList, err := data.Objects()
// if err != nil {
// ...
// }
//
// ```
func (f *DataFrame) Objects() []map[string]interface{} {
ol := []map[string]interface{}{}
for _, key := range f.Keys {
if obj, found := f.ObjectMap[key]; found == true && obj != nil {
rec := obj.(map[string]interface{})
ol = append(ol, rec)
}
}
return ol
}