2016-11-21 15:01:14 +00:00
package api
import (
"net/http"
"github.com/dannyvankooten/ana/models"
2016-12-08 08:16:43 +00:00
"github.com/dannyvankooten/ana/db"
2016-11-21 15:01:14 +00:00
"encoding/json"
2016-12-05 20:48:33 +00:00
"github.com/gorilla/mux"
"time"
2016-11-21 15:01:14 +00:00
)
2016-11-21 16:08:24 +00:00
// URL: /api/visits
2016-11-22 21:33:50 +00:00
var GetVisitsHandler = http . HandlerFunc ( func ( w http . ResponseWriter , r * http . Request ) {
2016-12-08 08:16:43 +00:00
stmt , err := db . Conn . Prepare ( ` SELECT
2016-11-21 15:01:14 +00:00
id ,
COALESCE ( browser_name , ' ' ) AS browser_name ,
COALESCE ( browser_language , ' ' ) AS browser_language ,
COALESCE ( device_os , ' ' ) AS device_os ,
ip_address ,
path ,
COALESCE ( screen_resolution , ' ' ) AS screen_resolution ,
timestamp
2016-11-22 16:03:29 +00:00
FROM visits
ORDER BY timestamp DESC
LIMIT ? ` )
2016-11-21 15:01:14 +00:00
2016-11-21 16:08:24 +00:00
checkError ( err )
defer stmt . Close ( )
2016-11-21 15:01:14 +00:00
2016-11-26 14:39:29 +00:00
limit := getRequestedLimit ( r )
2016-11-22 16:03:29 +00:00
rows , err := stmt . Query ( & limit )
2016-11-21 16:08:24 +00:00
checkError ( err )
2016-11-21 15:01:14 +00:00
results := make ( [ ] models . Visit , 0 )
defer rows . Close ( )
for rows . Next ( ) {
var v models . Visit
2016-11-21 16:08:24 +00:00
err = rows . Scan ( & v . ID , & v . BrowserName , & v . BrowserLanguage , & v . DeviceOS , & v . IpAddress , & v . Path , & v . ScreenResolution , & v . Timestamp ) ;
checkError ( err )
2016-11-21 15:01:14 +00:00
results = append ( results , v )
2016-11-21 16:08:24 +00:00
}
err = rows . Err ( ) ;
checkError ( err )
w . Header ( ) . Set ( "Content-Type" , "application/json" )
json . NewEncoder ( w ) . Encode ( results )
2016-11-22 21:33:50 +00:00
} )
2016-11-21 15:01:14 +00:00
2016-11-26 14:39:29 +00:00
// URL: /api/visits/count
var GetVisitsCountHandler = http . HandlerFunc ( func ( w http . ResponseWriter , r * http . Request ) {
2016-11-26 16:19:15 +00:00
before , after := getRequestedPeriods ( r )
2016-12-08 08:16:43 +00:00
stmt , err := db . Conn . Prepare ( ` SELECT COUNT(DISTINCT(ip_address)) FROM visits WHERE UNIX_TIMESTAMP(timestamp) <= ? AND UNIX_TIMESTAMP(timestamp) >= ? ` )
2016-11-26 14:39:29 +00:00
checkError ( err )
defer stmt . Close ( )
var result int
2016-11-26 16:19:15 +00:00
stmt . QueryRow ( before , after ) . Scan ( & result )
2016-11-26 14:39:29 +00:00
w . Header ( ) . Set ( "Content-Type" , "application/json" )
json . NewEncoder ( w ) . Encode ( result )
} )
2016-11-21 16:08:24 +00:00
// URL: /api/visits/count/realtime
2016-11-26 14:39:29 +00:00
var GetVisitsRealtimeCountHandler = http . HandlerFunc ( func ( w http . ResponseWriter , r * http . Request ) {
2016-11-21 16:08:24 +00:00
var result int
2016-12-08 08:16:43 +00:00
db . Conn . QueryRow ( ` SELECT COUNT(DISTINCT(ip_address)) FROM visits WHERE timestamp >= DATE_SUB(CURRENT_TIMESTAMP, INTERVAL 3 HOUR_MINUTE) AND timestamp <= CURRENT_TIMESTAMP ` ) . Scan ( & result )
2016-11-21 16:08:24 +00:00
w . Header ( ) . Set ( "Content-Type" , "application/json" )
json . NewEncoder ( w ) . Encode ( result )
2016-11-22 21:33:50 +00:00
} )
2016-11-22 16:03:29 +00:00
2016-12-05 20:48:33 +00:00
// URL: /api/visits/count/group/:period
var GetVisitsPeriodCountHandler = http . HandlerFunc ( func ( w http . ResponseWriter , r * http . Request ) {
vars := mux . Vars ( r )
period := vars [ "period" ]
formats := map [ string ] string {
"day" : "%Y-%m-%d" ,
"month" : "%Y-%m" ,
}
2016-12-08 08:16:43 +00:00
stmt , err := db . Conn . Prepare ( ` SELECT
2016-12-05 20:48:33 +00:00
COUNT ( DISTINCT ( ip_address ) ) AS count , DATE_FORMAT ( timestamp , ? ) AS date_group
2016-11-22 16:03:29 +00:00
FROM visits
2016-11-26 16:19:15 +00:00
WHERE UNIX_TIMESTAMP ( timestamp ) <= ? AND UNIX_TIMESTAMP ( timestamp ) >= ?
2016-11-22 16:03:29 +00:00
GROUP BY date_group ` )
checkError ( err )
defer stmt . Close ( )
2016-11-26 16:19:15 +00:00
before , after := getRequestedPeriods ( r )
2016-12-05 20:48:33 +00:00
rows , err := stmt . Query ( formats [ period ] , before , after )
2016-11-22 16:03:29 +00:00
checkError ( err )
results := make ( [ ] Datapoint , 0 )
defer rows . Close ( )
for rows . Next ( ) {
v := Datapoint { }
err = rows . Scan ( & v . Count , & v . Label ) ;
checkError ( err )
results = append ( results , v )
}
2016-12-05 20:48:33 +00:00
d := time . Hour * 24 ;
if period == "month" {
d = d * 30
}
results = fillDatapoints ( before , after , d , results )
2016-11-22 16:03:29 +00:00
w . Header ( ) . Set ( "Content-Type" , "application/json" )
json . NewEncoder ( w ) . Encode ( results )
2016-11-22 21:33:50 +00:00
} )