Add `count.RealtimeVisitors` func.

This commit is contained in:
Danny van Kooten 2016-12-24 15:12:01 +02:00
parent 7dc7dee2f4
commit 68a7a0caa9
2 changed files with 12 additions and 8 deletions

View File

@ -2,9 +2,9 @@ package api
import (
"encoding/json"
"github.com/dannyvankooten/ana/count"
"github.com/dannyvankooten/ana/db"
"net/http"
"github.com/dannyvankooten/ana/count"
)
// URL: /api/visitors/count
@ -17,11 +17,7 @@ var GetVisitorsCountHandler = http.HandlerFunc(func(w http.ResponseWriter, r *ht
// URL: /api/visitors/count/realtime
var GetVisitorsRealtimeCountHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var result int
db.Conn.QueryRow(`
SELECT COUNT(DISTINCT(pv.visitor_id))
FROM pageviews pv
WHERE pv.timestamp >= DATE_SUB(CURRENT_TIMESTAMP, INTERVAL 3 HOUR_MINUTE) AND pv.timestamp <= CURRENT_TIMESTAMP`).Scan(&result)
result := count.RealtimeVisitors()
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(result)
})

View File

@ -4,7 +4,15 @@ import (
"github.com/dannyvankooten/ana/db"
)
// TODO: Convert to total_visitors table.
// RealtimeVisitors returns the total number of visitors in the last 3 minutes
func RealtimeVisitors() int {
var result int
db.Conn.QueryRow(`
SELECT COUNT(DISTINCT(pv.visitor_id))
FROM pageviews pv
WHERE pv.timestamp >= DATE_SUB(CURRENT_TIMESTAMP, INTERVAL 3 HOUR_MINUTE) AND pv.timestamp <= CURRENT_TIMESTAMP`).Scan(&result)
return result
}
// Visitors returns the number of total visitors between the given timestamps
func Visitors(before int64, after int64) float64 {