archive pageviews per page

This commit is contained in:
Danny van Kooten 2016-12-11 16:23:13 +01:00
parent 8ca48819d3
commit 6b672c55b4
5 changed files with 119 additions and 77 deletions

View File

@ -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
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) >= ?
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 pageviews DESC
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()

View File

@ -45,7 +45,6 @@ class Pageviews extends Component {
<td class="muted">{i+1}</td>
<td><a href={"//" + p.Hostname + p.Path}>{p.Path.substring(0, 50)}{p.Path.length > 50 ? '..' : ''}</a></td>
<td>{numbers.formatWithComma(p.Count)}</td>
<td>{numbers.formatWithComma(p.CountUnique)}</td>
</tr>
));
@ -59,7 +58,6 @@ class Pageviews extends Component {
<th>#</th>
<th>URL</th>
<th>Pageviews</th>
<th>Unique</th>
</tr>
</thead>
<tbody>{tableRows}</tbody>

View File

@ -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) {

View File

@ -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)
}
}

View File

@ -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")
}