2016-12-11 10:53:03 +00:00
|
|
|
package count
|
2016-12-11 09:58:58 +00:00
|
|
|
|
2016-12-11 13:50:01 +00:00
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"github.com/dannyvankooten/ana/db"
|
|
|
|
"log"
|
|
|
|
"time"
|
2016-12-11 09:58:58 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Archive struct {
|
2016-12-11 13:50:01 +00:00
|
|
|
ID int64
|
|
|
|
Metric string
|
|
|
|
Value string
|
|
|
|
Count int64
|
|
|
|
Date string
|
2016-12-11 09:58:58 +00:00
|
|
|
}
|
|
|
|
|
2016-12-11 11:52:10 +00:00
|
|
|
type Point struct {
|
2016-12-11 13:50:01 +00:00
|
|
|
Label string
|
|
|
|
Value int
|
|
|
|
PercentageValue float64
|
2016-12-11 11:52:10 +00:00
|
|
|
}
|
|
|
|
|
2016-12-11 10:53:03 +00:00
|
|
|
func (a *Archive) Save(Conn *sql.DB) error {
|
2016-12-11 13:50:01 +00:00
|
|
|
stmt, err := db.Conn.Prepare(`INSERT INTO archive(
|
2016-12-11 09:58:58 +00:00
|
|
|
metric,
|
|
|
|
value,
|
|
|
|
count,
|
|
|
|
date
|
|
|
|
) VALUES( ?, ?, ?, ? )`)
|
2016-12-11 13:50:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer stmt.Close()
|
|
|
|
|
|
|
|
result, err := stmt.Exec(
|
|
|
|
a.Metric,
|
|
|
|
a.Value,
|
|
|
|
a.Count,
|
|
|
|
a.Date,
|
|
|
|
)
|
|
|
|
a.ID, _ = result.LastInsertId()
|
|
|
|
|
|
|
|
return err
|
2016-12-11 09:58:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func CreateArchives() {
|
2016-12-11 13:50:01 +00:00
|
|
|
CreatePageviewArchives()
|
|
|
|
CreateVisitorArchives()
|
2016-12-11 09:58:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func CreatePageviewArchives() {
|
2016-12-11 13:50:01 +00:00
|
|
|
stmt, err := db.Conn.Prepare(`
|
2016-12-11 09:58:58 +00:00
|
|
|
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`)
|
2016-12-11 13:50:01 +00:00
|
|
|
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")
|
2016-12-11 09:58:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func CreateVisitorArchives() {
|
2016-12-11 13:50:01 +00:00
|
|
|
stmt, err := db.Conn.Prepare(`
|
2016-12-11 09:58:58 +00:00
|
|
|
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`)
|
2016-12-11 13:50:01 +00:00
|
|
|
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")
|
2016-12-11 09:58:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func checkError(err error) {
|
2016-12-11 13:50:01 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2016-12-11 09:58:58 +00:00
|
|
|
}
|
2016-12-11 11:52:10 +00:00
|
|
|
|
|
|
|
func Custom(sql string, before int64, after int64, limit int, total float64) []Point {
|
2016-12-11 13:50:01 +00:00
|
|
|
stmt, err := db.Conn.Prepare(sql)
|
|
|
|
checkError(err)
|
|
|
|
defer stmt.Close()
|
2016-12-11 11:52:10 +00:00
|
|
|
|
2016-12-11 13:50:01 +00:00
|
|
|
rows, err := stmt.Query(before, after, limit)
|
|
|
|
checkError(err)
|
|
|
|
defer rows.Close()
|
2016-12-11 11:52:10 +00:00
|
|
|
|
2016-12-11 13:50:01 +00:00
|
|
|
results := newPointSlice(rows, total)
|
|
|
|
return results
|
2016-12-11 11:52:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func newPointSlice(rows *sql.Rows, total float64) []Point {
|
2016-12-11 13:50:01 +00:00
|
|
|
results := make([]Point, 0)
|
|
|
|
for rows.Next() {
|
|
|
|
var d Point
|
|
|
|
err := rows.Scan(&d.Label, &d.Value)
|
|
|
|
checkError(err)
|
2016-12-11 11:52:10 +00:00
|
|
|
|
2016-12-11 13:50:01 +00:00
|
|
|
d.PercentageValue = float64(d.Value) / total * 100
|
|
|
|
results = append(results, d)
|
|
|
|
}
|
2016-12-11 11:52:10 +00:00
|
|
|
|
2016-12-11 13:50:01 +00:00
|
|
|
return results
|
2016-12-11 11:52:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func fill(start int64, end int64, points []Point) []Point {
|
2016-12-11 13:50:01 +00:00
|
|
|
// be smart about received timestamps
|
|
|
|
if start > end {
|
|
|
|
tmp := end
|
|
|
|
end = start
|
|
|
|
start = tmp
|
|
|
|
}
|
|
|
|
|
|
|
|
startTime := time.Unix(start, 0)
|
|
|
|
endTime := time.Unix(end, 0)
|
|
|
|
newPoints := make([]Point, 0)
|
|
|
|
step := time.Hour * 24
|
|
|
|
|
|
|
|
for startTime.Before(endTime) || startTime.Equal(endTime) {
|
|
|
|
point := Point{
|
|
|
|
Value: 0,
|
|
|
|
Label: startTime.Format("2006-01-02"),
|
|
|
|
}
|
|
|
|
|
|
|
|
for j, p := range points {
|
|
|
|
if p.Label == point.Label || p.Label == startTime.Format("2006-01") {
|
|
|
|
point.Value = p.Value
|
|
|
|
points[j] = points[len(points)-1]
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
newPoints = append(newPoints, point)
|
|
|
|
startTime = startTime.Add(step)
|
|
|
|
}
|
|
|
|
|
|
|
|
return newPoints
|
2016-12-11 11:52:10 +00:00
|
|
|
}
|