Skip to content
This repository was archived by the owner on Mar 4, 2025. It is now read-only.

Commit 81a2b41

Browse files
committed
common, database, webui: Record web logins in the backend
With this, we should be able to get a better idea of how many web logins occur over a given time period. Combining that with the similar info for our other daemons should let us figure out how many Monthly Active Users we have.
1 parent f3d84f8 commit 81a2b41

File tree

5 files changed

+55
-8
lines changed

5 files changed

+55
-8
lines changed

common/postgresql.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5326,3 +5326,25 @@ func VisualisationSaveParams(dbOwner, dbName, visName string, visParams VisParam
53265326
}
53275327
return
53285328
}
5329+
5330+
// RecordWebLogin records the start time of a user login session, for stats purposes
5331+
func RecordWebLogin(userName string) (err error) {
5332+
// Add the new user to the database
5333+
dbQuery := `
5334+
WITH u AS (
5335+
SELECT user_id
5336+
FROM users
5337+
WHERE lower(user_name) = lower($1)
5338+
)
5339+
INSERT INTO webui_logins (user_id)
5340+
SELECT (SELECT user_id FROM u)`
5341+
commandTag, err := pdb.Exec(context.Background(), dbQuery, userName)
5342+
if err != nil {
5343+
return
5344+
}
5345+
if numRows := commandTag.RowsAffected(); numRows != 1 {
5346+
err = errors.New(fmt.Sprintf("Wrong number of rows (%d) affected while adding a webUI login record for '%s' to the database",
5347+
numRows, SanitiseLogString(userName)))
5348+
}
5349+
return
5350+
}
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
BEGIN;
2-
DROP TRIGGER track_applied_migrations ON schema_migrations;
3-
DROP FUNCTION track_applied_migration();
4-
DROP TABLE schema_migrations_history;
2+
DROP TRIGGER IF EXISTS track_applied_migrations ON schema_migrations;
3+
DROP FUNCTION IF EXISTS track_applied_migration();
4+
DROP TABLE IF EXISTS schema_migrations_history;
55
COMMIT;
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
BEGIN;
2+
DROP TABLE IF EXISTS public.webui_logins;
3+
DROP INDEX IF EXISTS webui_logins_user_id_index;
4+
COMMIT;
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
BEGIN;
2+
3+
CREATE TABLE IF NOT EXISTS public.webui_logins
4+
(
5+
id bigserial,
6+
user_id bigint
7+
constraint webui_logins_users_user_id_fk
8+
references public.users (user_id),
9+
login_date timestamptz default now() not null
10+
);
11+
12+
CREATE INDEX IF NOT EXISTS webui_logins_user_id_index
13+
on public.webui_logins (user_id);
14+
15+
COMMIT;

webui/main.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"context"
45
"crypto/tls"
56
"encoding/csv"
67
"encoding/json"
@@ -108,20 +109,20 @@ func auth0CallbackHandler(w http.ResponseWriter, r *http.Request) {
108109
}
109110
code := r.URL.Query().Get("code")
110111
if code == "" {
111-
log.Printf("Login failure from '%v', probably due to blocked 3rd party cookies", r.RemoteAddr)
112+
log.Printf("Login failure from '%s', probably due to blocked 3rd party cookies", r.RemoteAddr)
112113
errorPage(w, r, http.StatusInternalServerError,
113114
"Login failure. Please allow 3rd party cookies from https://dbhub.eu.auth0.com then try again (it should then work).")
114115
return
115116
}
116-
token, err := conf.Exchange(oauth2.NoContext, code)
117+
token, err := conf.Exchange(context.Background(), code)
117118
if err != nil {
118119
log.Printf("Login failure: %s", err.Error())
119120
errorPage(w, r, http.StatusInternalServerError, "Login failed")
120121
return
121122
}
122123

123124
// Retrieve the user info (JSON format)
124-
conn := conf.Client(oauth2.NoContext, token)
125+
conn := conf.Client(context.Background(), token)
125126
userInfo, err := conn.Get("https://" + com.Conf.Auth0.Domain + "/userinfo")
126127
if err != nil {
127128
errorPage(w, r, http.StatusInternalServerError, err.Error())
@@ -170,7 +171,7 @@ func auth0CallbackHandler(w http.ResponseWriter, r *http.Request) {
170171
if ok {
171172
auth0Conn = co.(string)
172173
}
173-
if auth0Conn != "Test2DB" { // The Auth0 fallback profile pic's seem pretty lousy, so avoid those
174+
if auth0Conn != "Test2DB" { // The Auth0 fallback profile pics seem pretty lousy, so avoid those
174175
p, ok := profile["picture"]
175176
if ok && p.(string) != "" {
176177
avatarURL = p.(string)
@@ -275,7 +276,12 @@ func auth0CallbackHandler(w http.ResponseWriter, r *http.Request) {
275276
return
276277
}
277278

278-
// Login completed, so bounce to the users' profile page
279+
// Login completed, so record it and bounce them to their profile page
280+
err = com.RecordWebLogin(userName)
281+
if err != nil {
282+
// Although something went wrong here, lets just log it to our backend for admin follow up
283+
log.Println(err)
284+
}
279285
http.Redirect(w, r, "/"+userName, http.StatusSeeOther)
280286
}
281287

0 commit comments

Comments
 (0)