-
Notifications
You must be signed in to change notification settings - Fork 0
/
mongo.go
60 lines (54 loc) · 1.6 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
package mongo
import (
"time"
"github.com/andreandradecosta/rpimonitor"
"github.com/pkg/errors"
mgo "gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
)
// SampleService is responsible for interacting with Mongo DB.
type SampleService struct {
mongoSession *mgo.Session
}
// NewSampleService connects do Mongo and returns a service instance.
func NewSampleService(url string) (*SampleService, error) {
session, err := mgo.Dial(url)
return &SampleService{
mongoSession: session,
}, errors.Wrap(err, "error connecting to mongo")
}
// Query returns a collections of samples stored in the specified time interval.
func (s *SampleService) Query(start, end time.Time) ([]rpimonitor.Sample, error) {
if s.mongoSession == nil {
return nil, errors.New("No session to Mongo")
}
session := s.mongoSession.Copy()
defer session.Close()
c := session.DB("rpimonitor").C("samples")
result := make([]rpimonitor.Sample, 1)
err := c.Find(bson.M{
"localTime": bson.M{
"$gte": start,
"$lte": end,
},
}).Select(bson.M{
"localTime": 1,
"timestamp": 1,
"metrics.load": 1,
"metrics.virtualMemory": 1,
"metrics.swapMemory": 1,
"metrics.temperature": 1,
"metrics.daysUptime": 1,
}).Sort(
"localTime",
).All(&result)
return result, errors.Wrapf(err, "query failed [%s]-[%s]", start, end)
}
// Write stores a sample metrics in the database.
func (s *SampleService) Write(sample *rpimonitor.Sample) error {
if s.mongoSession == nil {
return errors.New("No session to Mongo")
}
defer s.mongoSession.Refresh()
return s.mongoSession.DB("rpimonitor").C("samples").Insert(sample)
}