Use count package in /visitors/count and /pageviews/count endpoints.

This commit is contained in:
Danny van Kooten 2016-12-11 11:57:51 +01:00
parent 2f110fc059
commit 00f8d2cbba
9 changed files with 29 additions and 27 deletions

View File

@ -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(`

View File

@ -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(`

View File

@ -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

View File

@ -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)
})

View File

@ -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(`

View File

@ -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(`

View File

@ -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)
})

19
count/pageviews.go Normal file
View File

@ -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
}

View File

@ -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