-
Notifications
You must be signed in to change notification settings - Fork 0
/
metadata.go
82 lines (76 loc) · 1.93 KB
/
metadata.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
package main
import (
"encoding/json"
"log"
srvConfig "github.com/CHESSComputing/golib/config"
mongo "github.com/CHESSComputing/golib/mongo"
)
// metaInsert inserts record into MLHub database
func metaInsert(rec Record) error {
var records []any
records = append(records, rec)
mongo.UpsertAny(
srvConfig.Config.MLHub.MongoDB.DBName,
srvConfig.Config.MLHub.MongoDB.DBColl,
records)
return nil
}
// metaUpdate updates record in MLHub database
func metaUpdate(rec Record) error {
spec := map[string]any{"model": rec.Model}
meta := map[string]any{"model": rec.Model, "type": rec.Type}
if Verbose > 0 {
log.Printf("update meta-record for spec %+v", spec)
}
err := mongo.UpsertRecord(
srvConfig.Config.MLHub.MongoDB.DBName,
srvConfig.Config.MLHub.MongoDB.DBColl,
spec,
meta)
return err
}
// metaRemove removes given model from MLHub database
func metaRemove(spec map[string]any) error {
if Verbose > 0 {
log.Printf("remove meta-record for spec %+v", spec)
}
err := mongo.Remove(
srvConfig.Config.MLHub.MongoDB.DBName,
srvConfig.Config.MLHub.MongoDB.DBColl,
spec)
return err
}
// metaRecords retrieves records from underlying MLHub database
func metaRecords(model, mlType, version string) ([]Record, error) {
spec := map[string]any{}
if model != "" {
spec["model"] = model
}
if version != "" {
spec["version"] = version
}
if mlType != "" {
spec["type"] = mlType
}
results := mongo.Get(
srvConfig.Config.MLHub.MongoDB.DBName,
srvConfig.Config.MLHub.MongoDB.DBColl,
spec, 0, -1)
var records []Record
for _, rec := range results {
var r Record
delete(rec, "_id")
data, err := json.Marshal(rec)
if err != nil {
log.Printf("Umable to marshal record %+v, error %v", rec, err)
continue
}
err = json.Unmarshal(data, &r)
if err != nil {
log.Printf("Umable to unmarshal record %+v to Record data-struct, error %v", rec, err)
continue
}
records = append(records, r)
}
return records, nil
}