-
Notifications
You must be signed in to change notification settings - Fork 1
/
result.go
115 lines (95 loc) · 2.81 KB
/
result.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
package run
import (
"fmt"
"strings"
"time"
)
// Result stores the data from a single run of the apply loop.
// The functions associated with Result convert raw data into the desired formats for insertion into the status page template.
type Result struct {
Successes []ApplyAttempt
Failures []ApplyAttempt
CommitHash string
FullCommit string
DiffURLFormat string
}
// Duration returns the total duration for all the modules summed in seconds
func (r *Result) Duration() float64 {
return r.Finish().Sub(r.Start()).Seconds()
}
// Finish returns the finish time for the last apply attempt
func (r *Result) Finish() time.Time {
var finish time.Time
attempts := append(r.Successes, r.Failures...)
for _, attempt := range attempts {
if finish.IsZero() || attempt.Finish.After(finish) {
finish = attempt.Finish
}
}
return finish
}
// FormattedStart returns the Start time in the format "YYYY-MM-DD hh:mm:ss -0000 GMT"
func (r *Result) FormattedStart() string {
return r.Start().Truncate(time.Second).String()
}
// FormattedFinish returns the Finish time in the format "YYYY-MM-DD hh:mm:ss -0000 GMT"
func (r *Result) FormattedFinish() string {
return r.Finish().Truncate(time.Second).String()
}
// FormattedDuration returns the total duration as a string, truncated to 3 decimal places.
func (r *Result) FormattedDuration() string {
return fmt.Sprintf("%.3f sec", r.Duration())
}
// Start returns the start time for the first apply attempt
func (r *Result) Start() time.Time {
var start time.Time
attempts := append(r.Successes, r.Failures...)
for _, attempt := range attempts {
if start.IsZero() || attempt.Start.Before(start) {
start = attempt.Start
}
}
return start
}
// TotalModules returns the total count of apply attempts, both successes and failures.
func (r *Result) TotalModules() int {
return len(r.Successes) + len(r.Failures)
}
// LastCommitLink returns a URL for the most recent commit if the envar $DIFF_URL_FORMAT is specified, otherwise it returns empty string.
func (r *Result) LastCommitLink() string {
if r.CommitHash == "" || r.DiffURLFormat == "" || !strings.Contains(r.DiffURLFormat, "%s") {
return ""
}
return fmt.Sprintf(r.DiffURLFormat, r.CommitHash)
}
type Filtered struct {
FilteredBy string
Total int
Modules []ApplyAttempt
}
func (r *Result) Filter(filteredBy string) Filtered {
filtered := Filtered{
FilteredBy: filteredBy,
Total: r.TotalModules(),
}
switch filteredBy {
case "failure":
filtered.Modules = r.Failures
return filtered
case "plan_only":
for _, a := range r.Successes {
if a.DryRun {
filtered.Modules = append(filtered.Modules, a)
}
}
return filtered
case "applied":
for _, a := range r.Successes {
if !a.DryRun {
filtered.Modules = append(filtered.Modules, a)
}
}
return filtered
}
return filtered
}