diff --git a/api/screen-resolutions.go b/api/screens.go similarity index 53% rename from api/screen-resolutions.go rename to api/screens.go index 893adcc..2fda3cf 100644 --- a/api/screen-resolutions.go +++ b/api/screens.go @@ -2,8 +2,10 @@ package api import ( "encoding/json" - "github.com/dannyvankooten/ana/count" + "fmt" "net/http" + + "github.com/dannyvankooten/ana/count" ) // URL: /api/screen-resolutions @@ -13,17 +15,10 @@ var GetScreenResolutionsHandler = http.HandlerFunc(func(w http.ResponseWriter, r // get total total := count.Visitors(before, after) + fmt.Printf("Total # of visitors: %d\n", total) + // get rows - results := count.Custom(` - 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) + results := count.Screens(before, after, getRequestedLimit(r), total) w.Header().Set("Content-Type", "application/json") json.NewEncoder(w).Encode(results) diff --git a/count/count.go b/count/count.go index fabf942..dbcfb62 100644 --- a/count/count.go +++ b/count/count.go @@ -53,6 +53,7 @@ func CreateArchives() { CreatePageviewArchives() CreateVisitorArchives() CreatePageviewArchivesPerPage() + CreateScreenArchives() } func checkError(err error) { diff --git a/count/screens.go b/count/screens.go new file mode 100644 index 0000000..3aa0939 --- /dev/null +++ b/count/screens.go @@ -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") +}