fathom/pkg/api/params.go

52 lines
1.0 KiB
Go
Raw Normal View History

package api
import (
2016-12-11 13:50:01 +00:00
"net/http"
"strconv"
2016-12-11 14:33:23 +00:00
"strings"
2016-12-11 13:50:01 +00:00
"time"
)
// TODO: Move params into Params struct (with defaults)
2016-12-04 12:23:09 +00:00
const defaultPeriod = 7
const defaultLimit = 10
func getRequestedLimit(r *http.Request) int64 {
limit, err := strconv.ParseInt(r.URL.Query().Get("limit"), 10, 64)
2016-12-11 13:50:01 +00:00
if err != nil || limit == 0 {
limit = defaultLimit
2016-12-11 13:50:01 +00:00
}
2016-12-11 13:50:01 +00:00
return limit
}
func getRequestedDatePeriods(r *http.Request) (time.Time, time.Time) {
var startDate, endDate time.Time
2016-12-11 13:50:01 +00:00
var err error
beforeUnix, err := strconv.ParseInt(r.URL.Query().Get("before"), 10, 64)
if err != nil || beforeUnix == 0 {
endDate = time.Now()
} else {
endDate = time.Unix(beforeUnix, 0)
2016-12-11 13:50:01 +00:00
}
afterUnix, err := strconv.ParseInt(r.URL.Query().Get("after"), 10, 64)
if err != nil || afterUnix == 0 {
startDate = endDate.AddDate(0, 0, -defaultPeriod)
} else {
startDate = time.Unix(afterUnix, 0)
2016-12-11 13:50:01 +00:00
}
return startDate, endDate
}
2016-12-11 14:33:23 +00:00
func parseMajorMinor(v string) string {
parts := strings.SplitN(v, ".", 3)
if len(parts) > 1 {
v = parts[0] + "." + parts[1]
}
return v
}