diff --git a/README.md b/README.md index c5bb64a..b360e56 100644 --- a/README.md +++ b/README.md @@ -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` diff --git a/database.go b/database.go index 0a07646..36c4ab8 100644 --- a/database.go +++ b/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)) diff --git a/main.go b/main.go index cc39882..d815821 100644 --- a/main.go +++ b/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"),