Skip to content

Commit

Permalink
metrics service init part
Browse files Browse the repository at this point in the history
  • Loading branch information
Junjiequan committed Dec 16, 2024
1 parent 6a1cda5 commit 72084b7
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 32 deletions.
1 change: 0 additions & 1 deletion src/metrics-and-logs/access-logs/access-logs.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ export class AccessLogsService {
@InjectModel(AccessLog.name)
private accessLogModel: Model<AccessLogDocument>,
) {}
// Method to log/store metrics

async create(accessLogData: AccessLog): Promise<AccessLog> {
const accessLog = new this.accessLogModel(accessLogData);
Expand Down
53 changes: 41 additions & 12 deletions src/metrics-and-logs/metrics/metrics.controller.ts
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,
);
}
}
2 changes: 2 additions & 0 deletions src/metrics-and-logs/metrics/metrics.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
Expand All @@ -12,6 +13,7 @@ import { Metrics, MetricsSchema } from "./schemas/metrics.schema";
schema: MetricsSchema,
},
]),
AccessLogsModule,
],
providers: [MetricsService, MetricsController],
exports: [],
Expand Down
31 changes: 12 additions & 19 deletions src/metrics-and-logs/metrics/metrics.service.ts
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();
}
}

0 comments on commit 72084b7

Please sign in to comment.