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

View File

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

View File

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