/
assignment.go
59 lines (51 loc) · 1.91 KB
/
assignment.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
package ag
import (
context "context"
"time"
"google.golang.org/protobuf/proto"
)
const (
days = time.Duration(24 * time.Hour)
zero = time.Duration(0)
)
// SinceDeadline returns the duration since the deadline.
// A positive duration means the deadline has passed, whereas
// a negative duration means the deadline has not yet passed.
func (a *Assignment) SinceDeadline(now time.Time) (time.Duration, error) {
deadline, err := time.ParseInLocation(TimeLayout, a.GetDeadline(), now.Location())
if err != nil {
// this should not happen if deadlines are parsed and recorded correctly
return zero, err
}
return now.Sub(deadline), nil
}
// WithTimeout returns a context with an execution timeout set to the assignment's specified
// container timeout. If the assignment has no container timeout, the provided timeout value
// is used instead.
func (a *Assignment) WithTimeout(timeout time.Duration) (context.Context, context.CancelFunc) {
t := a.GetContainerTimeout()
if t > 0 {
timeout = time.Duration(t) * time.Minute
}
return context.WithTimeout(context.Background(), timeout)
}
// IsApproved returns an approved submission status if this assignment is already approved
// for the latest submission, or if the score of the latest submission is sufficient
// to autoapprove the assignment.
func (a *Assignment) IsApproved(latest *Submission, score uint32) Submission_Status {
if a.GetAutoApprove() && score >= a.GetScoreLimit() {
return Submission_APPROVED
}
// keep existing status if already approved/revision/rejected
return latest.GetStatus()
}
// CloneWithoutSubmissions returns a deep copy of the assignment without submissions.
func (a *Assignment) CloneWithoutSubmissions() *Assignment {
clone := proto.Clone(a).(*Assignment)
clone.Submissions = nil
return clone
}
// GradedManually returns true if the assignment will be graded manually.
func (a *Assignment) GradedManually() bool {
return a.GetReviewers() > 0
}