mirror of
https://github.com/status-im/fathom.git
synced 2025-03-01 03:20:27 +00:00
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)
|
before, after := getRequestedPeriods(r)
|
||||||
|
|
||||||
// get total
|
// get total
|
||||||
total := count.TotalVisitors(before, after)
|
total := count.Visitors(before, after)
|
||||||
|
|
||||||
// get rows
|
// get rows
|
||||||
stmt, err := db.Conn.Prepare(`
|
stmt, err := db.Conn.Prepare(`
|
||||||
|
@ -12,7 +12,7 @@ var GetCountriesHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.R
|
|||||||
before, after := getRequestedPeriods(r)
|
before, after := getRequestedPeriods(r)
|
||||||
|
|
||||||
// get total
|
// get total
|
||||||
total := count.TotalVisitors(before, after)
|
total := count.Visitors(before, after)
|
||||||
|
|
||||||
// get rows
|
// get rows
|
||||||
stmt, err := db.Conn.Prepare(`
|
stmt, err := db.Conn.Prepare(`
|
||||||
|
@ -12,7 +12,7 @@ var GetLanguagesHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.R
|
|||||||
before, after := getRequestedPeriods(r)
|
before, after := getRequestedPeriods(r)
|
||||||
|
|
||||||
// get total
|
// get total
|
||||||
total := count.TotalVisitors(before, after)
|
total := count.Visitors(before, after)
|
||||||
|
|
||||||
stmt, err := db.Conn.Prepare(`
|
stmt, err := db.Conn.Prepare(`
|
||||||
SELECT
|
SELECT
|
||||||
|
@ -4,6 +4,7 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"github.com/dannyvankooten/ana/models"
|
"github.com/dannyvankooten/ana/models"
|
||||||
"github.com/dannyvankooten/ana/db"
|
"github.com/dannyvankooten/ana/db"
|
||||||
|
"github.com/dannyvankooten/ana/count"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
"time"
|
"time"
|
||||||
@ -49,17 +50,7 @@ var GetPageviewsHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.R
|
|||||||
// URL: /api/pageviews/count
|
// URL: /api/pageviews/count
|
||||||
var GetPageviewsCountHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
var GetPageviewsCountHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
before, after := getRequestedPeriods(r)
|
before, after := getRequestedPeriods(r)
|
||||||
stmt, err := db.Conn.Prepare(`
|
result := count.Pageviews(before, after)
|
||||||
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)
|
|
||||||
|
|
||||||
w.Header().Set("Content-Type", "application/json")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
json.NewEncoder(w).Encode(result)
|
json.NewEncoder(w).Encode(result)
|
||||||
})
|
})
|
||||||
|
@ -13,7 +13,7 @@ var GetReferrersHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.R
|
|||||||
before, after := getRequestedPeriods(r)
|
before, after := getRequestedPeriods(r)
|
||||||
|
|
||||||
// get total
|
// get total
|
||||||
total := count.TotalVisitors(before, after)
|
total := count.Visitors(before, after)
|
||||||
|
|
||||||
// get rows
|
// get rows
|
||||||
stmt, err := db.Conn.Prepare(`
|
stmt, err := db.Conn.Prepare(`
|
||||||
|
@ -12,7 +12,7 @@ var GetScreenResolutionsHandler = http.HandlerFunc(func(w http.ResponseWriter, r
|
|||||||
before, after := getRequestedPeriods(r)
|
before, after := getRequestedPeriods(r)
|
||||||
|
|
||||||
// get total
|
// get total
|
||||||
total := count.TotalVisitors(before, after)
|
total := count.Visitors(before, after)
|
||||||
|
|
||||||
// get rows
|
// get rows
|
||||||
stmt, err := db.Conn.Prepare(`
|
stmt, err := db.Conn.Prepare(`
|
||||||
|
@ -3,6 +3,7 @@ package api
|
|||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"github.com/dannyvankooten/ana/db"
|
"github.com/dannyvankooten/ana/db"
|
||||||
|
"github.com/dannyvankooten/ana/count"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
"time"
|
"time"
|
||||||
@ -11,16 +12,7 @@ import (
|
|||||||
// URL: /api/visitors/count
|
// URL: /api/visitors/count
|
||||||
var GetVisitorsCountHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
var GetVisitorsCountHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
before, after := getRequestedPeriods(r)
|
before, after := getRequestedPeriods(r)
|
||||||
stmt, err := db.Conn.Prepare(`
|
result := count.Visitors(before, after)
|
||||||
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)
|
|
||||||
w.Header().Set("Content-Type", "application/json")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
json.NewEncoder(w).Encode(result)
|
json.NewEncoder(w).Encode(result)
|
||||||
})
|
})
|
||||||
|
19
count/pageviews.go
Normal file
19
count/pageviews.go
Normal file
@ -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"
|
"github.com/dannyvankooten/ana/db"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TotalVisitors(before int64, after int64) float64 {
|
func Visitors(before int64, after int64) float64 {
|
||||||
// get total
|
// get total
|
||||||
stmt, err := db.Conn.Prepare(`
|
stmt, err := db.Conn.Prepare(`
|
||||||
SELECT
|
SELECT
|
Loading…
x
Reference in New Issue
Block a user