-
Notifications
You must be signed in to change notification settings - Fork 1
/
module.go
539 lines (452 loc) · 16.2 KB
/
module.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
package wsHandler
import (
"context"
"fmt"
"regexp"
"github.com/pixlise/core/v4/api/dbCollections"
"github.com/pixlise/core/v4/api/ws/wsHelpers"
"github.com/pixlise/core/v4/core/errorwithstatus"
"github.com/pixlise/core/v4/core/semanticversion"
"github.com/pixlise/core/v4/core/utils"
protos "github.com/pixlise/core/v4/generated-protos"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/mongo/readconcern"
"go.mongodb.org/mongo-driver/mongo/writeconcern"
)
func HandleDataModuleGetReq(req *protos.DataModuleGetReq, hctx wsHelpers.HandlerContext) (*protos.DataModuleGetResp, error) {
dbItem, owner, err := wsHelpers.GetUserObjectById[protos.DataModuleDB](false, req.Id, protos.ObjectType_OT_DATA_MODULE, dbCollections.ModulesName, hctx)
if err != nil {
return nil, err
}
module := &protos.DataModule{
Id: dbItem.Id,
Name: dbItem.Name,
Comments: dbItem.Comments,
ModifiedUnixSec: dbItem.ModifiedUnixSec,
}
module.Creator = wsHelpers.MakeOwnerSummary(owner, hctx.SessUser, hctx.Svcs.MongoDB, hctx.Svcs.TimeStamper)
// Get the version requested...
verRequested := req.Version
if verRequested == nil {
// Version is not supplied, get latest
verRequested, err = getLatestModuleVersion(req.Id, hctx.Svcs.MongoDB)
if err != nil {
return nil, err
}
}
// Get this specific version
moduleVersion, err := getModuleVersion(req.Id, verRequested, hctx.Svcs.MongoDB)
if err != nil {
if err == mongo.ErrNoDocuments {
return nil, errorwithstatus.MakeNotFoundError(req.Id + ", version: " + semanticversion.SemanticVersionToString(verRequested))
}
return nil, fmt.Errorf("Failed to get version: %v for module: %v. Error: %v", semanticversion.SemanticVersionToString(verRequested), req.Id, err)
}
versions, err := getModuleVersions(req.Id, hctx.Svcs.MongoDB)
if err != nil {
return nil, err
}
// Add all previous versions
module.Versions = versions
// Find the requested version and replace it with the one we got if it exists
fetchedSemanticVersion := semanticversion.SemanticVersionToString(moduleVersion.Version)
replacedFetchedVersion := false
for i, ver := range module.Versions {
if semanticversion.SemanticVersionToString(ver.Version) == fetchedSemanticVersion {
module.Versions[i] = moduleVersion
replacedFetchedVersion = true
break
}
}
// If we didn't find the version we fetched, add it to the end
if !replacedFetchedVersion {
module.Versions = append(module.Versions, moduleVersion)
}
return &protos.DataModuleGetResp{
Module: module,
}, nil
}
func HandleDataModuleListReq(req *protos.DataModuleListReq, hctx wsHelpers.HandlerContext) (*protos.DataModuleListResp, error) {
idToOwner, err := wsHelpers.ListAccessibleIDs(false, protos.ObjectType_OT_DATA_MODULE, hctx.Svcs, hctx.SessUser)
if err != nil {
return nil, err
}
ids := utils.GetMapKeys(idToOwner)
coll := hctx.Svcs.MongoDB.Collection(dbCollections.ModulesName)
filter := bson.M{"_id": bson.M{"$in": ids}}
opts := options.Find()
cursor, err := coll.Find(context.TODO(), filter, opts)
if err != nil {
return nil, err
}
items := []*protos.DataModuleDB{}
err = cursor.All(context.TODO(), &items)
if err != nil {
return nil, err
}
// Transform to map of output values
// And for each module, we list all versions. Note, that we're returning a map of modules by module ID
itemMap := map[string]*protos.DataModule{}
for _, item := range items {
versions, err := getModuleVersions(item.Id, hctx.Svcs.MongoDB)
if err != nil {
return nil, fmt.Errorf("Failed to query versions for module %v. Error: %v", item.Id, err)
}
// If we didn't get any versions returned, this is an error!
if len(versions) <= 0 {
return nil, fmt.Errorf("No versions for module %v", item.Id)
}
// Deep copy :(
itemWire := &protos.DataModule{
Id: item.Id,
Name: item.Name,
Comments: item.Comments,
ModifiedUnixSec: item.ModifiedUnixSec,
Versions: versions,
}
if owner, ok := idToOwner[item.Id]; ok {
itemWire.Creator = wsHelpers.MakeOwnerSummary(owner, hctx.SessUser, hctx.Svcs.MongoDB, hctx.Svcs.TimeStamper)
}
itemMap[item.Id] = itemWire
}
return &protos.DataModuleListResp{
Modules: itemMap,
}, nil
}
func getModuleVersion(moduleID string, version *protos.SemanticVersion, db *mongo.Database) (*protos.DataModuleVersion, error) {
// NOTE: This was initially built with a query:
// filter := bson.D{primitive.E{Key: "moduleid", Value: moduleID}, primitive.E{Key: "version", Value: version}}
// But now ID is composed of these fields so it's more direct to query by ID
ctx := context.TODO()
coll := db.Collection(dbCollections.ModuleVersionsName)
result := &protos.DataModuleVersion{}
id := moduleID + "-v" + semanticversion.SemanticVersionToString(version)
verResult := coll.FindOne(ctx, bson.M{"_id": id})
if verResult.Err() != nil {
return nil, verResult.Err()
}
// Read the module item
err := verResult.Decode(&result)
return result, err
}
func getLatestModuleVersion(moduleID string, db *mongo.Database) (*protos.SemanticVersion, error) {
ctx := context.TODO()
coll := db.Collection(dbCollections.ModuleVersionsName)
cursor, err := coll.Aggregate(ctx, bson.A{
bson.D{{Key: "$match", Value: bson.D{{Key: "moduleid", Value: moduleID}}}},
bson.D{
{Key: "$sort",
Value: bson.D{
{Key: "version.major", Value: -1},
{Key: "version.minor", Value: -1},
{Key: "version.patch", Value: -1},
},
},
},
bson.D{{Key: "$limit", Value: 1}},
bson.D{{Key: "$project", Value: bson.D{{Key: "version", Value: 1}}}},
})
if err != nil {
return nil, err
}
defer cursor.Close(ctx)
ver := &protos.DataModuleVersion{}
for cursor.Next(ctx) {
err = cursor.Decode(ver)
break
}
return ver.Version, err
}
func getModuleVersions(moduleID string, db *mongo.Database) ([]*protos.DataModuleVersion, error) {
ctx := context.TODO()
coll := db.Collection(dbCollections.ModuleVersionsName)
filter := bson.D{primitive.E{Key: "moduleid", Value: moduleID}}
opts := options.Find().SetProjection(bson.D{
{Key: "version", Value: true},
{Key: "tags", Value: true},
{Key: "comments", Value: true},
{Key: "timestampunixsec", Value: true},
})
cursor, err := coll.Find(ctx, filter, opts)
if err != nil {
return nil, err
}
allVersions := []*protos.DataModuleVersionDB{}
err = cursor.All(ctx, &allVersions)
if err != nil {
return nil, err
}
result := []*protos.DataModuleVersion{}
for _, versionDB := range allVersions {
ver := &protos.DataModuleVersion{
Version: versionDB.Version,
Tags: versionDB.Tags,
Comments: versionDB.Comments,
TimeStampUnixSec: versionDB.TimeStampUnixSec,
}
result = append(result, ver)
}
return result, nil
}
// Just validates the basics around a module, can be called from create or update
func validateModule(name string, comments string) error {
if !isValidModuleName(name) {
return fmt.Errorf("Invalid module name: %v", name)
}
if err := wsHelpers.CheckStringField(&comments, "Comments", 0, wsHelpers.DescriptionFieldMaxLength); err != nil {
return err
}
return nil
}
func createModule(name string, comments string, intialSourceCode string, tags []string, hctx wsHelpers.HandlerContext) (*protos.DataModule, error) {
ctx := context.TODO()
// It's a new item, check these fields...
err := validateModule(name, comments)
if err != nil {
return nil, errorwithstatus.MakeBadRequestError(err)
}
// Generate a new id
modId := hctx.Svcs.IDGen.GenObjectID()
module := &protos.DataModuleDB{
Id: modId,
Name: name,
Comments: comments,
}
// We need to create an ownership item along with it
ownerItem := wsHelpers.MakeOwnerForWrite(modId, protos.ObjectType_OT_DATA_MODULE, hctx.SessUser.User.Id, hctx.Svcs.TimeStamper.GetTimeNowSec())
module.ModifiedUnixSec = ownerItem.CreatedUnixSec
// Create the initial version
saveVer := &protos.SemanticVersion{Major: 0, Minor: 0, Patch: 1}
verId := modId + "-v" + semanticversion.SemanticVersionToString(saveVer)
version := &protos.DataModuleVersionDB{
Id: verId,
ModuleId: modId,
Version: saveVer,
SourceCode: intialSourceCode,
Comments: comments,
Tags: tags,
TimeStampUnixSec: ownerItem.CreatedUnixSec,
// TODO: doi metadata ??
}
wc := writeconcern.New(writeconcern.WMajority())
rc := readconcern.Snapshot()
txnOpts := options.Transaction().SetWriteConcern(wc).SetReadConcern(rc)
sess, err := hctx.Svcs.MongoDB.Client().StartSession()
if err != nil {
return nil, err
}
defer sess.EndSession(ctx)
// Write the 2 items in a single transaction
callback := func(sessCtx mongo.SessionContext) (interface{}, error) {
_, _err := hctx.Svcs.MongoDB.Collection(dbCollections.ModulesName).InsertOne(sessCtx, module)
if _err != nil {
return nil, _err
}
_, _err = hctx.Svcs.MongoDB.Collection(dbCollections.ModuleVersionsName).InsertOne(sessCtx, version)
if _err != nil {
return nil, _err
}
_, _err = hctx.Svcs.MongoDB.Collection(dbCollections.OwnershipName).InsertOne(sessCtx, ownerItem)
if _err != nil {
return nil, _err
}
return nil, nil
}
_, err = sess.WithTransaction(ctx, callback, txnOpts)
if err != nil {
return nil, err
}
// Make the return struct
moduleWire := &protos.DataModule{
Id: module.Id,
Name: module.Name,
Comments: module.Comments,
Creator: wsHelpers.MakeOwnerSummary(ownerItem, hctx.SessUser, hctx.Svcs.MongoDB, hctx.Svcs.TimeStamper),
ModifiedUnixSec: module.ModifiedUnixSec,
Versions: []*protos.DataModuleVersion{
{
Version: version.Version,
Tags: version.Tags,
Comments: version.Comments,
TimeStampUnixSec: version.TimeStampUnixSec,
SourceCode: version.SourceCode,
},
},
}
return moduleWire, nil
}
func updateModule(id string, name string, comments string, hctx wsHelpers.HandlerContext) (*protos.DataModule, error) {
ctx := context.TODO()
dbItem, owner, err := wsHelpers.GetUserObjectById[protos.DataModuleDB](true, id, protos.ObjectType_OT_DATA_MODULE, dbCollections.ModulesName, hctx)
if err != nil {
return nil, err
}
// Update fields
update := bson.D{}
if len(name) > 0 {
dbItem.Name = name
update = append(update, bson.E{Key: "name", Value: name})
}
if len(comments) > 0 {
dbItem.Comments = comments
update = append(update, bson.E{Key: "comments", Value: comments})
}
// Validate it
err = validateModule(dbItem.Name, dbItem.Comments)
if err != nil {
return nil, errorwithstatus.MakeBadRequestError(err)
}
// Update modified time
dbItem.ModifiedUnixSec = uint32(hctx.Svcs.TimeStamper.GetTimeNowSec())
update = append(update, bson.E{Key: "modifiedunixsec", Value: dbItem.ModifiedUnixSec})
// It's valid, update the DB
dbResult, err := hctx.Svcs.MongoDB.Collection(dbCollections.ModulesName).UpdateByID(ctx, id, bson.D{{Key: "$set", Value: update}})
if err != nil {
return nil, err
}
if dbResult.MatchedCount != 1 {
hctx.Svcs.Log.Errorf("DataModule UpdateByID result had unexpected counts %+v id: %v", dbResult, id)
}
// Return the merged item we validated, which in theory is in the DB now
result := &protos.DataModule{
Id: dbItem.Id,
Name: dbItem.Name,
Comments: dbItem.Comments,
ModifiedUnixSec: dbItem.ModifiedUnixSec,
Creator: wsHelpers.MakeOwnerSummary(owner, hctx.SessUser, hctx.Svcs.MongoDB, hctx.Svcs.TimeStamper),
}
return result, nil
}
func HandleDataModuleWriteReq(req *protos.DataModuleWriteReq, hctx wsHelpers.HandlerContext) (*protos.DataModuleWriteResp, error) {
var item *protos.DataModule
var err error
if len(req.Id) <= 0 {
item, err = createModule(req.Name, req.Comments, req.InitialSourceCode, req.InitialTags, hctx)
} else {
// Ensure no source code is set
if len(req.InitialSourceCode) > 0 {
return nil, errorwithstatus.MakeBadRequestError(fmt.Errorf("InitialSourceCode must not be set for module updates, only name and comments allowed to change"))
}
item, err = updateModule(req.Id, req.Name, req.Comments, hctx)
}
if err != nil {
return nil, err
}
return &protos.DataModuleWriteResp{
Module: item,
}, nil
}
// Some validation functions
func isValidModuleName(name string) bool {
// Limit length to something reasonable (this will be used in code so we don't want huge module names)
if len(name) > 20 {
return false
}
// Names must be valid Lua variable names...
match, err := regexp.MatchString("^[A-Za-z]$|^[A-Za-z_]+[A-Za-z0-9_]*[A-Za-z0-9]$", name)
if err != nil {
return false
}
return match
}
func HandleDataModuleAddVersionReq(req *protos.DataModuleAddVersionReq, hctx wsHelpers.HandlerContext) (*protos.DataModuleAddVersionResp, error) {
// Check that the version update field is a valid value
if !utils.ItemInSlice(req.VersionUpdate, []protos.VersionField{protos.VersionField_MV_MAJOR, protos.VersionField_MV_MINOR, protos.VersionField_MV_PATCH}) {
return nil, fmt.Errorf("Invalid version update field: %v", req.VersionUpdate)
}
// Validate the rest
if err := wsHelpers.CheckStringField(&req.ModuleId, "ModuleId", 1, wsHelpers.IdFieldMaxLength); err != nil {
return nil, err
}
if err := wsHelpers.CheckStringField(&req.SourceCode, "SourceCode", 1, wsHelpers.SourceCodeMaxLength); err != nil {
return nil, err
}
if err := wsHelpers.CheckStringField(&req.Comments, "Comments", 0, wsHelpers.DescriptionFieldMaxLength); err != nil {
return nil, err
}
if err := wsHelpers.CheckFieldLength(req.Tags, "Tags", 0, wsHelpers.TagListMaxLength); err != nil {
return nil, err
}
// Check that the module exists
dbItem, owner, err := wsHelpers.GetUserObjectById[protos.DataModuleDB](false, req.ModuleId, protos.ObjectType_OT_DATA_MODULE, dbCollections.ModulesName, hctx)
if err != nil {
return nil, err
}
module := &protos.DataModule{
Id: dbItem.Id,
Name: dbItem.Name,
Comments: dbItem.Comments,
ModifiedUnixSec: dbItem.ModifiedUnixSec,
}
module.Creator = wsHelpers.MakeOwnerSummary(owner, hctx.SessUser, hctx.Svcs.MongoDB, hctx.Svcs.TimeStamper)
ver, err := getLatestModuleVersion(req.ModuleId, hctx.Svcs.MongoDB)
if err != nil {
return nil, err
}
// Increment the version as needed
if req.VersionUpdate == protos.VersionField_MV_MAJOR {
ver.Major++
ver.Minor = 0
ver.Patch = 0
} else if req.VersionUpdate == protos.VersionField_MV_MINOR {
ver.Minor++
ver.Patch = 0
} else {
ver.Patch++
}
// Write out the new version
verId := req.ModuleId + "-v" + semanticversion.SemanticVersionToString(ver)
nowUnix := hctx.Svcs.TimeStamper.GetTimeNowSec()
verRec := &protos.DataModuleVersionDB{
Id: verId,
ModuleId: req.ModuleId,
SourceCode: req.SourceCode,
Version: ver,
Tags: req.Tags,
Comments: req.Comments,
TimeStampUnixSec: uint32(nowUnix),
//DOIMetadata: input.DOIMetadata,
}
ctx := context.TODO()
coll := hctx.Svcs.MongoDB.Collection(dbCollections.ModuleVersionsName)
insertResult, err := coll.InsertOne(ctx, verRec)
if err != nil {
return nil, err
}
if insertResult.InsertedID != verId {
hctx.Svcs.Log.Errorf("CreateModule (version): Expected Mongo insert to return ID %v, got %v", verId, insertResult.InsertedID)
}
// Add all previous versions
versions, err := getModuleVersions(req.ModuleId, hctx.Svcs.MongoDB)
if err != nil {
return nil, err
}
module.Versions = versions
// Find the requested version and replace it with the one we got if it exists
fetchedSemanticVersion := semanticversion.SemanticVersionToString(verRec.Version)
returnVersion := &protos.DataModuleVersion{
Version: verRec.Version,
Tags: verRec.Tags,
Comments: verRec.Comments,
TimeStampUnixSec: verRec.TimeStampUnixSec,
SourceCode: verRec.SourceCode,
}
replacedFetchedVersion := false
for i, ver := range module.Versions {
if semanticversion.SemanticVersionToString(ver.Version) == fetchedSemanticVersion {
module.Versions[i] = returnVersion
replacedFetchedVersion = true
break
}
}
// If we didn't find the version we fetched, add it to the end
if !replacedFetchedVersion {
module.Versions = append(module.Versions, returnVersion)
}
return &protos.DataModuleAddVersionResp{
Module: module,
}, nil
}