mirror of https://github.com/status-im/fathom.git
54 lines
1.3 KiB
Go
54 lines
1.3 KiB
Go
package api
|
|
|
|
import (
|
|
"net/http"
|
|
"github.com/dannyvankooten/ana/db"
|
|
"encoding/json"
|
|
)
|
|
|
|
// URL: /api/browsers
|
|
var GetBrowsersHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
before, after := getRequestedPeriods(r)
|
|
|
|
// get total
|
|
stmt, err := db.Conn.Prepare(`
|
|
SELECT
|
|
COUNT(DISTINCT(ip_address))
|
|
FROM visits
|
|
WHERE UNIX_TIMESTAMP(timestamp) <= ? AND UNIX_TIMESTAMP(timestamp) >= ?
|
|
`)
|
|
checkError(err)
|
|
defer stmt.Close()
|
|
var total float32
|
|
stmt.QueryRow(before, after).Scan(&total)
|
|
|
|
// get rows
|
|
stmt, err = db.Conn.Prepare(`
|
|
SELECT
|
|
browser_name,
|
|
COUNT(DISTINCT(ip_address)) AS count
|
|
FROM visits
|
|
WHERE UNIX_TIMESTAMP(timestamp) <= ? AND UNIX_TIMESTAMP(timestamp) >= ? AND browser_name IS NOT NULL
|
|
GROUP BY browser_name
|
|
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 = float32(d.Count) / total * 100
|
|
results = append(results, d)
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
json.NewEncoder(w).Encode(results)
|
|
})
|