fathom/api/languages.go

48 lines
1.1 KiB
Go
Raw Normal View History

package api
import (
"net/http"
2016-12-08 08:16:43 +00:00
"github.com/dannyvankooten/ana/db"
"github.com/dannyvankooten/ana/count"
"encoding/json"
)
// URL: /api/languages
var GetLanguagesHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
before, after := getRequestedPeriods(r)
// get total
total := count.TotalVisitors(before, after)
stmt, err := db.Conn.Prepare(`
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
2016-11-25 12:38:20 +00:00
ORDER BY count DESC
LIMIT ?`)
checkError(err)
defer stmt.Close()
rows, err := stmt.Query(before, after, defaultLimit)
checkError(err)
defer rows.Close()
results := make([]Datapoint, 0)
for rows.Next() {
var d Datapoint
err = rows.Scan(&d.Label, &d.Count);
checkError(err)
d.Percentage = float64(d.Count) / total * 100
results = append(results, d)
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(results)
})