Merge branch 'master' into feature/add-docker

This commit is contained in:
Mike Cao 2020-08-20 22:58:04 -07:00 committed by GitHub
commit c17cd6f390
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
26 changed files with 148 additions and 93 deletions

View File

@ -1,7 +1,18 @@
# umami # umami
Umami is a simple, fast, website analytics alternative to Google Analytics.
## Getting started
A detailed getting started guide can be found at [https://umami.is/docs/](https://umami.is/docs/)
## Installation from source ## Installation from source
### Requirements
- A server with Node.js 10.13 or newer
- A database (MySQL or Postgresql)
### Get the source code ### Get the source code
``` ```
@ -37,6 +48,8 @@ For Postgresql:
psql -h hostname -U username -d databasename -f sql/schema.postgresql.sql psql -h hostname -U username -d databasename -f sql/schema.postgresql.sql
``` ```
This will also create a login account with username **admin** and password **umami**.
### Configure umami ### Configure umami
Create an `.env` file with the following Create an `.env` file with the following

View File

@ -1,12 +1,9 @@
import React from 'react'; import React from 'react';
import { useSpring, animated } from 'react-spring'; import { useSpring, animated } from 'react-spring';
import { formatNumber } from '../../lib/format';
import styles from './MetricCard.module.css'; import styles from './MetricCard.module.css';
function defaultFormat(n) { const MetricCard = ({ value = 0, label, format = formatNumber }) => {
return Number(n).toFixed(0);
}
const MetricCard = ({ value = 0, label, format = defaultFormat }) => {
const props = useSpring({ x: value, from: { x: 0 } }); const props = useSpring({ x: value, from: { x: 0 } });
return ( return (

View File

@ -2,7 +2,7 @@
display: flex; display: flex;
flex-direction: column; flex-direction: column;
justify-content: center; justify-content: center;
width: 140px; min-width: 140px;
} }
.value { .value {

View File

@ -2,13 +2,16 @@ import React, { useState, useEffect } from 'react';
import classNames from 'classnames'; import classNames from 'classnames';
import MetricCard from './MetricCard'; import MetricCard from './MetricCard';
import { get } from 'lib/web'; import { get } from 'lib/web';
import { formatShortTime } from 'lib/format'; import { formatShortTime, formatNumber, formatLongNumber } from 'lib/format';
import styles from './MetricsBar.module.css'; import styles from './MetricsBar.module.css';
export default function MetricsBar({ websiteId, startDate, endDate, className }) { export default function MetricsBar({ websiteId, startDate, endDate, className }) {
const [data, setData] = useState({}); const [data, setData] = useState({});
const [format, setFormat] = useState(true);
const { pageviews, uniques, bounces, totaltime } = data; const { pageviews, uniques, bounces, totaltime } = data;
const formatFunc = format ? formatLongNumber : formatNumber;
async function loadData() { async function loadData() {
setData( setData(
await get(`/api/website/${websiteId}/metrics`, { await get(`/api/website/${websiteId}/metrics`, {
@ -18,14 +21,18 @@ export default function MetricsBar({ websiteId, startDate, endDate, className })
); );
} }
function handleSetFormat() {
setFormat(state => !state);
}
useEffect(() => { useEffect(() => {
loadData(); loadData();
}, [websiteId, startDate, endDate]); }, [websiteId, startDate, endDate]);
return ( return (
<div className={classNames(styles.container, className)}> <div className={classNames(styles.bar, className)} onClick={handleSetFormat}>
<MetricCard label="Views" value={pageviews} /> <MetricCard label="Views" value={pageviews} format={formatFunc} />
<MetricCard label="Visitors" value={uniques} /> <MetricCard label="Visitors" value={uniques} format={formatFunc} />
<MetricCard <MetricCard
label="Bounce rate" label="Bounce rate"
value={uniques ? (bounces / uniques) * 100 : 0} value={uniques ? (bounces / uniques) * 100 : 0}

View File

@ -1,8 +1,9 @@
.container { .bar {
display: flex; display: flex;
cursor: pointer;
} }
@media only screen and (max-width: 1000px) { @media only screen and (max-width: 992px) {
.container > div:last-child { .container > div:last-child {
display: none; display: none;
} }

View File

@ -2,11 +2,11 @@ import React, { useState, useEffect, useMemo } from 'react';
import { FixedSizeList } from 'react-window'; import { FixedSizeList } from 'react-window';
import { useSpring, animated, config } from 'react-spring'; import { useSpring, animated, config } from 'react-spring';
import classNames from 'classnames'; import classNames from 'classnames';
import CheckVisible from 'components/helpers/CheckVisible';
import Button from 'components/common/Button'; import Button from 'components/common/Button';
import Arrow from 'assets/arrow-right.svg'; import Arrow from 'assets/arrow-right.svg';
import { get } from 'lib/web'; import { get } from 'lib/web';
import { percentFilter } from 'lib/filters'; import { percentFilter } from 'lib/filters';
import { formatNumber, formatLongNumber } from 'lib/format';
import styles from './RankingsChart.module.css'; import styles from './RankingsChart.module.css';
export default function RankingsChart({ export default function RankingsChart({
@ -23,6 +23,8 @@ export default function RankingsChart({
onExpand = () => {}, onExpand = () => {},
}) { }) {
const [data, setData] = useState(); const [data, setData] = useState();
const [format, setFormat] = useState(true);
const formatFunc = format ? formatLongNumber : formatNumber;
const rankings = useMemo(() => { const rankings = useMemo(() => {
if (data) { if (data) {
@ -48,6 +50,19 @@ export default function RankingsChart({
onDataLoad(updated); onDataLoad(updated);
} }
function handleSetFormat() {
setFormat(state => !state);
}
const Row = ({ index, style }) => {
const { x, y, z } = rankings[index];
return (
<div style={style}>
<AnimatedRow key={x} label={x} value={y} percent={z} animate={limit} format={formatFunc} />
</div>
);
};
useEffect(() => { useEffect(() => {
if (websiteId) { if (websiteId) {
loadData(); loadData();
@ -58,25 +73,23 @@ export default function RankingsChart({
return null; return null;
} }
const Row = ({ index, style }) => {
const { x, y, z } = rankings[index];
return (
<div style={style}>
<AnimatedRow key={x} label={x} value={y} percent={z} animate={limit} />
</div>
);
};
return ( return (
<div className={classNames(styles.container, className)}> <div className={classNames(styles.container, className)}>
<div className={styles.header}> <div className={styles.header} onClick={handleSetFormat}>
<div className={styles.title}>{title}</div> <div className={styles.title}>{title}</div>
<div className={styles.heading}>{heading}</div> <div className={styles.heading}>{heading}</div>
</div> </div>
<div className={styles.body}> <div className={styles.body}>
{limit ? ( {limit ? (
rankings.map(({ x, y, z }) => ( rankings.map(({ x, y, z }) => (
<AnimatedRow key={x} label={x} value={y} percent={z} animate={limit} /> <AnimatedRow
key={x}
label={x}
value={y}
percent={z}
animate={limit}
format={formatFunc}
/>
)) ))
) : ( ) : (
<FixedSizeList height={600} itemCount={rankings.length} itemSize={30}> <FixedSizeList height={600} itemCount={rankings.length} itemSize={30}>
@ -95,7 +108,7 @@ export default function RankingsChart({
); );
} }
const AnimatedRow = ({ label, value, percent, animate }) => { const AnimatedRow = ({ label, value = 0, percent, animate, format }) => {
const props = useSpring({ const props = useSpring({
width: percent, width: percent,
y: value, y: value,
@ -106,7 +119,7 @@ const AnimatedRow = ({ label, value, percent, animate }) => {
return ( return (
<div className={styles.row}> <div className={styles.row}>
<div className={styles.label}>{label}</div> <div className={styles.label}>{label}</div>
<animated.div className={styles.value}>{props.y.interpolate(n => n.toFixed(0))}</animated.div> <animated.div className={styles.value}>{props.y?.interpolate(format)}</animated.div>
<div className={styles.percent}> <div className={styles.percent}>
<animated.div <animated.div
className={styles.bar} className={styles.bar}

View File

@ -10,6 +10,7 @@
.header { .header {
display: flex; display: flex;
line-height: 40px; line-height: 40px;
cursor: pointer;
} }
.title { .title {

View File

@ -1,16 +1,18 @@
import React from 'react'; import React from 'react';
import ReactDOM from 'react-dom';
import { useSpring, animated } from 'react-spring'; import { useSpring, animated } from 'react-spring';
import styles from './Modal.module.css'; import styles from './Modal.module.css';
export default function Modal({ title, children }) { export default function Modal({ title, children }) {
const props = useSpring({ opacity: 1, from: { opacity: 0 } }); const props = useSpring({ opacity: 1, from: { opacity: 0 } });
return ( return ReactDOM.createPortal(
<animated.div className={styles.modal} style={props}> <animated.div className={styles.modal} style={props}>
<div className={styles.content}> <div className={styles.content}>
{title && <div className={styles.header}>{title}</div>} {title && <div className={styles.header}>{title}</div>}
<div className={styles.body}>{children}</div> <div className={styles.body}>{children}</div>
</div> </div>
</animated.div> </animated.div>,
document.getElementById('__modals'),
); );
} }

View File

@ -32,10 +32,10 @@ export default function LoginForm() {
const handleSubmit = async ({ username, password }) => { const handleSubmit = async ({ username, password }) => {
const response = await post('/api/auth/login', { username, password }); const response = await post('/api/auth/login', { username, password });
if (response?.token) { if (typeof response !== 'string') {
await Router.push('/'); await Router.push('/');
} else { } else {
setMessage('Incorrect username/password'); setMessage(response.startsWith('401') ? 'Incorrect username/password' : response);
} }
}; };

View File

@ -18,7 +18,7 @@ export default function TrackingCodeForm({ values, onClose }) {
rows={3} rows={3}
cols={60} cols={60}
spellCheck={false} spellCheck={false}
defaultValue={`<script async defer data-website-id="${values.website_uuid}" src="${document.location.origin}/umami.js" />`} defaultValue={`<script async defer data-website-id="${values.website_uuid}" src="${document.location.origin}/umami.js"></script>`}
readOnly readOnly
/> />
</FormRow> </FormRow>

View File

@ -16,6 +16,7 @@ export default function Layout({ title, children, header = true, footer = true }
</Head> </Head>
{header && <Header />} {header && <Header />}
<main className="container">{children}</main> <main className="container">{children}</main>
<div id="__modals" />
{footer && <Footer />} {footer && <Footer />}
</> </>
); );

View File

@ -1,20 +1,15 @@
.container { .container {
display: flex; display: flex;
flex: 1; flex: 1;
position: relative;
height: 100%; height: 100%;
} }
.container .menu { .container .menu {
display: flex;
flex-direction: column;
padding: 30px 0; padding: 30px 0;
border: 0; border: 0;
} }
.container .content { .container .content {
display: flex;
flex-direction: column;
border-left: 1px solid var(--gray300); border-left: 1px solid var(--gray300);
padding-left: 30px; padding-left: 30px;
} }

View File

@ -2,4 +2,5 @@
padding: 0 30px; padding: 0 30px;
background: var(--gray50); background: var(--gray50);
height: 100%; height: 100%;
overflow: hidden;
} }

View File

@ -2,9 +2,11 @@ import crypto from 'crypto';
import { v4, v5, validate } from 'uuid'; import { v4, v5, validate } from 'uuid';
import bcrypt from 'bcrypt'; import bcrypt from 'bcrypt';
import { JWT, JWE, JWK } from 'jose'; import { JWT, JWE, JWK } from 'jose';
import { startOfMonth } from 'date-fns';
const SALT_ROUNDS = 10; const SALT_ROUNDS = 10;
const KEY = JWK.asKey(Buffer.from(secret())); const KEY = JWK.asKey(Buffer.from(secret()));
const ROTATING_SALT = hash(startOfMonth(new Date()).toUTCString());
const CHARS = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'; const CHARS = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
export function hash(...args) { export function hash(...args) {
@ -15,10 +17,14 @@ export function secret() {
return hash(process.env.HASH_SALT); return hash(process.env.HASH_SALT);
} }
export function salt() {
return v5([secret(), ROTATING_SALT].join(''), v5.DNS);
}
export function uuid(...args) { export function uuid(...args) {
if (!args.length) return v4(); if (!args.length) return v4();
return v5(args.join(''), v5.DNS); return v5(args.join(''), salt());
} }
export function isValidId(s) { export function isValidId(s) {

View File

@ -35,6 +35,5 @@ export default prisma;
export async function runQuery(query) { export async function runQuery(query) {
return query.catch(e => { return query.catch(e => {
console.error(e); console.error(e);
throw e;
}); });
} }

View File

@ -39,3 +39,26 @@ export function formatShortTime(val, formats = ['m', 's'], space = '') {
return t; return t;
} }
export function formatNumber(n) {
return Number(n).toFixed(0);
}
export function formatLongNumber(value) {
const n = Number(value);
if (n >= 1000000) {
return `${(n / 1000000).toFixed(1)}m`;
}
if (n >= 100000) {
return `${(n / 1000).toFixed(0)}k`;
}
if (n >= 10000) {
return `${(n / 1000).toFixed(1)}k`;
}
if (n >= 1000) {
return `${(n / 1000).toFixed(2)}k`;
}
return formatNumber(n);
}

View File

@ -68,13 +68,13 @@ export async function getCountry(req, ip) {
lookup.close(); lookup.close();
return result.country.iso_code; return result?.country?.iso_code;
} }
export async function getClientInfo(req, { screen }) { export async function getClientInfo(req, { screen }) {
const userAgent = req.headers['user-agent'];
const ip = getIpAddress(req); const ip = getIpAddress(req);
const country = await getCountry(req, ip); const country = await getCountry(req, ip);
const userAgent = req.headers['user-agent'];
const browser = browserName(userAgent); const browser = browserName(userAgent);
const os = detectOS(userAgent); const os = detectOS(userAgent);
const device = getDevice(screen, browser, os); const device = getDevice(screen, browser, os);

View File

@ -1,6 +1,6 @@
import { getWebsiteByUuid, getSessionByUuid, createSession } from 'lib/queries'; import { getWebsiteByUuid, getSessionByUuid, createSession } from 'lib/queries';
import { getClientInfo } from 'lib/request'; import { getClientInfo } from 'lib/request';
import { uuid, isValidId, parseToken } from 'lib/crypto'; import { uuid, isValidId } from 'lib/crypto';
export async function verifySession(req) { export async function verifySession(req) {
const { payload } = req.body; const { payload } = req.body;
@ -9,49 +9,42 @@ export async function verifySession(req) {
throw new Error('Invalid request'); throw new Error('Invalid request');
} }
const { website: website_uuid, hostname, screen, language, session } = payload; const { website: website_uuid, hostname, screen, language } = payload;
const token = await parseToken(session);
if (!isValidId(website_uuid)) { if (!isValidId(website_uuid)) {
throw new Error(`Invalid website: ${website_uuid}`); throw new Error(`Invalid website: ${website_uuid}`);
} }
if (!token || token.website_uuid !== website_uuid) { const { userAgent, browser, os, ip, country, device } = await getClientInfo(req, payload);
const { userAgent, browser, os, ip, country, device } = await getClientInfo(req, payload);
const website = await getWebsiteByUuid(website_uuid); const website = await getWebsiteByUuid(website_uuid);
if (!website) { if (!website) {
throw new Error(`Website not found: ${website_uuid}`); throw new Error(`Website not found: ${website_uuid}`);
}
const { website_id } = website;
const session_uuid = uuid(website_id, hostname, ip, userAgent, os);
let session = await getSessionByUuid(session_uuid);
if (!session) {
session = await createSession(website_id, {
session_uuid,
hostname,
browser,
os,
screen,
language,
country,
device,
});
}
const { session_id } = session;
return {
website_id,
website_uuid,
session_id,
session_uuid,
};
} }
return token; const { website_id } = website;
const session_uuid = uuid(website_id, hostname, ip, userAgent, os);
let session = await getSessionByUuid(session_uuid);
if (!session) {
session = await createSession(website_id, {
session_uuid,
hostname,
browser,
os,
screen,
language,
country,
device,
});
}
const { session_id } = session;
return {
website_id,
session_id,
};
} }

View File

@ -1,6 +1,6 @@
{ {
"name": "umami", "name": "umami",
"version": "0.5.0", "version": "0.12.0",
"description": "A simple, fast, website analytics alternative to Google Analytics. ", "description": "A simple, fast, website analytics alternative to Google Analytics. ",
"author": "Mike Cao <mike@mikecao.com>", "author": "Mike Cao <mike@mikecao.com>",
"license": "MIT", "license": "MIT",

View File

@ -15,6 +15,7 @@ export default async (req, res) => {
const cookie = serialize(AUTH_COOKIE_NAME, token, { const cookie = serialize(AUTH_COOKIE_NAME, token, {
path: '/', path: '/',
httpOnly: true, httpOnly: true,
sameSite: true,
maxAge: 60 * 60 * 24 * 365, maxAge: 60 * 60 * 24 * 365,
}); });

View File

@ -1,16 +1,15 @@
import { savePageView, saveEvent } from 'lib/queries'; import { savePageView, saveEvent } from 'lib/queries';
import { useCors, useSession } from 'lib/middleware'; import { useCors, useSession } from 'lib/middleware';
import { createToken } from 'lib/crypto';
import { ok, badRequest } from 'lib/response'; import { ok, badRequest } from 'lib/response';
export default async (req, res) => { export default async (req, res) => {
await useCors(req, res); await useCors(req, res);
await useSession(req, res); await useSession(req, res);
const { session } = req;
const token = await createToken(session);
const { website_id, session_id } = session;
const { type, payload } = req.body; const { type, payload } = req.body;
const {
session: { website_id, session_id },
} = req;
if (type === 'pageview') { if (type === 'pageview') {
const { url, referrer } = payload; const { url, referrer } = payload;
@ -24,5 +23,5 @@ export default async (req, res) => {
return badRequest(res); return badRequest(res);
} }
return ok(res, { session: token }); return ok(res);
}; };

View File

@ -16,7 +16,7 @@ export default function SharePage() {
if (website) { if (website) {
setWebsiteId(website.website_id); setWebsiteId(website.website_id);
} else { } else if (typeof window !== 'undefined') {
setNotFound(true); setNotFound(true);
} }
} }

File diff suppressed because one or more lines are too long

View File

@ -104,4 +104,4 @@ end;
$$ $$
insert into account (username, password, is_admin) values ('admin', '$2a$10$jsVC1XMAIIQtL0On8twztOmAr20YTVcsd4.yJncKspEwsBkeq6VFW', true); insert into account (username, password, is_admin) values ('admin', '$2b$10$BUli0c.muyCW1ErNJc3jL.vFRFtFJWrT8/GcR4A.sUdCznaXiqFXa', true);

View File

@ -69,4 +69,4 @@ create index event_created_at_idx on event(created_at);
create index event_website_id_idx on event(website_id); create index event_website_id_idx on event(website_id);
create index event_session_id_idx on event(session_id); create index event_session_id_idx on event(session_id);
insert into account (username, password, is_admin) values ('admin', '$2a$10$jsVC1XMAIIQtL0On8twztOmAr20YTVcsd4.yJncKspEwsBkeq6VFW', true); insert into account (username, password, is_admin) values ('admin', '$2b$10$BUli0c.muyCW1ErNJc3jL.vFRFtFJWrT8/GcR4A.sUdCznaXiqFXa', true);

View File

@ -2,18 +2,20 @@ import 'promise-polyfill/src/polyfill';
import 'unfetch/polyfill'; import 'unfetch/polyfill';
import { post, hook } from '../lib/web'; import { post, hook } from '../lib/web';
((window, sessionKey) => { (window => {
const { const {
screen: { width, height }, screen: { width, height },
navigator: { language }, navigator: { language },
location: { hostname, pathname, search }, location: { hostname, pathname, search },
localStorage: store,
document, document,
history, history,
} = window; } = window;
const script = document.querySelector('script[data-website-id]'); const script = document.querySelector('script[data-website-id]');
const website = script && script.getAttribute('data-website-id');
if (!script) return;
const website = script.getAttribute('data-website-id');
const hostUrl = new URL(script.src).origin; const hostUrl = new URL(script.src).origin;
const screen = `${width}x${height}`; const screen = `${width}x${height}`;
const listeners = []; const listeners = [];
@ -21,9 +23,10 @@ import { post, hook } from '../lib/web';
let currentUrl = `${pathname}${search}`; let currentUrl = `${pathname}${search}`;
let currentRef = document.referrer; let currentRef = document.referrer;
/* Collect metrics */
const collect = (type, params) => { const collect = (type, params) => {
const payload = { const payload = {
session: store.getItem(sessionKey),
url: currentUrl, url: currentUrl,
referrer: currentRef, referrer: currentRef,
website, website,
@ -41,7 +44,7 @@ import { post, hook } from '../lib/web';
return post(`${hostUrl}/api/collect`, { return post(`${hostUrl}/api/collect`, {
type, type,
payload, payload,
}).then(({ session }) => session && store.setItem(sessionKey, session)); });
}; };
const pageView = () => collect('pageview').then(() => setTimeout(loadEvents, 300)); const pageView = () => collect('pageview').then(() => setTimeout(loadEvents, 300));
@ -86,4 +89,4 @@ import { post, hook } from '../lib/web';
/* Start */ /* Start */
pageView(); pageView();
})(window, 'umami.session'); })(window);