fathom/pkg/count/screens.go

64 lines
1.6 KiB
Go
Raw Normal View History

2016-12-23 14:49:23 +00:00
package count
import (
"github.com/usefathom/fathom/pkg/datastore"
2016-12-23 14:49:23 +00:00
)
// TotalUniqueScreens returns the total # of screens between two given timestamps
func TotalUniqueScreens(before int64, after int64) int {
var total int
stmt, err := datastore.DB.Prepare(`
SELECT
IFNULL( SUM(t.count_unique), 0 )
FROM total_screens t
WHERE UNIX_TIMESTAMP(t.date) <= ? AND UNIX_TIMESTAMP(t.date) >= ?`)
checkError(err)
defer stmt.Close()
err = stmt.QueryRow(before, after).Scan(&total)
checkError(err)
return total
}
2016-12-23 14:49:23 +00:00
// Screens returns a point slice containing screen data per size
func Screens(before int64, after int64, limit int) []Point {
stmt, err := datastore.DB.Prepare(`
2016-12-23 14:49:23 +00:00
SELECT
t.value,
SUM(t.count_unique) AS count
FROM total_screens t
WHERE UNIX_TIMESTAMP(t.date) <= ? AND UNIX_TIMESTAMP(t.date) >= ?
GROUP BY t.value
2016-12-23 14:49:23 +00:00
ORDER BY count DESC
LIMIT ?`)
checkError(err)
defer stmt.Close()
rows, err := stmt.Query(before, after, limit)
checkError(err)
points := newPointSlice(rows)
total := TotalUniqueScreens(before, after)
points = calculatePointPercentages(points, total)
return points
2016-12-23 14:49:23 +00:00
}
// CreateScreenTotals aggregates screen data into daily totals
func CreateScreenTotals(since string) {
rows := queryTotalRows(`
2016-12-23 14:49:23 +00:00
SELECT
v.screen_resolution,
COUNT(*) AS count,
COUNT(DISTINCT(pv.visitor_id)) AS count_unique,
2016-12-23 14:49:23 +00:00
DATE_FORMAT(pv.timestamp, "%Y-%m-%d") AS date_group
FROM pageviews pv
LEFT JOIN visitors v ON v.id = pv.visitor_id
WHERE pv.timestamp > ?
GROUP BY date_group, v.screen_resolution`, since)
2016-12-23 14:49:23 +00:00
processTotalRows(rows, "total_screens")
2016-12-23 14:49:23 +00:00
}