-
Notifications
You must be signed in to change notification settings - Fork 1
/
workdb.go
439 lines (370 loc) · 9.75 KB
/
workdb.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
package main
import (
"encoding/json"
"errors"
"fmt"
"log"
"github.com/boltdb/bolt"
)
var (
// path to the WorkDB file
workDBPath = mainConfig.WorkDBPath
)
const (
// name of the database's work bucket
workBucket = "Work"
)
var (
// ErrRanOutOfItems means chooseUniqueWorkItem can't
// find a suitable WorkItem for this particular user
ErrRanOutOfItems = errors.New("Block checkout reached capacity for this user")
// ErrWorkItemDoesntExist is thrown when a workItemMap access is
// made with a key that doesn't exist
ErrWorkItemDoesntExist = errors.New("This work Item doesn't exist")
)
/*
WorkItem represents a work item at the granularity of
a single CLAN file. Each WorkItem signifies a single
block from that specific clan file
*/
type WorkItem struct {
ID string `json:"id"`
FileName string `json:"filename"`
Block int `json:"block"`
Active bool `json:"active"`
BlockPath string `json:"block_path"`
TimesCoded int `json:"times_coded"`
Training bool `json:"training"`
Reliability bool `json:"reliability"`
TrainingPackNum int `json:"train_pack_num"`
}
// WorkDB is a wrapper around a boltDB
type WorkDB struct {
db *bolt.DB
}
// LoadWorkDB loads the global workDB
func LoadWorkDB() *WorkDB {
localWorkDB := &WorkDB{db: new(bolt.DB)}
err := localWorkDB.Open()
if err != nil {
return nil
}
return localWorkDB
}
// Open opens the database and returns error on failure
func (db *WorkDB) Open() error {
workDB, openErr := bolt.Open(workDBPath, 0600, nil)
if openErr != nil {
log.Fatal(openErr)
return openErr
}
db.db = workDB
err := db.db.Update(func(tx *bolt.Tx) error {
_, updateErr := tx.CreateBucketIfNotExists([]byte(workBucket))
if updateErr != nil {
log.Fatal(updateErr)
return updateErr
}
return updateErr
})
return err
}
// Close closes the database
func (db *WorkDB) Close() {
db.db.Close()
}
/*
fillWithDataMap fills the global workDB with the active/inactive
map of all the work items. Keys are WorkItem ID's and the values
are the bool values from the map.
true = active
false = inactive
*/
func (db *WorkDB) fillWithItemMap(itemMap WorkItemMap) {
for id, item := range itemMap {
// turn WorkItem into []byte
encodedItem, encodeErr := item.encode()
if encodeErr != nil {
log.Fatal(encodeErr)
}
updateErr := db.db.Update(func(tx *bolt.Tx) error {
bucket := tx.Bucket([]byte(workBucket))
err := bucket.Put([]byte(id), encodedItem)
return err
})
if updateErr != nil {
log.Fatal(updateErr)
}
}
}
/*
loadItemMap reads the WorkItemMap from the workDB.
*/
func (db *WorkDB) loadItemMap() WorkItemMap {
var itemMap = make(WorkItemMap)
err := db.db.View(func(tx *bolt.Tx) error {
bucket := tx.Bucket([]byte(workBucket))
cursor := bucket.Cursor()
for k, v := cursor.First(); k != nil; k, v = cursor.Next() {
//fmt.Println("\nWorkItem from DB:")
//fmt.Printf("key=%s, value=%s\n", k, v)
currItem, err := decodeWorkItemJSON(v)
if err != nil {
log.Fatal(err)
}
itemMap[currItem.ID] = *currItem
}
return nil
})
if err != nil {
log.Fatal(err)
}
return itemMap
}
func (wi *WorkItem) encode() ([]byte, error) {
enc, err := json.MarshalIndent(wi, "", " ")
if err != nil {
return nil, err
}
return enc, nil
}
func decodeWorkItemJSON(data []byte) (*WorkItem, error) {
var workItem *WorkItem
err := json.Unmarshal(data, &workItem)
if err != nil {
return nil, err
}
return workItem, nil
}
/*
workItemIsActive checks to see if a WorkItem
is part of the global activeWorkItems map.
*/
func workItemIsActive(item WorkItem) bool {
value := workItemMap[item.ID]
if value.Active {
return true
}
return false
}
/*
inactivateWorkItem sets the WorkItem to false
in the workItemMap
*/
func inactivateWorkItem(item WorkItem, request IDSRequest) {
value := workItemMap[item.ID]
value.Active = false
//value.TimesCoded++
workItemMap[item.ID] = value
workDB.persistWorkItem(value)
// update the User's WorkItem list on disk
user, getUsrError := labsDB.getUser(request.LabKey, request.Username)
if getUsrError != nil {
return
}
user.inactivateWorkItem(value)
labsDB.setUser(user)
}
func inactivateIncompleteWorkItem(item WorkItem, request IDSRequest) {
value := workItemMap[item.ID]
value.Active = false
workItemMap[item.ID] = value
workDB.persistWorkItem(value)
// update the User's WorkItem list on disk
user, getUsrError := labsDB.getUser(request.LabKey, request.Username)
if getUsrError != nil {
return
}
user.inactivateIncompleteWorkItem(value)
labsDB.setUser(user)
}
/*
activateWorkItem sets the WorkItem active status to true
in the workItemMap.
Also adds the work item to the User's checked out WorkItem
list
*/
func activateWorkItem(item WorkItem, request BlockReq) {
value := workItemMap[item.ID]
value.Active = true
// update the workItemMap (in memory)
workItemMap[item.ID] = value
// update the WorkItem value on disk
workDB.persistWorkItem(value)
// update the User's WorkItem list on disk
user, getUsrError := labsDB.getUser(request.LabKey, request.Username)
if getUsrError != nil {
return
}
user.addWorkItem(item.ID)
labsDB.setUser(user)
}
func chooseRegularWorkItem(request BlockReq) (WorkItem, error) {
var workItem WorkItem
user, getUsrErr := labsDB.getUser(request.LabKey, request.Username)
if getUsrErr != nil {
return WorkItem{}, ErrUserDoesntExist
}
for _, item := range workItemMap {
if blockAppropriateForUser(item, request, user) {
activateWorkItem(item, request)
fmt.Println("Selected Item: ")
fmt.Println(item)
return item, nil
}
}
fmt.Println("\nRan out of unique items for this user")
return workItem, ErrRanOutOfItems
}
func blockAppropriateForUser(item WorkItem, request BlockReq, user User) bool {
if item.Active {
return false
} else if item.TimesCoded >= numRealBlockPasses {
fmt.Println("item has been coded already")
return false
} else if item.Training {
return false
} else if item.Reliability {
return false
} else if user.prevCoded(item.ID) {
return false
}
return true
}
func userHasBlockFromFile(item WorkItem, request BlockReq, user User) bool {
/*
Check if user already has a block
from the same file
*/
for _, userItem := range user.ActiveWorkItems {
userWorkItem := workItemMap[userItem]
if userWorkItem.FileName == item.FileName {
return true
}
}
return false
}
func (db *WorkDB) compareWithWorkItemMap(itemMap WorkItemMap) []WorkItem {
// missmatched WorkItems
var diffs []WorkItem
for key, value := range itemMap {
var itemBytes []byte
db.db.View(func(tx *bolt.Tx) error {
bucket := tx.Bucket([]byte(workBucket))
itemBytes = bucket.Get([]byte(key))
workItem, err := decodeWorkItemJSON(itemBytes)
if err != nil {
log.Fatal(err)
}
switch {
case workItem.Block != value.Block:
diffs = append(diffs, value)
break
case workItem.BlockPath != value.BlockPath:
diffs = append(diffs, value)
break
case workItem.FileName != value.FileName:
diffs = append(diffs, value)
break
}
return nil
})
}
return diffs
}
func (db *WorkDB) persistWorkItem(item WorkItem) {
// turn WorkItem into []byte
encodedItem, err := item.encode()
if err != nil {
log.Fatal(err)
}
updateErr := db.db.Update(func(tx *bolt.Tx) error {
bucket := tx.Bucket([]byte(workBucket))
err := bucket.Put([]byte(item.ID), encodedItem)
return err
})
if updateErr != nil {
log.Fatal(updateErr)
}
}
func (db *WorkDB) persistWorkItemMap(itemMap WorkItemMap) {
for _, item := range itemMap {
db.persistWorkItem(item)
}
}
func chooseSpecificBlock(req BlockReq) (WorkItem, error) {
var workItem WorkItem
workItem, exists := workItemMap[req.ItemID]
if !exists {
return workItem, ErrWorkItemDoesntExist
}
if !workItem.Training && !workItem.Reliability && workItem.TimesCoded >= numRealBlockPasses {
return workItem, ErrBlockGroupFull
}
activateWorkItem(workItem, req)
return workItem, nil
}
func chooseTrainingWorkItem(request BlockReq) (WorkItem, error) {
var workItem WorkItem
user, getUsrErr := labsDB.getUser(request.LabKey, request.Username)
if getUsrErr != nil {
return WorkItem{}, ErrUserDoesntExist
}
for _, item := range workItemMap {
if blockAppropriateForUserTraining(item, request, user) {
activateWorkItem(item, request)
fmt.Println("Selected Item: ")
fmt.Println(item)
return item, nil
}
}
fmt.Println("\nRan out of unique items for this user")
return workItem, ErrRanOutOfItems
}
func chooseSpecificTrainingBlock(req BlockReq) (WorkItem, error) {
var workItem WorkItem
workItem, exists := workItemMap[req.ItemID]
if !exists {
return workItem, ErrWorkItemDoesntExist
}
activateWorkItem(workItem, req)
return workItem, nil
}
func chooseReliabilityWorkItem(request BlockReq) (WorkItem, error) {
var workItem WorkItem
user, getUsrErr := labsDB.getUser(request.LabKey, request.Username)
if getUsrErr != nil {
return WorkItem{}, ErrUserDoesntExist
}
for _, item := range workItemMap {
if blockAppropriateForUserReliability(item, request, user) {
activateWorkItem(item, request)
fmt.Println("Selected Item: ")
fmt.Println(item)
return item, nil
}
}
fmt.Println("\nRan out of unique items for this user")
return workItem, ErrRanOutOfItems
}
func blockAppropriateForUserTraining(item WorkItem, request BlockReq, user User) bool {
if !item.Training {
return false
} else if user.hasThisBlock(item.ID) {
fmt.Println("user has this block already")
return false
}
return true
}
func blockAppropriateForUserReliability(item WorkItem, request BlockReq, user User) bool {
if !item.Reliability {
return false
} else if user.prevCodedRelia(item.ID) {
fmt.Println("item has been coded already by this user")
return false
} else if user.hasThisBlock(item.ID) {
fmt.Println("user has block from this file")
return false
}
return true
}