-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexceptions.go
554 lines (473 loc) · 15.4 KB
/
exceptions.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
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
package main
import (
"errors"
"fmt"
"log"
"os"
"os/user"
"time"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/mysql"
_ "github.com/jinzhu/gorm/dialects/sqlite"
"github.com/olekukonko/tablewriter"
)
type Exception struct {
// The gorm.Model inclusion here adds a number of fields that are automatically
// used and updated by gorm: ID, CreatedAt, UpdatedAt, and DeletedAt
gorm.Model
Username string `gorm:"type:varchar(10);not null"`
SubmittedDate *time.Time `gorm:"default:NULL"`
StartDate *time.Time `gorm:"default:NULL"`
EndDate *time.Time `gorm:"default:NULL"`
Service string `gorm:"type:varchar(16);not null"`
ExceptionType string `gorm:"type:varchar(128);not null"`
ExceptionDetail string `gorm:"type:varchar(512);not null"`
FormFiles []FormFile `gorm:"foreignkey:ExceptionID"`
Comments []Comment `gorm:"foreignkey:ExceptionID"`
StatusChanges []StatusChange `gorm:"foreignkey:ExceptionID"`
Status string `gorm:"default:'(none)'; not null"`
}
type FormFile struct {
gorm.Model
ExceptionID uint
FileName string `gorm:"type:text"`
FileContents []byte `gorm:"type:mediumblob"` // Note: in MySQL 5.5 and 8.0 at least, mediumblobs can hold a maximum of 16 megabytes. This *SHOULD* be fine for all our cases.
}
type Comment struct {
gorm.Model
ExceptionID uint
CommentBy string `gorm:"type:varchar(10); not null"`
CommentText string `gorm:"type:text; not null"`
}
type StatusChange struct {
gorm.Model
ExceptionID uint
OldStatus string `gorm:"type:varchar(16);default:'none';not null"`
NewStatus string `gorm:"type:varchar(16);default:'none';not null"`
Changer string `gorm:"type:varchar(10); not null"`
}
// This contains a map that determines which statuses can become which other
//
// statuses. It returns true if a state change is allowed, false otherwise.
//
// Note that the state change functions have force flags that skip this check
//
// for awkward cases, and it won't break anything -- this is just to direct flow.
func isValidChange(oldStatus string, newStatus string) bool {
validChanges := make(map[string][]string)
validChanges["(none)"] = []string{"undecided"}
validChanges["undecided"] = []string{"approved", "rejected"}
validChanges["approved"] = []string{"implemented"}
validChanges["implemented"] = []string{"removed"}
validChanges["removed"] = []string{}
validChanges["rejected"] = []string{}
for _, v := range validChanges[oldStatus] {
if v == newStatus {
return true
}
}
return false
}
// Pulls an Exception from the database, by ID (primary key).
func GetException(id uint) *Exception {
db := getDB()
defer db.Close()
exception := &Exception{}
db.Set("gorm:auto_preload", true).First(&exception, id)
return exception
}
// This was added relatively late, because it seemed like the wrong thing
//
// to do (double-tracking the status based on the last audit result and
// a separate field) but it made some things *much* easier, at the risk
// of being able to become inconsistent.
//
// Hence, GetTrackedStatus exists because it used to be GetStatus.
func (exception *Exception) GetStatus() string {
return exception.Status
}
// Gets the current status of an Exception by getting the most recent
//
// status update and checking its new status field.
//
// Note that this is faster if preloading has been done, but doesn't require it.
func (exception *Exception) GetTrackedStatus() string {
db := getDB()
defer db.Close()
// If the exception status changes have been preloaded correctly,
// the db and the object should agree on the number of changes
// If not, we have to grab them ourselves
canonicalStatusChangeCount := db.Model(exception).Association("StatusChanges").Count()
if canonicalStatusChangeCount == 0 {
return "(none)"
}
var statusChanges []StatusChange
var lastStatusChange StatusChange
if canonicalStatusChangeCount != len(exception.StatusChanges) {
// Currently this db call orders *all* the statusChanges --
// I'm not sure how to change the query line to make it
// stop doing that but it seems like it should be possible >:T
db.Model(exception).Related(&statusChanges).Last(&lastStatusChange)
} else {
lastStatusChange = exception.StatusChanges[canonicalStatusChangeCount-1]
}
return lastStatusChange.NewStatus
}
// This function calls the gorm Delete function on an Exception object.
// Because the Exception struct has a DeletedAt field, this will soft-delete
//
// the database entry, filling the DeletedAt field with the time and making
// all gorm's queries ignore the entry by default.
//
// The data will still be in the database.
func SoftDeleteException(id uint) []error {
db := getDB()
defer db.Close()
exception := &Exception{}
db.Set("gorm:auto_preload", true).First(&exception, id)
if exception.ID == 0 {
return []error{errors.New(fmt.Sprintf("Could not find exception with id %d", id))}
}
return db.Delete(&exception).GetErrors()
}
// (CLI entry point for SoftDeletion.)
func edelete(ID uint) {
errors := SoftDeleteException(ID)
if len(errors) != 0 {
log.Fatal(errors)
}
return
}
func (exception *Exception) ChangeStatusTo(newStatus string, checkChangeValidity bool) error {
currentStatus := exception.GetStatus()
if (!checkChangeValidity) && (!isValidChange(currentStatus, newStatus)) {
return errors.New(fmt.Sprintf("Proposed status change (%s -> %s) is invalid -- use -f to force", currentStatus, newStatus))
}
currentUser, err := user.Current()
if err != nil {
panic("Could not get the current username")
}
statusChange := &StatusChange{
ExceptionID: exception.ID,
OldStatus: currentStatus,
NewStatus: newStatus,
Changer: currentUser.Username,
}
// See the note on GetStatus
exception.Status = newStatus
db := getDB()
defer db.Close()
db.Create(statusChange)
db.NewRecord(statusChange)
db.Save(exception)
return nil
}
func (exception *Exception) AddComment(text string) (uint, error) {
// TODO: refactor this and the comment() function together
currentUser, err := user.Current()
if err != nil {
panic("could not get current user name")
}
currentUsername := currentUser.Username
comment := &Comment{ExceptionID: exception.ID, CommentText: text, CommentBy: currentUsername}
db := getDB()
defer db.Close()
db.Save(comment)
return comment.ID, nil
}
func (exception *Exception) DurationRemaining() (*time.Duration, string) {
if exception.EndDate == nil || exception.StartDate == nil {
return nil, "--"
}
if time.Now().After(*exception.EndDate) {
return nil, "finished"
}
if time.Now().Before(*exception.StartDate) {
return nil, "not started yet"
}
duration := time.Until(*exception.EndDate)
return &duration, ""
}
func undecide(ID uint, force bool) {
exception := GetException(ID)
err := exception.ChangeStatusTo("undecided", force)
if err != nil {
log.Fatal(err)
}
}
func approve(ID uint, force bool) {
exception := GetException(ID)
err := exception.ChangeStatusTo("approved", force)
if err != nil {
log.Fatal(err)
}
}
func reject(ID uint, force bool) {
exception := GetException(ID)
err := exception.ChangeStatusTo("rejected", force)
if err != nil {
log.Fatal(err)
}
}
func implement(ID uint, force bool) {
exception := GetException(ID)
err := exception.ChangeStatusTo("implemented", force)
if err != nil {
log.Fatal(err)
}
}
func remove(ID uint, force bool) {
exception := GetException(ID)
err := exception.ChangeStatusTo("removed", force)
if err != nil {
log.Fatal(err)
}
}
func notYetImplemented() {
log.Fatal("This thing not yet implemented.\n")
panic("!")
}
func list(kind string) {
timeNow := getDBTimeNow()
// In theory you'd use these to determine unset but you can just use zero instead
//zeroTime := "FROM_UNIXTIME(0)" // MySQL
//zeroTime := "date(0, 'unixepoch')" // SQLite (I think)
db := getDB()
defer db.Close()
// *listService is a CLI arg
if *listService != "" {
db = db.Where("service = '" + (*listService) + "'")
}
var listSet []Exception
switch kind {
case "all":
db.Find(&listSet)
printExceptionTableSummary(listSet)
case "pending":
db.Where(timeNow + " < start_date AND status = 'approved'").Find(&listSet)
printExceptionTableSummary(listSet)
case "undecided":
db.Where("status = 'undecided'").Find(&listSet)
printExceptionTableSummary(listSet)
case "approved":
db.Where("status = 'approved'").Find(&listSet)
printExceptionTableSummary(listSet)
case "needed":
db.Where("status = 'approved' AND start_date > " + timeNow).Find(&listSet)
printExceptionTableSummary(listSet)
case "active":
// "active" is synonymous with "implemented" here to make the interface make... some manner of sense
// so we fall through to it
// (go case statements do not otherwise fall through)
fallthrough
case "implemented":
db.Where("status = 'implemented'").Find(&listSet)
printExceptionTableSummary(listSet)
case "overdue":
db.Where("status = 'implemented' AND end_date < " + timeNow).Find(&listSet)
printExceptionTableSummary(listSet)
case "removed":
db.Where("status = 'removed'").Find(&listSet)
printExceptionTableSummary(listSet)
case "todo":
db.Where("(status = 'implemented' AND end_date < " + timeNow + ") OR (status = 'approved' AND start_date > " + timeNow + ") OR (status = 'undecided')").Find(&listSet)
printExceptionTableSummary(listSet)
case "inconsistent":
// Ideally we'd move this out into a call like IsInconsistent and then run for each Exception
// but that would be *much* slower
// I removed a lot of possibles here that relied on no-longer-existent fields. More checking
// might be useful.
db.Where("(submitted_date IS NULL) OR " +
"(start_date IS NULL AND end_date IS NOT NULL) OR " +
"(start_date > end_date) " +
"").Find(&listSet)
printExceptionTableSummary(listSet)
default:
notYetImplemented()
}
}
//var epochZero = time.Date(1970, time.January, 1, 0, 0, 0, 0, time.UTC)
func stringFromDate(timeIn *time.Time) string {
if timeIn == nil {
return "--"
} else {
return timeIn.Format("2006-01-02")
}
}
func printExceptionTableSummary(exceptions []Exception) {
db := getDB()
defer db.Close()
if len(exceptions) == 0 {
log.Print("No such records found.")
return
}
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"ID", "Username", "Status", "Sub Date", "Start Date", "End Date", "Service", "Type", "Detail", "Attachments", "Comments"})
table.SetBorder(false)
for _, ex := range exceptions {
var statusString string
numComments := db.Model(&ex).Association("Comments").Count()
numAttachments := db.Model(&ex).Association("FormFiles").Count()
statusString = ex.GetStatus()
table.Append([]string{fmt.Sprintf("%d", ex.ID),
ex.Username,
statusString,
stringFromDate(ex.SubmittedDate),
stringFromDate(ex.StartDate),
stringFromDate(ex.EndDate),
ex.Service,
ex.ExceptionType,
ex.ExceptionDetail,
fmt.Sprintf("%d", numAttachments),
fmt.Sprintf("%d", numComments),
})
}
table.Render()
}
func submitWithAllParts(username string, submitDateString string, startDateString string, endDateString string, service string, exceptionType string, details string) (uint, error) {
// First convert dates into proper formats
var submitDate time.Time
var startDate time.Time
var endDate time.Time
var err error
submitDate, err = time.Parse("2006-01-02", submitDateString)
if err != nil {
log.Fatalln("Could not parse submit date")
}
startDate, err = time.Parse("2006-01-02", startDateString)
if err != nil {
log.Fatalln("Could not parse start date")
}
endDate, err = time.Parse("2006-01-02", endDateString)
if err != nil {
log.Fatalln("Could not parse end date")
}
username, err = filterSubmittedUsername(username)
if err != nil {
log.Fatalln(err)
}
service, err = filterSubmittedService(service)
if err != nil {
log.Fatalln(err)
}
exceptionType, err = filterSubmittedExceptionType(exceptionType)
if err != nil {
log.Fatalln(err)
}
// Then create the exception
exception := Exception{Username: username,
SubmittedDate: &submitDate,
StartDate: &startDate,
EndDate: &endDate,
Service: service,
ExceptionType: exceptionType,
ExceptionDetail: details}
db := getDB()
defer db.Close()
db.NewRecord(exception)
db.Create(&exception)
exception.ChangeStatusTo("undecided", true)
return exception.ID, nil
}
func comment(id uint, commentText string) (uint, error) {
db := getDB()
defer db.Close()
exception := &Exception{}
exRetrErrors := db.First(&exception, id).GetErrors()
if exception.ID == 0 {
log.Fatalln("No record of that exception.")
return 0, errors.New("No record of that exception.")
}
for _, v := range exRetrErrors {
fmt.Println(v)
}
// commentTextArg is a command line option set in cli.go
var err error
if commentText == "" {
commentText, err = getTextFromEditor()
if err != nil {
return 0, err
}
}
currentUser, err := user.Current()
currentUsername := currentUser.Username
comment := &Comment{ExceptionID: id, CommentText: commentText, CommentBy: currentUsername}
db.Save(comment)
return comment.ID, nil
}
func timeRemaining(exception *Exception) string {
remaining, msg := exception.DurationRemaining()
if remaining == nil {
return msg
}
return fmt.Sprintf("%d days", int(remaining.Hours()/24))
}
func details(id uint) {
db := getDB()
defer db.Close()
exception := &Exception{}
var comments []Comment
var files []FormFile
var statusChanges []StatusChange
errors := db.Set("gorm:auto_preload", true).First(&exception, id).GetErrors()
if exception.ID == 0 {
log.Print("No record of that exception.")
return
}
if len(errors) != 0 {
for _, v := range errors {
fmt.Println(v)
}
log.Fatalln("Errors getting exception from DB! See above.")
}
table := tablewriter.NewWriter(os.Stdout)
table.SetBorder(false)
table.SetAlignment(tablewriter.ALIGN_LEFT)
table.SetColWidth(80)
timeRemaining := timeRemaining(exception)
data := [][]string{
[]string{"ID", fmt.Sprint(exception.ID)},
[]string{"Username", exception.Username},
[]string{"Service", exception.Service},
[]string{"Type", exception.ExceptionType},
[]string{"Detail", exception.ExceptionDetail},
[]string{"Created", exception.CreatedAt.Format("2006-01-02 15:04:05 MST")},
[]string{"Updated", exception.UpdatedAt.Format("2006-01-02 15:04:05 MST")},
[]string{"Submitted", stringFromDate(exception.SubmittedDate)},
[]string{"Starts", stringFromDate(exception.StartDate)},
[]string{"Ends", stringFromDate(exception.EndDate)},
[]string{"Remaining", timeRemaining},
[]string{"Status", exception.GetStatus()},
}
db.Model(&exception).Related(&statusChanges)
if len(statusChanges) == 0 {
data = append(data, []string{"Status Updates", "(none)"})
} else {
statusRowLabel := "Status Change"
for _, v := range statusChanges {
data = append(data, []string{statusRowLabel, fmt.Sprintf("%s -> %s, by %s [%s]", v.OldStatus, v.NewStatus, v.Changer, v.UpdatedAt.Format("2006-01-02"))})
}
}
db.Model(&exception).Related(&files)
if len(files) == 0 {
data = append(data, []string{"File", "(none)"})
} else {
fileRowLabel := "File"
for _, v := range files {
data = append(data, []string{fileRowLabel, fmt.Sprintf("%s (%d bytes)", v.FileName, len(v.FileContents))})
fileRowLabel = ""
}
}
db.Model(&exception).Related(&comments)
if len(comments) == 0 {
data = append(data, []string{"Comment", "(none)"})
} else {
commentRowLabel := "Comment"
for _, v := range comments {
data = append(data, []string{commentRowLabel, v.CommentText})
commentRowLabel = ""
}
}
table.AppendBulk(data)
table.Render()
}