-
Notifications
You must be signed in to change notification settings - Fork 1
/
models_history.go
90 lines (75 loc) · 2.08 KB
/
models_history.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
package main
import (
"strconv"
"github.com/skybon/mgoHelpers"
"gopkg.in/mgo.v2/bson"
)
type HistoryDataEntryBase struct {
Status int64
Message string
Location LocationEntry
Source SourceEntry
Measurements MeasurementArray
RequestTime int64
WType string
Url string
}
type HistoryDataEntry struct {
mgoHelpers.DbEntryBase `bson:",inline"`
HistoryDataEntryBase `bson:",inline"`
}
func MakeHistoryDataEntry() HistoryDataEntry {
var entry HistoryDataEntry
entry.SetBsonID(bson.NewObjectId())
return entry
}
type WeatherHistory struct {
Database *mgoHelpers.MongoDb
Collection string
}
func (h *WeatherHistory) Add(entry HistoryDataEntry) {
h.Database.Insert(h.Collection, entry)
}
func (h *WeatherHistory) ReadHistory(entryid string, status int64, source string, wtype string, country string, locationid string, requeststart string, requestend string) (result []HistoryDataEntry) {
result = []HistoryDataEntry{}
query := make(map[string]interface{})
if entryid != "" {
query["_id"], _ = mgoHelpers.GetObjectIDFromString(entryid)
} else {
if status != 0 {
query["status"] = status
}
if source != "" {
query["source.name"] = source
}
if wtype != "" {
query["wtype"] = wtype
}
if country != "" {
query["location.iso_country"] = country
}
if locationid != "" {
query["location._id"], _ = mgoHelpers.GetObjectIDFromString(locationid)
}
if requeststart != "" || requestend != "" {
requestquery := make(map[string]int64)
if requeststart != "" {
requestquery[`$gte`], _ = strconv.ParseInt(requeststart, 10, 64)
}
if requestend != "" {
requestquery[`$lte`], _ = strconv.ParseInt(requestend, 10, 64)
}
query["requesttime"] = requestquery
}
}
h.Database.Find(h.Collection, query, &result)
return result
}
func (this *WeatherHistory) Clear() (err error) {
err = this.Database.RemoveAll(this.Collection)
return err
}
func NewWeatherHistory(db_instance *mgoHelpers.MongoDb) (history WeatherHistory) {
history = WeatherHistory{Database: db_instance, Collection: "WeatherHistory"}
return history
}