📈 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!
TypeScript chartsclickhousedockergdprgoogle-analyticsmetricsmonitoringmysqlnestjsprivacyredisself-hostedselfhostedstatisticstailwindcssweb-analyticswebanalyticswebsite
added GET /captcha/birdseye
Blue Mousec7a95d653fa4
diff --git a/src/analytics/analytics.controller.ts b/src/analytics/analytics.controller.tsindex 527c2c459..47eea0de1 100644--- a/src/analytics/analytics.controller.ts+++ b/src/analytics/analytics.controller.ts@@ -624,6 +624,24 @@ export class AnalyticsController {return this.analyticsService.getSummary(pidsArray, 'w')}+ @Get('/captcha/birdseye')+ @Auth([], true, true)+ // returns overall short statistics per CAPTCHA project+ async getCaptchaOverallStats(+ @Query() data,+ @CurrentUserId() uid: string,+ ): Promise<any> {+ const { pids, pid } = data+ const pidsArray = getPIDsArray(pids, pid)++ for (let i = 0; i < _size(pidsArray); ++i) {+ this.analyticsService.validatePID(pidsArray[i])+ await this.analyticsService.checkProjectAccess(pidsArray[i], uid)+ }++ return this.analyticsService.getCaptchaSummary(pidsArray, 'w')+ }+@UseGuards(SelfhostedGuard)@Public()@Get('/generalStats')diff --git a/src/analytics/analytics.service.ts b/src/analytics/analytics.service.tsindex 7025ca9f8..fa4ab83de 100644--- a/src/analytics/analytics.service.ts+++ b/src/analytics/analytics.service.ts@@ -471,6 +471,55 @@ export class AnalyticsService {return result}+ async getCaptchaSummary(pids: string[], period: 'w' | 'M' = 'w'): Promise<Object> {+ const result = {}++ for (let i = 0; i < _size(pids); ++i) {+ const pid = pids[i]+ if (!isValidPID(pid))+ throw new BadRequestException(+ `The provided Project ID (${pid}) is incorrect`,+ )++ const now = dayjs.utc().format('YYYY-MM-DD HH:mm:ss')+ const oneWRaw = dayjs.utc().subtract(1, period)+ const oneWeek = oneWRaw.format('YYYY-MM-DD HH:mm:ss')+ const twoWeeks = oneWRaw.subtract(1, period).format('YYYY-MM-DD HH:mm:ss')++ const query1 = `SELECT count() FROM captcha WHERE pid = {pid:FixedString(12)} AND created BETWEEN {oneWeek:String} AND {now:String}`+ const query2 = `SELECT count() FROM captcha WHERE pid = {pid:FixedString(12)} AND created BETWEEN {twoWeeks:String} AND {oneWeek:String}`++ const paramsData = {+ params: {+ pid,+ oneWeek,+ twoWeeks,+ now,+ },+ }++ try {+ const q1res = await clickhouse.query(query1, paramsData).toPromise()+ const q2res = await clickhouse.query(query2, paramsData).toPromise()++ const thisWeek = q1res?.['count()'] || 0+ const lastWeek = q2res?.['count()'] || 0++ result[pid] = {+ thisWeek,+ lastWeek,+ percChange: calculateRelativePercentage(lastWeek, thisWeek),+ }+ } catch {+ throw new InternalServerErrorException(+ "Can't process the provided PID. Please, try again later.",+ )+ }+ }++ return result+ }+async groupByTimeBucket(timeBucket: TimeBucketType,from: string,