2016-11-24 12:55:59 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2016-12-11 13:50:01 +00:00
|
|
|
"encoding/json"
|
|
|
|
"github.com/dannyvankooten/ana/count"
|
|
|
|
"net/http"
|
2016-11-24 12:55:59 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// URL: /api/languages
|
|
|
|
var GetLanguagesHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2016-12-11 13:50:01 +00:00
|
|
|
before, after := getRequestedPeriods(r)
|
2016-11-24 13:18:40 +00:00
|
|
|
|
2016-12-11 13:50:01 +00:00
|
|
|
// get total
|
|
|
|
total := count.Visitors(before, after)
|
2016-11-24 13:18:40 +00:00
|
|
|
|
2016-12-11 13:50:01 +00:00
|
|
|
results := count.Custom(`
|
2016-11-24 12:55:59 +00:00
|
|
|
SELECT
|
2016-12-10 13:16:05 +00:00
|
|
|
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
|
2016-12-11 11:52:10 +00:00
|
|
|
LIMIT ?`, before, after, getRequestedLimit(r), total,
|
2016-12-11 13:50:01 +00:00
|
|
|
)
|
2016-11-24 12:55:59 +00:00
|
|
|
|
2016-12-11 13:50:01 +00:00
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
json.NewEncoder(w).Encode(results)
|
2016-11-24 12:55:59 +00:00
|
|
|
})
|