feat: implement project settings management with CRUD operations and caching
This commit is contained in:
@ -18,6 +18,7 @@ import { LoggerService } from "../logger/logger.service";
|
||||
import { TLogType } from "../logger/logger.types";
|
||||
import { QueryResponse } from "src/vm/vm.constants";
|
||||
import { Query } from "../entities/query.entity";
|
||||
import { Token } from "src/api/entities/token.entity";
|
||||
|
||||
@UseGuards(ApiTokenGuard)
|
||||
export abstract class BaseQueryController {
|
||||
@ -34,9 +35,16 @@ export abstract class BaseQueryController {
|
||||
|
||||
@Post("create")
|
||||
async createQuery(
|
||||
@Body() queryData: { projectToken: string; source: string }
|
||||
@Req() req: Request & { apiToken: Token },
|
||||
@Body() queryData: { source: string }
|
||||
) {
|
||||
return this.queryHandlerService.createQuery(queryData, this.getIsCommand());
|
||||
return this.queryHandlerService.createQuery(
|
||||
{
|
||||
...queryData,
|
||||
projectToken: req?.apiToken.project.id,
|
||||
},
|
||||
this.getIsCommand()
|
||||
);
|
||||
}
|
||||
|
||||
@Post("update/:id")
|
||||
|
||||
@ -18,6 +18,7 @@ import { FunctionService } from "src/query/function/function.service";
|
||||
import { SessionService } from "../session/session.service";
|
||||
import { TLog, TLogType } from "../logger/logger.types";
|
||||
import { LoggerService } from "../logger/logger.service";
|
||||
import { ProjectSettingService } from "src/project/settings/project.setting.service";
|
||||
|
||||
@Injectable()
|
||||
export class QueryExecuterService {
|
||||
@ -31,6 +32,7 @@ export class QueryExecuterService {
|
||||
readonly databaseManagerService: DatabaseManagerService,
|
||||
readonly redisNodeService: RedisNodeService,
|
||||
readonly sessionService: SessionService,
|
||||
readonly projectSettingService: ProjectSettingService,
|
||||
@InjectQueue(QUEUE_NAMES.QUERY) private queryQueue: Queue
|
||||
) {
|
||||
this.queueEvents = new QueueEvents(this.queryQueue.name);
|
||||
|
||||
@ -1,6 +1,7 @@
|
||||
import { Controller, Inject, Post, UseGuards } from "@nestjs/common";
|
||||
import { Controller, Inject, Post, Req, UseGuards } from "@nestjs/common";
|
||||
import { ApiTokenGuard } from "src/api/guards/api-token.guard";
|
||||
import { FunctionService } from "./function.service";
|
||||
import { Token } from "src/api/entities/token.entity";
|
||||
|
||||
@Controller("functions")
|
||||
@UseGuards(ApiTokenGuard)
|
||||
@ -11,12 +12,19 @@ export class FunctionController {
|
||||
) {}
|
||||
|
||||
@Post("create")
|
||||
async createFunction(projectId: string, name: string, source: string) {
|
||||
return this.functionService.create(projectId, name, source);
|
||||
async createFunction(
|
||||
@Req() req: Request & { apiToken: Token },
|
||||
name: string,
|
||||
source: string
|
||||
) {
|
||||
return this.functionService.create(req.apiToken.project.id, name, source);
|
||||
}
|
||||
|
||||
@Post("delete")
|
||||
async deleteFunction(projectId: string, name: string) {
|
||||
return this.functionService.deleteFunction(projectId, name);
|
||||
async deleteFunction(
|
||||
@Req() req: Request & { apiToken: Token },
|
||||
name: string
|
||||
) {
|
||||
return this.functionService.deleteFunction(req.apiToken.project.id, name);
|
||||
}
|
||||
}
|
||||
|
||||
@ -18,6 +18,8 @@ import { SessionService } from "./session/session.service";
|
||||
import { Log } from "./logger/entities/log.entity";
|
||||
import { LoggerService } from "./logger/logger.service";
|
||||
import { LoggerController } from "./logger/logger.controller";
|
||||
import { ProjectSettingService } from "src/project/settings/project.setting.service";
|
||||
import { ProjectSetting } from "src/project/settings/entities/project.setting.entity";
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
@ -27,7 +29,7 @@ import { LoggerController } from "./logger/logger.controller";
|
||||
forwardRef(() => QueueModule),
|
||||
forwardRef(() => RedisModule),
|
||||
forwardRef(() => RedisManagerModule),
|
||||
TypeOrmModule.forFeature([Query, FunctionEntity, Log]),
|
||||
TypeOrmModule.forFeature([Query, FunctionEntity, Log, ProjectSetting]),
|
||||
],
|
||||
controllers: [
|
||||
QueryController,
|
||||
@ -41,12 +43,14 @@ import { LoggerController } from "./logger/logger.controller";
|
||||
LoggerService,
|
||||
QueryHandlerService,
|
||||
FunctionService,
|
||||
ProjectSettingService,
|
||||
],
|
||||
exports: [
|
||||
QueryExecuterService,
|
||||
TypeOrmModule,
|
||||
QueryHandlerService,
|
||||
LoggerService,
|
||||
ProjectSettingService,
|
||||
],
|
||||
})
|
||||
export class QueryModule {}
|
||||
|
||||
@ -1,9 +1,15 @@
|
||||
import { Inject, Injectable } from "@nestjs/common";
|
||||
import { ProjectSettingService } from "src/project/settings/project.setting.service";
|
||||
import { RedisClient } from "src/redis/redis.service";
|
||||
|
||||
@Injectable()
|
||||
export class SessionService {
|
||||
constructor(@Inject(RedisClient) private readonly redisClient: RedisClient) {}
|
||||
constructor(
|
||||
@Inject(RedisClient)
|
||||
private readonly redisClient: RedisClient,
|
||||
@Inject(ProjectSettingService)
|
||||
private readonly projectSettingService: ProjectSettingService
|
||||
) {}
|
||||
|
||||
private generateSessionId(length: number = 16): string {
|
||||
const characters =
|
||||
@ -16,6 +22,11 @@ export class SessionService {
|
||||
return `${result}_${new Date().getTime()}`;
|
||||
}
|
||||
|
||||
async getSessionTTL(projectId: string): Promise<number> {
|
||||
const ttl = await this.projectSettingService.get("sessionTTL", projectId);
|
||||
return ttl ? parseInt(ttl) : 3600;
|
||||
}
|
||||
|
||||
async create(prefix: string): Promise<{ sessionId: string }> {
|
||||
const sessionId = this.generateSessionId();
|
||||
await this.set(sessionId, prefix, {
|
||||
@ -33,7 +44,11 @@ export class SessionService {
|
||||
const data = await this.redisClient.get(`${prefix}:${sessionId}`);
|
||||
|
||||
if (data) {
|
||||
await this.redisClient.set(`${prefix}:${sessionId}`, data, 3600);
|
||||
await this.redisClient.set(
|
||||
`${prefix}:${sessionId}`,
|
||||
data,
|
||||
await this.getSessionTTL(prefix)
|
||||
);
|
||||
return data;
|
||||
}
|
||||
|
||||
@ -41,7 +56,11 @@ export class SessionService {
|
||||
}
|
||||
|
||||
async set(sessionId: string, prefix: string, data: any): Promise<void> {
|
||||
await this.redisClient.set(`${prefix}:${sessionId}`, data, 3600);
|
||||
await this.redisClient.set(
|
||||
`${prefix}:${sessionId}`,
|
||||
data,
|
||||
await this.getSessionTTL(prefix)
|
||||
);
|
||||
}
|
||||
|
||||
async delete(sessionId: string, prefix: string): Promise<void> {
|
||||
|
||||
Reference in New Issue
Block a user