mirror of https://github.com/status-im/fathom.git
Use count package in /visitors/count and /pageviews/count endpoints.
This commit is contained in:
parent
2f110fc059
commit
00f8d2cbba
|
@ -12,7 +12,7 @@ var GetBrowsersHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Re
|
|||
before, after := getRequestedPeriods(r)
|
||||
|
||||
// get total
|
||||
total := count.TotalVisitors(before, after)
|
||||
total := count.Visitors(before, after)
|
||||
|
||||
// get rows
|
||||
stmt, err := db.Conn.Prepare(`
|
||||
|
|
|
@ -12,7 +12,7 @@ var GetCountriesHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.R
|
|||
before, after := getRequestedPeriods(r)
|
||||
|
||||
// get total
|
||||
total := count.TotalVisitors(before, after)
|
||||
total := count.Visitors(before, after)
|
||||
|
||||
// get rows
|
||||
stmt, err := db.Conn.Prepare(`
|
||||
|
|
|
@ -12,7 +12,7 @@ var GetLanguagesHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.R
|
|||
before, after := getRequestedPeriods(r)
|
||||
|
||||
// get total
|
||||
total := count.TotalVisitors(before, after)
|
||||
total := count.Visitors(before, after)
|
||||
|
||||
stmt, err := db.Conn.Prepare(`
|
||||
SELECT
|
||||
|
|
|
@ -4,6 +4,7 @@ import (
|
|||
"net/http"
|
||||
"github.com/dannyvankooten/ana/models"
|
||||
"github.com/dannyvankooten/ana/db"
|
||||
"github.com/dannyvankooten/ana/count"
|
||||
"encoding/json"
|
||||
"github.com/gorilla/mux"
|
||||
"time"
|
||||
|
@ -49,17 +50,7 @@ var GetPageviewsHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.R
|
|||
// URL: /api/pageviews/count
|
||||
var GetPageviewsCountHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
before, after := getRequestedPeriods(r)
|
||||
stmt, err := db.Conn.Prepare(`
|
||||
SELECT
|
||||
SUM(a.count) AS count
|
||||
FROM archive a
|
||||
WHERE a.metric = 'pageviews' AND UNIX_TIMESTAMP(a.date) <= ? AND UNIX_TIMESTAMP(a.date) >= ?`)
|
||||
checkError(err)
|
||||
defer stmt.Close()
|
||||
|
||||
var result int
|
||||
stmt.QueryRow(before, after).Scan(&result)
|
||||
|
||||
result := count.Pageviews(before, after)
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(result)
|
||||
})
|
||||
|
|
|
@ -13,7 +13,7 @@ var GetReferrersHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.R
|
|||
before, after := getRequestedPeriods(r)
|
||||
|
||||
// get total
|
||||
total := count.TotalVisitors(before, after)
|
||||
total := count.Visitors(before, after)
|
||||
|
||||
// get rows
|
||||
stmt, err := db.Conn.Prepare(`
|
||||
|
|
|
@ -12,7 +12,7 @@ var GetScreenResolutionsHandler = http.HandlerFunc(func(w http.ResponseWriter, r
|
|||
before, after := getRequestedPeriods(r)
|
||||
|
||||
// get total
|
||||
total := count.TotalVisitors(before, after)
|
||||
total := count.Visitors(before, after)
|
||||
|
||||
// get rows
|
||||
stmt, err := db.Conn.Prepare(`
|
||||
|
|
|
@ -3,6 +3,7 @@ package api
|
|||
import (
|
||||
"net/http"
|
||||
"github.com/dannyvankooten/ana/db"
|
||||
"github.com/dannyvankooten/ana/count"
|
||||
"encoding/json"
|
||||
"github.com/gorilla/mux"
|
||||
"time"
|
||||
|
@ -11,16 +12,7 @@ import (
|
|||
// URL: /api/visitors/count
|
||||
var GetVisitorsCountHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
before, after := getRequestedPeriods(r)
|
||||
stmt, err := db.Conn.Prepare(`
|
||||
SELECT
|
||||
SUM(a.count)
|
||||
FROM archive a
|
||||
WHERE a.metric = 'visitors' AND UNIX_TIMESTAMP(a.date) <= ? AND UNIX_TIMESTAMP(a.date) >= ?`)
|
||||
checkError(err)
|
||||
defer stmt.Close()
|
||||
|
||||
var result int
|
||||
stmt.QueryRow(before, after).Scan(&result)
|
||||
result := count.Visitors(before, after)
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(result)
|
||||
})
|
||||
|
|
|
@ -0,0 +1,19 @@
|
|||
package count
|
||||
|
||||
import(
|
||||
"github.com/dannyvankooten/ana/db"
|
||||
)
|
||||
|
||||
func Pageviews(before int64, after int64) float64 {
|
||||
// get total
|
||||
stmt, err := db.Conn.Prepare(`
|
||||
SELECT
|
||||
SUM(a.count)
|
||||
FROM archive a
|
||||
WHERE a.metric = 'pageviews' AND UNIX_TIMESTAMP(a.date) <= ? AND UNIX_TIMESTAMP(a.date) >= ?`)
|
||||
checkError(err)
|
||||
defer stmt.Close()
|
||||
var total float64
|
||||
stmt.QueryRow(before, after).Scan(&total)
|
||||
return total
|
||||
}
|
|
@ -4,7 +4,7 @@ import(
|
|||
"github.com/dannyvankooten/ana/db"
|
||||
)
|
||||
|
||||
func TotalVisitors(before int64, after int64) float64 {
|
||||
func Visitors(before int64, after int64) float64 {
|
||||
// get total
|
||||
stmt, err := db.Conn.Prepare(`
|
||||
SELECT
|
Loading…
Reference in New Issue