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 totalMonthlyEvents param

Blue Mouse61aef755433d
diff --git a/src/analytics/analytics.service.ts b/src/analytics/analytics.service.ts
index 70e8bda2b..99a87ddc1 100644
--- a/src/analytics/analytics.service.ts
+++ b/src/analytics/analytics.service.ts
@@ -22,9 +22,8 @@ import {
import { ACCOUNT_PLANS } from '../user/entities/user.entity'
import {
- redis, isValidPID, getRedisProjectKey, redisProjectCacheTimeout,
- UNIQUE_SESSION_LIFE_TIME, clickhouse, getPercentageChange, getRedisUserCountKey,
- redisProjectCountCacheTimeout, isSelfhosted, REDIS_SESSION_SALT_KEY,
+ redis, isValidPID, getRedisProjectKey, redisProjectCacheTimeout, UNIQUE_SESSION_LIFE_TIME, clickhouse,
+ getPercentageChange, isSelfhosted, REDIS_SESSION_SALT_KEY,
} from '../common/constants'
import { getProjectsClickhouse } from '../common/utils'
import { PageviewsDTO } from './dto/pageviews.dto'
@@ -100,50 +99,6 @@ export class AnalyticsService {
}
}
- // Returns amount of existing events starting from month
- async getRedisCount(uid: string): Promise<number | null> {
- const countKey = getRedisUserCountKey(uid)
- let count = await redis.get(countKey)
-
- if (_isEmpty(count)) {
- const now = dayjs.utc().format('YYYY-MM-DD HH:mm:ss')
- const monthStart = dayjs.utc().startOf('month').format('YYYY-MM-DD HH:mm:ss')
-
- let pids
-
- if (isSelfhosted) {
- const projects = await getProjectsClickhouse()
- pids = _map(projects, ({ id }) => id)
- } else {
- pids = await this.projectService.find({
- where: {
- admin: uid,
- },
- select: ['id'],
- })
- }
-
- const count_query = `SELECT COUNT() FROM analytics WHERE pid IN (${_join(_map(pids, el => `'${el.id}'`), ',')}) AND created BETWEEN '${monthStart}' AND '${now}'`
- const result = await clickhouse.query(count_query).toPromise()
-
- const pageviews = result[0]['count()']
- count = pageviews
-
- await redis.set(countKey, `${pageviews}`, 'EX', redisProjectCountCacheTimeout)
- } else {
- try {
- // @ts-ignore
- count = Number(count)
- } catch (e) {
- console.error(e)
- throw new InternalServerErrorException('Error while processing project')
- }
- }
-
- // @ts-ignore
- return count
- }
-
checkOrigin(project: Project, origin: string): void {
if (!_isEmpty(project.origins) && !_isEmpty(origin)) {
if (origin === 'null') {
@@ -210,9 +165,9 @@ export class AnalyticsService {
if (!project.active) throw new BadRequestException('Incoming analytics is disabled for this project')
if (!isSelfhosted) {
- const count = await this.getRedisCount(project.admin.id)
+ const count = await this.projectService.getRedisCount(project.admin.id)
const maxCount = ACCOUNT_PLANS[project.admin.planCode].monthlyUsageLimit || 0
-
+
if (count >= maxCount) {
throw new ForbiddenException('You have exceeded the available monthly request limit for your account. Please upgrade your account plan if you need more requests.')
}
diff --git a/src/common/constants.ts b/src/common/constants.ts
index 298547b72..707b47346 100644
--- a/src/common/constants.ts
+++ b/src/common/constants.ts
@@ -29,7 +29,6 @@ const clickhouse = new ClickHouse({
format: 'json',
raw: false,
config: {
- session_id: 'swetrixAPI',
session_timeout: 60,
output_format_json_quote_64bit_integers: 0,
enable_http_compression: 0,
diff --git a/src/project/project.controller.ts b/src/project/project.controller.ts
index 62c823e5e..a2d1d77ce 100644
--- a/src/project/project.controller.ts
+++ b/src/project/project.controller.ts
@@ -51,12 +51,19 @@ export class ProjectController {
results,
page_total: _size(formatted),
total: _size(formatted),
+ totalMonthlyEvents: 0, // not needed as it's selfhosed
}
} else {
const where = Object()
where.admin = userId
- return await this.projectService.paginate({ take, skip }, where)
+ const paginated = await this.projectService.paginate({ take, skip }, where)
+ const totalMonthlyEvents = await this.projectService.getRedisCount(userId)
+
+ return {
+ ...paginated,
+ totalMonthlyEvents,
+ }
}
}
diff --git a/src/project/project.service.ts b/src/project/project.service.ts
index 771ba68e3..40a41fbc1 100644
--- a/src/project/project.service.ts
+++ b/src/project/project.service.ts
@@ -1,4 +1,4 @@
-import { ForbiddenException, Injectable, BadRequestException, UnprocessableEntityException } from '@nestjs/common'
+import { ForbiddenException, Injectable, BadRequestException, UnprocessableEntityException, InternalServerErrorException } from '@nestjs/common'
import { InjectRepository } from '@nestjs/typeorm'
import { Repository } from 'typeorm'
import * as _isEmpty from 'lodash/isEmpty'
@@ -8,11 +8,19 @@ import * as _size from 'lodash/size'
import * as _split from 'lodash/split'
import * as _join from 'lodash/join'
import * as _find from 'lodash/find'
+import * as _map from 'lodash/map'
+import * as dayjs from 'dayjs'
+import * as utc from 'dayjs/plugin/utc'
import { Pagination, PaginationOptionsInterface } from '../common/pagination'
import { Project } from './entity/project.entity'
import { ProjectDTO } from './dto/project.dto'
-import { isValidPID } from '../common/constants'
+import {
+ isValidPID, redisProjectCountCacheTimeout, getRedisUserCountKey, redis, clickhouse, isSelfhosted,
+} from '../common/constants'
+import { getProjectsClickhouse } from '../common/utils'
+
+dayjs.extend(utc)
@Injectable()
export class ProjectService {
@@ -120,7 +128,6 @@ export class ProjectService {
updProject.origins = _isString(updProject.origins) ? updProject.origins : _join(updProject.origins, ',')
return updProject
-
}
formatFromClickhouse(project: object): object {
@@ -139,4 +146,48 @@ export class ProjectService {
if (!_isArray(projectDTO.origins)) throw new UnprocessableEntityException('The list of allowed origins has to be an array of strings')
if (_size(_join(projectDTO.origins, ',')) > 300) throw new UnprocessableEntityException('The list of allowed origins has to be smaller than 300 symbols')
}
+
+ // Returns amount of existing events starting from month
+ async getRedisCount(uid: string): Promise<number | null> {
+ const countKey = getRedisUserCountKey(uid)
+ let count = await redis.get(countKey)
+
+ if (_isEmpty(count)) {
+ const now = dayjs.utc().format('YYYY-MM-DD HH:mm:ss')
+ const monthStart = dayjs.utc().startOf('month').format('YYYY-MM-DD HH:mm:ss')
+
+ let pids
+
+ if (isSelfhosted) {
+ const projects = await getProjectsClickhouse()
+ pids = _map(projects, ({ id }) => id)
+ } else {
+ pids = await this.find({
+ where: {
+ admin: uid,
+ },
+ select: ['id'],
+ })
+ }
+
+ const count_query = `SELECT COUNT() FROM analytics WHERE pid IN (${_join(_map(pids, el => `'${el.id}'`), ',')}) AND created BETWEEN '${monthStart}' AND '${now}'`
+ const result = await clickhouse.query(count_query).toPromise()
+
+ const pageviews = result[0]['count()']
+ count = pageviews
+
+ await redis.set(countKey, `${pageviews}`, 'EX', redisProjectCountCacheTimeout)
+ } else {
+ try {
+ // @ts-ignore
+ count = Number(count)
+ } catch (e) {
+ console.error(e)
+ throw new InternalServerErrorException('Error while processing project')
+ }
+ }
+
+ // @ts-ignore
+ return count
+ }
}