-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhandlers.go
More file actions
491 lines (464 loc) · 15.2 KB
/
handlers.go
File metadata and controls
491 lines (464 loc) · 15.2 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
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
package main
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"log"
"net/http"
"strings"
"github.com/gin-gonic/gin"
mapstructure "github.com/mitchellh/mapstructure"
srvConfig "github.com/CHESSComputing/golib/config"
lexicon "github.com/CHESSComputing/golib/lexicon"
mongo "github.com/CHESSComputing/golib/mongo"
ql "github.com/CHESSComputing/golib/ql"
services "github.com/CHESSComputing/golib/services"
)
// Handler for adding new records to the database
func AddHandler(c *gin.Context) {
// Get single record OR multiple records to submit
defer c.Request.Body.Close()
body, err := ioutil.ReadAll(c.Request.Body)
if err != nil {
log.Printf("ReadAll error: %v", err)
resp := services.Response("SpecScans", http.StatusInternalServerError, services.ReaderError, err)
c.JSON(http.StatusInternalServerError, resp)
return
}
// Try to unmarshal body as multiple records
var records []UserRecord
err = json.Unmarshal(body, &records)
if err != nil {
// Fallback: try to unmarshal body as single record
var record UserRecord
err = json.Unmarshal(body, &record)
if err != nil {
log.Printf("Unmarshal error: %v", err)
resp := services.Response("SpecScans", http.StatusInternalServerError, services.UnmarshalError, err)
c.JSON(http.StatusInternalServerError, resp)
return
}
records = []UserRecord{record}
}
if Verbose > 0 {
log.Printf("AddHandler received request %+v", records)
}
rec_ch := make(chan map[string]any)
err_ch := make(chan error)
defer close(rec_ch)
defer close(err_ch)
for _, record := range records {
go addRecord(record, rec_ch, err_ch)
}
var result_records []map[string]any
var result_err string
for i := 0; i < len(records); i++ {
select {
case new_record := <-rec_ch:
result_records = append(result_records, new_record)
log.Printf("New record: %+v", new_record)
case add_err := <-err_ch:
result_err = fmt.Sprintf("%s; %s", result_err, add_err)
log.Printf("Error adding record: %s", add_err)
}
}
var httpcode, srvcode int
if result_err == "" {
httpcode = http.StatusOK
srvcode = services.OK
} else {
if len(result_records) == 0 {
httpcode = http.StatusUnprocessableEntity
} else {
httpcode = http.StatusMultiStatus
}
srvcode = services.TransactionError
}
response := services.ServiceResponse{
HttpCode: httpcode,
SrvCode: srvcode,
Service: "SpecScans",
Error: result_err,
Results: services.ServiceResults{
NRecords: len(result_records),
Records: result_records,
},
}
c.JSON(http.StatusOK, response)
return
}
// Handler for editing a record already in the database
func EditHandler(c *gin.Context) {
// Get single record OR multiple records to edit
defer c.Request.Body.Close()
body, err := ioutil.ReadAll(c.Request.Body)
if err != nil {
log.Printf("ReadAll error: %v", err)
resp := services.Response("SpecScans", http.StatusInternalServerError, services.ReaderError, err)
c.JSON(http.StatusInternalServerError, resp)
return
}
// Try to unmarshal body as multiple record edits
var edits []map[string]any
err = json.Unmarshal(body, &edits)
if err != nil {
// Fallback: try to unmarshal body as single record edit
var edit map[string]any
err = json.Unmarshal(body, &edit)
if err != nil {
log.Printf("Unmarshal error: %v", err)
resp := services.Response("SpecScans", http.StatusInternalServerError, services.UnmarshalError, err)
c.JSON(http.StatusInternalServerError, resp)
return
}
edits = []map[string]any{edit}
}
if Verbose > 0 {
log.Printf("EditHandler received request %+v", edits)
}
rec_ch := make(chan map[string]any)
err_ch := make(chan error)
defer close(rec_ch)
defer close(err_ch)
for _, edit := range edits {
go editRecord(edit, rec_ch, err_ch)
}
var result_records []map[string]any
var result_err string
for i := 0; i < len(edits); i++ {
select {
case new_record := <-rec_ch:
result_records = append(result_records, new_record)
log.Printf("Edited record: %+v", new_record)
case edit_err := <-err_ch:
result_err = fmt.Sprintf("%s; %s", result_err, edit_err)
log.Printf("Error editing record: %s", edit_err)
}
}
var httpcode, srvcode int
if result_err == "" {
httpcode = http.StatusOK
srvcode = services.OK
} else {
if len(result_records) == 0 {
httpcode = http.StatusUnprocessableEntity
} else {
httpcode = http.StatusMultiStatus
}
srvcode = services.TransactionError
}
response := services.ServiceResponse{
HttpCode: httpcode,
SrvCode: srvcode,
Service: "SpecScans",
Error: result_err,
Results: services.ServiceResults{
NRecords: len(result_records),
Records: result_records,
},
}
c.JSON(http.StatusOK, response)
return
}
// Handler for querying the databases for records
func SearchHandler(c *gin.Context) {
matching_records := *new([]UserRecord)
// Parse database query from request
var query_request services.ServiceRequest
if err := c.Bind(&query_request); err != nil {
resp := services.Response("SpecScans", http.StatusInternalServerError, services.ParseError, err)
c.JSON(http.StatusInternalServerError, resp)
return
}
log.Printf("service request: %+v", query_request)
// Get all attributes we need for querying the mongodb
query := query_request.ServiceQuery.Query
idx := query_request.ServiceQuery.Idx
limit := query_request.ServiceQuery.Limit
spec, err := ql.ParseQuery(query)
if Verbose > 0 {
log.Printf("search query='%s' spec=%+v", query, spec)
}
if err != nil {
rec := services.Response("SpecScans", http.StatusInternalServerError, services.ParseError, err)
c.JSON(http.StatusInternalServerError, rec)
return
}
if len(spec) == 0 &&
strings.Contains(query, srvConfig.Config.DID.Separator) &&
strings.Contains(query, srvConfig.Config.DID.Divider) {
// User's query string did not represent a mapping, but it could be a DID.
query = fmt.Sprintf("{\"did\": \"%s\"}", query)
}
// Get query string as map of values
log.Printf("### query: %+v", query)
queries, err := getServiceQueriesByDBType(QLM, "SpecScans", query)
if err != nil {
resp := services.Response("SpecScans", http.StatusInternalServerError, services.ParseError, err)
c.JSON(http.StatusInternalServerError, resp)
return
}
log.Printf("queries %+v", queries)
if queries["mongo"] == nil {
if queries["sql"] == nil {
// queries["mongo"] == nil && queries["sql"] == nil
// User query is empty -- match _all_ records
mongo_records, err := getMongoRecords(map[string]any{}, idx, limit)
if err != nil {
resp := services.Response("SpecScans", http.StatusInternalServerError, services.QueryError, err)
c.JSON(http.StatusInternalServerError, resp)
return
}
matching_records, err = CompleteMongoRecords(mongo_records...)
if err != nil {
resp := services.Response("SpecScans", http.StatusInternalServerError, services.QueryError, err)
c.JSON(http.StatusInternalServerError, resp)
return
}
} else {
// queries["mongo"] == nil && queries["sql"] != nil
// Search for matching records by motor positions only, then complete all
// the matching motor records with their mongodb portion
motor_records, err := getMotorRecords(queries["sql"])
if err != nil {
resp := services.Response("SpecScans", http.StatusInternalServerError, services.QueryError, err)
c.JSON(http.StatusInternalServerError, resp)
return
}
matching_records, err = CompleteMotorRecords(motor_records...)
if err != nil {
resp := services.Response("SpecScans", http.StatusInternalServerError, services.QueryError, err)
c.JSON(http.StatusInternalServerError, resp)
return
}
}
} else {
mongo_records, err := getMongoRecords(queries["mongo"], idx, limit)
if err != nil {
resp := services.Response("SpecScans", http.StatusInternalServerError, services.QueryError, err)
c.JSON(http.StatusInternalServerError, resp)
return
}
if queries["sql"] == nil {
// queries["mongo"] != nil && queries["sql"] == nil
// Search for matching records in the mongodb only, then complete all
// matching mongo records with their motors component
matching_records, err = CompleteMongoRecords(mongo_records...)
if err != nil {
resp := services.Response("SpecScans", http.StatusInternalServerError, services.QueryError, err)
c.JSON(http.StatusInternalServerError, resp)
return
}
} else {
// queries["mongo"] != nil && queries["sql"] != nil
// Search both dbs separately, then return the _intersection_ of the two
// matching sets (NB: doesn't allow conditional filtering on fields in
// separate dbs!).
motor_records, err := getMotorRecords(queries["sql"])
if err != nil {
resp := services.Response("SpecScans", http.StatusInternalServerError, services.QueryError, err)
c.JSON(http.StatusInternalServerError, resp)
return
}
matching_records = getIntersectionRecords(mongo_records, motor_records)
}
}
var map_records []map[string]any
err = Decode(matching_records, &map_records)
if err != nil {
resp := services.Response("SpecScans", http.StatusInternalServerError, services.ParseError, err)
c.JSON(http.StatusInternalServerError, resp)
return
}
response := services.ServiceResponse{
HttpCode: http.StatusOK,
SrvCode: services.OK,
Service: "SpecScans",
ServiceQuery: query_request.ServiceQuery,
Results: services.ServiceResults{
NRecords: len(map_records),
Records: map_records,
},
}
c.JSON(http.StatusOK, response)
return
}
// Helper function to add a single record to the database(s)
// (to be called as a goroutine)
func addRecord(record UserRecord, rec_ch chan map[string]any, err_ch chan error) {
_, err := validateRecord(record)
if err != nil {
err_ch <- err
return
}
// Decompose the user-submitted record into the portions will be submitted to
// the two separate dbs.
mongo_record, motor_record := DecomposeRecord(record)
// Insert the motor mnes & positions record
// (do this first since we can easily check the uniqueness of the new record's
// scan ID with the SQL db)
_, err = InsertMotors(motor_record, MotorsDb)
if err != nil {
err_ch <- err
return
}
// If submitting the motor record was successful, submit the other portion of
// the record to mongodb.
var mongo_record_map map[string]any
err = Decode(mongo_record, &mongo_record_map)
if err != nil {
err_ch <- err
return
}
mongo.Insert(
srvConfig.Config.SpecScans.MongoDB.DBName,
srvConfig.Config.SpecScans.MongoDB.DBColl,
[]map[string]any{mongo_record_map})
// Send SID of new record
result_record := map[string]any{"sid": mongo_record.ScanId}
rec_ch <- result_record
}
// Helper function to edit a single record in the database(s)
// (to be called as a goroutine)
func editRecord(edit map[string]any, rec_ch chan map[string]any, err_ch chan error) {
// Get unedited version of the record to edit as map[string]any
// (look it up by start_time or spec_file & scan_number, whichever is available)
query := map[string]any{}
sid, ok := edit["sid"]
if !ok {
spec_file, ok := edit["spec_file"]
if !ok {
err_ch <- errors.New("Edit must contain \"sid\" or \"spec_file\" and \"scan_number\" to identify the record to edit")
return
}
scan_number, ok := edit["scan_number"]
if !ok {
err_ch <- errors.New("Edit must contain \"sid\" or \"spec_file\" and \"scan_number\" to identify the record to edit")
return
}
query["spec_file"] = spec_file
query["scan_number"] = scan_number
} else {
query["sid"] = sid
}
original_records, err := getMongoRecords(query, 0, 0)
if len(original_records) != 1 {
err_ch <- errors.New(fmt.Sprintf("Edit request matched %d existing records. Must match exactly 1.", len(original_records)))
return
}
var edited_record map[string]any
err = mapstructure.Decode(original_records[0], &edited_record)
if err != nil {
err_ch <- err
return
}
for k, v := range edit {
edited_record[k] = v
}
_, err = validateRecord(edited_record)
if err != nil {
err_ch <- err
return
}
// Update the record with the edited parameters
update_spec := map[string]any{"$set": map[string]any{}}
for k, v := range edit {
if k != "sid" && k != "spec_file" && k != "scan_number" {
update_spec["$set"].(map[string]any)[k] = v
}
}
err = mongo.UpsertRecord(
srvConfig.Config.SpecScans.MongoDB.DBName,
srvConfig.Config.SpecScans.MongoDB.DBColl,
query,
update_spec,
)
if err != nil {
err_ch <- err
return
}
rec_ch <- edited_record
}
func validateRecord(record any) (bool, error) {
var record_map map[string]any
switch record.(type) {
case map[string]any:
record_map = record.(map[string]any)
default:
err := mapstructure.Decode(record, &record_map)
if err != nil {
return false, fmt.Errorf("[SpecScansService.main.validateRecord] mapstructure.Decode error: %w", err)
}
}
err := lexicon.ValidateRecord(record_map)
if err != nil {
return false, fmt.Errorf("[SpecScansService.main.validateRecord] lexicon.ValidateRecord error: %w", err)
}
err = Schema.Validate(record_map)
if err != nil {
return false, fmt.Errorf("[SpecScansService.main.validateRecord] Schema.Validate error: %w", err)
}
return true, nil
}
// Returns map of queries for a single service sorted by the query
// keys' DBType
func getServiceQueriesByDBType(q ql.QLManager, servicename string, query string) (map[string]map[string]any, error) {
dbqueries := make(map[string]map[string]any)
queries, err := q.ServiceQueries(query)
if err != nil {
return dbqueries, fmt.Errorf("[SpecScansService.main.getServiceQueriesByDBType] q.ServiceQueries error: %w", err)
}
specscanquery := queries[servicename]
for key, val := range specscanquery {
for _, rec := range q.Records {
if rec.Service == servicename && (key == rec.Key || strings.HasPrefix(key, fmt.Sprintf("%s.", rec.Key))) {
if _, ok := dbqueries[rec.DBType]; ok {
dbqueries[rec.DBType][key] = val
} else {
dbqueries[rec.DBType] = map[string]any{key: val}
}
}
}
}
return dbqueries, nil
}
// Get matching records from the mongodb only
func getMongoRecords(query map[string]any, idx int, limit int) ([]MongoRecord, error) {
var mongo_records []MongoRecord
nrecords := mongo.Count(srvConfig.Config.SpecScans.MongoDB.DBName, srvConfig.Config.SpecScans.MongoDB.DBColl, query)
records := mongo.Get(srvConfig.Config.SpecScans.MongoDB.DBName, srvConfig.Config.SpecScans.MongoDB.DBColl, query, idx, limit)
if Verbose > 0 {
log.Printf("spec %v nrecords %d return idx=%d limit=%d", query, nrecords, idx, limit)
}
for _, record := range records {
var mongo_record MongoRecord
err := Decode(record, &mongo_record)
if err != nil {
log.Printf("ERROR: unable to decode record %+v into MongoRecord", record)
return mongo_records, fmt.Errorf("[SpecScansService.main.getMongoRecords] mapstructure.Decode error: %w", err)
}
mongo_records = append(mongo_records, mongo_record)
}
return mongo_records, nil
}
func getMotorRecords(query map[string]any) ([]MotorRecord, error) {
motor_records := QueryMotorsDb(query)
if Verbose > 0 {
log.Printf("query %v found %d records\n", query, len(motor_records))
}
return motor_records, nil
}
func Decode[T *MongoRecord | *UserRecord | *map[string]any | *[]map[string]any](record any, record_struct T) error {
bytes, err := json.Marshal(record)
if err != nil {
log.Printf("Error: unable to marshal record %+v", record)
return err
}
err = json.Unmarshal(bytes, &record_struct)
if err != nil {
log.Printf("Error: unable to unmarshal record %+v", record)
return err
}
return nil
}