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!

Implement AI feature and establish microservice communication

Yehor Dremliuhaeaf3375a0b97
diff --git a/.env.example b/.env.example
index 6c761faa6..aed32f492 100644
--- a/.env.example
+++ b/.env.example
@@ -94,3 +94,5 @@ IS_MASTER_NODE=true
# Generic
EMAIL_ACTION_ENCRYPTION_KEY=
+# AI
+AI_URL=
diff --git a/apps/production/src/project/dto/get-prediction-query.dto.ts b/apps/production/src/project/dto/get-prediction-query.dto.ts
new file mode 100644
index 000000000..ee7731490
--- /dev/null
+++ b/apps/production/src/project/dto/get-prediction-query.dto.ts
@@ -0,0 +1,24 @@
+import { ApiProperty } from '@nestjs/swagger'
+import { IsEnum, IsNotEmpty } from 'class-validator'
+
+export enum TimeFrameQueryEnum {
+ NEXT_1_HOUR = 'next_1_hour',
+ NEXT_4_HOUR = 'next_4_hour',
+ NEXT_8_HOUR = 'next_8_hour',
+ NEXT_12_HOUR = 'next_12_hour',
+ NEXT_24_HOUR = 'next_24_hour',
+ NEXT_72_HOUR = 'next_72_hour',
+ NEXT_168_HOUR = 'next_168_hour',
+}
+
+export class TimeFrameQueryEnumDTO {
+ @ApiProperty({
+ example: 'next_8_hour',
+ required: true,
+ description: 'The timeframe of prediction',
+ enum: TimeFrameQueryEnum,
+ })
+ @IsNotEmpty()
+ @IsEnum(TimeFrameQueryEnum)
+ timeframe: TimeFrameQueryEnum
+}
diff --git a/apps/production/src/project/project.controller.ts b/apps/production/src/project/project.controller.ts
index da55cdf1d..5db39ee57 100644
--- a/apps/production/src/project/project.controller.ts
+++ b/apps/production/src/project/project.controller.ts
@@ -107,6 +107,7 @@ import { ProjectIdDto } from './dto/project-id.dto'
import { CreateProjectViewDto } from './dto/create-project-view.dto'
import { UpdateProjectViewDto } from './dto/update-project-view.dto'
import { ProjectViewIdsDto } from './dto/project-view-ids.dto'
+import { TimeFrameQueryEnumDTO } from './dto/get-prediction-query.dto'
const PROJECTS_MAXIMUM = 50
@@ -2119,4 +2120,40 @@ export class ProjectController {
await this.projectsViewsRepository.deleteProjectView(params.viewId)
}
+
+ @ApiOperation({ summary: 'Get AI prediction' })
+ @ApiBearerAuth()
+ @HttpCode(HttpStatus.OK)
+ @Get(':projectId/predict')
+ @UseGuards(JwtAccessTokenGuard, RolesGuard)
+ @Roles(UserType.CUSTOMER, UserType.ADMIN)
+ async getAiPrediction(
+ @Param() params: ProjectIdDto,
+ @Query() query: TimeFrameQueryEnumDTO,
+ @CurrentUserId() userId: string,
+ @Headers() headers: { 'x-password'?: string },
+ ) {
+ const project = await this.projectService.findProject(params.projectId, [
+ 'admin',
+ 'share',
+ ])
+
+ if (!project) {
+ throw new NotFoundException('Project not found.')
+ }
+
+ const user = await this.userService.findUserV2(userId, ['roles'])
+
+ if (!user) {
+ throw new NotFoundException('User not found.')
+ }
+
+ this.projectService.allowedToView(project, userId, headers['x-password'])
+ // TODO add caching
+
+ return this.projectService.sendPredictAiRequest(
+ params.projectId,
+ query.timeframe,
+ )
+ }
}
diff --git a/apps/production/src/project/project.module.ts b/apps/production/src/project/project.module.ts
index 7a16710d8..d58a14096 100644
--- a/apps/production/src/project/project.module.ts
+++ b/apps/production/src/project/project.module.ts
@@ -1,5 +1,7 @@
import { Module, forwardRef } from '@nestjs/common'
import { TypeOrmModule } from '@nestjs/typeorm'
+import { HttpModule } from '@nestjs/axios'
+import { ConfigModule, ConfigService } from '@nestjs/config'
import { ProjectService } from './project.service'
import { ProjectController } from './project.controller'
@@ -23,6 +25,13 @@ import { ProjectViewCustomEventEntity } from './entity/project-view-custom-event
ProjectViewCustomEventEntity,
]),
forwardRef(() => UserModule),
+ HttpModule.registerAsync({
+ imports: [ConfigModule],
+ inject: [ConfigService],
+ useFactory: (configService: ConfigService) => ({
+ baseURL: configService.get('AI_URL'),
+ }),
+ }),
AppLoggerModule,
ActionTokensModule,
MailerModule,
diff --git a/apps/production/src/project/project.service.ts b/apps/production/src/project/project.service.ts
index e0662c800..b8c6b0c8e 100644
--- a/apps/production/src/project/project.service.ts
+++ b/apps/production/src/project/project.service.ts
@@ -6,6 +6,7 @@ import {
UnprocessableEntityException,
InternalServerErrorException,
ConflictException,
+ NotFoundException,
} from '@nestjs/common'
import { InjectRepository } from '@nestjs/typeorm'
import { Repository } from 'typeorm'
@@ -25,6 +26,9 @@ import * as _reduce from 'lodash/reduce'
import * as dayjs from 'dayjs'
import * as utc from 'dayjs/plugin/utc'
import { compareSync } from 'bcrypt'
+import { firstValueFrom } from 'rxjs'
+import { HttpService } from '@nestjs/axios'
+import { AxiosError } from 'axios'
import { UserService } from '../user/user.service'
import { ActionTokensService } from '../action-tokens/action-tokens.service'
@@ -68,6 +72,8 @@ import {
import { ReportFrequency } from './enums'
import { nFormatter } from '../common/utils'
import { browserArgs } from '../og-image/og-image.service'
+import { TimeFrameQueryEnum } from './dto/get-prediction-query.dto'
+import { AppLoggerService } from '../logger/logger.service'
dayjs.extend(utc)
@@ -286,6 +292,8 @@ export class ProjectService {
private readonly funnelRepository: Repository<Funnel>,
private readonly actionTokens: ActionTokensService,
private readonly mailerService: MailerService,
+ private readonly httpService: HttpService,
+ private readonly logger: AppLoggerService,
) {}
async getRedisProject(pid: string): Promise<Project | null> {
@@ -1345,4 +1353,25 @@ export class ProjectService {
async findProject(id: string, relations: string[]) {
return this.projectsRepository.findOne({ relations, where: { id } })
}
+
+ async sendPredictAiRequest(id: string, timeframe: TimeFrameQueryEnum) {
+ try {
+ const { data } = await firstValueFrom(
+ this.httpService.get('/predict', {
+ params: { pid: id, timeframe },
+ }),
+ )
+ return data
+ } catch (error) {
+ if (error instanceof AxiosError && error.response?.status === 404) {
+ throw new NotFoundException(
+ 'Data not found. Prediction is not available for this timeframe.',
+ )
+ }
+ this.logger.error(
+ `Error receiving prediction from AI service: ${error.message}`,
+ )
+ return null
+ }
+ }
}
diff --git a/apps/production/src/task-manager/task-manager.module.ts b/apps/production/src/task-manager/task-manager.module.ts
index fda77cc00..8462b6c7c 100644
--- a/apps/production/src/task-manager/task-manager.module.ts
+++ b/apps/production/src/task-manager/task-manager.module.ts
@@ -1,3 +1,5 @@
+import { HttpModule } from '@nestjs/axios'
+import { ConfigModule, ConfigService } from '@nestjs/config'
import { Module, forwardRef } from '@nestjs/common'
import { TypeOrmModule } from '@nestjs/typeorm'
import { AlertModule } from '../alert/alert.module'
@@ -21,6 +23,13 @@ import { Message } from '../integrations/telegram/entities/message.entity'
ActionTokensModule,
AlertModule,
forwardRef(() => AnalyticsModule),
+ HttpModule.registerAsync({
+ imports: [ConfigModule],
+ inject: [ConfigService],
+ useFactory: (configService: ConfigService) => ({
+ baseURL: configService.get('AI_URL'),
+ }),
+ }),
ExtensionsModule,
AppLoggerModule,
TypeOrmModule.forFeature([Message]),
diff --git a/apps/production/src/task-manager/task-manager.service.ts b/apps/production/src/task-manager/task-manager.service.ts
index 1fb15e0ce..ae79fb75f 100644
--- a/apps/production/src/task-manager/task-manager.service.ts
+++ b/apps/production/src/task-manager/task-manager.service.ts
@@ -16,6 +16,8 @@ import * as _includes from 'lodash/includes'
import * as _toNumber from 'lodash/toNumber'
import * as _reduce from 'lodash/reduce'
import * as _filter from 'lodash/filter'
+import { HttpService } from '@nestjs/axios'
+import { firstValueFrom } from 'rxjs'
import { AlertService } from '../alert/alert.service'
import { QueryCondition, QueryMetric, QueryTime } from '../alert/dto/alert.dto'
@@ -256,6 +258,7 @@ export class TaskManagerService {
private readonly telegramService: TelegramService,
private readonly payoutsService: PayoutsService,
private readonly configService: ConfigService,
+ private readonly httpService: HttpService,
) {}
generateUnsubscribeUrl(
@@ -1431,4 +1434,26 @@ export class TaskManagerService {
},
)
}
+
+ @Cron(CronExpression.EVERY_WEEK)
+ async sendTrainingAiRequest(): Promise<void> {
+ try {
+ await firstValueFrom(this.httpService.post('/run_training'))
+ } catch (error) {
+ this.logger.error(
+ `Error triggering training on the AI service: ${error.message}`,
+ )
+ }
+ }
+
+ @Cron(CronExpression.EVERY_HOUR)
+ async sendPredictAiRequest(): Promise<void> {
+ try {
+ await firstValueFrom(this.httpService.post('/run_prediction'))
+ } catch (error) {
+ this.logger.error(
+ `Error triggering prediction from AI service: ${error.message}`,
+ )
+ }
+ }
}