/
store.go
111 lines (94 loc) · 2.43 KB
/
store.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
package httpSmoke
import (
"fmt"
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
"log"
"time"
)
func NewMongoStore(db *mgo.Database) Store {
return &mongoStore{db}
}
type httpEndpoint struct {
ID bson.ObjectId `bson:"_id,omitempty"`
Name string
Endpoint string
Method string
Parameters []parameters
Threshold int
Chat uint32
LastChecked time.Time
LastSuccess time.Time
ErrorCount int
Passing bool
Error string
}
type parameters struct {
Name, Value string
}
type Store interface {
addHTMLEndpoint(endpoint httpEndpoint) error
getHTMLEndpoints() []httpEndpoint
getHTMLEndpointsByChat(chat uint32) ([]httpEndpoint, error)
successfulEndpointTest(endpoint *httpEndpoint) error
failedEndpointTest(endpoint *httpEndpoint, errorMessage string) error
}
type mongoStore struct {
mongo *mgo.Database
}
func (s *mongoStore) getHTMLEndpointsByChat(chat uint32) (result []httpEndpoint, err error) {
c := s.mongo.C("MonitorHtmlEndpoints")
q := c.Find(bson.M{"chat": chat})
err = q.All(&result)
return
}
func (s *mongoStore) addHTMLEndpoint(endpoint httpEndpoint) error {
c := s.mongo.C("MonitorHtmlEndpoints")
return c.Insert(endpoint)
}
func (s *mongoStore) getHTMLEndpoints() []httpEndpoint {
c := s.mongo.C("MonitorHtmlEndpoints")
q := c.Find(nil)
i, err := q.Count()
if err != nil {
log.Println(err)
return nil
}
r := make([]httpEndpoint, i)
err = q.All(&r)
if err != nil {
log.Println(err)
return nil
}
return r
}
func (s *mongoStore) successfulEndpointTest(endpoint *httpEndpoint) error {
c := s.mongo.C("MonitorHtmlEndpoints")
endpoint.LastChecked = time.Now()
endpoint.LastSuccess = time.Now()
endpoint.Passing = true
endpoint.Error = ""
endpoint.ErrorCount = 0
err := c.UpdateId(endpoint.ID, endpoint)
if err != nil {
return fmt.Errorf("error saving endpoint with success details: %s", err.Error())
}
return nil
}
func (s *mongoStore) failedEndpointTest(endpoint *httpEndpoint, errorMessage string) error {
c := s.mongo.C("MonitorHtmlEndpoints")
result := httpEndpoint{}
err := c.FindId(endpoint.ID).One(&result)
if err != nil {
return fmt.Errorf("error retreiving endpoint with success details: %s", err.Error())
}
result.LastChecked = time.Now()
result.Passing = false
result.Error = errorMessage
result.ErrorCount++
err = c.UpdateId(endpoint.ID, result)
if err != nil {
return fmt.Errorf("error saving endpoint with success details: %s", err.Error())
}
return nil
}