-
Notifications
You must be signed in to change notification settings - Fork 13
/
MongoClient.go
161 lines (133 loc) · 3.34 KB
/
MongoClient.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 (
"context"
"fmt"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/bsontype"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"log"
"strconv"
"time"
)
type MongoClient struct {
mongoUri string
mongoDb string
db *mongo.Database
client *mongo.Client
user string
}
func NewMongoClient(uri string, db string, user string, ctx context.Context) *MongoClient {
c := &MongoClient{
mongoUri: uri,
mongoDb: db,
user: user,
}
client, err := mongo.NewClient(options.Client().ApplyURI(c.mongoUri))
if err != nil {
log.Fatal(err)
}
c.client = client
err = client.Connect(ctx)
if err != nil {
log.Fatal(err)
}
err = client.Ping(ctx, nil)
if err != nil {
log.Fatal(err)
}
c.db = client.Database(c.mongoDb)
return c
}
func (c *MongoClient) Authorize(_ context.Context) {}
func (c *MongoClient) LoadDeviceStatuses(queue chan NsEntry, limit int64, skip int64, ctx context.Context) {
defer wg.Done()
fmt.Println("LoadDeviceStatuses from MongoDB, limit: ", limit, ", skip: ", skip)
collection := c.db.Collection("devicestatus")
filter := bson.D{{"openaps", bson.D{{"$exists", true}}}}
opts := options.Find()
opts.SetSort(bson.D{{"created_at", -1}})
if limit > 0 {
opts.SetLimit(limit)
}
if skip > 0 {
opts.SetSkip(skip)
}
cur, err := collection.Find(ctx, filter, opts)
if err != nil {
log.Fatal(err)
}
defer cur.Close(ctx)
var count = 0
for cur.Next(ctx) {
var entry NsEntry
err := cur.Decode(&entry)
if err != nil {
fmt.Println(cur.Current.String())
log.Fatal(err)
}
entry.User = c.user
if entry.OpenAps.Suggested.Bg > 0 {
field := cur.Current.Lookup("openaps", "suggested", "tick")
var tick float64 = 0
if field.Type == bsontype.String {
tick, err = strconv.ParseFloat(field.StringValue(), 32)
}
if field.Type == bsontype.Int32 {
tick = float64(field.AsInt64())
}
entry.OpenAps.Suggested.Tick = tick
}
queue <- entry
count++
fmt.Println("devicestatus time: ", entry.OpenAps.IOB.Time, "iob:", entry.OpenAps.IOB.IOB, ", bg: ", entry.OpenAps.Suggested.Bg)
}
fmt.Println("total devicestatuses sent: ", count)
if err := cur.Err(); err != nil {
log.Fatal(err)
}
}
func (c *MongoClient) LoadTreatments(queue chan NsTreatment, limit int64, skip int64, ctx context.Context) {
defer wg.Done()
fmt.Println("LoadTreatments from MongoDB, limit: ", limit, ", skip: ", skip)
collection := c.db.Collection("treatments")
filter := bson.D{}
opts := options.Find()
opts.SetSort(bson.D{{"created_at", -1}})
if limit > 0 {
opts.SetLimit(limit)
}
if skip > 0 {
opts.SetSkip(skip)
}
cur, err := collection.Find(ctx, filter, opts)
if err != nil {
log.Fatal(err)
}
defer cur.Close(ctx)
var count = 0
for cur.Next(ctx) {
var entry NsTreatment
err := cur.Decode(&entry)
if err != nil {
log.Fatal(err)
}
entry.User = c.user
strtime := cur.Current.Lookup("created_at").StringValue()
ptime, err := time.Parse(time.RFC3339, strtime)
if err != nil {
log.Fatal(err)
}
entry.CreatedAt = ptime
queue <- entry
count++
fmt.Println("treatment time: ", entry.CreatedAt, ", type: ", entry.EventType)
}
fmt.Println("total treatments sent: ", count)
if err := cur.Err(); err != nil {
log.Fatal(err)
}
}
func (c *MongoClient) Close(ctx context.Context) {
c.client.Disconnect(ctx)
}