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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
|
package sfive
import (
"database/sql"
"time"
_ "github.com/mattn/go-sqlite3"
"github.com/coopernurse/gorp"
)
// compared to JSON DTOs, DB types are flattened, and use key-relations instead of collections
// this is very much not normalized at all, because I'm too lazy to type
// table names
const (
tagsTn = "Tags"
sourceTagsTn = "StreamToTagMap"
sourcesTn = "Sources"
clientdataUpdatesTn = "ClientDataUpdates"
dataUpdatesTn = "DataUpdates"
)
// stored in tagsTn
type tagDb struct {
Id int
Name string
}
// stored in sourceTagsTn
// Stream m:n Tag
type sourceTagsDb struct {
TagId int // foreign key to tagsTn
SourceId int // foreign key to sourcesTn
}
// stored in sourcesTn
type sourceDb struct {
Id int
StreamId
SourceId
}
// stored in clientdataUpdatesTn
// ClientData n:1 DataUpdate
type clientDataDb struct {
Id int
DataUpdatesId int // foreign key to dataUpdatesTn
ClientData
}
// stored in dataUpdatesTn
// in DB, StatisticsData/DataUpdate is flattened compared to JSON DTOs
type dataUpdateDb struct {
Id int
SourceId int // foreign key to sourcesTn
StartTime int64 // time.Time
Duration int64 // time.Duration
ClientCount uint
BytesReceived uint
BytesSent uint
}
type sqliteStore struct {
db *gorp.DbMap
}
func tagsFromStatisticsData(value StatisticsData) []tagDb {
tags := make([]tagDb, len(value.SourceId.Tags))
for i := range value.SourceId.Tags {
tags[i] = tagDb{Id: -1, Name: value.SourceId.Tags[i]}
}
return tags
}
func sourceFromStatisticsData(value StatisticsData) sourceDb {
return sourceDb{
-1,
StreamId{
ContentId: value.SourceId.StreamId.ContentId,
Format: value.SourceId.StreamId.Format,
Quality: value.SourceId.StreamId.Quality,
},
SourceId{
Hostname: value.SourceId.Hostname},
}
}
func clientsFromStatisticsData(value StatisticsData) []clientDataDb {
res := make([]clientDataDb, len(value.Data.Clients))
for i := range value.Data.Clients {
res[i] = clientDataDb{-1, -1, value.Data.Clients[i]}
}
return res
}
func dataUpdateFromStatisticsData(value StatisticsData) dataUpdateDb {
return dataUpdateDb{
-1,
-1,
value.StartTime.Unix(),
int64(value.Duration.Seconds()),
value.Data.ClientCount,
value.Data.BytesReceived,
value.Data.BytesSent}
}
func updateFromStatisticsData(value StatisticsData) (dataUpdateDb, []clientDataDb, sourceDb, []tagDb) {
du := dataUpdateFromStatisticsData(value)
cd := clientsFromStatisticsData(value)
src := sourceFromStatisticsData(value)
tags := tagsFromStatisticsData(value)
return du, cd, src, tags
}
func initDb(path string) (res *gorp.DbMap, err error) {
// connect to db using standard Go database/sql API
db, err := sql.Open("sqlite3", path)
if err != nil {
return
}
dbmap := &gorp.DbMap{Db: db, Dialect: gorp.SqliteDialect{}}
// dbmap.TraceOn("[gorp]", log.New(os.Stdout, "myapp:", log.Lmicroseconds))
dbmap.AddTableWithName(tagDb{}, tagsTn).SetKeys(true, "Id").ColMap("Name").SetUnique(true)
dbmap.AddTableWithName(sourceTagsDb{}, sourceTagsTn).SetKeys(false, "TagId", "SourceId")
dbmap.AddTableWithName(sourceDb{}, sourcesTn).SetKeys(true, "Id").SetUniqueTogether("ContentId", "Format", "Quality", "Hostname")
dbmap.AddTableWithName(clientDataDb{}, clientdataUpdatesTn).SetKeys(true, "Id")
dbmap.AddTableWithName(dataUpdateDb{}, dataUpdatesTn).SetKeys(true, "Id")
// TODO use some real migration, yadda yadda
err = dbmap.CreateTablesIfNotExists()
if err != nil {
return
}
res = dbmap
return
}
func isEmptyFilter(filter *StatsFilter) bool {
if filter == nil {
return true
}
if filter.start == nil &&
filter.end == nil &&
filter.hostname == nil &&
filter.contentId == nil &&
filter.format == nil &&
filter.quality == nil &&
(filter.tagsAny == nil || len(filter.tagsAny) == 0) {
return true
}
return false
}
func insertAnd(needsAnd *bool) (res string) {
if *needsAnd {
res = " and"
*needsAnd = false
}
return
}
func getFilteredDataUpdateSelect(filter *StatsFilter) (string, map[string]interface{}) {
if isEmptyFilter(filter) {
return dataUpdatesTn, nil
}
query := "(select * from " + dataUpdatesTn + " where"
parameters := make(map[string]interface{})
needsAnd := false
if filter.start != nil {
query += insertAnd(&needsAnd)
query += " StartTime >= :filterstart"
parameters["filterstart"] = filter.start.Unix()
needsAnd = true
}
if filter.end != nil {
query += insertAnd(&needsAnd)
query += " StartTime < :filterend"
parameters["filterend"] = filter.end.Unix()
needsAnd = true
}
// TODO other fields
query += ")"
return query, parameters
}
func (s sqliteStore) findTag(name string) (tag *tagDb, err error) {
t := tagDb{}
err = s.db.SelectOne(&t, "select * from "+tagsTn+" where Name = ?", name)
if err == nil {
tag = &t
}
return
}
func (s sqliteStore) insertNewTags(tags []tagDb) (err error) {
for i := range tags {
t, err := s.findTag(tags[i].Name)
if err != nil {
_, err = s.db.Exec("insert into "+tagsTn+" VALUES (NULL, ?)", tags[i].Name)
}
t, err = s.findTag(tags[i].Name)
if err == nil {
tags[i] = *t
} else {
break
}
}
return
}
func (s sqliteStore) findSource(src sourceDb) (res *sourceDb, err error) {
t := sourceDb{}
err = s.db.SelectOne(
&t,
"select Id from "+sourcesTn+" where ContentId = ? and Format = ? and Quality = ? and Hostname = ?",
src.ContentId,
src.Format,
src.Quality,
src.Hostname)
if err == nil {
res = &t
}
return
}
func (s sqliteStore) insertNewSource(src *sourceDb) (err error) {
t, err := s.findSource(*src)
if err == nil {
*src = *t
} else {
err = s.db.Insert(src)
}
return
}
func (s sqliteStore) insertSourceTagLinks(src sourceDb, tags []tagDb) (err error) {
st := make([]sourceTagsDb, len(tags))
for i := range tags {
st[i].TagId = tags[i].Id
st[i].SourceId = src.Id
}
for i := range st {
_, err = s.db.Exec(
"insert or ignore into "+sourceTagsTn+" values (?,?)",
st[i].SourceId,
st[i].TagId)
// err = s.db.Insert(&st[i])
if err != nil {
// TODO
//fmt.Printf("st\n")
return
}
}
return
}
func (s sqliteStore) insertDataUpdateEntry(src sourceDb, du *dataUpdateDb) (err error) {
du.SourceId = src.Id
err = s.db.Insert(du)
if err != nil {
//fmt.Printf("du\n")
return
}
return
}
func (s sqliteStore) insertDataUpdateClientEntries(cd []clientDataDb, du dataUpdateDb) (err error) {
for i := range cd {
cd[i].DataUpdatesId = du.Id
err = s.db.Insert(&cd[i])
if err != nil {
// TODO
return
}
}
return
}
// this function is the biggest pile of copy/pasted crap while sick that is still compilable.
func (s sqliteStore) Append(update StatisticsData) (err error) {
du, cd, src, tags := updateFromStatisticsData(update)
// s.db.TraceOn("", log.New(os.Stdout, "gorptest: ", log.Lmicroseconds))
tx, err := s.db.Begin()
if err != nil {
return
}
err = s.insertNewTags(tags)
if err != nil {
return
}
err = s.insertNewSource(&src)
if err != nil {
//fmt.Printf("src\n")
return
}
err = s.insertSourceTagLinks(src, tags)
if err != nil {
return
}
err = s.insertDataUpdateEntry(src, &du)
if err != nil {
return
}
err = s.insertDataUpdateClientEntries(cd, du)
if err != nil {
return
}
return tx.Commit()
}
func castArrayToString(value []interface{}) []string {
res := make([]string, len(value))
for i := range value {
res[i] = value[i].(string)
}
return res
}
func (s sqliteStore) GetTags() ([]string, error) {
res, dbErr := s.db.Select("", "select Name from "+tagsTn)
if dbErr == nil {
sRes := castArrayToString(res)
return sRes, nil
}
return nil, dbErr
}
func (s sqliteStore) GetSources() (res []sourceDb, err error) {
qres, err := s.db.Select(sourceDb{}, "select * from "+sourcesTn)
if err == nil {
res = make([]sourceDb, len(qres))
for i := range qres {
res[i] = *qres[i].(*sourceDb)
}
}
return
}
func (s sqliteStore) GetSource(id int) (res sourceDb, err error) {
err = s.db.SelectOne(&res, "select * from "+sourcesTn+" where Id = ?", id)
return
}
func (s sqliteStore) GetUpdate(id int) (res dataUpdateDb, err error) {
err = s.db.SelectOne(&res, "select * from "+dataUpdatesTn+" where Id = ?", id)
return
}
func (s sqliteStore) GetUpdates(filter *StatsFilter) (res []dataUpdateDb, err error) {
sourceSql, parameters := getFilteredDataUpdateSelect(filter)
updates, err := s.db.Select(
dataUpdateDb{},
"select Id, SourceId, StartTime, ClientCount, BytesReceived, BytesSent from "+sourceSql,
parameters)
if err == nil {
res = make([]dataUpdateDb, len(updates))
for i := range updates {
res[i] = *updates[i].(*dataUpdateDb)
}
}
return
}
func getCountEntriesSql() string {
return "count(*) as UpdateCount"
}
// TODO this is totally utterly wrong
func getClientCountSql() string {
return "avg(ClientCount) as ClientCount"
}
func getBpsStatsSql() string {
return "sum(BytesSent) as BytesSent, sum(BytesReceived) as BytesReceived, min(StartTime) as StartTime, max(StartTime) as LastStartTime"
}
func (s sqliteStore) CountUpdateEntries(filter *StatsFilter) (count int64, err error) {
sourceSql, parameters := getFilteredDataUpdateSelect(filter)
count, err = s.db.SelectInt("select "+getCountEntriesSql()+" from "+sourceSql, parameters)
return
}
func (s sqliteStore) CountClients(filter *StatsFilter) int {
sourceSql, parameters := getFilteredDataUpdateSelect(filter)
count, err := s.db.SelectInt("select "+getClientCountSql()+" from "+sourceSql, parameters)
if err != nil {
s5l.Printf("CountClients: %v", err)
return -1
}
return int(count)
}
type bpsQueryResult struct {
BytesSent uint
BytesReceived uint
StartTime time.Time
LastStartTime time.Time
}
func (s sqliteStore) GetAverageBps(filter *StatsFilter) (uint, error) {
sourceSql, parameters := getFilteredDataUpdateSelect(filter)
res := bpsQueryResult{}
err := s.db.SelectOne(
res,
"select "+getBpsStatsSql()+" from "+sourceSql, parameters)
if err == nil {
bps := (res.BytesSent + res.BytesReceived) / uint(res.StartTime.Sub(res.LastStartTime).Seconds())
return bps, nil
}
return 0, err
}
type statsResult struct {
UpdateCount *int
ClientCount *float32
BytesSent *uint
BytesReceived *uint
StartTime *int64
LastStartTime *int64
}
func (s sqliteStore) GetStats(filter *StatsFilter) (statsResult, error) { // (map[string]interface{}, error) {
sourceSql, parameters := getFilteredDataUpdateSelect(filter)
_ = sourceSql
sql := "select " + getCountEntriesSql() + "," + getClientCountSql() + "," + getBpsStatsSql() + " from " + sourceSql
s5l.Printf("stats sql: %v", sql)
res := statsResult{}
err := s.db.SelectOne(&res, sql, parameters)
return res, err
}
func NewStore(path string) (store sqliteStore, err error) {
db, err := initDb(path)
if err != nil {
return
}
store = sqliteStore{db}
return
}
func (s sqliteStore) Close() {
s.db.Db.Close()
}
|