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
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
|
package sfive
import (
"database/sql"
"encoding/json"
"fmt"
"time"
// needed for gorp tracing
// "log"
// "os"
"github.com/boltdb/bolt"
_ "github.com/mattn/go-sqlite3"
"github.com/pborman/uuid"
"gopkg.in/gorp.v2"
)
type sqliteStore struct {
db *gorp.DbMap
dbBolt *bolt.DB
hubId string
}
func sourceFromStatisticsData(value StatisticsData) sourceDb {
return sourceDb{
Hostname: value.SourceId.Hostname,
StreamId: streamIdDb{
ContentId: value.SourceId.StreamId.ContentId,
Format: value.SourceId.StreamId.Format,
Quality: value.SourceId.StreamId.Quality,
},
Tags: value.SourceId.Tags,
}
}
func dataUpdateFromStatisticsData(value StatisticsData) dataUpdateDb {
return dataUpdateDb{
-1,
-1,
value.SourceHubUuid,
value.SourceHubDataUpdateId,
value.StartTime.Unix(),
value.Duration,
value.Data.ClientCount,
value.Data.BytesReceived,
value.Data.BytesSent}
}
func updateFromStatisticsData(value StatisticsData) (dataUpdateDb, []ClientData, sourceDb) {
du := dataUpdateFromStatisticsData(value)
cd := value.Data.Clients
src := sourceFromStatisticsData(value)
return du, cd, src
}
func initDbBolt(boltPath string) (boltDb *bolt.DB, err error) {
boltDb, err = bolt.Open(boltPath, 0600, &bolt.Options{Timeout: 1 * time.Second})
if err != nil {
return
}
err = boltDb.Update(func(tx *bolt.Tx) error {
if _, err := tx.CreateBucketIfNotExists([]byte(sourcesFwdBn)); err != nil {
return err
}
if _, err := tx.CreateBucketIfNotExists([]byte(sourcesRevBn)); err != nil {
return err
}
if _, err := tx.CreateBucketIfNotExists([]byte(clientDataBn)); err != nil {
return err
}
if _, err := tx.CreateBucketIfNotExists([]byte(userAgentsFwdBn)); err != nil {
return err
}
_, err := tx.CreateBucketIfNotExists([]byte(userAgentsRevBn))
return err
})
return
}
func initDbSqlite(sqlitePath string) (dbmap *gorp.DbMap, hubId string, err error) {
var db *sql.DB
db, err = sql.Open("sqlite3", sqlitePath)
if err != nil {
return
}
dbmap = &gorp.DbMap{Db: db, Dialect: gorp.SqliteDialect{}}
// dbmap.TraceOn("[gorp]", log.New(os.Stdout, "myapp:", log.Lmicroseconds))
dbmap.AddTableWithName(dataUpdateDb{}, dataUpdatesTn).SetKeys(true, "Id")
dbmap.AddTableWithName(hubInfoDb{}, hubInfoTn).SetKeys(false, "Name")
// TODO use some real migration, yadda yadda
if err = dbmap.CreateTablesIfNotExists(); err != nil {
return
}
hubId, err = dbmap.SelectStr("select Value from " + hubInfoTn + " where Name = 'HubUuid'")
// TODO handle only not-found this way
if err != nil || hubId == "" {
hubId = uuid.New()
_, err = db.Exec("insert into "+hubInfoTn+" values ('HubUuid', ?)", hubId)
if err != nil {
hubId = ""
return
}
}
return
}
func insertAnd(needsAnd *bool) (res string) {
if *needsAnd {
res = " and"
*needsAnd = false
}
return
}
func (s sqliteStore) insertDataUpdateEntry(srcId int, du *dataUpdateDb) (err error) {
du.SourceId = srcId
err = s.db.Insert(du)
if err != nil {
return
}
return
}
func (s sqliteStore) insertNewSource(tx *bolt.Tx, src sourceDb) (srcId int, err error) {
bf := tx.Bucket([]byte(sourcesFwdBn))
bf.FillPercent = 1.0 // we only do appends
br := tx.Bucket([]byte(sourcesRevBn))
br.FillPercent = 1.0 // we only do appends
slug := src.String()
bSrcId := bf.Get([]byte(slug))
if bSrcId != nil {
return btoi(bSrcId), nil
}
var jsonData []byte
if jsonData, err = json.Marshal(src); err != nil {
return
}
next, _ := bf.NextSequence()
srcId = int(next)
if err = bf.Put([]byte(slug), itob(srcId)); err != nil {
return
}
if err = br.Put(itob(srcId), jsonData); err != nil {
return
}
return srcId, err
}
func (s sqliteStore) insertNewUserAgent(tx *bolt.Tx, ua string) (uaId int, err error) {
bf := tx.Bucket([]byte(userAgentsFwdBn))
bf.FillPercent = 1.0 // we only do appends
br := tx.Bucket([]byte(userAgentsRevBn))
br.FillPercent = 1.0 // we only do appends
bUaId := bf.Get([]byte(ua))
if bUaId != nil {
return btoi(bUaId), nil
}
next, _ := bf.NextSequence()
uaId = int(next)
if err = bf.Put([]byte(ua), itob(uaId)); err != nil {
return
}
if err = br.Put(itob(uaId), []byte(ua)); err != nil {
return
}
return uaId, err
}
func (s sqliteStore) insertDataUpdateClientEntries(tx *bolt.Tx, duId int, cd []ClientData) error {
if len(cd) == 0 {
return nil
}
b := tx.Bucket([]byte(clientDataBn))
b.FillPercent = 1.0 // we only do appends
data := []clientDataDb{}
for _, c := range cd {
uaId, err := s.insertNewUserAgent(tx, c.UserAgent)
if err != nil {
return err
}
data = append(data, clientDataDb{c.Ip, uaId, c.BytesSent})
}
jsonData, err := json.Marshal(data)
if err != nil {
return err
}
return b.Put(itob(duId), jsonData)
}
func (s sqliteStore) appendItem(tx *bolt.Tx, du dataUpdateDb, cd []ClientData, src sourceDb) (err error) {
var srcId int
if srcId, err = s.insertNewSource(tx, src); err != nil {
return
}
if err = s.insertDataUpdateEntry(srcId, &du); err != nil {
return
}
if err = s.insertDataUpdateClientEntries(tx, du.Id, cd); err != nil {
return
}
return
}
func (s sqliteStore) Append(update StatisticsData) (err error) {
return s.AppendMany([]StatisticsData{update})
}
func (s sqliteStore) AppendMany(updates []StatisticsData) (err error) {
var tx *gorp.Transaction
tx, err = s.db.Begin()
if err != nil {
return
}
err = s.dbBolt.Update(func(tx *bolt.Tx) error {
for _, update := range updates {
du, cd, src := updateFromStatisticsData(update)
if err := s.appendItem(tx, du, cd, src); err != nil {
return err
}
}
return nil
})
if err != nil {
tx.Rollback()
return
}
return tx.Commit()
}
func (s sqliteStore) getSource(tx *bolt.Tx, id int) (res sourceDb, err error) {
b := tx.Bucket([]byte(sourcesRevBn))
jsonData := b.Get(itob(id))
if jsonData == nil {
err = ErrNotFound
return
}
if err = json.Unmarshal(jsonData, &res); err != nil {
return
}
return
}
func (s sqliteStore) GetSources() (res []SourceId, err error) {
res = []SourceId{}
err = s.dbBolt.View(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte(sourcesRevBn))
c := b.Cursor()
for k, v := c.First(); k != nil; k, v = c.Next() {
var s sourceDb
if err := json.Unmarshal(v, &s); err != nil {
return err
}
var src SourceId
src.CopyFromSourceDb(s)
res = append(res, src)
}
return nil
})
return
}
func (s sqliteStore) GetSource(id int) (res SourceId, err error) {
err = s.dbBolt.View(func(tx *bolt.Tx) error {
src, err := s.getSource(tx, id)
if err != nil {
return err
}
res.CopyFromSourceDb(src)
return nil
})
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) getClientsByUpdateId(tx *bolt.Tx, id int) (res []ClientData, err error) {
bc := tx.Bucket([]byte(clientDataBn))
bu := tx.Bucket([]byte(userAgentsRevBn))
jsonData := bc.Get(itob(id))
if jsonData == nil {
return
}
data := []clientDataDb{}
if err = json.Unmarshal(jsonData, &data); err != nil {
return
}
for _, c := range data {
cd := ClientData{Ip: c.Ip, BytesSent: c.BytesSent}
ua := bu.Get(itob(c.UserAgentId))
if ua != nil {
cd.UserAgent = string(ua)
}
res = append(res, cd)
}
return
}
func (s sqliteStore) CreateStatisticsDataFrom(tx *bolt.Tx, dat dataUpdateDb) (res StatisticsData, err error) {
var clients []ClientData
if clients, err = s.getClientsByUpdateId(tx, dat.Id); err != nil {
return
}
var src sourceDb
if src, err = s.getSource(tx, dat.SourceId); err != nil {
return
}
res.CopyFromDataUpdateDb(dat, s.hubId)
res.Hostname = src.Hostname
res.StreamId.ContentId = src.StreamId.ContentId
res.StreamId.Format = src.StreamId.Format
res.StreamId.Quality = src.StreamId.Quality
res.Tags = src.Tags
res.Data.Clients = clients
return
}
func (s sqliteStore) CreateStatisticsDatasFrom(dat []interface{}) (res []StatisticsData, err error) {
err = s.dbBolt.View(func(tx *bolt.Tx) error {
for i := range dat {
sd, err := s.CreateStatisticsDataFrom(tx, *dat[i].(*dataUpdateDb))
if err != nil {
return err
}
res = append(res, sd)
}
return nil
})
return
}
func (s sqliteStore) GetUpdatesAfter(id int) (res []StatisticsData, err error) {
parameters := make(map[string]interface{})
sql := "SELECT * FROM " + dataUpdatesTn + " WHERE Id > :afterUpdateId limit :limit"
parameters["afterUpdateId"] = id
parameters["limit"] = 5000
var updates []interface{}
updates, err = s.db.Select(dataUpdateDb{}, sql, parameters)
s5tl.Printf("sql: %s", sql)
if err == nil {
res, _ = s.CreateStatisticsDatasFrom(updates)
}
return
}
func (s sqliteStore) GetUpdates(filter *StatsFilter) (res []StatisticsData, err error) {
err = fmt.Errorf("not implemented!")
return
// limit := 5000
// if filter.limit == nil {
// filter.limit = &limit
// } else if *filter.limit > limit {
// *filter.limit = limit
// }
// sourceSql, parameters := getFilteredDataUpdateSelect(filter)
// sql := "SELECT " + updateColumnSelect + " FROM " + sourceSql
// s5tl.Printf("store: sql: %s", sql)
// var updates []interface{}
// updates, err = s.db.Select(dataUpdateQueryResult{}, sql, parameters)
// if err == nil {
// res, _ = s.CreateStatisticsDatasFrom(updates)
// }
// return
}
type lastUpdateQueryResult struct {
MaxDataUpdateId *int
}
func (s sqliteStore) GetLastUpdateForUuid(uuid string) (updateId *int, err error) {
result := lastUpdateQueryResult{}
err = s.db.SelectOne(
&result,
"select max(SourceHubDataUpdateId) as MaxDataUpdateId from "+dataUpdatesTn+" where SourceHubUuid = ?",
uuid)
if err == nil {
updateId = result.MaxDataUpdateId
} else {
s5l.Printf("db: failed to find max SourceHubDataUpdateId for %s: %v", uuid, err)
}
return
}
func (s sqliteStore) GetLastUpdateId() (updateId *int, err error) {
result := lastUpdateQueryResult{}
err = s.db.SelectOne(&result, "select max(Id) as MaxDataUpdateId from "+dataUpdatesTn)
if err == nil {
updateId = result.MaxDataUpdateId
} else {
s5l.Printf("db: failed to find max DataUpdateId: %v", err)
}
return
}
type statsResult struct {
UpdateCount *int
HubCount *int
SourcesCount *int
ClientCount *float32
BytesSent *uint
BytesReceived *uint
StartTime *int64
LastStartTime *int64
}
type StatsResult struct {
UpdateCount int
HubCount int
SourcesCount int
ClientCount float32
BytesSent uint
BytesReceived uint
StartTime time.Time
LastStartTime time.Time
}
func toApiStatsResult(value statsResult) (res StatsResult) {
if value.UpdateCount != nil {
res.UpdateCount = *value.UpdateCount
}
if value.HubCount != nil {
res.HubCount = *value.HubCount
}
if value.SourcesCount != nil {
res.SourcesCount = *value.SourcesCount
}
if value.ClientCount != nil {
res.ClientCount = *value.ClientCount
}
if value.BytesSent != nil {
res.BytesSent = *value.BytesSent
}
if value.BytesReceived != nil {
res.BytesReceived = *value.BytesReceived
}
if value.StartTime != nil {
res.StartTime = time.Unix(*value.StartTime, 0)
}
if value.LastStartTime != nil {
res.LastStartTime = time.Unix(*value.LastStartTime, 0)
}
return res
}
// var (
// statsGroupSelect = `
// SELECT
// count(*) as UpdateCount,
// SourceHubUuid as SourceHubUuid,
// count(distinct SourceId) as SourcesCount,
// avg(ClientCount) as ClientCount,
// sum(BytesSent) as BytesSent,
// sum(BytesReceived) as BytesReceived,
// min(StartTime) as StartTime,
// max(StartTime) as LastStartTime
// FROM
// `
// statsGroupClause = `
// GROUP BY
// SourceHubUuid
// `
// statsAggregateSelect = `
// SELECT
// sum(UpdateCount) as UpdateCount,
// count(distinct SourceHubUuid) as HubCount,
// sum(SourcesCount) as SourcesCount,
// sum(ClientCount) as ClientCount,
// sum(BytesSent) as BytesSent,
// sum(BytesReceived) as BytesReceived,
// min(StartTime) as StartTime,
// max(LastStartTime) as LastStartTime
// FROM
// `
// )
func (s sqliteStore) GetStats(filter *StatsFilter) (StatsResult, error) { // (map[string]interface{}, error) {
return StatsResult{}, fmt.Errorf("not implemented!")
// sourceSql, parameters := getFilteredDataUpdateSelect(filter)
// _ = sourceSql
// sql := fmt.Sprintf("%s (%s %s %s)", statsAggregateSelect, statsGroupSelect, sourceSql, statsGroupClause)
// s5tl.Printf("store: stats sql: %s", sql)
// res := statsResult{}
// err := s.db.SelectOne(&res, sql, parameters)
// if err == nil {
// return toApiStatsResult(res), nil
// }
// return StatsResult{}, err
}
func (s sqliteStore) GetStoreId() (uuid string, err error) {
uuid, err = s.db.SelectStr("select Value from HubInfo where Name = ?", "HubUuid")
return
}
func NewStore(sqlitePath, boltPath string) (sqliteStore, error) {
boltDb, err := initDbBolt(boltPath)
if err != nil {
return sqliteStore{}, err
}
db, hubid, err := initDbSqlite(sqlitePath)
if err != nil {
boltDb.Close()
return sqliteStore{}, err
}
return sqliteStore{db, boltDb, hubid}, nil
}
func (s sqliteStore) Close() {
s.db.Db.Close()
s.dbBolt.Close()
}
|