Add fromrel / torel
This commit is contained in:
parent
7800e5fe07
commit
4037788bd0
|
@ -64,6 +64,8 @@ The following filters are available:
|
|||
|
||||
`to`: end time of the selection
|
||||
|
||||
`fromrel` / `torel`: relative time from now for `to` or `from` (e.g `-2h45m`, valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h"
|
||||
|
||||
`url`: filter URLs containing the string provided, so `word` filters out all URLs that don't contain `word`
|
||||
|
||||
`ref`: filter referrers containing the string provided, so `word` filters out all refferers that don't contain `word`
|
||||
|
|
17
database.go
17
database.go
|
@ -11,6 +11,7 @@ import (
|
|||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Database struct {
|
||||
|
@ -81,7 +82,9 @@ const (
|
|||
type ViewsRequest struct {
|
||||
view View
|
||||
from string
|
||||
fromRel string
|
||||
to string
|
||||
toRel string
|
||||
url string
|
||||
ref string
|
||||
ua string
|
||||
|
@ -197,6 +200,20 @@ func (request *ViewsRequest) buildFilter() (filters string, parameters []sql.Nam
|
|||
}
|
||||
|
||||
func (request *ViewsRequest) buildDateTimeFilter(namedArg *[]sql.NamedArg) (dateTimeFilter string) {
|
||||
// Generate absolute from / to from relative ones
|
||||
if len(request.fromRel) > 0 {
|
||||
duration, e := time.ParseDuration(request.fromRel)
|
||||
if e == nil {
|
||||
request.from = time.Now().Add(duration).Format("2006-01-02 15:04:05")
|
||||
}
|
||||
}
|
||||
if len(request.toRel) > 0 {
|
||||
duration, e := time.ParseDuration(request.toRel)
|
||||
if e == nil {
|
||||
request.to = time.Now().Add(duration).Format("2006-01-02 15:04:05")
|
||||
}
|
||||
}
|
||||
// Build filter
|
||||
selector := "datetime(time, 'localtime')"
|
||||
if len(request.from) > 0 && len(request.to) > 0 {
|
||||
*namedArg = append(*namedArg, sql.Named("from", request.from))
|
||||
|
|
2
main.go
2
main.go
|
@ -134,7 +134,9 @@ func StatsHandler(w http.ResponseWriter, r *http.Request) {
|
|||
result, e := app.db.request(&ViewsRequest{
|
||||
view: view,
|
||||
from: queries.Get("from"),
|
||||
fromRel: queries.Get("fromrel"),
|
||||
to: queries.Get("to"),
|
||||
toRel: queries.Get("torel"),
|
||||
url: queries.Get("url"),
|
||||
ref: queries.Get("ref"),
|
||||
ua: queries.Get("ua"),
|
||||
|
|
Reference in New Issue