mirror of https://github.com/status-im/fathom.git
archive pageviews per page
This commit is contained in:
parent
8ca48819d3
commit
6b672c55b4
|
@ -11,17 +11,31 @@ import (
|
||||||
// URL: /api/pageviews
|
// URL: /api/pageviews
|
||||||
var GetPageviewsHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
var GetPageviewsHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
before, after := getRequestedPeriods(r)
|
before, after := getRequestedPeriods(r)
|
||||||
stmt, err := db.Conn.Prepare(`SELECT
|
|
||||||
p.hostname,
|
stmt, err := db.Conn.Prepare(`
|
||||||
p.path,
|
SELECT
|
||||||
COUNT(*) AS pageviews,
|
p.hostname,
|
||||||
COUNT(DISTINCT(pv.visitor_id)) AS pageviews_unique
|
p.path,
|
||||||
FROM pageviews pv
|
SUM(a.count) AS count,
|
||||||
LEFT JOIN pages p ON pv.page_id = p.id
|
"0" AS count_unique
|
||||||
WHERE UNIX_TIMESTAMP(pv.timestamp) <= ? AND UNIX_TIMESTAMP(pv.timestamp) >= ?
|
FROM archive a
|
||||||
GROUP BY p.path, p.hostname
|
LEFT JOIN pages p ON p.id = a.value
|
||||||
ORDER BY pageviews DESC
|
WHERE metric = 'pageviews.page' AND UNIX_TIMESTAMP(a.date) <= ? AND UNIX_TIMESTAMP(a.date) >= ?
|
||||||
LIMIT ?`)
|
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)
|
checkError(err)
|
||||||
defer stmt.Close()
|
defer stmt.Close()
|
||||||
|
|
||||||
|
|
|
@ -45,7 +45,6 @@ class Pageviews extends Component {
|
||||||
<td class="muted">{i+1}</td>
|
<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><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.Count)}</td>
|
||||||
<td>{numbers.formatWithComma(p.CountUnique)}</td>
|
|
||||||
</tr>
|
</tr>
|
||||||
));
|
));
|
||||||
|
|
||||||
|
@ -59,7 +58,6 @@ class Pageviews extends Component {
|
||||||
<th>#</th>
|
<th>#</th>
|
||||||
<th>URL</th>
|
<th>URL</th>
|
||||||
<th>Pageviews</th>
|
<th>Pageviews</th>
|
||||||
<th>Unique</th>
|
|
||||||
</tr>
|
</tr>
|
||||||
</thead>
|
</thead>
|
||||||
<tbody>{tableRows}</tbody>
|
<tbody>{tableRows}</tbody>
|
||||||
|
|
|
@ -47,70 +47,7 @@ func (a *Archive) Save(Conn *sql.DB) error {
|
||||||
func CreateArchives() {
|
func CreateArchives() {
|
||||||
CreatePageviewArchives()
|
CreatePageviewArchives()
|
||||||
CreateVisitorArchives()
|
CreateVisitorArchives()
|
||||||
}
|
CreatePageviewArchivesPerPage()
|
||||||
|
|
||||||
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")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func checkError(err error) {
|
func checkError(err error) {
|
||||||
|
|
|
@ -44,3 +44,64 @@ func PageviewsPerDay(before int64, after int64) []Point {
|
||||||
results = fill(after, before, results)
|
results = fill(after, before, results)
|
||||||
return 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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -44,3 +44,35 @@ func VisitorsPerDay(before int64, after int64) []Point {
|
||||||
|
|
||||||
return results
|
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")
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue