π 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 a smart extension tagging system
Yevhenii Kulisidie01d9fbe0195
diff --git a/src/marketplace/extensions/entities/extension-to-project.entity.ts b/src/marketplace/extensions/entities/extension-to-project.entity.tsindex fda1e76e0..b1a06a0e3 100644--- a/src/marketplace/extensions/entities/extension-to-project.entity.ts+++ b/src/marketplace/extensions/entities/extension-to-project.entity.ts@@ -1,6 +1,7 @@import { Project } from '../../../project/entity/project.entity'import {Column,+ CreateDateColumn,Entity,JoinColumn,ManyToOne,@@ -29,4 +30,7 @@ export class ExtensionToProject {@ManyToOne(() => Project, project => project.extensions)@JoinColumn()project: Project++ @CreateDateColumn()+ createdAt: Date}diff --git a/src/marketplace/extensions/entities/extension.entity.ts b/src/marketplace/extensions/entities/extension.entity.tsindex 51a56d161..70f54b9d2 100644--- a/src/marketplace/extensions/entities/extension.entity.ts+++ b/src/marketplace/extensions/entities/extension.entity.ts@@ -82,4 +82,7 @@ export class Extension {@OneToMany(() => Complaint, complaint => complaint.extension)@JoinTable()complaints: Complaint[]++ @Column({ type: 'simple-array' })+ tags: string[]}diff --git a/src/marketplace/extensions/extensions.service.ts b/src/marketplace/extensions/extensions.service.tsindex b3af9728c..59f7ccb05 100644--- a/src/marketplace/extensions/extensions.service.ts+++ b/src/marketplace/extensions/extensions.service.ts@@ -164,4 +164,25 @@ export class ExtensionsService {relations,})}++ async find(options: FindManyOptions<Extension>): Promise<Extension[]> {+ return await this.extensionRepository.find(options)+ }++ async getExtensionInstallCount(+ extensionId: string,+ twoWeeksAgo?: Date,+ ): Promise<number> {+ const query = this.extensionToProjectRepository+ .createQueryBuilder('extensionToProject')+ .where('extensionToProject.extensionId = :extensionId', { extensionId })++ if (twoWeeksAgo) {+ query.andWhere('extensionToProject.createdAt > :twoWeeksAgo', {+ twoWeeksAgo,+ })+ }++ return await query.getCount()+ }}diff --git a/src/task-manager/task-manager.module.ts b/src/task-manager/task-manager.module.tsindex da99b9db5..51c3d2fe2 100644--- a/src/task-manager/task-manager.module.ts+++ b/src/task-manager/task-manager.module.ts@@ -6,6 +6,7 @@ import { ProjectModule } from '../project/project.module'import { AnalyticsModule } from '../analytics/analytics.module'import { ActionTokensModule } from '../action-tokens/action-tokens.module'import { AlertModule } from 'src/alert/alert.module'+import { ExtensionsModule } from 'src/marketplace/extensions/extensions.module'@Module({imports: [@@ -15,6 +16,7 @@ import { AlertModule } from 'src/alert/alert.module'ActionTokensModule,AlertModule,forwardRef(() => AnalyticsModule),+ ExtensionsModule,],providers: [TaskManagerService],exports: [TaskManagerService],diff --git a/src/task-manager/task-manager.service.ts b/src/task-manager/task-manager.service.tsindex 47ebb3b4c..449e5e4ee 100644--- a/src/task-manager/task-manager.service.ts+++ b/src/task-manager/task-manager.service.ts@@ -1,6 +1,6 @@import { Injectable } from '@nestjs/common'import { Cron, CronExpression } from '@nestjs/schedule'-import { IsNull, LessThan, In, Not, Between } from 'typeorm'+import { IsNull, LessThan, In, Not, Between, MoreThan, Like } from 'typeorm'import * as bcrypt from 'bcrypt'import * as dayjs from 'dayjs'import * as utc from 'dayjs/plugin/utc'@@ -21,7 +21,11 @@ import { ActionTokenType } from '../action-tokens/action-token.entity'import { LetterTemplate } from '../mailer/letter'import { AnalyticsService } from '../analytics/analytics.service'import {- ReportFrequency, ACCOUNT_PLANS, PlanCode, BillingFrequency, TRIAL_DURATION,+ ReportFrequency,+ ACCOUNT_PLANS,+ PlanCode,+ BillingFrequency,+ TRIAL_DURATION,} from '../user/entities/user.entity'import {clickhouse,@@ -42,11 +46,9 @@ import { getRandomTip } from '../common/utils'import { InjectBot } from 'nestjs-telegraf'import { TelegrafContext } from 'src/user/user.controller'import { Telegraf } from 'telegraf'-import {- QueryCondition,- QueryMetric,- QueryTime,-} from 'src/alert/dto/alert.dto'+import { QueryCondition, QueryMetric, QueryTime } from 'src/alert/dto/alert.dto'+import { ExtensionsService } from 'src/marketplace/extensions/extensions.service'+import { Extension } from 'src/marketplace/extensions/entities/extension.entity'dayjs.extend(utc)@@ -88,7 +90,8 @@ export class TaskManagerService {private readonly actionTokensService: ActionTokensService,private readonly alertService: AlertService,@InjectBot() private bot: Telegraf<TelegrafContext>,- ) { }+ private readonly extensionsService: ExtensionsService,+ ) {}@Cron(CronExpression.EVERY_MINUTE)async saveLogData(): Promise<void> {@@ -319,7 +322,9 @@ export class TaskManagerService {const pageviews =(await clickhouse.query(PVquery).toPromise())[0]['count()'] +(await clickhouse.query(CEquery).toPromise())[0]['count()']- const performance = (await clickhouse.query(PFquery).toPromise())[0]['count()']+ const performance = (await clickhouse.query(PFquery).toPromise())[0][+ 'count()'+ ]await redis.set(REDIS_USERS_COUNT_KEY, users, 'EX', 630)await redis.set(REDIS_PROJECTS_COUNT_KEY, projects, 'EX', 630)@@ -387,7 +392,7 @@ export class TaskManagerService {const users = await this.userService.find({where: {cancellationEffectiveDate: Not(IsNull()),- }+ },})for (let i = 0; i < _size(users); ++i) {@@ -412,12 +417,13 @@ export class TaskManagerService {const users = await this.userService.find({where: {planCode: PlanCode.trial,- trialEndDate: Between( // between today & tomorrow+ trialEndDate: Between(+ // between today & tomorrownew Date(),new Date(new Date().getTime() + 24 * 60 * 60 * 1000),),trialReminderSent: false,- }+ },})for (let i = 0; i < _size(users); ++i) {@@ -438,12 +444,15 @@ export class TaskManagerService {where: {planCode: PlanCode.trial,trialEndDate: IsNull(),- }+ },})for (let i = 0; i < _size(users); ++i) {await this.userService.update(users[i].id, {- trialEndDate: new Date(new Date(users[i].created).getTime() + TRIAL_DURATION * 24 * 60 * 60 * 1000),+ trialEndDate: new Date(+ new Date(users[i].created).getTime() ++ TRIAL_DURATION * 24 * 60 * 60 * 1000,+ ),})}}@@ -451,14 +460,16 @@ export class TaskManagerService {@Cron(CronExpression.EVERY_2_HOURS)async trialEnd(): Promise<void> {const users = await this.userService.find({- where: [{- planCode: PlanCode.trial,- trialEndDate: LessThan(new Date()),- },- {- planCode: PlanCode.trial,- trialEndDate: IsNull(),- }]+ where: [+ {+ planCode: PlanCode.trial,+ trialEndDate: LessThan(new Date()),+ },+ {+ planCode: PlanCode.trial,+ trialEndDate: IsNull(),+ },+ ],})for (let i = 0; i < _size(users); ++i) {@@ -468,10 +479,7 @@ export class TaskManagerService {planCode: PlanCode.none,// trialEndDate: null,})- await this.mailerService.sendEmail(- email,- LetterTemplate.TrialExpired,- )+ await this.mailerService.sendEmail(email, LetterTemplate.TrialExpired)await this.projectService.clearProjectsRedisCache(id)}}@@ -489,11 +497,14 @@ export class TaskManagerService {['admin'],)- const alerts = await this.alertService.findWhere({- project: In(_map(projects, 'id')),- active: true,- queryMetric: QueryMetric.ONLINE_USERS,- }, ['project'])+ const alerts = await this.alertService.findWhere(+ {+ project: In(_map(projects, 'id')),+ active: true,+ queryMetric: QueryMetric.ONLINE_USERS,+ },+ ['project'],+ )for (let i = 0; i < _size(alerts); ++i) {const alert = alerts[i]@@ -508,9 +519,7 @@ export class TaskManagerService {}}- const online = await this.analyticsService.getOnlineUserCount(- project.id,- )+ const online = await this.analyticsService.getOnlineUserCount(project.id)if (online >= alert.queryValue) {// @ts-ignore@@ -542,11 +551,14 @@ export class TaskManagerService {['admin'],)- const alerts = await this.alertService.findWhere({- project: In(_map(projects, 'id')),- active: true,- queryMetric: Not(QueryMetric.ONLINE_USERS),- }, ['project'])+ const alerts = await this.alertService.findWhere(+ {+ project: In(_map(projects, 'id')),+ active: true,+ queryMetric: Not(QueryMetric.ONLINE_USERS),+ },+ ['project'],+ )for (let i = 0; i < _size(alerts); ++i) {const alert = alerts[i]@@ -561,7 +573,9 @@ export class TaskManagerService {}}- const isUnique = Number(alert.queryMetric === QueryMetric.UNIQUE_PAGE_VIEWS)+ const isUnique = Number(+ alert.queryMetric === QueryMetric.UNIQUE_PAGE_VIEWS,+ )const time = getQueryTime(alert.queryTime)const createdCondition = getQueryCondition(alert.queryCondition)const query = `SELECT count() FROM analytics WHERE pid = '${project.id}' AND unique = '${isUnique}' AND created ${createdCondition} now() - ${time}`@@ -575,10 +589,15 @@ export class TaskManagerService {lastTriggered: new Date(),})- const queryMetric = alert.queryMetric === QueryMetric.UNIQUE_PAGE_VIEWS- ? 'unique page views'- : 'page views'- const text = `π Alert *${alert.name}* got triggered!\nYour project *${project.name}* has had *${count}* ${queryMetric} in the last ${getQueryTimeString(alert.queryTime)}!`+ const queryMetric =+ alert.queryMetric === QueryMetric.UNIQUE_PAGE_VIEWS+ ? 'unique page views'+ : 'page views'+ const text = `π Alert *${alert.name}* got triggered!\nYour project *${+ project.name+ }* has had *${count}* ${queryMetric} in the last ${getQueryTimeString(+ alert.queryTime,+ )}!`this.bot.telegram.sendMessage(project.admin.telegramChatId, text, {parse_mode: 'Markdown',@@ -586,4 +605,77 @@ export class TaskManagerService {}}}++ @Cron('0 * * * *')+ async handleNewExtensions() {+ const twoWeeksAgo = new Date(Date.now() - 14 * 24 * 60 * 60 * 1000)+ const extensions = await this.extensionsService.find({+ where: {+ createdAt: MoreThan(twoWeeksAgo),+ },+ })+ for (const extension of extensions) {+ if (!extension.tags.includes('New')) {+ extension.tags.push('New')+ await this.extensionsService.save(extension)+ }+ }+ const oldExtensions = await this.extensionsService.find({+ where: {+ createdAt: LessThan(twoWeeksAgo),+ tags: Like('%New%'),+ },+ })+ for (const extension of oldExtensions) {+ extension.tags = extension.tags.filter(tag => tag !== 'New')+ await this.extensionsService.save(extension)+ }+ }++ @Cron('0 * * * *')+ async handleTrendingExtensions() {+ const twoWeeksAgo = new Date(Date.now() - 14 * 24 * 60 * 60 * 1000)+ const extensions = await this.extensionsService.find({+ where: {+ createdAt: MoreThan(twoWeeksAgo),+ },+ })+ for (const extension of extensions) {+ const currentInstalls =+ await this.extensionsService.getExtensionInstallCount(extension.id)+ const twoWeeksBeforeInstalls =+ await this.extensionsService.getExtensionInstallCount(+ extension.id,+ twoWeeksAgo,+ )+ if (+ currentInstalls > twoWeeksBeforeInstalls * 2 &&+ currentInstalls > 0.9 * (await this.getAverageInstalls(extensions))+ ) {+ if (!extension.tags.includes('Trending')) {+ extension.tags.push('Trending')+ await this.extensionsService.save(extension)+ }+ } else {+ if (extension.tags.includes('Trending')) {+ extension.tags = extension.tags.filter(tag => tag !== 'Trending')+ await this.extensionsService.save(extension)+ }+ }+ }+ }++ private async getAverageInstalls(+ extensions: Extension[],+ twoWeeksAgo?: Date,+ ) {+ let totalInstalls = 0+ for (const extension of extensions) {+ totalInstalls += await this.extensionsService.getExtensionInstallCount(+ extension.id,+ twoWeeksAgo,+ )+ }+ return totalInstalls / extensions.length+ }}