diff --git a/src/metrics-and-logs/access-logs/access-logs.service.ts b/src/metrics-and-logs/access-logs/access-logs.service.ts index 974b260b4..a850992b2 100644 --- a/src/metrics-and-logs/access-logs/access-logs.service.ts +++ b/src/metrics-and-logs/access-logs/access-logs.service.ts @@ -7,7 +7,6 @@ export class AccessLogsService { @InjectModel(AccessLog.name) private accessLogModel: Model, ) {} - // Method to log/store metrics async create(accessLogData: AccessLog): Promise { const accessLog = new this.accessLogModel(accessLogData); diff --git a/src/metrics-and-logs/metrics/metrics.controller.ts b/src/metrics-and-logs/metrics/metrics.controller.ts index 1ea8794df..eaf416f4d 100644 --- a/src/metrics-and-logs/metrics/metrics.controller.ts +++ b/src/metrics-and-logs/metrics/metrics.controller.ts @@ -1,20 +1,49 @@ -import { Controller } from "@nestjs/common"; -import { ApiTags } from "@nestjs/swagger"; +import { Controller, Get, Query } from "@nestjs/common"; +import { ApiOperation, ApiQuery, ApiTags } from "@nestjs/swagger"; +import { MetricsService } from "./metrics.service"; @ApiTags("metrics") @Controller("metrics") export class MetricsController { - constructor() {} - logMetrics( - user: string | null, - ip: string | undefined, - userAgent: string | undefined, - endpoint: string, - statusCode: number, - responseTime: number, + constructor(private readonly metricsService: MetricsService) {} + + //TODO: correct checkpolies and api description + @Get("/find") + @ApiOperation({ summary: "Get metrics" }) + @ApiQuery({ + name: "query", + description: `{ "endpoint": { "$regex": "20.500.12269", "$options": "i" } } \n +{ "userId": "example" } \n +{ "$or": [ { "userId": "example" }, { "endpoint": { "$regex": "20.500.12269", "$options": "i" } } ] }`, + required: false, + type: String, + }) + @ApiQuery({ + name: "projection", + description: '{"userId": 1, "endpoint": 1, "createdAt": 1}', + required: false, + type: String, + }) + @ApiQuery({ + name: "options", + description: + "{ limit: 10, skip: 5, sort: { createdAt: -1 }, maxTimeMS: 1000, hint: { createdAt: 1 } }", + required: false, + type: String, + }) + async find( + @Query("query") query: string, + @Query("projection") + projection?: string, + @Query("options") options?: string, ) { - console.log( - `User ID: ${user}, IP: ${ip}, User-Agent: ${userAgent}, Endpoint: ${endpoint}, Status Code: ${statusCode}, Response Time: ${responseTime}ms`, + const parsedQuery = query ? JSON.parse(query) : {}; + const parsedOption = options ? JSON.parse(options) : {}; + const parsedProjection = projection ? JSON.parse(projection) : null; + return this.metricsService.find( + parsedQuery, + parsedProjection, + parsedOption, ); } } diff --git a/src/metrics-and-logs/metrics/metrics.module.ts b/src/metrics-and-logs/metrics/metrics.module.ts index 873a5c306..308bc167b 100644 --- a/src/metrics-and-logs/metrics/metrics.module.ts +++ b/src/metrics-and-logs/metrics/metrics.module.ts @@ -3,6 +3,7 @@ import { MetricsService } from "./metrics.service"; import { MetricsController } from "./metrics.controller"; import { MongooseModule } from "@nestjs/mongoose"; import { Metrics, MetricsSchema } from "./schemas/metrics.schema"; +import { AccessLogsModule } from "../access-logs/access-logs.module"; @Module({ imports: [ @@ -12,6 +13,7 @@ import { Metrics, MetricsSchema } from "./schemas/metrics.schema"; schema: MetricsSchema, }, ]), + AccessLogsModule, ], providers: [MetricsService, MetricsController], exports: [], diff --git a/src/metrics-and-logs/metrics/metrics.service.ts b/src/metrics-and-logs/metrics/metrics.service.ts index 86fdc7966..10d9ae444 100644 --- a/src/metrics-and-logs/metrics/metrics.service.ts +++ b/src/metrics-and-logs/metrics/metrics.service.ts @@ -1,31 +1,24 @@ import { InjectModel } from "@nestjs/mongoose"; import { Metrics, MetricsDocument } from "./schemas/metrics.schema"; -import { Model } from "mongoose"; -import { Logger } from "@nestjs/common"; +import { FilterQuery, Model, ProjectionType, QueryOptions } from "mongoose"; +import { AccessLogsService } from "../access-logs/access-logs.service"; export class MetricsService { - accessLogsService: any; constructor( @InjectModel(Metrics.name) private metricsModel: Model, + private readonly accessLogsService: AccessLogsService, ) {} - - async createMetric(metricData: Partial): Promise { - const metric = new this.metricsModel(metricData); - return metric.save(); - } - - async generateCompactMetrics() { - Logger.log("Starting metrics compaction..."); - const rawLogs = await this.accessLogsService.findLogs({}); - // Compact logic here - const compactedMetrics = this.compactLogs(rawLogs); - await this.createMetric(compactedMetrics); - Logger.log("Metrics compaction completed."); + async create(metricsData: Metrics): Promise { + const metrics = new this.metricsModel(metricsData); + return metrics.save(); } - private compactLogs(logs: any[]): Partial { - // Implement compaction logic - return {}; + async find( + query: FilterQuery, + projection: ProjectionType | null | undefined, + options: QueryOptions | null | undefined, + ): Promise { + return this.metricsModel.find(query, projection, options).exec(); } }