src/metrics/metrics.controller.ts
metrics
Methods |
|
| Async fetchMetricAndAlertData | |||||||||
fetchMetricAndAlertData(req, query: literal type)
|
|||||||||
Decorators :
@Get('fetch/data')
|
|||||||||
|
Defined in src/metrics/metrics.controller.ts:57
|
|||||||||
|
Parameters :
Returns :
unknown
|
| Async fetchMetricEntries | ||||||
fetchMetricEntries(query: literal type)
|
||||||
Decorators :
@Get('fetch')
|
||||||
|
Defined in src/metrics/metrics.controller.ts:33
|
||||||
|
Parameters :
Returns :
unknown
|
| Async updateMetricsMetadata | |||||||||
updateMetricsMetadata(id: string, requestBody: MetricMetadataDto)
|
|||||||||
Decorators :
@Patch(':id')
|
|||||||||
|
Defined in src/metrics/metrics.controller.ts:129
|
|||||||||
|
Parameters :
Returns :
unknown
|
import {
Controller,
Get,
Patch,
Param,
Body,
Query,
Request,
UsePipes,
ValidationPipe,
} from "@nestjs/common";
import { MetricsService } from "./metrics.service";
import { MetricSources, MetricStatus } from "./entities/metrics.entity";
import { AlertService } from "../alert/alert.service";
import { AlertCategory, AlertStatus } from "../alert/entities/alert.entity";
import { MetricMetadataDto } from "./dto/metrics.dto";
import { groupBy } from "../common/helper";
const validationPipe = new ValidationPipe({
whitelist: true,
forbidNonWhitelisted: true,
});
@Controller("metrics")
export class MetricsController {
constructor(
private readonly metricsService: MetricsService,
private readonly alertService: AlertService,
) {}
@Get("fetch")
@UsePipes(validationPipe)
async fetchMetricEntries(
@Query()
query: {
projectId?: number;
sourceId?: number;
sourceType?: MetricSources;
name?: string;
type?: string;
status?: MetricStatus;
},
) {
const filter = {
projectId: query.projectId ? +query.projectId : undefined,
sourceId: query.sourceId ? +query.sourceId : undefined,
sourceType: query.sourceType,
name: query.name,
type: query.type,
status: query.status ? query.status : MetricStatus.ACTIVE,
};
return this.metricsService.fetchMetricEntries(filter);
}
@Get("fetch/data")
@UsePipes(validationPipe)
async fetchMetricAndAlertData(
@Request() req,
@Query()
query: {
startTime?: number;
endTime?: number;
sourceId: number;
sourceType: MetricSources;
},
) {
const userId = req.user.id;
const endTime = new Date();
const startTime = new Date(new Date().setDate(endTime.getDate() - 1));
const queryOptions = {
startTime: startTime.getTime(),
endTime: endTime.getTime(),
};
if ("startTime" in query) {
queryOptions["startTime"] = +query["startTime"];
}
if ("endTime" in query) {
queryOptions["endTime"] = +query["endTime"];
}
const metricsData = await this.metricsService.fetchMetricsData({
source: {
id: +query.sourceId,
type: query.sourceType,
},
startTime: new Date(queryOptions.startTime),
endTime: new Date(queryOptions.endTime),
});
const userAlerts = await this.alertService.findMany({
userId: userId,
status: { notIn: [AlertStatus.DELETED] },
});
const alertTriggers = await this.alertService.getAlertTriggers({
startTime: new Date(queryOptions.startTime),
endTime: new Date(queryOptions.endTime),
targetTable: [
this.alertService.alertCategoryToTableMap[AlertCategory.METRIC],
],
targetField: [
this.alertService.alertCategoryToFieldMap[AlertCategory.METRIC],
],
targetId: metricsData["targetIds"],
alertId: userAlerts.map((alert) => alert.id),
});
const groupedAlertTriggers = groupBy(
alertTriggers,
({ alertId }) => alertId,
);
const alerts = await this.alertService.findMany({
id: { in: Object.keys(groupedAlertTriggers).map((k) => +k) },
});
return {
alerts: alerts,
alertTriggers: groupedAlertTriggers,
metricEntries: metricsData["metricEntries"],
metricsData: metricsData["metricsData"],
};
}
@Patch(":id")
@UsePipes(validationPipe)
async updateMetricsMetadata(
@Param("id") id: string,
@Body() requestBody: MetricMetadataDto,
) {
return this.metricsService.updateMetricsMetadata(+id, requestBody);
}
}