mirror of https://github.com/status-im/fathom.git
refactor db conn into db package
This commit is contained in:
parent
06e9c5fdfd
commit
93ee878bd3
|
@ -16,8 +16,7 @@ This is nowhere near being usable, let alone stable. Treat as a proof of concept
|
||||||
};
|
};
|
||||||
a=d.createElement('script'),
|
a=d.createElement('script'),
|
||||||
m=d.getElementsByTagName('script')[0];
|
m=d.getElementsByTagName('script')[0];
|
||||||
a.async=1;
|
a.async=1; a.src=u;
|
||||||
a.src=u;
|
|
||||||
m.parentNode.insertBefore(a,m)
|
m.parentNode.insertBefore(a,m)
|
||||||
})(document, window, '//ana.dev/tracker.js', 'ana');
|
})(document, window, '//ana.dev/tracker.js', 'ana');
|
||||||
ana('setTrackerUrl', '//ana.dev/collect');
|
ana('setTrackerUrl', '//ana.dev/collect');
|
||||||
|
|
11
ROADMAP.md
11
ROADMAP.md
|
@ -5,15 +5,18 @@ This is a general draft document for thoughts and todo's, without any structure
|
||||||
|
|
||||||
### What's cooking?
|
### What's cooking?
|
||||||
|
|
||||||
- Replace chart with custom D3 chart.
|
- Return 0px GIF in /collect endpoint
|
||||||
- Allow setting custom limit in table overviews.
|
- Hand out unique ID to each visitor
|
||||||
|
- Reference site URL when tracking.
|
||||||
|
- Reference path & title when tracking (indexed by path, update title when changes)
|
||||||
|
- Track referrals, use tables from aforementioned points.
|
||||||
|
- Bulk process tracking requests (Redis or in-memory?)
|
||||||
- Allow sorting in table overviews.
|
- Allow sorting in table overviews.
|
||||||
- Choose a OS license & settle on name.
|
- Choose a OS license & settle on name.
|
||||||
- JS client for consuming API endpoints.
|
- JS client for consuming API endpoints.
|
||||||
- Envelope API responses.
|
- Envelope API responses & perhaps return total in table overview?
|
||||||
- Track canonical URL's.
|
- Track canonical URL's.
|
||||||
- Show referrals.
|
- Show referrals.
|
||||||
- Country flags.
|
|
||||||
- Geolocate unknown IP addresses periodically.
|
- Geolocate unknown IP addresses periodically.
|
||||||
- Mask last part of IP address.
|
- Mask last part of IP address.
|
||||||
|
|
||||||
|
|
14
ana.go
14
ana.go
|
@ -4,31 +4,25 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"log"
|
"log"
|
||||||
"github.com/dannyvankooten/ana/core"
|
"github.com/dannyvankooten/ana/db"
|
||||||
"github.com/dannyvankooten/ana/api"
|
"github.com/dannyvankooten/ana/api"
|
||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
"github.com/gorilla/handlers"
|
"github.com/gorilla/handlers"
|
||||||
"github.com/joho/godotenv"
|
"github.com/joho/godotenv"
|
||||||
seed "github.com/dannyvankooten/ana/db"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
||||||
// load .env file
|
// load .env file
|
||||||
err := godotenv.Load()
|
err := godotenv.Load()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal("Error loading .env file")
|
log.Fatal("Error loading .env file")
|
||||||
}
|
}
|
||||||
|
|
||||||
db := core.SetupDatabaseConnection()
|
conn := db.SetupDatabaseConnection()
|
||||||
defer db.Close()
|
defer conn.Close()
|
||||||
|
|
||||||
// seed 1000 records
|
|
||||||
seed.Seed(100)
|
|
||||||
|
|
||||||
r := mux.NewRouter()
|
|
||||||
|
|
||||||
// register routes
|
// register routes
|
||||||
|
r := mux.NewRouter()
|
||||||
r.HandleFunc("/collect", api.CollectHandler).Methods("GET")
|
r.HandleFunc("/collect", api.CollectHandler).Methods("GET")
|
||||||
r.Handle("/api/session", api.Login).Methods("POST")
|
r.Handle("/api/session", api.Login).Methods("POST")
|
||||||
r.Handle("/api/session", api.Logout).Methods("DELETE")
|
r.Handle("/api/session", api.Logout).Methods("DELETE")
|
||||||
|
|
|
@ -2,7 +2,7 @@ package api
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"github.com/dannyvankooten/ana/core"
|
"github.com/dannyvankooten/ana/db"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -11,7 +11,7 @@ var GetBrowsersHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Re
|
||||||
before, after := getRequestedPeriods(r)
|
before, after := getRequestedPeriods(r)
|
||||||
|
|
||||||
// get total
|
// get total
|
||||||
stmt, err := core.DB.Prepare(`
|
stmt, err := db.Conn.Prepare(`
|
||||||
SELECT
|
SELECT
|
||||||
COUNT(DISTINCT(ip_address))
|
COUNT(DISTINCT(ip_address))
|
||||||
FROM visits
|
FROM visits
|
||||||
|
@ -23,7 +23,7 @@ var GetBrowsersHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Re
|
||||||
stmt.QueryRow(before, after).Scan(&total)
|
stmt.QueryRow(before, after).Scan(&total)
|
||||||
|
|
||||||
// get rows
|
// get rows
|
||||||
stmt, err = core.DB.Prepare(`
|
stmt, err = db.Conn.Prepare(`
|
||||||
SELECT
|
SELECT
|
||||||
browser_name,
|
browser_name,
|
||||||
COUNT(DISTINCT(ip_address)) AS count
|
COUNT(DISTINCT(ip_address)) AS count
|
||||||
|
|
|
@ -5,7 +5,7 @@ import (
|
||||||
"log"
|
"log"
|
||||||
"github.com/mssola/user_agent"
|
"github.com/mssola/user_agent"
|
||||||
"github.com/dannyvankooten/ana/models"
|
"github.com/dannyvankooten/ana/models"
|
||||||
"github.com/dannyvankooten/ana/core"
|
"github.com/dannyvankooten/ana/db"
|
||||||
)
|
)
|
||||||
|
|
||||||
func CollectHandler(w http.ResponseWriter, r *http.Request) {
|
func CollectHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
@ -17,7 +17,7 @@ func CollectHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// prepare statement for inserting data
|
// prepare statement for inserting data
|
||||||
stmt, err := core.DB.Prepare(`INSERT INTO visits(
|
stmt, err := db.Conn.Prepare(`INSERT INTO visits(
|
||||||
ip_address,
|
ip_address,
|
||||||
path,
|
path,
|
||||||
referrer_url,
|
referrer_url,
|
||||||
|
|
|
@ -2,7 +2,7 @@ package api
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"github.com/dannyvankooten/ana/core"
|
"github.com/dannyvankooten/ana/db"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -11,7 +11,7 @@ var GetCountriesHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.R
|
||||||
before, after := getRequestedPeriods(r)
|
before, after := getRequestedPeriods(r)
|
||||||
|
|
||||||
// get total
|
// get total
|
||||||
stmt, err := core.DB.Prepare(`
|
stmt, err := db.Conn.Prepare(`
|
||||||
SELECT
|
SELECT
|
||||||
COUNT(DISTINCT(ip_address))
|
COUNT(DISTINCT(ip_address))
|
||||||
FROM visits
|
FROM visits
|
||||||
|
@ -23,7 +23,7 @@ var GetCountriesHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.R
|
||||||
stmt.QueryRow(before, after).Scan(&total)
|
stmt.QueryRow(before, after).Scan(&total)
|
||||||
|
|
||||||
// get rows
|
// get rows
|
||||||
stmt, err = core.DB.Prepare(`
|
stmt, err = db.Conn.Prepare(`
|
||||||
SELECT
|
SELECT
|
||||||
country,
|
country,
|
||||||
COUNT(DISTINCT(ip_address)) AS count
|
COUNT(DISTINCT(ip_address)) AS count
|
||||||
|
|
|
@ -2,7 +2,7 @@ package api
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"github.com/dannyvankooten/ana/core"
|
"github.com/dannyvankooten/ana/db"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -10,7 +10,7 @@ import (
|
||||||
var GetLanguagesHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
var GetLanguagesHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
before, after := getRequestedPeriods(r)
|
before, after := getRequestedPeriods(r)
|
||||||
|
|
||||||
stmt, err := core.DB.Prepare(`
|
stmt, err := db.Conn.Prepare(`
|
||||||
SELECT
|
SELECT
|
||||||
COUNT(DISTINCT(ip_address))
|
COUNT(DISTINCT(ip_address))
|
||||||
FROM visits
|
FROM visits
|
||||||
|
@ -21,7 +21,7 @@ var GetLanguagesHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.R
|
||||||
var total float32
|
var total float32
|
||||||
stmt.QueryRow(before, after).Scan(&total)
|
stmt.QueryRow(before, after).Scan(&total)
|
||||||
|
|
||||||
stmt, err = core.DB.Prepare(`
|
stmt, err = db.Conn.Prepare(`
|
||||||
SELECT
|
SELECT
|
||||||
browser_language,
|
browser_language,
|
||||||
COUNT(DISTINCT(ip_address)) AS count
|
COUNT(DISTINCT(ip_address)) AS count
|
||||||
|
|
|
@ -3,7 +3,7 @@ package api
|
||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"github.com/dannyvankooten/ana/models"
|
"github.com/dannyvankooten/ana/models"
|
||||||
"github.com/dannyvankooten/ana/core"
|
"github.com/dannyvankooten/ana/db"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
"time"
|
"time"
|
||||||
|
@ -12,7 +12,7 @@ import (
|
||||||
// URL: /api/pageviews
|
// URL: /api/pageviews
|
||||||
var GetPageviewsHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
var GetPageviewsHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
before, after := getRequestedPeriods(r)
|
before, after := getRequestedPeriods(r)
|
||||||
stmt, err := core.DB.Prepare(`SELECT
|
stmt, err := db.Conn.Prepare(`SELECT
|
||||||
path,
|
path,
|
||||||
COUNT(ip_address) AS pageviews,
|
COUNT(ip_address) AS pageviews,
|
||||||
COUNT(DISTINCT(ip_address)) AS pageviews_unique
|
COUNT(DISTINCT(ip_address)) AS pageviews_unique
|
||||||
|
@ -47,7 +47,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 := core.DB.Prepare(`SELECT COUNT(*) FROM visits WHERE UNIX_TIMESTAMP(timestamp) <= ? AND UNIX_TIMESTAMP(timestamp) >= ?`)
|
stmt, err := db.Conn.Prepare(`SELECT COUNT(*) FROM visits WHERE UNIX_TIMESTAMP(timestamp) <= ? AND UNIX_TIMESTAMP(timestamp) >= ?`)
|
||||||
checkError(err)
|
checkError(err)
|
||||||
defer stmt.Close()
|
defer stmt.Close()
|
||||||
|
|
||||||
|
@ -67,7 +67,7 @@ var GetPageviewsPeriodCountHandler = http.HandlerFunc(func(w http.ResponseWriter
|
||||||
"month": "%Y-%m",
|
"month": "%Y-%m",
|
||||||
}
|
}
|
||||||
before, after := getRequestedPeriods(r)
|
before, after := getRequestedPeriods(r)
|
||||||
stmt, err := core.DB.Prepare(`SELECT
|
stmt, err := db.Conn.Prepare(`SELECT
|
||||||
COUNT(*) AS count, DATE_FORMAT(timestamp, ?) AS date_group
|
COUNT(*) AS count, DATE_FORMAT(timestamp, ?) AS date_group
|
||||||
FROM visits
|
FROM visits
|
||||||
WHERE UNIX_TIMESTAMP(timestamp) <= ? AND UNIX_TIMESTAMP(timestamp) >= ?
|
WHERE UNIX_TIMESTAMP(timestamp) <= ? AND UNIX_TIMESTAMP(timestamp) >= ?
|
||||||
|
|
|
@ -2,7 +2,7 @@ package api
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"github.com/dannyvankooten/ana/core"
|
"github.com/dannyvankooten/ana/db"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -11,7 +11,7 @@ var GetScreenResolutionsHandler = http.HandlerFunc(func(w http.ResponseWriter, r
|
||||||
before, after := getRequestedPeriods(r)
|
before, after := getRequestedPeriods(r)
|
||||||
|
|
||||||
// get total
|
// get total
|
||||||
stmt, err := core.DB.Prepare(`
|
stmt, err := db.Conn.Prepare(`
|
||||||
SELECT
|
SELECT
|
||||||
COUNT(DISTINCT(ip_address))
|
COUNT(DISTINCT(ip_address))
|
||||||
FROM visits
|
FROM visits
|
||||||
|
@ -22,7 +22,7 @@ var GetScreenResolutionsHandler = http.HandlerFunc(func(w http.ResponseWriter, r
|
||||||
stmt.QueryRow(before, after).Scan(&total)
|
stmt.QueryRow(before, after).Scan(&total)
|
||||||
|
|
||||||
// get rows
|
// get rows
|
||||||
stmt, err = core.DB.Prepare(`
|
stmt, err = db.Conn.Prepare(`
|
||||||
SELECT
|
SELECT
|
||||||
screen_resolution,
|
screen_resolution,
|
||||||
COUNT(DISTINCT(ip_address)) AS count
|
COUNT(DISTINCT(ip_address)) AS count
|
||||||
|
|
|
@ -3,7 +3,7 @@ package api
|
||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"github.com/dannyvankooten/ana/models"
|
"github.com/dannyvankooten/ana/models"
|
||||||
"github.com/dannyvankooten/ana/core"
|
"github.com/dannyvankooten/ana/db"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
"time"
|
"time"
|
||||||
|
@ -11,7 +11,7 @@ import (
|
||||||
|
|
||||||
// URL: /api/visits
|
// URL: /api/visits
|
||||||
var GetVisitsHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
var GetVisitsHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
stmt, err := core.DB.Prepare(`SELECT
|
stmt, err := db.Conn.Prepare(`SELECT
|
||||||
id,
|
id,
|
||||||
COALESCE(browser_name, '') AS browser_name,
|
COALESCE(browser_name, '') AS browser_name,
|
||||||
COALESCE(browser_language, '') AS browser_language,
|
COALESCE(browser_language, '') AS browser_language,
|
||||||
|
@ -50,7 +50,7 @@ var GetVisitsHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Requ
|
||||||
// URL: /api/visits/count
|
// URL: /api/visits/count
|
||||||
var GetVisitsCountHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
var GetVisitsCountHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
before, after := getRequestedPeriods(r)
|
before, after := getRequestedPeriods(r)
|
||||||
stmt, err := core.DB.Prepare(`SELECT COUNT(DISTINCT(ip_address)) FROM visits WHERE UNIX_TIMESTAMP(timestamp) <= ? AND UNIX_TIMESTAMP(timestamp) >= ?`)
|
stmt, err := db.Conn.Prepare(`SELECT COUNT(DISTINCT(ip_address)) FROM visits WHERE UNIX_TIMESTAMP(timestamp) <= ? AND UNIX_TIMESTAMP(timestamp) >= ?`)
|
||||||
|
|
||||||
checkError(err)
|
checkError(err)
|
||||||
defer stmt.Close()
|
defer stmt.Close()
|
||||||
|
@ -65,7 +65,7 @@ var GetVisitsCountHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http
|
||||||
// URL: /api/visits/count/realtime
|
// URL: /api/visits/count/realtime
|
||||||
var GetVisitsRealtimeCountHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
var GetVisitsRealtimeCountHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
var result int
|
var result int
|
||||||
core.DB.QueryRow(`SELECT COUNT(DISTINCT(ip_address)) FROM visits WHERE timestamp >= DATE_SUB(CURRENT_TIMESTAMP, INTERVAL 3 HOUR_MINUTE) AND timestamp <= CURRENT_TIMESTAMP`).Scan(&result)
|
db.Conn.QueryRow(`SELECT COUNT(DISTINCT(ip_address)) FROM visits WHERE timestamp >= DATE_SUB(CURRENT_TIMESTAMP, INTERVAL 3 HOUR_MINUTE) AND timestamp <= CURRENT_TIMESTAMP`).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)
|
||||||
|
@ -80,7 +80,7 @@ var GetVisitsPeriodCountHandler = http.HandlerFunc(func(w http.ResponseWriter, r
|
||||||
"month": "%Y-%m",
|
"month": "%Y-%m",
|
||||||
}
|
}
|
||||||
|
|
||||||
stmt, err := core.DB.Prepare(`SELECT
|
stmt, err := db.Conn.Prepare(`SELECT
|
||||||
COUNT(DISTINCT(ip_address)) AS count, DATE_FORMAT(timestamp, ?) AS date_group
|
COUNT(DISTINCT(ip_address)) AS count, DATE_FORMAT(timestamp, ?) AS date_group
|
||||||
FROM visits
|
FROM visits
|
||||||
WHERE UNIX_TIMESTAMP(timestamp) <= ? AND UNIX_TIMESTAMP(timestamp) >= ?
|
WHERE UNIX_TIMESTAMP(timestamp) <= ? AND UNIX_TIMESTAMP(timestamp) >= ?
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
package core
|
package db
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
|
@ -8,22 +8,22 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
)
|
)
|
||||||
|
|
||||||
var DB *sql.DB
|
var Conn *sql.DB
|
||||||
|
|
||||||
func SetupDatabaseConnection() *sql.DB {
|
func SetupDatabaseConnection() *sql.DB {
|
||||||
var err error
|
var err error
|
||||||
var dataSourceName = fmt.Sprintf("%s:%s@%s/%s", os.Getenv("ANA_DATABASE_USER"), os.Getenv("ANA_DATABASE_PASSWORD"), os.Getenv("ANA_DATABASE_HOST"), os.Getenv("ANA_DATABASE_NAME"))
|
var dataSourceName = fmt.Sprintf("%s:%s@%s/%s", os.Getenv("ANA_DATABASE_USER"), os.Getenv("ANA_DATABASE_PASSWORD"), os.Getenv("ANA_DATABASE_HOST"), os.Getenv("ANA_DATABASE_NAME"))
|
||||||
|
|
||||||
DB, err = sql.Open("mysql", dataSourceName)
|
Conn, err = sql.Open("mysql", dataSourceName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err.Error()) // Just for example purpose. You should use proper error handling instead of panic
|
log.Fatal(err.Error()) // Just for example purpose. You should use proper error handling instead of panic
|
||||||
}
|
}
|
||||||
|
|
||||||
// Open doesn't open a connection. Validate DSN data:
|
// Open doesn't open a connection. Validate DSN data:
|
||||||
err = DB.Ping()
|
err = Conn.Ping()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err.Error()) // proper error handling instead of panic in your app
|
log.Fatal(err.Error()) // proper error handling instead of panic in your app
|
||||||
}
|
}
|
||||||
|
|
||||||
return DB
|
return Conn
|
||||||
}
|
}
|
|
@ -1,7 +1,6 @@
|
||||||
package db
|
package db
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/dannyvankooten/ana/core"
|
|
||||||
"github.com/dannyvankooten/ana/models"
|
"github.com/dannyvankooten/ana/models"
|
||||||
"log"
|
"log"
|
||||||
"time"
|
"time"
|
||||||
|
@ -61,7 +60,7 @@ var screenResolutions = []string {
|
||||||
func Seed(n int) {
|
func Seed(n int) {
|
||||||
|
|
||||||
// prepare statement for inserting data
|
// prepare statement for inserting data
|
||||||
stmt, err := core.DB.Prepare(`INSERT INTO visits(
|
stmt, err := Conn.Prepare(`INSERT INTO visits(
|
||||||
browser_language,
|
browser_language,
|
||||||
browser_name,
|
browser_name,
|
||||||
browser_version,
|
browser_version,
|
||||||
|
|
Loading…
Reference in New Issue