diff options
author | Steve Manuel <nilslice@gmail.com> | 2016-11-04 14:00:54 -0700 |
---|---|---|
committer | Steve Manuel <nilslice@gmail.com> | 2016-11-04 14:00:54 -0700 |
commit | ba0bdb862ed6ad2b586365df7ae1db3ae0f004d4 (patch) | |
tree | d4717ace21b9ffddb6b6a2947c719d0b582d0133 /system/api/analytics/batch.go | |
parent | 33e38247b580e9f5b38a4aa8a186f3ac2c814f05 (diff) |
adding batchPrune func to delete old analytics data, eventually would like to add a backup service. renaming recordChan for clarity
Diffstat (limited to 'system/api/analytics/batch.go')
-rw-r--r-- | system/api/analytics/batch.go | 50 |
1 files changed, 48 insertions, 2 deletions
diff --git a/system/api/analytics/batch.go b/system/api/analytics/batch.go index 1fee247..1b35339 100644 --- a/system/api/analytics/batch.go +++ b/system/api/analytics/batch.go @@ -3,20 +3,28 @@ package analytics import ( "encoding/json" "strconv" + "time" "github.com/boltdb/bolt" ) // batchInsert is effectively a specialized version of SetContentMulti from the // db package, iterating over a []apiRequest instead of []url.Values -func batchInsert(batch []apiRequest) error { +func batchInsert(requests chan apiRequest) error { + var reqs []apiRequest + batchSize := len(requestChan) + + for i := 0; i < batchSize; i++ { + reqs = append(reqs, <-requestChan) + } + err := store.Update(func(tx *bolt.Tx) error { b, err := tx.CreateBucketIfNotExists([]byte("requests")) if err != nil { return err } - for _, apiReq := range batch { + for _, apiReq := range reqs { // get the next available ID and convert to string // also set effectedID to int of ID id, err := b.NextSequence() @@ -45,3 +53,41 @@ func batchInsert(batch []apiRequest) error { return nil } + +// batchPrune takes a duration to evaluate apiRequest dates against. If any of +// the apiRequest timestamps are before the threshold, they are removed. +// TODO: add feature to alternatively backup old analytics to cloud +func batchPrune(threshold time.Duration) error { + now := time.Now() + today := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.UTC) + max := today.Add(threshold) + + // iterate through all request data + err := store.Update(func(tx *bolt.Tx) error { + b := tx.Bucket([]byte("requests")) + + b.ForEach(func(k, v []byte) error { + var r apiRequest + err := json.Unmarshal(v, &r) + if err != nil { + return err + } + + // delete if timestamp is below or equal to max + ts := time.Unix(r.Timestamp, 0) + if ts.Equal(max) || ts.Before(max) { + err := b.Delete(k) + if err != nil { + return err + } + } + + return nil + }) + }) + if err != nil { + return err + } + + return nil +} |