diff options
author | Steve Manuel <nilslice@gmail.com> | 2016-12-12 21:09:32 -0800 |
---|---|---|
committer | Steve Manuel <nilslice@gmail.com> | 2016-12-12 21:09:32 -0800 |
commit | 0c1c118ae3e42c92cf9d47e41903a8c256019935 (patch) | |
tree | 8cd94e57f5845ec867d679605536fb6f7b0a730f /system/api/analytics/init.go | |
parent | f902c05ae661bad0745e8869718af4cdcc015e17 (diff) |
inserting metrics data into cache
Diffstat (limited to 'system/api/analytics/init.go')
-rw-r--r-- | system/api/analytics/init.go | 36 |
1 files changed, 29 insertions, 7 deletions
diff --git a/system/api/analytics/init.go b/system/api/analytics/init.go index 95ad8f4..1bd5507 100644 --- a/system/api/analytics/init.go +++ b/system/api/analytics/init.go @@ -158,7 +158,7 @@ func ChartData() (map[string]interface{}, error) { var requests = []apiRequest{} var metrics = [14]apiMetric{} - err := store.View(func(tx *bolt.Tx) error { + err := store.Update(func(tx *bolt.Tx) error { m := tx.Bucket([]byte("__metrics")) b := tx.Bucket([]byte("__requests")) @@ -270,14 +270,36 @@ CHECK_REQUEST: // loop through total and unique to see which dates are accounted for and // insert data from metrics array where dates are not - for i := range metrics { - if total[i] == 0 { - total[i] = metrics[i].Total - } + err = store.Update(func(tx *bolt.Tx) error { + b := tx.Bucket([]byte("__metrics")) + + for i := range metrics { + if total[i] == 0 { + total[i] = metrics[i].Total + } + + if unique[i] == 0 { + unique[i] = metrics[i].Unique + } + + k := metrics[i].Date + if b.Get([]byte(k)) == nil { + v, err := json.Marshal(metrics[i]) + if err != nil { + return err + } - if unique[i] == 0 { - unique[i] = metrics[i].Unique + err = b.Put([]byte(k), v) + if err != nil { + return err + } + } } + + return nil + }) + if err != nil { + return nil, err } // marshal array counts to js arrays for output to chart |