Add "count" view
This commit is contained in:
parent
b270ed6299
commit
022bbebe48
|
@ -58,7 +58,7 @@ You can request statistics via the `/stats` endpoint and specifying filters via
|
||||||
|
|
||||||
The following filters are available:
|
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`
|
`from`: start time of the selection in the format `YYYY-MM-DD HH:MM`, e.g. `2019-01` or `2019-01-01 01:00`
|
||||||
|
|
||||||
|
|
47
database.go
47
database.go
|
@ -82,6 +82,7 @@ const (
|
||||||
MONTHS
|
MONTHS
|
||||||
ALLHOURS
|
ALLHOURS
|
||||||
ALLDAYS
|
ALLDAYS
|
||||||
|
COUNT
|
||||||
)
|
)
|
||||||
|
|
||||||
type ViewsRequest struct {
|
type ViewsRequest struct {
|
||||||
|
@ -112,26 +113,34 @@ func (db *Database) request(request *ViewsRequest) (resultRows []*RequestResultR
|
||||||
rows, e := db.sqlDB.Query(statement, namedArgs...)
|
rows, e := db.sqlDB.Query(statement, namedArgs...)
|
||||||
if e != nil {
|
if e != nil {
|
||||||
return
|
return
|
||||||
} else {
|
}
|
||||||
resultRows = []*RequestResultRow{}
|
columns, e := rows.Columns()
|
||||||
for rows.Next() {
|
if e != nil {
|
||||||
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,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
return
|
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) {
|
func (request *ViewsRequest) buildStatement() (statement string, parameters []sql.NamedArg) {
|
||||||
|
@ -181,6 +190,8 @@ func (request *ViewsRequest) buildStatement() (statement string, parameters []sq
|
||||||
format = "%Y-%m"
|
format = "%Y-%m"
|
||||||
}
|
}
|
||||||
statement = "SELECT strftime('" + format + "', time, 'localtime') as first, count(*) as second from views" + filters + "group by first" + orderStatement + limitStatement + ";"
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
2
main.go
2
main.go
|
@ -130,6 +130,8 @@ func StatsHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
view = ALLHOURS
|
view = ALLHOURS
|
||||||
case "alldays":
|
case "alldays":
|
||||||
view = ALLDAYS
|
view = ALLDAYS
|
||||||
|
case "count":
|
||||||
|
view = COUNT
|
||||||
}
|
}
|
||||||
result, e := app.db.request(&ViewsRequest{
|
result, e := app.db.request(&ViewsRequest{
|
||||||
view: view,
|
view: view,
|
||||||
|
|
Reference in New Issue