Fix gometalinter issues throughout count package.

This commit is contained in:
Danny van Kooten 2016-12-23 16:03:11 +02:00
parent 0289ba8cfe
commit 1d75102d5f
3 changed files with 18 additions and 5 deletions

View File

@ -2,11 +2,13 @@ package count
import ( import (
"database/sql" "database/sql"
"github.com/dannyvankooten/ana/db"
"log" "log"
"time" "time"
"github.com/dannyvankooten/ana/db"
) )
// The Archive model contains data for a daily metric total
type Archive struct { type Archive struct {
ID int64 ID int64
Metric string Metric string
@ -15,12 +17,14 @@ type Archive struct {
Date string Date string
} }
// Point represents a data point, will always have a Label and Value
type Point struct { type Point struct {
Label string Label string
Value int Value int
PercentageValue float64 PercentageValue float64
} }
// Save the Archive in the given database connection
func (a *Archive) Save(Conn *sql.DB) error { func (a *Archive) Save(Conn *sql.DB) error {
stmt, err := db.Conn.Prepare(`INSERT INTO archive( stmt, err := db.Conn.Prepare(`INSERT INTO archive(
metric, metric,
@ -44,6 +48,7 @@ func (a *Archive) Save(Conn *sql.DB) error {
return err return err
} }
// CreateArchives calls all archive creation func's consecutively
func CreateArchives() { func CreateArchives() {
CreatePageviewArchives() CreatePageviewArchives()
CreateVisitorArchives() CreateVisitorArchives()
@ -56,6 +61,7 @@ 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 { func Custom(sql string, before int64, after int64, limit int, total float64) []Point {
stmt, err := db.Conn.Prepare(sql) stmt, err := db.Conn.Prepare(sql)
checkError(err) checkError(err)
@ -70,7 +76,7 @@ func Custom(sql string, before int64, after int64, limit int, total float64) []P
} }
func newPointSlice(rows *sql.Rows, total float64) []Point { func newPointSlice(rows *sql.Rows, total float64) []Point {
results := make([]Point, 0) var results []Point
for rows.Next() { for rows.Next() {
var d Point var d Point
err := rows.Scan(&d.Label, &d.Value) err := rows.Scan(&d.Label, &d.Value)
@ -93,7 +99,7 @@ func fill(start int64, end int64, points []Point) []Point {
startTime := time.Unix(start, 0) startTime := time.Unix(start, 0)
endTime := time.Unix(end, 0) endTime := time.Unix(end, 0)
newPoints := make([]Point, 0) var newPoints []Point
step := time.Hour * 24 step := time.Hour * 24
for startTime.Before(endTime) || startTime.Equal(endTime) { for startTime.Before(endTime) || startTime.Equal(endTime) {

View File

@ -4,6 +4,7 @@ import (
"github.com/dannyvankooten/ana/db" "github.com/dannyvankooten/ana/db"
) )
// Pageviews returns the total number of pageviews between the given timestamps
func Pageviews(before int64, after int64) float64 { func Pageviews(before int64, after int64) float64 {
// get total // get total
stmt, err := db.Conn.Prepare(` stmt, err := db.Conn.Prepare(`
@ -18,6 +19,7 @@ func Pageviews(before int64, after int64) float64 {
return total return total
} }
// PageviewsPerDay returns a slice of data points representing the number of pageviews per day
func PageviewsPerDay(before int64, after int64) []Point { func PageviewsPerDay(before int64, after int64) []Point {
stmt, err := db.Conn.Prepare(`SELECT stmt, err := db.Conn.Prepare(`SELECT
SUM(a.count) AS count, SUM(a.count) AS count,
@ -32,7 +34,7 @@ func PageviewsPerDay(before int64, after int64) []Point {
checkError(err) checkError(err)
defer rows.Close() defer rows.Close()
results := make([]Point, 0) var results []Point
defer rows.Close() defer rows.Close()
for rows.Next() { for rows.Next() {
p := Point{} p := Point{}
@ -45,6 +47,7 @@ func PageviewsPerDay(before int64, after int64) []Point {
return results return results
} }
// CreatePageviewArchives aggregates pageview data into daily totals
func CreatePageviewArchives() { func CreatePageviewArchives() {
stmt, err := db.Conn.Prepare(` stmt, err := db.Conn.Prepare(`
SELECT SELECT
@ -77,6 +80,7 @@ func CreatePageviewArchives() {
db.Conn.Exec("COMMIT") db.Conn.Exec("COMMIT")
} }
// CreatePageviewArchivesPerPage aggregates pageview data for each page into daily totals
func CreatePageviewArchivesPerPage() { func CreatePageviewArchivesPerPage() {
stmt, err := db.Conn.Prepare(`SELECT stmt, err := db.Conn.Prepare(`SELECT
pv.page_id, pv.page_id,

View File

@ -4,6 +4,7 @@ import (
"github.com/dannyvankooten/ana/db" "github.com/dannyvankooten/ana/db"
) )
// Visitors returns the number of total visitors between the given timestamps
func Visitors(before int64, after int64) float64 { func Visitors(before int64, after int64) float64 {
// get total // get total
stmt, err := db.Conn.Prepare(` stmt, err := db.Conn.Prepare(`
@ -18,6 +19,7 @@ func Visitors(before int64, after int64) float64 {
return total return total
} }
// VisitorsPerDay returns a point slice containing visitor data per day
func VisitorsPerDay(before int64, after int64) []Point { func VisitorsPerDay(before int64, after int64) []Point {
stmt, err := db.Conn.Prepare(`SELECT stmt, err := db.Conn.Prepare(`SELECT
SUM(a.count) AS count, SUM(a.count) AS count,
@ -31,7 +33,7 @@ func VisitorsPerDay(before int64, after int64) []Point {
rows, err := stmt.Query(before, after) rows, err := stmt.Query(before, after)
checkError(err) checkError(err)
results := make([]Point, 0) var results []Point
defer rows.Close() defer rows.Close()
for rows.Next() { for rows.Next() {
p := Point{} p := Point{}
@ -45,6 +47,7 @@ func VisitorsPerDay(before int64, after int64) []Point {
return results return results
} }
// CreateVisitorArchives aggregates visitor data into daily totals
func CreateVisitorArchives() { func CreateVisitorArchives() {
stmt, err := db.Conn.Prepare(` stmt, err := db.Conn.Prepare(`
SELECT SELECT