diff --git a/api/pageviews.go b/api/pageviews.go index 34a8ec7..2770f95 100644 --- a/api/pageviews.go +++ b/api/pageviews.go @@ -11,17 +11,31 @@ import ( // URL: /api/pageviews var GetPageviewsHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { before, after := getRequestedPeriods(r) - stmt, err := db.Conn.Prepare(`SELECT - p.hostname, - p.path, - COUNT(*) AS pageviews, - COUNT(DISTINCT(pv.visitor_id)) AS pageviews_unique - FROM pageviews pv - LEFT JOIN pages p ON pv.page_id = p.id - WHERE UNIX_TIMESTAMP(pv.timestamp) <= ? AND UNIX_TIMESTAMP(pv.timestamp) >= ? - GROUP BY p.path, p.hostname - ORDER BY pageviews DESC - LIMIT ?`) + + stmt, err := db.Conn.Prepare(` + SELECT + p.hostname, + p.path, + SUM(a.count) AS count, + "0" AS count_unique + FROM archive a + LEFT JOIN pages p ON p.id = a.value + WHERE metric = 'pageviews.page' AND UNIX_TIMESTAMP(a.date) <= ? AND UNIX_TIMESTAMP(a.date) >= ? + GROUP BY p.path, p.hostname + ORDER BY count DESC + LIMIT ?`) + + // stmt, err := db.Conn.Prepare(`SELECT + // p.hostname, + // p.path, + // COUNT(*) AS pageviews, + // COUNT(DISTINCT(pv.visitor_id)) AS pageviews_unique + // FROM pageviews pv + // LEFT JOIN pages p ON pv.page_id = p.id + // WHERE UNIX_TIMESTAMP(pv.timestamp) <= ? AND UNIX_TIMESTAMP(pv.timestamp) >= ? + // GROUP BY p.path, p.hostname + // ORDER BY pageviews DESC + // LIMIT ?`) checkError(err) defer stmt.Close() diff --git a/assets/js/components/Pageviews.js b/assets/js/components/Pageviews.js index 42fa025..54609ec 100644 --- a/assets/js/components/Pageviews.js +++ b/assets/js/components/Pageviews.js @@ -45,7 +45,6 @@ class Pageviews extends Component { {i+1} {p.Path.substring(0, 50)}{p.Path.length > 50 ? '..' : ''} {numbers.formatWithComma(p.Count)} - {numbers.formatWithComma(p.CountUnique)} )); @@ -59,7 +58,6 @@ class Pageviews extends Component { # URL Pageviews - Unique {tableRows} diff --git a/count/count.go b/count/count.go index d6699a5..5de5d9d 100644 --- a/count/count.go +++ b/count/count.go @@ -47,70 +47,7 @@ func (a *Archive) Save(Conn *sql.DB) error { func CreateArchives() { CreatePageviewArchives() CreateVisitorArchives() -} - -func CreatePageviewArchives() { - stmt, err := db.Conn.Prepare(` - SELECT - COUNT(*) AS count, - DATE_FORMAT(pv.timestamp, "%Y-%m-%d") AS date_group - FROM pageviews pv - WHERE NOT EXISTS( - SELECT a.id - FROM archive a - WHERE a.metric = 'pageviews' AND a.date = DATE_FORMAT(pv.timestamp, "%Y-%m-%d") - ) - GROUP BY date_group`) - 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: "pageviews", - Value: "", - } - err = rows.Scan(&a.Count, &a.Date) - checkError(err) - a.Save(db.Conn) - } - db.Conn.Exec("COMMIT") -} - -func CreateVisitorArchives() { - stmt, err := db.Conn.Prepare(` - SELECT - COUNT(DISTINCT(pv.visitor_id)) AS count, - DATE_FORMAT(pv.timestamp, "%Y-%m-%d") AS date_group - FROM pageviews pv - WHERE NOT EXISTS( - SELECT a.id - FROM archive a - WHERE a.metric = 'visitors' AND a.date = DATE_FORMAT(pv.timestamp, "%Y-%m-%d") - ) - GROUP BY date_group`) - 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: "visitors", - Value: "", - } - err = rows.Scan(&a.Count, &a.Date) - checkError(err) - a.Save(db.Conn) - } - db.Conn.Exec("COMMIT") + CreatePageviewArchivesPerPage() } func checkError(err error) { diff --git a/count/pageviews.go b/count/pageviews.go index 62ce2cb..acc53fc 100644 --- a/count/pageviews.go +++ b/count/pageviews.go @@ -44,3 +44,64 @@ func PageviewsPerDay(before int64, after int64) []Point { results = fill(after, before, results) return results } + +func CreatePageviewArchives() { + stmt, err := db.Conn.Prepare(` + SELECT + COUNT(*) AS count, + DATE_FORMAT(pv.timestamp, "%Y-%m-%d") AS date_group + FROM pageviews pv + WHERE NOT EXISTS( + SELECT a.id + FROM archive a + WHERE a.metric = 'pageviews' AND a.date = DATE_FORMAT(pv.timestamp, "%Y-%m-%d") + ) + GROUP BY date_group`) + 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: "pageviews", + Value: "", + } + err = rows.Scan(&a.Count, &a.Date) + checkError(err) + a.Save(db.Conn) + } + db.Conn.Exec("COMMIT") +} + +func CreatePageviewArchivesPerPage() { + stmt, err := db.Conn.Prepare(`SELECT + pv.page_id, + COUNT(*) AS count, + DATE_FORMAT(pv.timestamp, "%Y-%m-%d") AS date_group + FROM pageviews pv + WHERE NOT EXISTS ( + SELECT a.id + FROM archive a + WHERE a.metric = 'pageviews.page' AND a.date = DATE_FORMAT(pv.timestamp, "%Y-%m-%d") AND a.value = pv.page_id + ) + GROUP BY pv.page_id, date_group`) + checkError(err) + defer stmt.Close() + + rows, err := stmt.Query() + checkError(err) + defer rows.Close() + + for rows.Next() { + a := Archive{ + Metric: "pageviews.page", + } + err = rows.Scan(&a.Value, &a.Count, &a.Date) + checkError(err) + a.Save(db.Conn) + } +} diff --git a/count/visitors.go b/count/visitors.go index 87f5be2..590b868 100644 --- a/count/visitors.go +++ b/count/visitors.go @@ -44,3 +44,35 @@ func VisitorsPerDay(before int64, after int64) []Point { return results } + +func CreateVisitorArchives() { + stmt, err := db.Conn.Prepare(` + SELECT + COUNT(DISTINCT(pv.visitor_id)) AS count, + DATE_FORMAT(pv.timestamp, "%Y-%m-%d") AS date_group + FROM pageviews pv + WHERE NOT EXISTS( + SELECT a.id + FROM archive a + WHERE a.metric = 'visitors' AND a.date = DATE_FORMAT(pv.timestamp, "%Y-%m-%d") + ) + GROUP BY date_group`) + 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: "visitors", + Value: "", + } + err = rows.Scan(&a.Count, &a.Date) + checkError(err) + a.Save(db.Conn) + } + db.Conn.Exec("COMMIT") +}