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"
|
|
|
|
"log"
|
|
|
|
"time"
|
2016-12-23 14:03:11 +00:00
|
|
|
|
|
|
|
"github.com/dannyvankooten/ana/db"
|
2016-12-11 09:58:58 +00:00
|
|
|
)
|
|
|
|
|
2016-12-23 14:03:11 +00:00
|
|
|
// The Archive model contains data for a daily metric total
|
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-23 14:03:11 +00:00
|
|
|
// Point represents a data point, will always have a Label and Value
|
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-23 14:03:11 +00:00
|
|
|
// Save the Archive in the given database connection
|
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
|
|
|
}
|
|
|
|
|
2016-12-23 14:03:11 +00:00
|
|
|
// CreateArchives calls all archive creation func's consecutively
|
2016-12-11 09:58:58 +00:00
|
|
|
func CreateArchives() {
|
2016-12-11 13:50:01 +00:00
|
|
|
CreatePageviewArchives()
|
|
|
|
CreateVisitorArchives()
|
2016-12-11 15:23:13 +00:00
|
|
|
CreatePageviewArchivesPerPage()
|
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
|
|
|
|
2016-12-23 14:03:11 +00:00
|
|
|
// Custom perofmrs a custom count query, returning a slice of data points
|
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-23 14:03:11 +00:00
|
|
|
var results []Point
|
2016-12-11 13:50:01 +00:00
|
|
|
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)
|
2016-12-23 14:03:11 +00:00
|
|
|
var newPoints []Point
|
2016-12-11 13:50:01 +00:00
|
|
|
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
|
|
|
}
|