This repository has been archived by the owner on Dec 2, 2018. It is now read-only.
/
mongo.go
161 lines (140 loc) · 3.49 KB
/
mongo.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
package main
import (
"fmt"
"labix.org/v2/mgo"
"labix.org/v2/mgo/bson"
"net/url"
"time"
)
type MongoConfig struct {
Host string `json:",omitempty"`
Port string `json:",omitempty"`
Username string `json:",omitempty"`
Password string `json:",omitempty"`
Database string `json:",omitempty"`
}
func openMongo(c *MongoConfig) (b Backend, err error) {
//build a url for connecting based on the config
u := &url.URL{
Scheme: "mongodb",
Host: c.Host,
}
//only add credentials and database in the url if they're specified
if c.Username != "" && c.Password == "" {
u.User = url.User(c.Username)
u.Path = "/" + c.Database
}
if c.Username != "" && c.Password != "" {
u.User = url.UserPassword(c.Username, c.Password)
u.Path = "/" + c.Database
}
s, err := mgo.Dial(u.String())
if err != nil {
err = fmt.Errorf("dial %s: %s", u, err)
return
}
b = &mongoBackend{
tasks: s.DB(c.Database).C("tasks"),
startlog: s.DB(c.Database).C("startlog"),
}
return
}
type mongoBackend struct {
tasks *mgo.Collection
startlog *mgo.Collection
}
type d map[string]interface{}
func (m *mongoBackend) Save(task *Task) (err error) {
//while theres a task with this name, increment the number on the end of it
candidate := task.Name
for i := 1; ; i++ {
var n int
n, err = m.tasks.Find(d{"name": candidate}).Count()
if err != nil {
return
}
if n == 0 {
task.Name = candidate
break
}
candidate = fmt.Sprintf("%s%d", task.Name, i)
}
err = m.tasks.Insert(task)
return
}
func (m *mongoBackend) Load(name string) (task *Task, err error) {
task = new(Task)
err = m.tasks.Find(d{"name": name}).One(task)
return
}
func (m *mongoBackend) SetDescription(task *Task, desc string) (err error) {
ch := bson.D{
{"$set", d{"description": desc}},
}
err = m.tasks.Update(d{"name": task.Name}, ch)
return
}
func (m *mongoBackend) AddAnnotation(task *Task, a Annotation) (err error) {
//create the change document
ch := bson.D{
{"$push", d{"annotations": a}},
{"$inc", d{"estimate": a.EstimateDelta}},
{"$inc", d{"actual": a.ActualDelta}},
}
err = m.tasks.Update(d{"name": task.Name}, ch)
return
}
func (m *mongoBackend) PopAnnotation(task *Task) (err error) {
if len(task.Annotations) == 0 {
err = fmt.Errorf("no annotations to undo")
}
//get the last annotation and negate it
a := task.Annotations[len(task.Annotations)-1].Negate()
//create the change document
ch := bson.D{
{"$pop", d{"annotations": 1}},
{"$inc", d{"estimate": a.EstimateDelta}},
{"$inc", d{"actual": a.ActualDelta}},
}
err = m.tasks.Update(d{"name": task.Name}, ch)
return
}
func (m *mongoBackend) Rename(oldn, newn string) (err error) {
ch := d{"$set": d{"name": newn}}
err = m.tasks.Update(d{"name": oldn}, ch)
return
}
func (m *mongoBackend) Remove(name string) (err error) {
err = m.tasks.Remove(d{"name": name})
return
}
func (m *mongoBackend) Start(name string) (err error) {
err = m.startlog.Insert(StartLog{
Name: name,
When: time.Now(),
})
return
}
func (m *mongoBackend) Stop() (err error) {
_, err = m.startlog.RemoveAll(nil)
return
}
func (m *mongoBackend) Status() (log *StartLog, err error) {
n, err := m.startlog.Count()
if err != nil {
return
}
if n == 0 {
return
}
log = new(StartLog)
err = m.startlog.Find(nil).One(log)
return
}
func (m *mongoBackend) Find(regex string, before, after time.Time) (tasks []*Task, err error) {
err = m.tasks.Find(d{
"name": d{"$regex": regex},
"annotations.when": d{"$lt": after, "$gte": before},
}).All(&tasks)
return
}