-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6a1cda5
commit 72084b7
Showing
4 changed files
with
55 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<MetricsDocument>, | ||
private readonly accessLogsService: AccessLogsService, | ||
) {} | ||
|
||
async createMetric(metricData: Partial<Metrics>): Promise<Metrics> { | ||
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<Metrics> { | ||
const metrics = new this.metricsModel(metricsData); | ||
return metrics.save(); | ||
} | ||
|
||
private compactLogs(logs: any[]): Partial<Metrics> { | ||
// Implement compaction logic | ||
return {}; | ||
async find( | ||
query: FilterQuery<MetricsDocument>, | ||
projection: ProjectionType<MetricsDocument> | null | undefined, | ||
options: QueryOptions<MetricsDocument> | null | undefined, | ||
): Promise<Metrics[]> { | ||
return this.metricsModel.find(query, projection, options).exec(); | ||
} | ||
} |