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'),
|
||||
m=d.getElementsByTagName('script')[0];
|
||||
a.async=1;
|
||||
a.src=u;
|
||||
a.async=1; a.src=u;
|
||||
m.parentNode.insertBefore(a,m)
|
||||
})(document, window, '//ana.dev/tracker.js', 'ana');
|
||||
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?
|
||||
|
||||
- Replace chart with custom D3 chart.
|
||||
- Allow setting custom limit in table overviews.
|
||||
- Return 0px GIF in /collect endpoint
|
||||
- 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.
|
||||
- Choose a OS license & settle on name.
|
||||
- JS client for consuming API endpoints.
|
||||
- Envelope API responses.
|
||||
- Envelope API responses & perhaps return total in table overview?
|
||||
- Track canonical URL's.
|
||||
- Show referrals.
|
||||
- Country flags.
|
||||
- Geolocate unknown IP addresses periodically.
|
||||
- Mask last part of IP address.
|
||||
|
||||
|
|
14
ana.go
14
ana.go
|
@ -4,31 +4,25 @@ import (
|
|||
"net/http"
|
||||
"os"
|
||||
"log"
|
||||
"github.com/dannyvankooten/ana/core"
|
||||
"github.com/dannyvankooten/ana/db"
|
||||
"github.com/dannyvankooten/ana/api"
|
||||
"github.com/gorilla/mux"
|
||||
"github.com/gorilla/handlers"
|
||||
"github.com/joho/godotenv"
|
||||
seed "github.com/dannyvankooten/ana/db"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
||||
// load .env file
|
||||
err := godotenv.Load()
|
||||
if err != nil {
|
||||
log.Fatal("Error loading .env file")
|
||||
}
|
||||
|
||||
db := core.SetupDatabaseConnection()
|
||||
defer db.Close()
|
||||
|
||||
// seed 1000 records
|
||||
seed.Seed(100)
|
||||
|
||||
r := mux.NewRouter()
|
||||
conn := db.SetupDatabaseConnection()
|
||||
defer conn.Close()
|
||||
|
||||
// register routes
|
||||
r := mux.NewRouter()
|
||||
r.HandleFunc("/collect", api.CollectHandler).Methods("GET")
|
||||
r.Handle("/api/session", api.Login).Methods("POST")
|
||||
r.Handle("/api/session", api.Logout).Methods("DELETE")
|
||||
|
|
|
@ -2,7 +2,7 @@ package api
|
|||
|
||||
import (
|
||||
"net/http"
|
||||
"github.com/dannyvankooten/ana/core"
|
||||
"github.com/dannyvankooten/ana/db"
|
||||
"encoding/json"
|
||||
)
|
||||
|
||||
|
@ -11,7 +11,7 @@ var GetBrowsersHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Re
|
|||
before, after := getRequestedPeriods(r)
|
||||
|
||||
// get total
|
||||
stmt, err := core.DB.Prepare(`
|
||||
stmt, err := db.Conn.Prepare(`
|
||||
SELECT
|
||||
COUNT(DISTINCT(ip_address))
|
||||
FROM visits
|
||||
|
@ -23,7 +23,7 @@ var GetBrowsersHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Re
|
|||
stmt.QueryRow(before, after).Scan(&total)
|
||||
|
||||
// get rows
|
||||
stmt, err = core.DB.Prepare(`
|
||||
stmt, err = db.Conn.Prepare(`
|
||||
SELECT
|
||||
browser_name,
|
||||
COUNT(DISTINCT(ip_address)) AS count
|
||||
|
|
|
@ -5,7 +5,7 @@ import (
|
|||
"log"
|
||||
"github.com/mssola/user_agent"
|
||||
"github.com/dannyvankooten/ana/models"
|
||||
"github.com/dannyvankooten/ana/core"
|
||||
"github.com/dannyvankooten/ana/db"
|
||||
)
|
||||
|
||||
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
|
||||
stmt, err := core.DB.Prepare(`INSERT INTO visits(
|
||||
stmt, err := db.Conn.Prepare(`INSERT INTO visits(
|
||||
ip_address,
|
||||
path,
|
||||
referrer_url,
|
||||
|
|
|
@ -2,7 +2,7 @@ package api
|
|||
|
||||
import (
|
||||
"net/http"
|
||||
"github.com/dannyvankooten/ana/core"
|
||||
"github.com/dannyvankooten/ana/db"
|
||||
"encoding/json"
|
||||
)
|
||||
|
||||
|
@ -11,7 +11,7 @@ var GetCountriesHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.R
|
|||
before, after := getRequestedPeriods(r)
|
||||
|
||||
// get total
|
||||
stmt, err := core.DB.Prepare(`
|
||||
stmt, err := db.Conn.Prepare(`
|
||||
SELECT
|
||||
COUNT(DISTINCT(ip_address))
|
||||
FROM visits
|
||||
|
@ -23,7 +23,7 @@ var GetCountriesHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.R
|
|||
stmt.QueryRow(before, after).Scan(&total)
|
||||
|
||||
// get rows
|
||||
stmt, err = core.DB.Prepare(`
|
||||
stmt, err = db.Conn.Prepare(`
|
||||
SELECT
|
||||
country,
|
||||
COUNT(DISTINCT(ip_address)) AS count
|
||||
|
|
|
@ -2,7 +2,7 @@ package api
|
|||
|
||||
import (
|
||||
"net/http"
|
||||
"github.com/dannyvankooten/ana/core"
|
||||
"github.com/dannyvankooten/ana/db"
|
||||
"encoding/json"
|
||||
)
|
||||
|
||||
|
@ -10,7 +10,7 @@ import (
|
|||
var GetLanguagesHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
before, after := getRequestedPeriods(r)
|
||||
|
||||
stmt, err := core.DB.Prepare(`
|
||||
stmt, err := db.Conn.Prepare(`
|
||||
SELECT
|
||||
COUNT(DISTINCT(ip_address))
|
||||
FROM visits
|
||||
|
@ -21,7 +21,7 @@ var GetLanguagesHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.R
|
|||
var total float32
|
||||
stmt.QueryRow(before, after).Scan(&total)
|
||||
|
||||
stmt, err = core.DB.Prepare(`
|
||||
stmt, err = db.Conn.Prepare(`
|
||||
SELECT
|
||||
browser_language,
|
||||
COUNT(DISTINCT(ip_address)) AS count
|
||||
|
|
|
@ -3,7 +3,7 @@ package api
|
|||
import (
|
||||
"net/http"
|
||||
"github.com/dannyvankooten/ana/models"
|
||||
"github.com/dannyvankooten/ana/core"
|
||||
"github.com/dannyvankooten/ana/db"
|
||||
"encoding/json"
|
||||
"github.com/gorilla/mux"
|
||||
"time"
|
||||
|
@ -12,7 +12,7 @@ import (
|
|||
// URL: /api/pageviews
|
||||
var GetPageviewsHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
before, after := getRequestedPeriods(r)
|
||||
stmt, err := core.DB.Prepare(`SELECT
|
||||
stmt, err := db.Conn.Prepare(`SELECT
|
||||
path,
|
||||
COUNT(ip_address) AS pageviews,
|
||||
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
|
||||
var GetPageviewsCountHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
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)
|
||||
defer stmt.Close()
|
||||
|
||||
|
@ -67,7 +67,7 @@ var GetPageviewsPeriodCountHandler = http.HandlerFunc(func(w http.ResponseWriter
|
|||
"month": "%Y-%m",
|
||||
}
|
||||
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
|
||||
FROM visits
|
||||
WHERE UNIX_TIMESTAMP(timestamp) <= ? AND UNIX_TIMESTAMP(timestamp) >= ?
|
||||
|
|
|
@ -2,7 +2,7 @@ package api
|
|||
|
||||
import (
|
||||
"net/http"
|
||||
"github.com/dannyvankooten/ana/core"
|
||||
"github.com/dannyvankooten/ana/db"
|
||||
"encoding/json"
|
||||
)
|
||||
|
||||
|
@ -11,7 +11,7 @@ var GetScreenResolutionsHandler = http.HandlerFunc(func(w http.ResponseWriter, r
|
|||
before, after := getRequestedPeriods(r)
|
||||
|
||||
// get total
|
||||
stmt, err := core.DB.Prepare(`
|
||||
stmt, err := db.Conn.Prepare(`
|
||||
SELECT
|
||||
COUNT(DISTINCT(ip_address))
|
||||
FROM visits
|
||||
|
@ -22,7 +22,7 @@ var GetScreenResolutionsHandler = http.HandlerFunc(func(w http.ResponseWriter, r
|
|||
stmt.QueryRow(before, after).Scan(&total)
|
||||
|
||||
// get rows
|
||||
stmt, err = core.DB.Prepare(`
|
||||
stmt, err = db.Conn.Prepare(`
|
||||
SELECT
|
||||
screen_resolution,
|
||||
COUNT(DISTINCT(ip_address)) AS count
|
||||
|
|
|
@ -3,7 +3,7 @@ package api
|
|||
import (
|
||||
"net/http"
|
||||
"github.com/dannyvankooten/ana/models"
|
||||
"github.com/dannyvankooten/ana/core"
|
||||
"github.com/dannyvankooten/ana/db"
|
||||
"encoding/json"
|
||||
"github.com/gorilla/mux"
|
||||
"time"
|
||||
|
@ -11,7 +11,7 @@ import (
|
|||
|
||||
// URL: /api/visits
|
||||
var GetVisitsHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
stmt, err := core.DB.Prepare(`SELECT
|
||||
stmt, err := db.Conn.Prepare(`SELECT
|
||||
id,
|
||||
COALESCE(browser_name, '') AS browser_name,
|
||||
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
|
||||
var GetVisitsCountHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
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)
|
||||
defer stmt.Close()
|
||||
|
@ -65,7 +65,7 @@ var GetVisitsCountHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http
|
|||
// URL: /api/visits/count/realtime
|
||||
var GetVisitsRealtimeCountHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
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")
|
||||
json.NewEncoder(w).Encode(result)
|
||||
|
@ -80,7 +80,7 @@ var GetVisitsPeriodCountHandler = http.HandlerFunc(func(w http.ResponseWriter, r
|
|||
"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
|
||||
FROM visits
|
||||
WHERE UNIX_TIMESTAMP(timestamp) <= ? AND UNIX_TIMESTAMP(timestamp) >= ?
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
package core
|
||||
package db
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
|
@ -8,22 +8,22 @@ import (
|
|||
"fmt"
|
||||
)
|
||||
|
||||
var DB *sql.DB
|
||||
var Conn *sql.DB
|
||||
|
||||
func SetupDatabaseConnection() *sql.DB {
|
||||
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"))
|
||||
|
||||
DB, err = sql.Open("mysql", dataSourceName)
|
||||
Conn, err = sql.Open("mysql", dataSourceName)
|
||||
if err != nil {
|
||||
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:
|
||||
err = DB.Ping()
|
||||
err = Conn.Ping()
|
||||
if err != nil {
|
||||
log.Fatal(err.Error()) // proper error handling instead of panic in your app
|
||||
}
|
||||
|
||||
return DB
|
||||
return Conn
|
||||
}
|
|
@ -1,7 +1,6 @@
|
|||
package db
|
||||
|
||||
import (
|
||||
"github.com/dannyvankooten/ana/core"
|
||||
"github.com/dannyvankooten/ana/models"
|
||||
"log"
|
||||
"time"
|
||||
|
@ -61,7 +60,7 @@ var screenResolutions = []string {
|
|||
func Seed(n int) {
|
||||
|
||||
// prepare statement for inserting data
|
||||
stmt, err := core.DB.Prepare(`INSERT INTO visits(
|
||||
stmt, err := Conn.Prepare(`INSERT INTO visits(
|
||||
browser_language,
|
||||
browser_name,
|
||||
browser_version,
|
||||
|
|
Loading…
Reference in New Issue