From 00f8d2cbba4852de7cd29262accc5bd99f65e6cb Mon Sep 17 00:00:00 2001 From: Danny van Kooten Date: Sun, 11 Dec 2016 11:57:51 +0100 Subject: [PATCH] Use count package in /visitors/count and /pageviews/count endpoints. --- api/browsers.go | 2 +- api/countries.go | 2 +- api/languages.go | 2 +- api/pageviews.go | 13 ++----------- api/referrers.go | 2 +- api/screen-resolutions.go | 2 +- api/visitors.go | 12 ++---------- count/pageviews.go | 19 +++++++++++++++++++ count/{totals.go => visitors.go} | 2 +- 9 files changed, 29 insertions(+), 27 deletions(-) create mode 100644 count/pageviews.go rename count/{totals.go => visitors.go} (87%) diff --git a/api/browsers.go b/api/browsers.go index f5f0a42..d8fabf2 100644 --- a/api/browsers.go +++ b/api/browsers.go @@ -12,7 +12,7 @@ var GetBrowsersHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Re before, after := getRequestedPeriods(r) // get total - total := count.TotalVisitors(before, after) + total := count.Visitors(before, after) // get rows stmt, err := db.Conn.Prepare(` diff --git a/api/countries.go b/api/countries.go index 93eebcc..ed5a657 100644 --- a/api/countries.go +++ b/api/countries.go @@ -12,7 +12,7 @@ var GetCountriesHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.R before, after := getRequestedPeriods(r) // get total - total := count.TotalVisitors(before, after) + total := count.Visitors(before, after) // get rows stmt, err := db.Conn.Prepare(` diff --git a/api/languages.go b/api/languages.go index 1f44d30..1f6fd8c 100644 --- a/api/languages.go +++ b/api/languages.go @@ -12,7 +12,7 @@ var GetLanguagesHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.R before, after := getRequestedPeriods(r) // get total - total := count.TotalVisitors(before, after) + total := count.Visitors(before, after) stmt, err := db.Conn.Prepare(` SELECT diff --git a/api/pageviews.go b/api/pageviews.go index ccf5618..b4cd5d6 100644 --- a/api/pageviews.go +++ b/api/pageviews.go @@ -4,6 +4,7 @@ import ( "net/http" "github.com/dannyvankooten/ana/models" "github.com/dannyvankooten/ana/db" + "github.com/dannyvankooten/ana/count" "encoding/json" "github.com/gorilla/mux" "time" @@ -49,17 +50,7 @@ var GetPageviewsHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.R // URL: /api/pageviews/count var GetPageviewsCountHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { before, after := getRequestedPeriods(r) - stmt, err := db.Conn.Prepare(` - SELECT - SUM(a.count) AS count - FROM archive a - WHERE a.metric = 'pageviews' AND UNIX_TIMESTAMP(a.date) <= ? AND UNIX_TIMESTAMP(a.date) >= ?`) - checkError(err) - defer stmt.Close() - - var result int - stmt.QueryRow(before, after).Scan(&result) - + result := count.Pageviews(before, after) w.Header().Set("Content-Type", "application/json") json.NewEncoder(w).Encode(result) }) diff --git a/api/referrers.go b/api/referrers.go index 3befca0..ebde874 100644 --- a/api/referrers.go +++ b/api/referrers.go @@ -13,7 +13,7 @@ var GetReferrersHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.R before, after := getRequestedPeriods(r) // get total - total := count.TotalVisitors(before, after) + total := count.Visitors(before, after) // get rows stmt, err := db.Conn.Prepare(` diff --git a/api/screen-resolutions.go b/api/screen-resolutions.go index 47fed73..afef3c1 100644 --- a/api/screen-resolutions.go +++ b/api/screen-resolutions.go @@ -12,7 +12,7 @@ var GetScreenResolutionsHandler = http.HandlerFunc(func(w http.ResponseWriter, r before, after := getRequestedPeriods(r) // get total - total := count.TotalVisitors(before, after) + total := count.Visitors(before, after) // get rows stmt, err := db.Conn.Prepare(` diff --git a/api/visitors.go b/api/visitors.go index 95a602d..95f5e04 100644 --- a/api/visitors.go +++ b/api/visitors.go @@ -3,6 +3,7 @@ package api import ( "net/http" "github.com/dannyvankooten/ana/db" + "github.com/dannyvankooten/ana/count" "encoding/json" "github.com/gorilla/mux" "time" @@ -11,16 +12,7 @@ import ( // URL: /api/visitors/count var GetVisitorsCountHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { before, after := getRequestedPeriods(r) - stmt, err := db.Conn.Prepare(` - SELECT - SUM(a.count) - FROM archive a - WHERE a.metric = 'visitors' AND UNIX_TIMESTAMP(a.date) <= ? AND UNIX_TIMESTAMP(a.date) >= ?`) - checkError(err) - defer stmt.Close() - - var result int - stmt.QueryRow(before, after).Scan(&result) + result := count.Visitors(before, after) w.Header().Set("Content-Type", "application/json") json.NewEncoder(w).Encode(result) }) diff --git a/count/pageviews.go b/count/pageviews.go new file mode 100644 index 0000000..e29a4fb --- /dev/null +++ b/count/pageviews.go @@ -0,0 +1,19 @@ +package count + +import( + "github.com/dannyvankooten/ana/db" +) + +func Pageviews(before int64, after int64) float64 { + // get total + stmt, err := db.Conn.Prepare(` + SELECT + SUM(a.count) + FROM archive a + WHERE a.metric = 'pageviews' AND UNIX_TIMESTAMP(a.date) <= ? AND UNIX_TIMESTAMP(a.date) >= ?`) + checkError(err) + defer stmt.Close() + var total float64 + stmt.QueryRow(before, after).Scan(&total) + return total +} diff --git a/count/totals.go b/count/visitors.go similarity index 87% rename from count/totals.go rename to count/visitors.go index adc20b5..2e6ecd2 100644 --- a/count/totals.go +++ b/count/visitors.go @@ -4,7 +4,7 @@ import( "github.com/dannyvankooten/ana/db" ) -func TotalVisitors(before int64, after int64) float64 { +func Visitors(before int64, after int64) float64 { // get total stmt, err := db.Conn.Prepare(` SELECT