mirror of
https://github.com/umami-software/umami.git
synced 2026-02-12 00:27:11 +01:00
Database refactoring.
This commit is contained in:
parent
bb184dc2cc
commit
467c7f289f
37 changed files with 566 additions and 591 deletions
|
|
@ -1,40 +1,51 @@
|
|||
import { ClickHouse } from 'clickhouse';
|
||||
import dateFormat from 'dateformat';
|
||||
import debug from 'debug';
|
||||
import { FILTER_IGNORED } from 'lib/constants';
|
||||
import { CLICKHOUSE_DATE_FORMATS } from './constants';
|
||||
import { CLICKHOUSE } from 'lib/db';
|
||||
|
||||
export const CLICKHOUSE_DATE_FORMATS = {
|
||||
minute: '%Y-%m-%d %H:%M:00',
|
||||
hour: '%Y-%m-%d %H:00:00',
|
||||
day: '%Y-%m-%d',
|
||||
month: '%Y-%m-01',
|
||||
year: '%Y-01-01',
|
||||
};
|
||||
|
||||
const log = debug('clickhouse');
|
||||
|
||||
function getClient() {
|
||||
if (!process.env.ANALYTICS_URL) {
|
||||
if (!process.env.CLICKHOUSE_URL) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const url = new URL(process.env.ANALYTICS_URL);
|
||||
const database = url.pathname.replace('/', '');
|
||||
const {
|
||||
hostname,
|
||||
port,
|
||||
pathname,
|
||||
username = 'default',
|
||||
password,
|
||||
} = new URL(process.env.CLICKHOUSE_URL);
|
||||
|
||||
return new ClickHouse({
|
||||
url: url.hostname,
|
||||
port: Number(url.port),
|
||||
basicAuth: url.password
|
||||
? {
|
||||
username: url.username || 'default',
|
||||
password: url.password,
|
||||
}
|
||||
: null,
|
||||
const client = new ClickHouse({
|
||||
url: hostname,
|
||||
port: Number(port),
|
||||
format: 'json',
|
||||
config: {
|
||||
database,
|
||||
database: pathname.replace('/', ''),
|
||||
},
|
||||
basicAuth: password ? { username, password } : null,
|
||||
});
|
||||
|
||||
if (process.env.NODE_ENV !== 'production') {
|
||||
global[CLICKHOUSE] = clickhouse;
|
||||
}
|
||||
|
||||
log('Clickhouse initialized');
|
||||
|
||||
return client;
|
||||
}
|
||||
|
||||
const clickhouse = global.clickhouse || getClient();
|
||||
|
||||
if (process.env.NODE_ENV !== 'production') {
|
||||
global.clickhouse = clickhouse;
|
||||
}
|
||||
|
||||
export { clickhouse };
|
||||
|
||||
function getDateStringQuery(data, unit) {
|
||||
return `formatDateTime(${data}, '${CLICKHOUSE_DATE_FORMATS[unit]}')`;
|
||||
}
|
||||
|
|
@ -176,7 +187,12 @@ async function findFirst(data) {
|
|||
return data[0] ?? null;
|
||||
}
|
||||
|
||||
// Initialization
|
||||
const clickhouse = global[CLICKHOUSE] || getClient();
|
||||
|
||||
export default {
|
||||
client: clickhouse,
|
||||
log,
|
||||
getDateStringQuery,
|
||||
getDateQuery,
|
||||
getDateFormat,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue