mirror of
https://github.com/status-im/fathom.git
synced 2025-03-01 03:20:27 +00:00
Add archive logic for screen sizes
This commit is contained in:
parent
7ac4569b7a
commit
e7cca8ee72
@ -2,8 +2,10 @@ package api
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"github.com/dannyvankooten/ana/count"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/dannyvankooten/ana/count"
|
||||||
)
|
)
|
||||||
|
|
||||||
// URL: /api/screen-resolutions
|
// URL: /api/screen-resolutions
|
||||||
@ -13,17 +15,10 @@ var GetScreenResolutionsHandler = http.HandlerFunc(func(w http.ResponseWriter, r
|
|||||||
// get total
|
// get total
|
||||||
total := count.Visitors(before, after)
|
total := count.Visitors(before, after)
|
||||||
|
|
||||||
|
fmt.Printf("Total # of visitors: %d\n", total)
|
||||||
|
|
||||||
// get rows
|
// get rows
|
||||||
results := count.Custom(`
|
results := count.Screens(before, after, getRequestedLimit(r), total)
|
||||||
SELECT
|
|
||||||
v.screen_resolution,
|
|
||||||
COUNT(DISTINCT(pv.visitor_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.screen_resolution
|
|
||||||
ORDER BY count DESC
|
|
||||||
LIMIT ?`, before, after, getRequestedLimit(r), total)
|
|
||||||
|
|
||||||
w.Header().Set("Content-Type", "application/json")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
json.NewEncoder(w).Encode(results)
|
json.NewEncoder(w).Encode(results)
|
@ -53,6 +53,7 @@ func CreateArchives() {
|
|||||||
CreatePageviewArchives()
|
CreatePageviewArchives()
|
||||||
CreateVisitorArchives()
|
CreateVisitorArchives()
|
||||||
CreatePageviewArchivesPerPage()
|
CreatePageviewArchivesPerPage()
|
||||||
|
CreateScreenArchives()
|
||||||
}
|
}
|
||||||
|
|
||||||
func checkError(err error) {
|
func checkError(err error) {
|
||||||
|
60
count/screens.go
Normal file
60
count/screens.go
Normal file
@ -0,0 +1,60 @@
|
|||||||
|
package count
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/dannyvankooten/ana/db"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Screens returns a point slice containing screen data per size
|
||||||
|
func Screens(before int64, after int64, limit int, total float64) []Point {
|
||||||
|
// TODO: Calculate total instead of requiring it as a parameter.
|
||||||
|
stmt, err := db.Conn.Prepare(`
|
||||||
|
SELECT
|
||||||
|
a.value,
|
||||||
|
SUM(a.count) AS count
|
||||||
|
FROM archive a
|
||||||
|
WHERE a.metric = 'screens' AND UNIX_TIMESTAMP(a.date) <= ? AND UNIX_TIMESTAMP(a.date) >= ?
|
||||||
|
GROUP BY a.value
|
||||||
|
ORDER BY count DESC
|
||||||
|
LIMIT ?`)
|
||||||
|
checkError(err)
|
||||||
|
defer stmt.Close()
|
||||||
|
|
||||||
|
rows, err := stmt.Query(before, after, limit)
|
||||||
|
checkError(err)
|
||||||
|
|
||||||
|
return newPointSlice(rows, total)
|
||||||
|
}
|
||||||
|
|
||||||
|
// CreateScreenArchives aggregates screen data into daily totals
|
||||||
|
func CreateScreenArchives() {
|
||||||
|
stmt, err := db.Conn.Prepare(`
|
||||||
|
SELECT
|
||||||
|
v.screen_resolution,
|
||||||
|
COUNT(DISTINCT(pv.visitor_id)) AS count,
|
||||||
|
DATE_FORMAT(pv.timestamp, "%Y-%m-%d") AS date_group
|
||||||
|
FROM pageviews pv
|
||||||
|
LEFT JOIN visitors v ON v.id = pv.visitor_id
|
||||||
|
WHERE NOT EXISTS(
|
||||||
|
SELECT a.id
|
||||||
|
FROM archive a
|
||||||
|
WHERE a.metric = 'screens' AND a.date = DATE_FORMAT(pv.timestamp, "%Y-%m-%d")
|
||||||
|
)
|
||||||
|
GROUP BY date_group, v.screen_resolution`)
|
||||||
|
checkError(err)
|
||||||
|
defer stmt.Close()
|
||||||
|
|
||||||
|
rows, err := stmt.Query()
|
||||||
|
checkError(err)
|
||||||
|
defer rows.Close()
|
||||||
|
|
||||||
|
db.Conn.Exec("START TRANSACTION")
|
||||||
|
for rows.Next() {
|
||||||
|
a := Archive{
|
||||||
|
Metric: "screens",
|
||||||
|
}
|
||||||
|
err = rows.Scan(&a.Value, &a.Count, &a.Date)
|
||||||
|
checkError(err)
|
||||||
|
a.Save(db.Conn)
|
||||||
|
}
|
||||||
|
db.Conn.Exec("COMMIT")
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user