-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdata.go
148 lines (137 loc) · 3.86 KB
/
data.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
package main
import (
"errors"
"fmt"
"log"
"strings"
"time"
srvConfig "github.com/CHESSComputing/golib/config"
"github.com/CHESSComputing/golib/globus"
utils "github.com/CHESSComputing/golib/utils"
)
// helper function to validate input data record against schema
func validateData(sname string, rec map[string]any) error {
if smgr, ok := _smgr.Map[sname]; ok {
schema := smgr.Schema
err := schema.Validate(rec)
if err != nil {
return err
}
} else {
msg := fmt.Sprintf("No schema '%s' found for your record", sname)
log.Printf("ERROR: %s, schema map %+v", msg, _smgr.Map)
return errors.New(msg)
}
return nil
}
// helper function to preprocess given record
/*
func preprocess(rec map[string]any) map[string]any {
r := make(map[string]any)
for k, v := range rec {
switch val := v.(type) {
case string:
r[strings.ToLower(k)] = strings.ToLower(val)
case []string:
var vals []string
for _, vvv := range val {
vals = append(vals, strings.ToLower(vvv))
}
r[strings.ToLower(k)] = vals
case []interface{}:
var vals []string
for _, vvv := range val {
s := fmt.Sprintf("%v", vvv)
vals = append(vals, strings.ToLower(s))
}
r[strings.ToLower(k)] = vals
default:
r[strings.ToLower(k)] = val
}
}
return r
}
*/
// helper function to create globus link
func globusLink(rec map[string]any) (string, error) {
v, ok := rec["data_location_raw"]
if !ok {
return "", errors.New("no data_location_raw attribute in meta-data record")
}
path := v.(string)
pat := "CHESS Raw"
gurl, err := globus.ChessGlobusLink(pat, path)
return gurl, err
}
// helper function to insert data into backend DB
func insertData(sname string, rec map[string]any, attrs, sep, div string, updateRecord bool) (string, error) {
// load our schema
if _, err := _smgr.Load(sname); err != nil {
msg := fmt.Sprintf("unable to load %s error %v", sname, err)
log.Println("ERROR: ", msg)
return "", errors.New(msg)
}
// check if data satisfies to one of the schema
if err := validateData(sname, rec); err != nil {
return "", err
}
if _, ok := rec["date"]; !ok {
rec["date"] = time.Now().Unix()
}
rec["schema_file"] = sname
rec["schema"] = schemaName(sname)
if link, err := globusLink(rec); err == nil {
rec["globus_link"] = link
} else {
log.Printf("ERROR: unable to create globus link %v", err)
}
// generate unique id
didValue, ok := rec["did"]
did := fmt.Sprintf("%s", didValue)
if !ok || did == "" {
// create did out of provided attributes
did = utils.CreateDID(rec, attrs, sep, div)
rec["did"] = did
}
// for testing purposes with hey we will replace __PLACEHOLDER__ in DID
if strings.Contains(did, "__PLACEHOLDER__") {
tstamp := fmt.Sprintf("%d", time.Now().UnixNano())
did = strings.Replace(did, "__PLACEHOLDER__", tstamp, -1)
}
// based on updateRecord decide if we'll insert or update record
var err error
if updateRecord {
// rec["path"] = path
// add record to metaDB DB
var records []map[string]any
records = append(records, rec)
err = metaDB.Upsert(
srvConfig.Config.CHESSMetaData.MongoDB.DBName,
srvConfig.Config.CHESSMetaData.MongoDB.DBColl,
"did", records)
if err != nil {
log.Printf("ERROR: unable to metaDB.Upsert for did=%s, error=%v", did, err)
}
return did, err
}
// check if did already exist in metaDB
spec := map[string]any{"did": did}
records := metaDB.Get(
srvConfig.Config.CHESSMetaData.MongoDB.DBName,
srvConfig.Config.CHESSMetaData.MongoDB.DBColl,
spec, 0, 1)
if len(records) > 0 {
msg := fmt.Sprintf("Record with did=%s found in MetaData database %+v", did, records)
return did, errors.New(msg)
}
if Verbose > 0 {
log.Printf("insert data %+v", rec)
}
// insert record to metaDB
err = metaDB.InsertRecord(
srvConfig.Config.CHESSMetaData.MongoDB.DBName,
srvConfig.Config.CHESSMetaData.MongoDB.DBColl,
rec)
log.Println("metaDB.InsertRecord", err)
return did, err
}