2016-11-21 16:36:25 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"github.com/dannyvankooten/ana/models"
|
|
|
|
"github.com/dannyvankooten/ana/core"
|
|
|
|
"encoding/json"
|
2016-11-23 21:26:47 +00:00
|
|
|
"strconv"
|
2016-11-21 16:36:25 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// URL: /api/pageviews
|
2016-11-22 21:33:50 +00:00
|
|
|
var GetPageviewsHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2016-11-21 16:36:25 +00:00
|
|
|
stmt, err := core.DB.Prepare(`SELECT
|
|
|
|
path,
|
2016-11-22 16:03:29 +00:00
|
|
|
COUNT(ip_address) AS pageviews,
|
|
|
|
COUNT(DISTINCT(ip_address)) AS pageviews_unique
|
2016-11-21 16:36:25 +00:00
|
|
|
FROM visits
|
2016-11-23 20:29:54 +00:00
|
|
|
WHERE timestamp >= DATE_SUB(CURRENT_TIMESTAMP, INTERVAL ? DAY)
|
2016-11-21 16:36:25 +00:00
|
|
|
GROUP BY path`)
|
|
|
|
checkError(err)
|
|
|
|
defer stmt.Close()
|
|
|
|
|
2016-11-23 20:29:54 +00:00
|
|
|
period := r.URL.Query().Get("period")
|
|
|
|
if period == "" {
|
|
|
|
period = "1"
|
|
|
|
}
|
|
|
|
|
|
|
|
rows, err := stmt.Query(period)
|
2016-11-21 16:36:25 +00:00
|
|
|
checkError(err)
|
|
|
|
|
|
|
|
results := make([]models.Pageview, 0)
|
|
|
|
defer rows.Close()
|
|
|
|
for rows.Next() {
|
|
|
|
var p models.Pageview
|
2016-11-22 16:03:29 +00:00
|
|
|
err = rows.Scan(&p.Path, &p.Count, &p.CountUnique);
|
2016-11-21 16:36:25 +00:00
|
|
|
checkError(err)
|
|
|
|
results = append(results, p)
|
|
|
|
}
|
|
|
|
|
|
|
|
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-23 19:30:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
// URL: /api/pageviews/count/day
|
|
|
|
var GetPageviewsDayCountHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
stmt, err := core.DB.Prepare(`SELECT
|
|
|
|
COUNT(*) AS count, DATE_FORMAT(timestamp, '%Y-%m-%d') AS date_group
|
|
|
|
FROM visits
|
2016-11-23 20:29:54 +00:00
|
|
|
WHERE timestamp >= DATE_SUB(CURRENT_TIMESTAMP, INTERVAL ? DAY)
|
2016-11-23 19:30:09 +00:00
|
|
|
GROUP BY date_group`)
|
|
|
|
checkError(err)
|
|
|
|
defer stmt.Close()
|
|
|
|
|
2016-11-23 21:26:47 +00:00
|
|
|
period, err := strconv.Atoi(r.URL.Query().Get("period"))
|
|
|
|
if err != nil || period == 0 {
|
|
|
|
period = 7
|
2016-11-23 20:29:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
rows, err := stmt.Query(period)
|
2016-11-23 19:30:09 +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-11-23 21:26:47 +00:00
|
|
|
results = fillDatapoints(period, results)
|
|
|
|
|
2016-11-23 19:30:09 +00:00
|
|
|
err = rows.Err();
|
|
|
|
checkError(err)
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
json.NewEncoder(w).Encode(results)
|
|
|
|
})
|