/
expected_duration.go
91 lines (81 loc) · 2.07 KB
/
expected_duration.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
package task
import (
"fmt"
"time"
"github.com/evergreen-ci/evergreen"
"github.com/mongodb/anser/bsonutil"
"github.com/pkg/errors"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo/options"
)
var DurationIndex = bson.D{
{Key: ProjectKey, Value: 1},
{Key: BuildVariantKey, Value: 1},
{Key: DisplayNameKey, Value: 1},
{Key: StatusKey, Value: 1},
{Key: FinishTimeKey, Value: 1},
{Key: StartTimeKey, Value: 1},
}
type expectedDurationResults struct {
DisplayName string `bson:"_id"`
ExpectedDuration float64 `bson:"exp_dur"`
StdDev float64 `bson:"std_dev"`
}
func getExpectedDurationsForWindow(name, project, buildVariant string, start, end time.Time) ([]expectedDurationResults, error) {
match := bson.M{
BuildVariantKey: buildVariant,
ProjectKey: project,
StatusKey: bson.M{
"$in": []string{evergreen.TaskSucceeded, evergreen.TaskFailed},
},
bsonutil.GetDottedKeyName(DetailsKey, TaskEndDetailTimedOut): bson.M{
"$ne": true,
},
StartTimeKey: bson.M{
"$gt": start,
},
FinishTimeKey: bson.M{
"$lte": end,
},
}
if name != "" {
match[DisplayNameKey] = name
}
pipeline := []bson.M{
{
"$match": match,
},
{
"$project": bson.M{
DisplayNameKey: 1,
TimeTakenKey: 1,
IdKey: 0,
},
},
{
"$group": bson.M{
"_id": fmt.Sprintf("$%s", DisplayNameKey),
"exp_dur": bson.M{
"$avg": fmt.Sprintf("$%s", TimeTakenKey),
},
"std_dev": bson.M{
"$stdDevPop": fmt.Sprintf("$%s", TimeTakenKey),
},
},
},
}
// anonymous struct for unmarshalling result bson
results := []expectedDurationResults{}
coll := evergreen.GetEnvironment().DB().Collection(Collection)
ctx, cancel := evergreen.GetEnvironment().Context()
defer cancel()
cursor, err := coll.Aggregate(ctx, pipeline, &options.AggregateOptions{Hint: DurationIndex})
if err != nil {
return nil, errors.Wrap(err, "aggregating task average duration")
}
err = cursor.All(ctx, &results)
if err != nil {
return nil, errors.Wrap(err, "iterating and decoding task average duration")
}
return results, nil
}