2016-11-21 15:01:14 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2016-11-21 16:36:25 +00:00
|
|
|
"log"
|
2016-11-23 21:26:47 +00:00
|
|
|
"time"
|
2016-11-21 15:01:14 +00:00
|
|
|
)
|
|
|
|
|
2016-11-23 21:26:47 +00:00
|
|
|
type Datapoint struct {
|
|
|
|
Count int
|
|
|
|
Label string
|
|
|
|
}
|
|
|
|
|
2016-11-24 12:55:59 +00:00
|
|
|
var defaultPeriod = 7
|
|
|
|
|
2016-11-21 16:36:25 +00:00
|
|
|
// log fatal errors
|
|
|
|
func checkError(err error) {
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2016-11-21 15:01:14 +00:00
|
|
|
}
|
2016-11-23 21:26:47 +00:00
|
|
|
|
|
|
|
func fillDatapoints(days int, points []Datapoint) []Datapoint {
|
|
|
|
|
|
|
|
now := time.Now().AddDate(0, 0, 1)
|
|
|
|
start := now.AddDate(0, 0, -days)
|
|
|
|
newPoints := make([]Datapoint, days)
|
|
|
|
|
|
|
|
for i := 0; i < days; i++ {
|
|
|
|
newPoints[i] = Datapoint{
|
|
|
|
Count: 0,
|
|
|
|
Label: start.AddDate(0, 0, i).Format("2006-01-02"),
|
|
|
|
}
|
|
|
|
|
|
|
|
for j, p := range points {
|
|
|
|
if p.Label == newPoints[i].Label {
|
|
|
|
newPoints[i].Count = p.Count
|
|
|
|
points[j] = points[len(points)-1]
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return newPoints
|
|
|
|
}
|