/
db_expressions.go
303 lines (261 loc) · 10.3 KB
/
db_expressions.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
// Licensed to NASA JPL under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. NASA JPL licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package expressionDB
import (
"context"
"errors"
"fmt"
"github.com/pixlise/core/v3/core/api"
"github.com/pixlise/core/v3/core/expressions/expressions"
"github.com/pixlise/core/v3/core/expressions/zenodo"
zenodoModels "github.com/pixlise/core/v3/core/expressions/zenodo-models"
"github.com/pixlise/core/v3/core/pixlUser"
"github.com/pixlise/core/v3/core/utils"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo/options"
)
func (e *ExpressionDB) ListExpressions(userID string, includeShared bool, retrieveUpdatedUserInfo bool) (expressions.DataExpressionLookup, error) {
result := expressions.DataExpressionLookup{}
if e.Expressions == nil {
return result, errors.New("ListExpressions: Mongo not connected")
}
// List all which have this user ID
filter := bson.D{primitive.E{Key: "origin.creator.userid", Value: userID}}
if includeShared {
filter = bson.D{
{"$or",
bson.A{
bson.D{{"origin.creator.userid", userID}},
bson.D{{"origin.shared", true}},
},
},
}
}
opts := options.Find().SetProjection(bson.D{{"sourcecode", 0}})
cursor, err := e.Expressions.Find(context.TODO(), filter, opts)
if err != nil {
return result, err
}
allExpressions := []expressions.DataExpression{}
err = cursor.All(context.TODO(), &allExpressions)
if err != nil {
return result, err
}
// Return them as a map while also fixing up user info
for _, exprItem := range allExpressions {
if retrieveUpdatedUserInfo {
// Get latest user details from Mongo
updatedCreator, creatorErr := e.Svcs.Users.GetCurrentCreatorDetails(exprItem.Origin.Creator.UserID)
if creatorErr != nil {
e.Svcs.Log.Infof("Failed to lookup user details for ID: %v (Name: %v), creator name in expression: %v. Error: %v", exprItem.Origin.Creator.UserID, exprItem.Origin.Creator.Name, exprItem.ID, creatorErr)
} else {
exprItem.Origin.Creator = updatedCreator
}
}
// Save in map
result[exprItem.ID] = exprItem
}
return result, nil
}
func (e *ExpressionDB) GetExpression(expressionID string, retrieveUpdatedUserInfo bool) (expressions.DataExpression, error) {
result := expressions.DataExpression{}
exprResult := e.Expressions.FindOne(context.TODO(), bson.M{"_id": expressionID})
if exprResult.Err() != nil {
return result, exprResult.Err()
}
// Read the expression item
err := exprResult.Decode(&result)
if err != nil {
return result, err
}
if retrieveUpdatedUserInfo {
// Get latest user details from Mongo
updatedCreator, creatorErr := e.Svcs.Users.GetCurrentCreatorDetails(result.Origin.Creator.UserID)
if creatorErr != nil {
e.Svcs.Log.Infof("Failed to lookup user details for ID: %v (Name: %v), creator name in expression: %v. Error: %v", result.Origin.Creator.UserID, result.Origin.Creator.Name, result.ID, creatorErr)
} else {
result.Origin.Creator = updatedCreator
}
}
return result, err
}
func (e *ExpressionDB) CreateExpression(input expressions.DataExpressionInput, creator pixlUser.UserInfo, createShared bool) (expressions.DataExpression, error) {
nowUnix := e.Svcs.TimeStamper.GetTimeNowSec()
exprID := e.Svcs.IDGen.GenObjectID()
expr := expressions.DataExpression{
ID: exprID,
Name: input.Name,
SourceCode: input.SourceCode,
SourceLanguage: input.SourceLanguage,
Comments: input.Comments,
Tags: input.Tags,
ModuleReferences: input.ModuleReferences,
Origin: pixlUser.APIObjectItem{
Shared: createShared,
Creator: creator,
CreatedUnixTimeSec: nowUnix,
ModifiedUnixTimeSec: nowUnix,
},
DOIMetadata: zenodoModels.DOIMetadata{},
// RecentExecStats is blank at this point!
}
// Write the expression itself
insertResult, err := e.Expressions.InsertOne(context.TODO(), expr)
if err != nil {
return expr, err
}
if insertResult.InsertedID != exprID {
e.Svcs.Log.Errorf("CreateExpression: Expected Mongo insert to return ID %v, got %v", exprID, insertResult.InsertedID)
}
return expr, nil
}
// Replaces the existing expression with the new one
// This assumes a GetExpression was required already to validate user permissions to this expression, etc
// therefore the prevUnixTime should be available. This way we can preserve the creation time but set a new
// modified time now. Also now requires the existing expressƒion shared and source code field!
func (e *ExpressionDB) UpdateExpression(
expressionID string,
input expressions.DataExpressionInput,
creator pixlUser.UserInfo,
createdUnixTimeSec int64,
isShared bool,
existingSourceCode string,
) (expressions.DataExpression, error) {
filter := bson.D{{"_id", expressionID}}
// NOTE: originally wanted to just edit the new fields, like so:
/*
update := bson.D{
{"$set", bson.D{
{"name", input.Name},
{"sourceCode", input.SourceCode},
{"sourceLanguage", input.SourceLanguage},
{"comments", input.Comments},
{"tags", input.Tags},
}},
}
result, err := e.Expressions.UpdateOne(context.TODO(), filter, update)
*/
// But the above seemed risky because BSON field names may change as the struct changes, but this code wouldn't!
// So instead opting to replace with a new record as this will obey struct field naming
// Only complication is we've since introduced the idea of sending in a blank string for source code meaning preserve the old code
// so we have to read the existing expression here in that case:
sourceCode := input.SourceCode
if len(sourceCode) <= 0 {
// Use existing source field...
sourceCode = existingSourceCode
}
if len(sourceCode) <= 0 {
return expressions.DataExpression{}, fmt.Errorf("Expression source code field cannot be blank, when updating expression: %v", expressionID)
}
nowUnix := e.Svcs.TimeStamper.GetTimeNowSec()
replacement := expressions.DataExpression{
ID: expressionID,
Name: input.Name,
SourceCode: sourceCode,
SourceLanguage: input.SourceLanguage,
Comments: input.Comments,
Tags: input.Tags,
ModuleReferences: input.ModuleReferences,
Origin: pixlUser.APIObjectItem{
Shared: isShared,
Creator: creator,
CreatedUnixTimeSec: createdUnixTimeSec,
ModifiedUnixTimeSec: nowUnix,
},
DOIMetadata: input.DOIMetadata,
// Expression was edited, so any previous RecentExecStats are no longer valid, so blank!
}
updResult, err := e.Expressions.ReplaceOne(context.TODO(), filter, replacement)
if err != nil {
return replacement, err
}
if updResult.MatchedCount != 1 || updResult.ModifiedCount != 1 {
e.Svcs.Log.Errorf("UpdateExpression for %v: Expected Mongo ReplaceOne to modify 1 item, got match: %v, modified: %v", expressionID, updResult.MatchedCount, updResult.ModifiedCount)
}
return replacement, nil
}
func (e *ExpressionDB) StoreExpressionRecentRunStats(expressionID string, stats expressions.DataExpressionExecStats) error {
filter := bson.D{{"_id", expressionID}}
update := bson.D{
{"$set", bson.D{
{"recentExecStats", stats},
}},
}
updResult, err := e.Expressions.UpdateOne(context.TODO(), filter, update)
if err != nil {
return err
}
// Make sure it worked
if updResult.MatchedCount != 1 || updResult.ModifiedCount != 1 {
return api.MakeNotFoundError(expressionID)
//err = fmt.Errorf("StoreExpressionRecentRunStats for %v: Expected Mongo ReplaceOne to modify 1 item, got match: %v, modified: %v", expressionID, updResult.MatchedCount, updResult.ModifiedCount)
//return err
}
return nil
}
func (e *ExpressionDB) DeleteExpression(expressionID string) error {
delResult, err := e.Expressions.DeleteOne(context.TODO(), bson.M{"_id": expressionID})
if err != nil {
return err
}
// Should really get 1 deletion... Or 0 is still valid...
if delResult.DeletedCount > 1 {
e.Svcs.Log.Errorf("DeleteExpression %v: expected to delete 1 item, instead deleted %v", expressionID, delResult.DeletedCount)
}
return nil
}
func (e *ExpressionDB) PublishExpressionToZenodo(expressionID string, zipData []byte, zenodoURI string, zenodoToken string) (expressions.DataExpression, error) {
result := expressions.DataExpression{}
strippedID, _ := utils.StripSharedItemIDPrefix(expressionID)
exprResult := e.Expressions.FindOne(context.TODO(), bson.M{"_id": strippedID})
if exprResult.Err() != nil {
return result, exprResult.Err()
}
// Read the expression item
err := exprResult.Decode(&result)
if err != nil {
return result, err
}
// Verify the expression exists and is shared before publishing
if result.ID == strippedID && result.Origin.Shared {
deposition, err := zenodo.PublishExpressionZipToZenodo(result, zipData, zenodoURI, zenodoToken)
if err != nil {
return result, err
}
// Update the expression with the DOI
filter := bson.D{{"_id", strippedID}}
// Add the returned DOI links to the stored metadata
result.DOIMetadata.DOI = deposition.DOI
result.DOIMetadata.DOILink = deposition.Links.DOI
result.DOIMetadata.DOIBadge = deposition.Links.Badge
update := bson.D{
{"$set", bson.D{
{"doiMetadata", result.DOIMetadata},
}},
}
updResult, err := e.Expressions.UpdateOne(context.TODO(), filter, update)
if err != nil {
return result, err
}
// Make sure it worked
if updResult.MatchedCount != 1 || updResult.ModifiedCount != 1 {
return result, api.MakeNotFoundError(strippedID)
}
}
return result, nil
}