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-24 13:18:40 +00:00
|
|
|
"strconv"
|
|
|
|
"net/http"
|
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 13:18:40 +00:00
|
|
|
Percentage float32 `json:",omitempty"`
|
2016-11-23 21:26:47 +00:00
|
|
|
}
|
|
|
|
|
2016-12-04 12:23:09 +00:00
|
|
|
const defaultPeriod = 7
|
|
|
|
const defaultLimit = 10
|
2016-11-24 12:55:59 +00:00
|
|
|
|
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
|
|
|
|
2016-12-05 20:48:33 +00:00
|
|
|
func fillDatapoints(start int64, end int64, step time.Duration, points []Datapoint) []Datapoint {
|
2016-11-26 16:19:15 +00:00
|
|
|
// be smart about received timestamps
|
|
|
|
if start > end {
|
|
|
|
tmp := end
|
|
|
|
end = start
|
|
|
|
start = tmp
|
|
|
|
}
|
2016-11-23 21:26:47 +00:00
|
|
|
|
2016-11-26 16:19:15 +00:00
|
|
|
startTime := time.Unix(start, 0)
|
|
|
|
endTime := time.Unix(end, 0)
|
|
|
|
newPoints := make([]Datapoint, 0)
|
2016-11-23 21:26:47 +00:00
|
|
|
|
2016-12-08 08:26:45 +00:00
|
|
|
for startTime.Before(endTime) || startTime.Equal(endTime) {
|
2016-11-26 16:19:15 +00:00
|
|
|
point := Datapoint{
|
2016-11-23 21:26:47 +00:00
|
|
|
Count: 0,
|
2016-11-26 16:19:15 +00:00
|
|
|
Label: startTime.Format("2006-01-02"),
|
2016-11-23 21:26:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for j, p := range points {
|
2016-12-05 20:48:33 +00:00
|
|
|
if p.Label == point.Label || p.Label == startTime.Format("2006-01") {
|
2016-11-26 16:19:15 +00:00
|
|
|
point.Count = p.Count
|
2016-11-23 21:26:47 +00:00
|
|
|
points[j] = points[len(points)-1]
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2016-11-26 16:19:15 +00:00
|
|
|
|
|
|
|
newPoints = append(newPoints, point)
|
2016-12-05 20:48:33 +00:00
|
|
|
startTime = startTime.Add(step)
|
2016-11-23 21:26:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return newPoints
|
|
|
|
}
|
2016-11-24 13:18:40 +00:00
|
|
|
|
2016-11-26 14:39:29 +00:00
|
|
|
func getRequestedLimit(r *http.Request) int {
|
|
|
|
limit, err := strconv.Atoi(r.URL.Query().Get("limit"))
|
|
|
|
if err != nil || limit == 0 {
|
|
|
|
limit = 10
|
|
|
|
}
|
|
|
|
|
|
|
|
return limit
|
|
|
|
}
|
|
|
|
|
2016-11-26 16:19:15 +00:00
|
|
|
func getRequestedPeriods(r *http.Request) (int64, int64) {
|
|
|
|
var before, after int64
|
|
|
|
var err error
|
|
|
|
|
|
|
|
before, err = strconv.ParseInt(r.URL.Query().Get("before"), 10, 64)
|
|
|
|
if err != nil || before == 0 {
|
|
|
|
before = time.Now().Unix()
|
2016-11-24 13:18:40 +00:00
|
|
|
}
|
2016-11-26 16:19:15 +00:00
|
|
|
|
|
|
|
after, err = strconv.ParseInt(r.URL.Query().Get("after"), 10, 64)
|
|
|
|
if err != nil || before == 0 {
|
|
|
|
after = time.Now().AddDate(0, 0, -7).Unix()
|
|
|
|
}
|
|
|
|
|
|
|
|
return before, after
|
2016-11-24 13:18:40 +00:00
|
|
|
}
|