From 022bbebe48fccdd579b95c370b2a3155878ca9fb Mon Sep 17 00:00:00 2001 From: Jan-Lukas Else Date: Sun, 26 May 2019 08:31:30 +0200 Subject: [PATCH] Add "count" view --- README.md | 2 +- database.go | 47 +++++++++++++++++++++++++++++------------------ main.go | 2 ++ 3 files changed, 32 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index b360e56..6efbf3d 100644 --- a/README.md +++ b/README.md @@ -58,7 +58,7 @@ You can request statistics via the `/stats` endpoint and specifying filters via The following filters are available: -`view`: specify what data (and it's view counts) gets displayed, you have the option between `pages` (tracked URLS), `referrers` (tracked refererrers - only hostnames e.g. google.com), `useragents` (tracked useragents with version - browsers or crawl bots with version), `useragentnames` (tracked useragents without version), `hours` / `days` / `weeks` / `months` (tracks grouped by hours / days / weeks / months), `allhours` / `alldays` (tracks grouped by hours / days including hours or days with zero visits, spanning from first to last track in selection) +`view`: specify what data (and it's view counts) gets displayed, you have the option between `pages` (tracked URLS), `referrers` (tracked refererrers - only hostnames e.g. google.com), `useragents` (tracked useragents with version - browsers or crawl bots with version), `useragentnames` (tracked useragents without version), `hours` / `days` / `weeks` / `months` (tracks grouped by hours / days / weeks / months), `allhours` / `alldays` (tracks grouped by hours / days including hours or days with zero visits, spanning from first to last track in selection), `count` (count all tracked views where filters apply) `from`: start time of the selection in the format `YYYY-MM-DD HH:MM`, e.g. `2019-01` or `2019-01-01 01:00` diff --git a/database.go b/database.go index 31a2aea..990c5b5 100644 --- a/database.go +++ b/database.go @@ -82,6 +82,7 @@ const ( MONTHS ALLHOURS ALLDAYS + COUNT ) type ViewsRequest struct { @@ -112,26 +113,34 @@ func (db *Database) request(request *ViewsRequest) (resultRows []*RequestResultR rows, e := db.sqlDB.Query(statement, namedArgs...) if e != nil { return - } else { - resultRows = []*RequestResultRow{} - for rows.Next() { - var first string - var second int - e = rows.Scan(&first, &second) - if e != nil { - _ = rows.Close() - return - } - if first == "" { - first = "Undefined" - } - resultRows = append(resultRows, &RequestResultRow{ - First: first, - Second: second, - }) - } + } + columns, e := rows.Columns() + if e != nil { return } + noOfColumns := len(columns) + resultRows = []*RequestResultRow{} + for rows.Next() { + var first string + var second int + if noOfColumns == 2 { + e = rows.Scan(&first, &second) + } else if noOfColumns == 1 { + e = rows.Scan(&second) + } + if e != nil { + _ = rows.Close() + return + } + if first == "" { + first = "Undefined" + } + resultRows = append(resultRows, &RequestResultRow{ + First: first, + Second: second, + }) + } + return } func (request *ViewsRequest) buildStatement() (statement string, parameters []sql.NamedArg) { @@ -181,6 +190,8 @@ func (request *ViewsRequest) buildStatement() (statement string, parameters []sq format = "%Y-%m" } statement = "SELECT strftime('" + format + "', time, 'localtime') as first, count(*) as second from views" + filters + "group by first" + orderStatement + limitStatement + ";" + case COUNT: + statement = "SELECT count(*) as second from views" + filters + ";" } return } diff --git a/main.go b/main.go index d815821..6f005e6 100644 --- a/main.go +++ b/main.go @@ -130,6 +130,8 @@ func StatsHandler(w http.ResponseWriter, r *http.Request) { view = ALLHOURS case "alldays": view = ALLDAYS + case "count": + view = COUNT } result, e := app.db.request(&ViewsRequest{ view: view,