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!

added GET usageinfo endpoint & functionality

Blue Mouse164a28544a95
diff --git a/apps/production/src/common/constants.ts b/apps/production/src/common/constants.ts
index 17c981b33..96a3bc31d 100644
--- a/apps/production/src/common/constants.ts
+++ b/apps/production/src/common/constants.ts
@@ -59,6 +59,7 @@ const isValidPID = (pid: string) => PID_REGEX.test(pid)
// redis keys
const getRedisProjectKey = (pid: string) => `pid_${pid}`
const getRedisUserCountKey = (uid: string) => `user_c_${uid}`
+const getRedisUserUsageInfoKey = (uid: string) => `user_ui_${uid}`
const getRedisCaptchaKey = (token: string) => `captcha_${hash(token)}`
const REDIS_LOG_DATA_CACHE_KEY = 'log_cache'
@@ -81,6 +82,9 @@ const redisProjectCacheTimeout = 3600
// 15 minutes
const redisProjectCountCacheTimeout = 900
+// 5 minutes
+const redisUserUsageinfoCacheTimeout = 300
+
// 30 minues -> the amount of time analytics requests within one session are counted as non-unique
const UNIQUE_SESSION_LIFE_TIME = 1800
@@ -134,4 +138,6 @@ export {
PRODUCTION_ORIGIN,
REDIS_SSO_UUID,
JWT_ACCESS_TOKEN_SECRET,
+ getRedisUserUsageInfoKey,
+ redisUserUsageinfoCacheTimeout,
}
diff --git a/apps/production/src/project/project.service.ts b/apps/production/src/project/project.service.ts
index 81e9d5b09..3bd53d3fb 100644
--- a/apps/production/src/project/project.service.ts
+++ b/apps/production/src/project/project.service.ts
@@ -21,11 +21,11 @@ import * as utc from 'dayjs/plugin/utc'
// @ts-ignore
import * as validateIP from 'validate-ip-node'
-import { UserService } from 'src/user/user.service'
-import { ActionTokensService } from 'src/action-tokens/action-tokens.service'
-import { ActionTokenType } from 'src/action-tokens/action-token.entity'
-import { MailerService } from 'src/mailer/mailer.service'
-import { LetterTemplate } from 'src/mailer/letter'
+import { UserService } from '../user/user.service'
+import { ActionTokensService } from '../action-tokens/action-tokens.service'
+import { ActionTokenType } from '../action-tokens/action-token.entity'
+import { MailerService } from '../mailer/mailer.service'
+import { LetterTemplate } from '../mailer/letter'
import { Pagination, PaginationOptionsInterface } from '../common/pagination'
import { Project } from './entity/project.entity'
import { ProjectShare, Role } from './entity/project-share.entity'
@@ -43,7 +43,10 @@ import {
redisProjectCacheTimeout,
isDevelopment,
PRODUCTION_ORIGIN,
+ getRedisUserUsageInfoKey,
+ redisUserUsageinfoCacheTimeout,
} from '../common/constants'
+import { IUsageInfoRedis } from '../user/interfaces'
import { ProjectSubscriber } from './entity'
import { AddSubscriberType } from './types'
import { GetSubscribersQueriesDto, UpdateSubscriberBodyDto } from './dto'
@@ -66,6 +69,13 @@ dayjs.extend(utc)
// }
// }
+const DEFAULT_USAGE_INFO = {
+ total: 0,
+ traffic: 0,
+ customEvents: 0,
+ captcha: 0,
+}
+
export const deleteProjectRedis = async (id: string) => {
const key = getRedisProjectKey(id)
@@ -390,7 +400,7 @@ export class ProjectService {
})
}
- // Returns amount of exirsting events starting from month
+ // Returns amount of existing events starting from month
async getRedisCount(uid: string): Promise<number | null> {
const countKey = getRedisUserCountKey(uid)
let count: string | number = await redis.get(countKey)
@@ -448,8 +458,73 @@ export class ProjectService {
}
}
- // @ts-ignore
- return count
+ return count as number
+ }
+
+ async getRedisUsageInfo(uid: string): Promise<IUsageInfoRedis | null> {
+ const key = getRedisUserUsageInfoKey(uid)
+ let info: string | IUsageInfoRedis = await redis.get(key)
+
+ if (_isEmpty(info)) {
+ const monthStart = dayjs
+ .utc()
+ .startOf('month')
+ .format('YYYY-MM-DD HH:mm:ss')
+ const monthEnd = dayjs.utc().endOf('month').format('YYYY-MM-DD HH:mm:ss')
+
+ const pids = await this.find({
+ where: {
+ admin: uid,
+ },
+ select: ['id'],
+ })
+
+ if (_isEmpty(pids)) {
+ return DEFAULT_USAGE_INFO
+ }
+
+ const countEVQuery = `SELECT COUNT() FROM analytics WHERE pid IN (${_join(
+ _map(pids, el => `'${el.id}'`),
+ ',',
+ )}) AND created BETWEEN '${monthStart}' AND '${monthEnd}'`
+ const countCustomEVQuery = `SELECT COUNT() FROM customEV WHERE pid IN (${_join(
+ _map(pids, el => `'${el.id}'`),
+ ',',
+ )}) AND created BETWEEN '${monthStart}' AND '${monthEnd}'`
+ const countCaptchaQuery = `SELECT COUNT() FROM captcha WHERE pid IN (${_join(
+ _map(pids, el => `'${el.id}'`),
+ ',',
+ )}) AND created BETWEEN '${monthStart}' AND '${monthEnd}'`
+
+ const traffic = (await clickhouse.query(countEVQuery).toPromise())[0]['count()']
+ const customEvents = (await clickhouse.query(countCustomEVQuery).toPromise())[0]['count()']
+ const captcha = (await clickhouse.query(countCaptchaQuery).toPromise())[0]['count()']
+
+ const total = traffic + customEvents + captcha
+
+ info = {
+ total,
+ traffic,
+ customEvents,
+ captcha,
+ }
+
+ await redis.set(
+ key,
+ JSON.stringify(info),
+ 'EX',
+ redisUserUsageinfoCacheTimeout,
+ )
+ } else {
+ try {
+ info = JSON.parse(info)
+ } catch (reason) {
+ console.error(`[ERROR][project -> getRedisUsageInfo] ${reason}`)
+ info = DEFAULT_USAGE_INFO
+ }
+ }
+
+ return info as IUsageInfoRedis
}
async clearProjectsRedisCache(uid: string): Promise<void> {
diff --git a/apps/production/src/user/interfaces/index.ts b/apps/production/src/user/interfaces/index.ts
new file mode 100644
index 000000000..48b8d31d7
--- /dev/null
+++ b/apps/production/src/user/interfaces/index.ts
@@ -0,0 +1,2 @@
+export * from './usage-info'
+export * from './meta-info'
diff --git a/apps/production/src/user/interfaces/meta-info.ts b/apps/production/src/user/interfaces/meta-info.ts
new file mode 100644
index 000000000..6fba3c8c6
--- /dev/null
+++ b/apps/production/src/user/interfaces/meta-info.ts
@@ -0,0 +1,5 @@
+export interface IMetaInfo {
+ country: string | null
+ symbol: string
+ code: string
+}
diff --git a/apps/production/src/user/interfaces/usage-info.ts b/apps/production/src/user/interfaces/usage-info.ts
new file mode 100644
index 000000000..5c88c839a
--- /dev/null
+++ b/apps/production/src/user/interfaces/usage-info.ts
@@ -0,0 +1,12 @@
+export interface IUsageInfoRedis {
+ total: number
+ traffic: number
+ customEvents: number
+ captcha: number
+}
+
+export interface IUsageInfo extends IUsageInfoRedis {
+ trafficPerc: number
+ customEventsPerc: number
+ captchaPerc: number
+}
diff --git a/apps/production/src/user/user.controller.ts b/apps/production/src/user/user.controller.ts
index f6fde1957..c72bc0f51 100644
--- a/apps/production/src/user/user.controller.ts
+++ b/apps/production/src/user/user.controller.ts
@@ -27,6 +27,7 @@ import * as _isEmpty from 'lodash/isEmpty'
import * as _includes from 'lodash/includes'
import * as _isString from 'lodash/isString'
import * as _omit from 'lodash/omit'
+import * as _round from 'lodash/round'
import { v4 as uuidv4 } from 'uuid'
import { Markup } from 'telegraf'
@@ -61,6 +62,7 @@ import { LetterTemplate } from '../mailer/letter'
import { AppLoggerService } from '../logger/logger.service'
import { UserProfileDTO } from './dto/user.dto'
import { checkRateLimit } from '../common/utils'
+import { IUsageInfo, IMetaInfo } from './interfaces'
dayjs.extend(utc)
@@ -617,7 +619,7 @@ export class UserController {
@Get('metainfo')
@Public()
- async getMetaInfo(@Headers() headers): Promise<any> {
+ async getMetaInfo(@Headers() headers): Promise<IMetaInfo> {
const country = headers['cf-ipcountry'] || 'XX'
return {
@@ -625,4 +627,18 @@ export class UserController {
...this.userService.getCurrencyByCountry(country),
}
}
+
+ @Get('usageinfo')
+ async getUsageInfo(@CurrentUserId() uid: string): Promise<IUsageInfo> {
+ const rawInfo = await this.projectService.getRedisUsageInfo(uid)
+
+ const info: IUsageInfo = {
+ ...rawInfo,
+ trafficPerc: _round((rawInfo.traffic / rawInfo.total) * 100, 2),
+ customEventsPerc: _round((rawInfo.customEvents / rawInfo.total) * 100, 2),
+ captchaPerc: _round((rawInfo.captcha / rawInfo.total) * 100, 2),
+ }
+
+ return info
+ }
}