Add archive logic for browser languages

This commit is contained in:
Danny van Kooten 2016-12-23 17:39:55 +02:00
parent 04a8f09e5e
commit 4dd16daa98
3 changed files with 64 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/languages // URL: /api/languages
@ -13,17 +14,7 @@ var GetLanguagesHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.R
// get total // get total
total := count.Visitors(before, after) total := count.Visitors(before, after)
results := count.Custom(` results := count.Languages(before, after, getRequestedLimit(r), total)
SELECT
v.browser_language,
COUNT(v.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) >= ?
GROUP BY v.browser_language
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)

View File

@ -54,6 +54,7 @@ func CreateArchives() {
CreateVisitorArchives() CreateVisitorArchives()
CreatePageviewArchivesPerPage() CreatePageviewArchivesPerPage()
CreateScreenArchives() CreateScreenArchives()
CreateLanguageArchives()
} }
func checkError(err error) { func checkError(err error) {

60
count/languages.go Normal file
View File

@ -0,0 +1,60 @@
package count
import (
"github.com/dannyvankooten/ana/db"
)
// Languages returns a point slice containing language data per language
func Languages(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 = 'languages' 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)
}
// CreateLanguageArchives aggregates screen data into daily totals
func CreateLanguageArchives() {
stmt, err := db.Conn.Prepare(`
SELECT
v.browser_language,
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 = 'languages' AND a.date = DATE_FORMAT(pv.timestamp, "%Y-%m-%d")
)
GROUP BY date_group, v.browser_language`)
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: "languages",
}
err = rows.Scan(&a.Value, &a.Count, &a.Date)
checkError(err)
a.Save(db.Conn)
}
db.Conn.Exec("COMMIT")
}