Instead of querying total, calculate it from row values instead.

This commit is contained in:
Danny van Kooten 2016-12-24 15:57:37 +02:00
parent fd598da502
commit 4a7dcf9439
5 changed files with 16 additions and 29 deletions

View File

@ -21,8 +21,7 @@ func Browsers(before int64, after int64, limit int) []Point {
rows, err := stmt.Query(before, after, limit)
checkError(err)
total := Visitors(before, after)
return newPointSlice(rows, total)
return newPointSlice(rows)
}
// CreateBrowserTotals aggregates screen data into daily totals

View File

@ -79,30 +79,24 @@ func checkError(err error) {
}
}
// Custom perofmrs a custom count query, returning a slice of data points
func Custom(sql string, before int64, after int64, limit int, total float64) []Point {
stmt, err := db.Conn.Prepare(sql)
checkError(err)
defer stmt.Close()
rows, err := stmt.Query(before, after, limit)
checkError(err)
defer rows.Close()
results := newPointSlice(rows, total)
return results
}
func newPointSlice(rows *sql.Rows, total float64) []Point {
func newPointSlice(rows *sql.Rows) []Point {
results := make([]Point, 0)
total := 0
// append point slices
for rows.Next() {
var d Point
err := rows.Scan(&d.Label, &d.Value)
checkError(err)
d.PercentageValue = float64(d.Value) / total * 100
results = append(results, d)
// sum total
total += d.Value
}
// calculate percentage values for each point
for i, d := range results {
results[i].PercentageValue = float64(d.Value) / float64(total) * 100
}
return results

View File

@ -22,9 +22,7 @@ func Languages(before int64, after int64, limit int) []Point {
rows, err := stmt.Query(before, after, limit)
checkError(err)
total := Visitors(before, after)
return newPointSlice(rows, total)
return newPointSlice(rows)
}
// CreateLanguageTotals aggregates screen data into daily totals

View File

@ -9,7 +9,7 @@ func Referrers(before int64, after int64, limit int) []Point {
stmt, err := db.Conn.Prepare(`
SELECT
t.value,
SUM(t.count_unique) AS count
SUM(t.count) AS count
FROM total_referrers t
WHERE UNIX_TIMESTAMP(t.date) <= ? AND UNIX_TIMESTAMP(t.date) >= ?
GROUP BY t.value
@ -21,9 +21,7 @@ func Referrers(before int64, after int64, limit int) []Point {
rows, err := stmt.Query(before, after, limit)
checkError(err)
total := Visitors(before, after)
return newPointSlice(rows, total)
return newPointSlice(rows)
}
// CreateReferrerTotals aggregates screen data into daily totals

View File

@ -22,9 +22,7 @@ func Screens(before int64, after int64, limit int) []Point {
rows, err := stmt.Query(before, after, limit)
checkError(err)
total := Visitors(before, after)
return newPointSlice(rows, total)
return newPointSlice(rows)
}
// CreateScreenTotals aggregates screen data into daily totals