diff options
author | Steve Manuel <nilslice@gmail.com> | 2016-11-01 21:36:25 -0700 |
---|---|---|
committer | Steve Manuel <nilslice@gmail.com> | 2016-11-01 21:36:25 -0700 |
commit | 6ee469616d56b069dd68391577f0cd9411abc46a (patch) | |
tree | 2ea2da04b0726706bd5b0aa81891b02f7fa1ac32 /system/api/analytics/batch.go | |
parent | ac647e2f77d05cc015a369832c2d428ec1cd6567 (diff) |
adding analytics tracking to API calls
Diffstat (limited to 'system/api/analytics/batch.go')
-rw-r--r-- | system/api/analytics/batch.go | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/system/api/analytics/batch.go b/system/api/analytics/batch.go new file mode 100644 index 0000000..1fee247 --- /dev/null +++ b/system/api/analytics/batch.go @@ -0,0 +1,47 @@ +package analytics + +import ( + "encoding/json" + "strconv" + + "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 { + err := store.Update(func(tx *bolt.Tx) error { + b, err := tx.CreateBucketIfNotExists([]byte("requests")) + if err != nil { + return err + } + + for _, apiReq := range batch { + // get the next available ID and convert to string + // also set effectedID to int of ID + id, err := b.NextSequence() + if err != nil { + return err + } + cid := strconv.FormatUint(id, 10) + + j, err := json.Marshal(apiReq) + if err != nil { + return err + } + + err = b.Put([]byte(cid), j) + if err != nil { + return err + } + } + + return nil + + }) + if err != nil { + return err + } + + return nil +} |