Skip to content

📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!

use clickhouse insert method instead of raw insert queries

Blue Mousebfdc136275ed
diff --git a/apps/production/src/analytics/analytics.service.ts b/apps/production/src/analytics/analytics.service.ts
index a7bd53646..17c5c84a1 100644
--- a/apps/production/src/analytics/analytics.service.ts
+++ b/apps/production/src/analytics/analytics.service.ts
@@ -3708,28 +3708,24 @@ export class AnalyticsService {
status: 'resolved' | 'active',
pid: string,
) {
- const params = _reduce(
+ const values = _reduce(
eids,
- (acc, curr, index) => ({
+ (acc, curr) => [
...acc,
- [`e_${index}`]: curr,
- }),
- {},
+ {
+ eid: curr,
+ status,
+ pid,
+ },
+ ],
+ [],
)
- const query = `INSERT INTO error_statuses (eid, pid, status) VALUES ${_map(
- params,
- (val, key) =>
- `({${key}:FixedString(32)}, {pid:FixedString(12)}, '${status}')`,
- ).join(', ')}`
-
try {
- await clickhouse.query({
- query,
- query_params: {
- ...params,
- pid,
- },
+ await clickhouse.insert({
+ table: 'error_statuses',
+ format: 'JSONEachRow',
+ values: [values],
})
} catch (reason) {
console.error('Error at PATCH error-status:')
diff --git a/apps/selfhosted/src/analytics/analytics.service.ts b/apps/selfhosted/src/analytics/analytics.service.ts
index b2ee34ab5..fe8d57121 100644
--- a/apps/selfhosted/src/analytics/analytics.service.ts
+++ b/apps/selfhosted/src/analytics/analytics.service.ts
@@ -982,7 +982,9 @@ export class AnalyticsService {
const keyParam = `qfk_${col}_${f}`
params[keyParam] = key
- query += `indexOf(meta.key, {${keyParam}:String}) > 0 AND meta.value[indexOf(meta.key, {${keyParam}:String})] ${isExclusive ? '!= ' : '='} {${param}:String}`
+ query += `indexOf(meta.key, {${keyParam}:String}) > 0 AND meta.value[indexOf(meta.key, {${keyParam}:String})] ${
+ isExclusive ? '!= ' : '='
+ } {${param}:String}`
continue
}
@@ -1008,7 +1010,9 @@ export class AnalyticsService {
}
query += isArrayDataset
- ? `indexOf(${sqlColumn}, {${param}:String}) ${isExclusive ? '=' : '>'} 0`
+ ? `indexOf(${sqlColumn}, {${param}:String}) ${
+ isExclusive ? '=' : '>'
+ } 0`
: `${isExclusive ? 'NOT ' : ''}${sqlColumn} = {${param}:String}`
}
@@ -3379,28 +3383,24 @@ export class AnalyticsService {
status: 'resolved' | 'active',
pid: string,
) {
- const params = _reduce(
+ const values = _reduce(
eids,
- (acc, curr, index) => ({
+ (acc, curr) => [
...acc,
- [`e_${index}`]: curr,
- }),
- {},
+ {
+ eid: curr,
+ status,
+ pid,
+ },
+ ],
+ [],
)
- const query = `INSERT INTO error_statuses (eid, pid, status) VALUES ${_map(
- params,
- (val, key) =>
- `({${key}:FixedString(32)}, {pid:FixedString(12)}, '${status}')`,
- ).join(', ')}`
-
try {
- await clickhouse.query({
- query,
- query_params: {
- ...params,
- pid,
- },
+ await clickhouse.insert({
+ table: 'error_statuses',
+ format: 'JSONEachRow',
+ values: [values],
})
} catch (reason) {
console.error('Error at PATCH error-status:')
diff --git a/apps/selfhosted/src/common/utils.ts b/apps/selfhosted/src/common/utils.ts
index 2d3c02deb..28f5a8b6f 100644
--- a/apps/selfhosted/src/common/utils.ts
+++ b/apps/selfhosted/src/common/utils.ts
@@ -156,18 +156,21 @@ const deleteFunnelClickhouse = async (id: string) => {
}
const createFunnelClickhouse = async (funnel: Partial<Funnel>) => {
- const query = `INSERT INTO funnel (*) VALUES ({id:String},{name:String},{steps:String},{projectId:FixedString(12)},'${dayjs
- .utc()
- .format('YYYY-MM-DD HH:mm:ss')}')`
+ const { id, name, steps, projectId } = funnel
- const { data } = await clickhouse
- .query({
- query,
- query_params: funnel,
- })
- .then(resultSet => resultSet.json())
-
- return data
+ await clickhouse.insert({
+ table: 'funnel',
+ format: 'JSONEachRow',
+ values: [
+ {
+ id,
+ name,
+ steps,
+ projectId,
+ created: dayjs.utc().format('YYYY-MM-DD HH:mm:ss'),
+ },
+ ],
+ })
}
const getProjectsClickhouse = async (id = null, search: string = null) => {
@@ -287,37 +290,41 @@ const deleteProjectClickhouse = async (id: string) => {
}
const createProjectClickhouse = async (project: Partial<Project>) => {
- const created = dayjs.utc().format('YYYY-MM-DD HH:mm:ss')
- const query = `INSERT INTO project (*) VALUES ({id:FixedString(12)},{name:String},'','',1,0,0,NULL,'${created}')`
+ const { id, name } = project
- const { data } = await clickhouse
- .query({
- query,
- query_params: project,
- })
- .then(resultSet => resultSet.json())
-
- return data
+ await clickhouse.insert({
+ table: 'project',
+ format: 'JSONEachRow',
+ values: [
+ {
+ id,
+ name,
+ origins: '',
+ ipBlacklist: '',
+ active: 1,
+ public: 0,
+ isPasswordProtected: 0,
+ passwordHash: null,
+ created: dayjs.utc().format('YYYY-MM-DD HH:mm:ss'),
+ },
+ ],
+ })
}
const saveRefreshTokenClickhouse = async (
userId: string,
refreshToken: string,
) => {
- const query =
- 'INSERT INTO refresh_token (*) VALUES ({userId:String},{refreshToken:String})'
-
- const { data } = await clickhouse
- .query({
- query,
- query_params: {
+ await clickhouse.insert({
+ table: 'refresh_token',
+ format: 'JSONEachRow',
+ values: [
+ {
userId,
refreshToken,
},
- })
- .then(resultSet => resultSet.json())
-
- return data
+ ],
+ })
}
const findRefreshTokenClickhouse = async (
@@ -368,19 +375,20 @@ interface IClickhouseUser {
const CLICKHOUSE_SETTINGS_ID = 'sfuser'
const createUserClickhouse = async (user: IClickhouseUser) => {
- const query = `INSERT INTO sfuser (*) VALUES ({id:String},{timezone:String},{timeFormat:String},{showLiveVisitorsInTitle:Int8})`
+ const { timezone, timeFormat, showLiveVisitorsInTitle } = user
- const { data } = await clickhouse
- .query({
- query,
- query_params: {
- ...user,
+ await clickhouse.insert({
+ table: 'sfuser',
+ format: 'JSONEachRow',
+ values: [
+ {
id: CLICKHOUSE_SETTINGS_ID,
+ timezone,
+ timeFormat,
+ showLiveVisitorsInTitle,
},
- })
- .then(resultSet => resultSet.json())
-
- return data
+ ],
+ })
}
const getUserClickhouse = async () => {