forked from evergreen-ci/logkeeper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
schema.go
108 lines (94 loc) · 2.81 KB
/
schema.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
package logkeeper
import (
"fmt"
"time"
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
)
type Test struct {
Id bson.ObjectId `bson:"_id"`
BuildId interface{} `bson:"build_id"`
BuildName string `bson:"build_name"`
Name string `bson:"name"`
Command string `bson:"command"`
Started time.Time `bson:"started"`
Ended *time.Time `bson:"ended"`
Info map[string]interface{} `bson:"info"`
Failed bool `bson:"failed"`
Phase string `bson:"phase"`
Seq int `bson:"seq"`
}
type LogKeeperBuild struct {
Id interface{} `bson:"_id"`
Builder string `bson:"builder"`
BuildNum int `bson:"buildnum"`
Started time.Time `bson:"started"`
Name string `bson:"name"`
Info map[string]interface{} `bson:"info"`
Phases []string `bson:"phases"`
Seq int `bson:"seq"`
}
// If "raw" is a bson.ObjectId, returns the string value of its .Hex() function.
// Otherwise, returns it's string representation if it implements Stringer, or
// string representation generated by fmt's %v formatter.
func stringifyId(raw interface{}) string {
if buildObjId, ok := raw.(bson.ObjectId); ok {
return buildObjId.Hex()
}
if asStr, ok := raw.(fmt.Stringer); ok {
return asStr.String()
}
return fmt.Sprintf("%v", raw)
}
func idFromString(raw string) interface{} {
if bson.IsObjectIdHex(raw) {
return bson.ObjectIdHex(raw)
}
return raw
}
func findTest(db *mgo.Database, id string) (*Test, error) {
if !bson.IsObjectIdHex(id) {
return nil, nil
}
test := &Test{}
err := db.C("tests").Find(bson.M{"_id": bson.ObjectIdHex(id)}).One(test)
if err == mgo.ErrNotFound {
return nil, nil
}
if err != nil {
return nil, err
}
return test, nil
}
func findTestsForBuild(db *mgo.Database, buildId string) ([]Test, error) {
queryBuildId := idFromString(buildId)
tests := []Test{}
err := db.C("tests").Find(bson.M{"build_id": queryBuildId}).Sort("started").All(&tests)
if err != nil {
return nil, err
}
return tests, nil
}
func findBuildById(db *mgo.Database, id string) (*LogKeeperBuild, error) {
queryBuildId := idFromString(id)
build := &LogKeeperBuild{}
err := db.C("builds").Find(bson.M{"_id": queryBuildId}).One(build)
if err == mgo.ErrNotFound {
return nil, nil
}
if err != nil {
return nil, err
}
return build, nil
}
func findBuildByBuilder(db *mgo.Database, builder string, buildnum int) (*LogKeeperBuild, error) {
build := &LogKeeperBuild{}
err := db.C("builds").Find(bson.M{"builder": builder, "buildnum": buildnum}).One(build)
if err == mgo.ErrNotFound {
return nil, nil
}
if err != nil {
return nil, err
}
return build, nil
}