mirror of https://github.com/status-im/fathom.git
abstract total visitor count away into count package
This commit is contained in:
parent
f25c18a7fe
commit
2f110fc059
|
@ -10,7 +10,7 @@ import (
|
|||
type Datapoint struct {
|
||||
Count int
|
||||
Label string
|
||||
Percentage float32 `json:",omitempty"`
|
||||
Percentage float64 `json:",omitempty"`
|
||||
}
|
||||
|
||||
const defaultPeriod = 7
|
||||
|
|
|
@ -3,6 +3,7 @@ package api
|
|||
import (
|
||||
"net/http"
|
||||
"github.com/dannyvankooten/ana/db"
|
||||
"github.com/dannyvankooten/ana/count"
|
||||
"encoding/json"
|
||||
)
|
||||
|
||||
|
@ -11,19 +12,10 @@ var GetBrowsersHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Re
|
|||
before, after := getRequestedPeriods(r)
|
||||
|
||||
// get total
|
||||
stmt, err := db.Conn.Prepare(`
|
||||
SELECT
|
||||
COUNT(DISTINCT(pv.visitor_id))
|
||||
FROM pageviews pv
|
||||
WHERE UNIX_TIMESTAMP(pv.timestamp) <= ? AND UNIX_TIMESTAMP(pv.timestamp) >= ?
|
||||
`)
|
||||
checkError(err)
|
||||
defer stmt.Close()
|
||||
var total float32
|
||||
stmt.QueryRow(before, after).Scan(&total)
|
||||
total := count.TotalVisitors(before, after)
|
||||
|
||||
// get rows
|
||||
stmt, err = db.Conn.Prepare(`
|
||||
stmt, err := db.Conn.Prepare(`
|
||||
SELECT
|
||||
v.browser_name,
|
||||
COUNT(DISTINCT(pv.visitor_id)) AS count
|
||||
|
@ -45,7 +37,7 @@ var GetBrowsersHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Re
|
|||
err = rows.Scan(&d.Label, &d.Count);
|
||||
checkError(err)
|
||||
|
||||
d.Percentage = float32(d.Count) / total * 100
|
||||
d.Percentage = float64(d.Count) / total * 100
|
||||
results = append(results, d)
|
||||
}
|
||||
|
||||
|
|
|
@ -3,6 +3,7 @@ package api
|
|||
import (
|
||||
"net/http"
|
||||
"github.com/dannyvankooten/ana/db"
|
||||
"github.com/dannyvankooten/ana/count"
|
||||
"encoding/json"
|
||||
)
|
||||
|
||||
|
@ -11,19 +12,10 @@ var GetCountriesHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.R
|
|||
before, after := getRequestedPeriods(r)
|
||||
|
||||
// get total
|
||||
stmt, err := db.Conn.Prepare(`
|
||||
SELECT
|
||||
COUNT(DISTINCT(pv.visitor_id))
|
||||
FROM pageviews pv
|
||||
WHERE UNIX_TIMESTAMP(pv.timestamp) <= ? AND UNIX_TIMESTAMP(pv.timestamp) >= ?
|
||||
`)
|
||||
checkError(err)
|
||||
defer stmt.Close()
|
||||
var total float32
|
||||
stmt.QueryRow(before, after).Scan(&total)
|
||||
total := count.TotalVisitors(before, after)
|
||||
|
||||
// get rows
|
||||
stmt, err = db.Conn.Prepare(`
|
||||
stmt, err := db.Conn.Prepare(`
|
||||
SELECT
|
||||
v.country,
|
||||
COUNT(DISTINCT(pv.visitor_id)) AS count
|
||||
|
@ -45,7 +37,7 @@ var GetCountriesHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.R
|
|||
err = rows.Scan(&d.Label, &d.Count);
|
||||
checkError(err)
|
||||
|
||||
d.Percentage = float32(d.Count) / total * 100
|
||||
d.Percentage = float64(d.Count) / total * 100
|
||||
results = append(results, d)
|
||||
}
|
||||
|
||||
|
|
|
@ -3,6 +3,7 @@ package api
|
|||
import (
|
||||
"net/http"
|
||||
"github.com/dannyvankooten/ana/db"
|
||||
"github.com/dannyvankooten/ana/count"
|
||||
"encoding/json"
|
||||
)
|
||||
|
||||
|
@ -10,19 +11,10 @@ import (
|
|||
var GetLanguagesHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
before, after := getRequestedPeriods(r)
|
||||
|
||||
stmt, err := db.Conn.Prepare(`
|
||||
SELECT
|
||||
COUNT(DISTINCT(pv.visitor_id))
|
||||
FROM pageviews pv
|
||||
LEFT JOIN visitors v ON v.id = pv.visitor_id
|
||||
WHERE UNIX_TIMESTAMP(pv.timestamp) <= ? AND UNIX_TIMESTAMP(pv.timestamp) >= ?
|
||||
`)
|
||||
checkError(err)
|
||||
defer stmt.Close()
|
||||
var total float32
|
||||
stmt.QueryRow(before, after).Scan(&total)
|
||||
// get total
|
||||
total := count.TotalVisitors(before, after)
|
||||
|
||||
stmt, err = db.Conn.Prepare(`
|
||||
stmt, err := db.Conn.Prepare(`
|
||||
SELECT
|
||||
v.browser_language,
|
||||
COUNT(v.id) AS count
|
||||
|
@ -46,7 +38,7 @@ var GetLanguagesHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.R
|
|||
err = rows.Scan(&d.Label, &d.Count);
|
||||
checkError(err)
|
||||
|
||||
d.Percentage = float32(d.Count) / total * 100
|
||||
d.Percentage = float64(d.Count) / total * 100
|
||||
results = append(results, d)
|
||||
}
|
||||
|
||||
|
|
|
@ -51,7 +51,7 @@ var GetPageviewsCountHandler = http.HandlerFunc(func(w http.ResponseWriter, r *h
|
|||
before, after := getRequestedPeriods(r)
|
||||
stmt, err := db.Conn.Prepare(`
|
||||
SELECT
|
||||
SUM(a.count) AS count
|
||||
SUM(a.count) AS count
|
||||
FROM archive a
|
||||
WHERE a.metric = 'pageviews' AND UNIX_TIMESTAMP(a.date) <= ? AND UNIX_TIMESTAMP(a.date) >= ?`)
|
||||
checkError(err)
|
||||
|
|
|
@ -3,6 +3,7 @@ package api
|
|||
import (
|
||||
"net/http"
|
||||
"github.com/dannyvankooten/ana/db"
|
||||
"github.com/dannyvankooten/ana/count"
|
||||
"encoding/json"
|
||||
"strings"
|
||||
)
|
||||
|
@ -12,18 +13,10 @@ var GetReferrersHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.R
|
|||
before, after := getRequestedPeriods(r)
|
||||
|
||||
// get total
|
||||
stmt, err := db.Conn.Prepare(`
|
||||
SELECT
|
||||
COUNT(DISTINCT(pv.visitor_id))
|
||||
FROM pageviews pv
|
||||
WHERE UNIX_TIMESTAMP(pv.timestamp) <= ? AND UNIX_TIMESTAMP(pv.timestamp) >= ?`)
|
||||
checkError(err)
|
||||
defer stmt.Close()
|
||||
var total float32
|
||||
stmt.QueryRow(before, after).Scan(&total)
|
||||
total := count.TotalVisitors(before, after)
|
||||
|
||||
// get rows
|
||||
stmt, err = db.Conn.Prepare(`
|
||||
stmt, err := db.Conn.Prepare(`
|
||||
SELECT
|
||||
pv.referrer_url,
|
||||
COUNT(DISTINCT(pv.visitor_id)) AS count
|
||||
|
@ -49,7 +42,7 @@ var GetReferrersHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.R
|
|||
d.Label = strings.TrimRight(d.Label, "/")
|
||||
checkError(err)
|
||||
|
||||
d.Percentage = float32(d.Count) / total * 100
|
||||
d.Percentage = float64(d.Count) / total * 100
|
||||
results = append(results, d)
|
||||
}
|
||||
|
||||
|
|
|
@ -3,6 +3,7 @@ package api
|
|||
import (
|
||||
"net/http"
|
||||
"github.com/dannyvankooten/ana/db"
|
||||
"github.com/dannyvankooten/ana/count"
|
||||
"encoding/json"
|
||||
)
|
||||
|
||||
|
@ -11,18 +12,10 @@ var GetScreenResolutionsHandler = http.HandlerFunc(func(w http.ResponseWriter, r
|
|||
before, after := getRequestedPeriods(r)
|
||||
|
||||
// get total
|
||||
stmt, err := db.Conn.Prepare(`
|
||||
SELECT
|
||||
COUNT(DISTINCT(pv.visitor_id))
|
||||
FROM pageviews pv
|
||||
WHERE UNIX_TIMESTAMP(pv.timestamp) <= ? AND UNIX_TIMESTAMP(pv.timestamp) >= ?`)
|
||||
checkError(err)
|
||||
defer stmt.Close()
|
||||
var total float32
|
||||
stmt.QueryRow(before, after).Scan(&total)
|
||||
total := count.TotalVisitors(before, after)
|
||||
|
||||
// get rows
|
||||
stmt, err = db.Conn.Prepare(`
|
||||
stmt, err := db.Conn.Prepare(`
|
||||
SELECT
|
||||
v.screen_resolution,
|
||||
COUNT(DISTINCT(pv.visitor_id)) AS count
|
||||
|
@ -44,7 +37,7 @@ var GetScreenResolutionsHandler = http.HandlerFunc(func(w http.ResponseWriter, r
|
|||
err = rows.Scan(&d.Label, &d.Count);
|
||||
checkError(err)
|
||||
|
||||
d.Percentage = float32(d.Count) / total * 100
|
||||
d.Percentage = float64(d.Count) / total * 100
|
||||
results = append(results, d)
|
||||
}
|
||||
|
||||
|
|
|
@ -15,9 +15,7 @@ var GetVisitorsCountHandler = http.HandlerFunc(func(w http.ResponseWriter, r *ht
|
|||
SELECT
|
||||
SUM(a.count)
|
||||
FROM archive a
|
||||
WHERE a.metric = 'visitors' AND UNIX_TIMESTAMP(a.date) <= ? AND UNIX_TIMESTAMP(a.date) >= ?
|
||||
`)
|
||||
|
||||
WHERE a.metric = 'visitors' AND UNIX_TIMESTAMP(a.date) <= ? AND UNIX_TIMESTAMP(a.date) >= ?`)
|
||||
checkError(err)
|
||||
defer stmt.Close()
|
||||
|
||||
|
|
|
@ -2,6 +2,7 @@ package commands
|
|||
|
||||
import(
|
||||
"github.com/dannyvankooten/ana/db"
|
||||
"github.com/dannyvankooten/ana/count"
|
||||
)
|
||||
|
||||
func seedData() {
|
||||
|
@ -9,5 +10,5 @@ func seedData() {
|
|||
}
|
||||
|
||||
func archiveData() {
|
||||
db.CreateArchives()
|
||||
count.CreateArchives()
|
||||
}
|
||||
|
|
|
@ -1,11 +1,11 @@
|
|||
package db
|
||||
package count
|
||||
|
||||
import(
|
||||
"database/sql"
|
||||
"log"
|
||||
"github.com/dannyvankooten/ana/db"
|
||||
)
|
||||
|
||||
|
||||
type Archive struct {
|
||||
ID int64
|
||||
Metric string
|
||||
|
@ -14,8 +14,8 @@ type Archive struct {
|
|||
Date string
|
||||
}
|
||||
|
||||
func (a *Archive) Save(conn *sql.DB) error {
|
||||
stmt, err := conn.Prepare(`INSERT INTO archive(
|
||||
func (a *Archive) Save(Conn *sql.DB) error {
|
||||
stmt, err := db.Conn.Prepare(`INSERT INTO archive(
|
||||
metric,
|
||||
value,
|
||||
count,
|
||||
|
@ -44,7 +44,7 @@ func CreateArchives() {
|
|||
}
|
||||
|
||||
func CreatePageviewArchives() {
|
||||
stmt, err := Conn.Prepare(`
|
||||
stmt, err := db.Conn.Prepare(`
|
||||
SELECT
|
||||
COUNT(*) AS count,
|
||||
DATE_FORMAT(pv.timestamp, "%Y-%m-%d") AS date_group
|
||||
|
@ -62,7 +62,7 @@ func CreatePageviewArchives() {
|
|||
checkError(err)
|
||||
defer rows.Close()
|
||||
|
||||
Conn.Exec("START TRANSACTION")
|
||||
db.Conn.Exec("START TRANSACTION")
|
||||
for rows.Next() {
|
||||
a := Archive{
|
||||
Metric: "pageviews",
|
||||
|
@ -70,21 +70,14 @@ func CreatePageviewArchives() {
|
|||
}
|
||||
err = rows.Scan(&a.Count, &a.Date);
|
||||
checkError(err)
|
||||
a.Save(Conn)
|
||||
a.Save(db.Conn)
|
||||
}
|
||||
Conn.Exec("COMMIT")
|
||||
db.Conn.Exec("COMMIT")
|
||||
}
|
||||
|
||||
func CreateVisitorArchives() {
|
||||
|
||||
/*
|
||||
SELECT
|
||||
COUNT(DISTINCT(visitor_id)) AS count, DATE_FORMAT(timestamp, ?) AS date_group
|
||||
FROM pageviews pv
|
||||
WHERE UNIX_TIMESTAMP(pv.timestamp) <= ? AND UNIX_TIMESTAMP(pv.timestamp) >= ?
|
||||
GROUP BY date_group
|
||||
*/
|
||||
stmt, err := Conn.Prepare(`
|
||||
stmt, err := db.Conn.Prepare(`
|
||||
SELECT
|
||||
COUNT(DISTINCT(pv.visitor_id)) AS count,
|
||||
DATE_FORMAT(pv.timestamp, "%Y-%m-%d") AS date_group
|
||||
|
@ -102,7 +95,7 @@ func CreateVisitorArchives() {
|
|||
checkError(err)
|
||||
defer rows.Close()
|
||||
|
||||
Conn.Exec("START TRANSACTION")
|
||||
db.Conn.Exec("START TRANSACTION")
|
||||
for rows.Next() {
|
||||
a := Archive{
|
||||
Metric: "visitors",
|
||||
|
@ -110,9 +103,9 @@ func CreateVisitorArchives() {
|
|||
}
|
||||
err = rows.Scan(&a.Count, &a.Date);
|
||||
checkError(err)
|
||||
a.Save(Conn)
|
||||
a.Save(db.Conn)
|
||||
}
|
||||
Conn.Exec("COMMIT")
|
||||
db.Conn.Exec("COMMIT")
|
||||
}
|
||||
|
||||
func checkError(err error) {
|
|
@ -0,0 +1,19 @@
|
|||
package count
|
||||
|
||||
import(
|
||||
"github.com/dannyvankooten/ana/db"
|
||||
)
|
||||
|
||||
func TotalVisitors(before int64, after int64) float64 {
|
||||
// get total
|
||||
stmt, err := db.Conn.Prepare(`
|
||||
SELECT
|
||||
SUM(a.count)
|
||||
FROM archive a
|
||||
WHERE a.metric = 'visitors' AND UNIX_TIMESTAMP(a.date) <= ? AND UNIX_TIMESTAMP(a.date) >= ?`)
|
||||
checkError(err)
|
||||
defer stmt.Close()
|
||||
var total float64
|
||||
stmt.QueryRow(before, after).Scan(&total)
|
||||
return total
|
||||
}
|
Loading…
Reference in New Issue