diff options
author | Steve Manuel <nilslice@gmail.com> | 2016-11-03 17:07:42 -0700 |
---|---|---|
committer | Steve Manuel <nilslice@gmail.com> | 2016-11-03 17:07:42 -0700 |
commit | 06859257193861b932e14b519f05638f4d737bad (patch) | |
tree | 2a13c48eee0915da516e07a968183b7bcffada63 | |
parent | a89e0300860bb25298aef37a79f18e1a5b9e63fc (diff) |
adding initial implementation with fake data of analytics chart
-rw-r--r-- | system/admin/admin.go | 51 | ||||
-rw-r--r-- | system/admin/handlers.go | 2 | ||||
-rw-r--r-- | system/api/analytics/init.go | 16 |
3 files changed, 65 insertions, 4 deletions
diff --git a/system/admin/admin.go b/system/admin/admin.go index 9ee86c3..a90eb96 100644 --- a/system/admin/admin.go +++ b/system/admin/admin.go @@ -20,6 +20,7 @@ var startAdminHTML = `<!doctype html> <script type="text/javascript" src="/admin/static/common/js/jquery-2.1.4.min.js"></script> <script type="text/javascript" src="/admin/static/common/js/util.js"></script> <script type="text/javascript" src="/admin/static/dashboard/js/materialize.min.js"></script> + <script type="text/javascript" src="/admin/static/dashboard/js/chart.bundle.min.js"></script> <script type="text/javascript" src="/admin/static/editor/js/materialNote.js"></script> <script type="text/javascript" src="/admin/static/editor/js/ckMaterializeOverrides.js"></script> @@ -367,6 +368,56 @@ func UsersList(req *http.Request) ([]byte, error) { return view, nil } +var analyticsHTML = ` +<div class="init col s5"> +<div class="card"> +<div class="card-content"> + <div class="card-title">API Requests</div> + <canvas id="analytics-chart" width="100%" height="100%"></canvas> + <script> + var ctx = document.getElementById("analytics-chart"); + var myChart = new Chart(ctx, { + type: 'bar', + data: { + labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"], + datasets: [{ + label: '# of Votes', + data: [12, 19, 3, 5, 2, 3], + backgroundColor: [ + 'rgba(255, 99, 132, 0.2)', + 'rgba(54, 162, 235, 0.2)', + 'rgba(255, 206, 86, 0.2)', + 'rgba(75, 192, 192, 0.2)', + 'rgba(153, 102, 255, 0.2)', + 'rgba(255, 159, 64, 0.2)' + ], + borderColor: [ + 'rgba(255,99,132,1)', + 'rgba(54, 162, 235, 1)', + 'rgba(255, 206, 86, 1)', + 'rgba(75, 192, 192, 1)', + 'rgba(153, 102, 255, 1)', + 'rgba(255, 159, 64, 1)' + ], + borderWidth: 1 + }] + }, + options: { + scales: { + yAxes: [{ + ticks: { + beginAtZero:true + } + }] + } + } + }); + </script> +</div> +</div> +</div> +` + var err400HTML = ` <div class="error-page e400 col s6"> <div class="card"> diff --git a/system/admin/handlers.go b/system/admin/handlers.go index 64f10ed..1291bbb 100644 --- a/system/admin/handlers.go +++ b/system/admin/handlers.go @@ -25,7 +25,7 @@ import ( ) func adminHandler(res http.ResponseWriter, req *http.Request) { - view, err := Admin(nil) + view, err := Admin([]byte(analyticsHTML)) if err != nil { log.Println(err) res.WriteHeader(http.StatusInternalServerError) diff --git a/system/api/analytics/init.go b/system/api/analytics/init.go index 3af1407..79cace0 100644 --- a/system/api/analytics/init.go +++ b/system/api/analytics/init.go @@ -9,6 +9,8 @@ import ( "strings" "time" + "runtime" + "github.com/boltdb/bolt" ) @@ -16,6 +18,7 @@ type apiRequest struct { URL string `json:"url"` Method string `json:"http_method"` Origin string `json:"origin"` + Proto string `json:"http_protocol"` RemoteAddr string `json:"ip_address"` Timestamp int64 `json:"timestamp"` External bool `json:"external"` @@ -34,6 +37,7 @@ func Record(req *http.Request) { URL: req.URL.String(), Method: req.Method, Origin: req.Header.Get("Origin"), + Proto: req.Proto, RemoteAddr: req.RemoteAddr, Timestamp: time.Now().Unix() * 1000, External: external, @@ -61,7 +65,7 @@ func Init() { log.Fatalln(err) } - recordChan = make(chan apiRequest, 1024*128) + recordChan = make(chan apiRequest, 1024*64*runtime.NumCPU()) go serve() @@ -72,8 +76,8 @@ func Init() { func serve() { // make timer to notify select to batch request insert from recordChan - // interval: 1 minute - apiRequestTimer := time.NewTicker(time.Minute * 1) + // interval: 30 seconds + apiRequestTimer := time.NewTicker(time.Second * 30) // make timer to notify select to remove old analytics // interval: 2 weeks @@ -101,3 +105,9 @@ func serve() { } } } + +// Week returns the []byte of javascript needed to chart a week of data by day +func Week() { + // get request analytics for today and the 6 days preceeding + +} |