From 0edf87941ac191ac6f070d571f4a4f476fc11740 Mon Sep 17 00:00:00 2001 From: Mike Cao Date: Mon, 20 Jul 2020 19:24:33 -0700 Subject: [PATCH] Minor tweaks. --- lib/db.js | 4 +++- lib/session.js | 6 +++--- public/umami.js | 2 +- rollup.config.js | 10 +--------- scripts/umami/index.js | 15 ++++++++------- sql/schema.postgresql.sql | 9 ++++++++- 6 files changed, 24 insertions(+), 22 deletions(-) diff --git a/lib/db.js b/lib/db.js index 7abbe9df..2fdc49bf 100644 --- a/lib/db.js +++ b/lib/db.js @@ -1,6 +1,8 @@ import { PrismaClient } from '@prisma/client'; -const prisma = new PrismaClient(); +const prisma = new PrismaClient({ + log: [process.env.NODE_ENV !== 'production' && 'query'], +}); export async function runQuery(query) { return query diff --git a/lib/session.js b/lib/session.js index daafffd5..d7f08cbe 100644 --- a/lib/session.js +++ b/lib/session.js @@ -1,7 +1,7 @@ import { getWebsite, getSession, createSession } from 'lib/db'; import { getCountry, getDevice, getIpAddress, hash, isValidSession } from 'lib/utils'; -export default async function checkSession(req) { +export default async req => { const { payload } = req.body; const { session } = payload; @@ -19,7 +19,7 @@ export default async function checkSession(req) { if (website) { const { website_id } = website; - const session_uuid = hash(`${website_id}${hostname}${ip}${userAgent}${os}`); + const session_uuid = hash(website_id, hostname, ip, userAgent, os); let session = await getSession(session_uuid); @@ -46,4 +46,4 @@ export default async function checkSession(req) { ].join(':'); } } -} +}; diff --git a/public/umami.js b/public/umami.js index d18ffc28..5e4f674c 100644 --- a/public/umami.js +++ b/public/umami.js @@ -1 +1 @@ -!function(){"use strict";function e(e){var t=this.constructor;return this.then((function(n){return t.resolve(e()).then((function(){return n}))}),(function(n){return t.resolve(e()).then((function(){return t.reject(n)}))}))}var t=setTimeout;function n(e){return Boolean(e&&void 0!==e.length)}function r(){}function o(e){if(!(this instanceof o))throw new TypeError("Promises must be constructed via new");if("function"!=typeof e)throw new TypeError("not a function");this._state=0,this._handled=!1,this._value=void 0,this._deferreds=[],f(e,this)}function i(e,t){for(;3===e._state;)e=e._value;0!==e._state?(e._handled=!0,o._immediateFn((function(){var n=1===e._state?t.onFulfilled:t.onRejected;if(null!==n){var r;try{r=n(e._value)}catch(e){return void s(t.promise,e)}u(t.promise,r)}else(1===e._state?u:s)(t.promise,e._value)}))):e._deferreds.push(t)}function u(e,t){try{if(t===e)throw new TypeError("A promise cannot be resolved with itself.");if(t&&("object"==typeof t||"function"==typeof t)){var n=t.then;if(t instanceof o)return e._state=3,e._value=t,void a(e);if("function"==typeof n)return void f((r=n,i=t,function(){r.apply(i,arguments)}),e)}e._state=1,e._value=t,a(e)}catch(t){s(e,t)}var r,i}function s(e,t){e._state=2,e._value=t,a(e)}function a(e){2===e._state&&0===e._deferreds.length&&o._immediateFn((function(){e._handled||o._unhandledRejectionFn(e._value)}));for(var t=0,n=e._deferreds.length;t { const args = [state, title, url]; + cb.apply(null, args); + return orig.apply(history, args); }; }; @@ -85,7 +87,7 @@ import 'unfetch/polyfill'; const removeEvents = () => { listeners.forEach(([element, type, listener]) => { - element.removeEventListener(type, listener, true); + element && element.removeEventListener(type, listener, true); }); listeners.length = 0; }; @@ -93,13 +95,12 @@ import 'unfetch/polyfill'; const loadEvents = () => { document.querySelectorAll("[class*='umami--']").forEach(element => { element.className.split(' ').forEach(className => { - if (/^umami--/.test(className)) { + if (/^umami--([a-z]+)--([a-z0-9_]+[a-z0-9-_]+)$/.test(className)) { const [, type, value] = className.split('--'); - if (type && value) { - const listener = () => pageEvent(type, value); - listeners.push([element, type, listener]); - element.addEventListener(type, listener, true); - } + const listener = () => pageEvent(type, value); + + listeners.push([element, type, listener]); + element.addEventListener(type, listener, true); } }); }); diff --git a/sql/schema.postgresql.sql b/sql/schema.postgresql.sql index 8076c1fa..e3c740bf 100644 --- a/sql/schema.postgresql.sql +++ b/sql/schema.postgresql.sql @@ -38,5 +38,12 @@ create table event ( ); create index on session(created_at); +create index on session(website_id); + create index on pageview(created_at); -create index on event(created_at); \ No newline at end of file +create index on pageview(website_id); +create index on pageview(session_id); + +create index on event(created_at); +create index on event(website_id); +create index on event(session_id); \ No newline at end of file