Add archive logic for browser names

This commit is contained in:
Danny van Kooten 2016-12-23 17:52:08 +02:00
parent 4dd16daa98
commit 0156385be1
3 changed files with 66 additions and 12 deletions

View File

@ -2,8 +2,9 @@ package api
import ( import (
"encoding/json" "encoding/json"
"github.com/dannyvankooten/ana/count"
"net/http" "net/http"
"github.com/dannyvankooten/ana/count"
) )
// URL: /api/browsers // URL: /api/browsers
@ -14,16 +15,7 @@ var GetBrowsersHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Re
total := count.Visitors(before, after) total := count.Visitors(before, after)
// get rows // get rows
results := count.Custom(` results := count.Browsers(before, after, getRequestedLimit(r), total)
SELECT
v.browser_name,
COUNT(DISTINCT(pv.visitor_id)) AS count
FROM pageviews pv
LEFT JOIN visitors v ON v.id = pv.visitor_id
WHERE UNIX_TIMESTAMP(pv.timestamp) <= ? AND UNIX_TIMESTAMP(pv.timestamp) >= ? AND v.browser_name IS NOT NULL
GROUP BY v.browser_name
ORDER BY count DESC
LIMIT ?`, before, after, getRequestedLimit(r), total)
w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(results) json.NewEncoder(w).Encode(results)

60
count/browsers.go Normal file
View File

@ -0,0 +1,60 @@
package count
import (
"github.com/dannyvankooten/ana/db"
)
// Browsers returns a point slice containing browser data per browser name
func Browsers(before int64, after int64, limit int, total float64) []Point {
// TODO: Calculate total instead of requiring it as a parameter.
stmt, err := db.Conn.Prepare(`
SELECT
a.value,
SUM(a.count) AS count
FROM archive a
WHERE a.metric = 'browsers' AND UNIX_TIMESTAMP(a.date) <= ? AND UNIX_TIMESTAMP(a.date) >= ?
GROUP BY a.value
ORDER BY count DESC
LIMIT ?`)
checkError(err)
defer stmt.Close()
rows, err := stmt.Query(before, after, limit)
checkError(err)
return newPointSlice(rows, total)
}
// CreateBrowserArchives aggregates screen data into daily totals
func CreateBrowserArchives() {
stmt, err := db.Conn.Prepare(`
SELECT
v.browser_name,
COUNT(DISTINCT(pv.visitor_id)) AS count,
DATE_FORMAT(pv.timestamp, "%Y-%m-%d") AS date_group
FROM pageviews pv
LEFT JOIN visitors v ON v.id = pv.visitor_id
WHERE NOT EXISTS(
SELECT a.id
FROM archive a
WHERE a.metric = 'browsers' AND a.date = DATE_FORMAT(pv.timestamp, "%Y-%m-%d")
)
GROUP BY date_group, v.browser_name`)
checkError(err)
defer stmt.Close()
rows, err := stmt.Query()
checkError(err)
defer rows.Close()
db.Conn.Exec("START TRANSACTION")
for rows.Next() {
a := Archive{
Metric: "browsers",
}
err = rows.Scan(&a.Value, &a.Count, &a.Date)
checkError(err)
a.Save(db.Conn)
}
db.Conn.Exec("COMMIT")
}

View File

@ -55,6 +55,7 @@ func CreateArchives() {
CreatePageviewArchivesPerPage() CreatePageviewArchivesPerPage()
CreateScreenArchives() CreateScreenArchives()
CreateLanguageArchives() CreateLanguageArchives()
CreateBrowserArchives()
} }
func checkError(err error) { func checkError(err error) {
@ -78,7 +79,8 @@ func Custom(sql string, before int64, after int64, limit int, total float64) []P
} }
func newPointSlice(rows *sql.Rows, total float64) []Point { func newPointSlice(rows *sql.Rows, total float64) []Point {
var results []Point results := make([]Point, 0)
for rows.Next() { for rows.Next() {
var d Point var d Point
err := rows.Scan(&d.Label, &d.Value) err := rows.Scan(&d.Label, &d.Value)