File size: 3,712 Bytes
4e83772 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 | import { AnalyticsQueryDTO } from "@/dtos/analytics.dto.js";
import { IAnalyticsController } from "@/interfaces/controller/IAnalyticsController.js";
import { IAnalyticsService } from "@/interfaces/service/IAnalyticsService.js";
import { AnalyticsMapper } from "@/mappers/AnalyticsMapper.js";
import { STATUS_CODES } from "@/utils/statusCodes.js";
import { Request, Response, NextFunction } from "express";
class AnalyticsController implements IAnalyticsController {
private analyticsService: IAnalyticsService;
constructor(analyticsService: IAnalyticsService) {
this.analyticsService = analyticsService;
}
async clearAnalytics(req: Request, res: Response, next: NextFunction): Promise<void> {
try {
const { projectId } = req.params;
await this.analyticsService.clearAnalytics(projectId);
res.status(STATUS_CODES.NO_CONTENT).json({ ok: true });
} catch (error) {
next(error);
}
}
async bandWidth(req: Request, res: Response, next: NextFunction): Promise<void> {
const { projectId } = req.params;
const { range, interval, limit } = req.validatedQuery as AnalyticsQueryDTO;
const userPlan = req.user?.plan as string;
try {
const [data, queryOptions] = await this.analyticsService.getBandwidthData(projectId, userPlan, {
range: range as string,
interval: interval as string,
});
const response = AnalyticsMapper.bandwidthResponseDTO(data, projectId, queryOptions);
res.json(response);
return;
} catch (error) {
next(error);
}
}
async overview(req: Request, res: Response, next: NextFunction): Promise<void> {
const { projectId } = req.params;
const { range, interval } = req.validatedQuery as AnalyticsQueryDTO;
const userPlan = req.user?.plan as string;
try {
const [data, queryOptions] = await this.analyticsService.getOverView(projectId, userPlan, {
range: range as string,
interval: interval as string,
});
const response = AnalyticsMapper.overviewResponse(data, projectId, queryOptions);
res.json(response);
return;
} catch (error) {
next(error);
}
}
async realtime(req: Request, res: Response, next: NextFunction): Promise<void> {
const { projectId } = req.params;
const { interval } = req.validatedQuery as AnalyticsQueryDTO;
try {
const [data, queryOptions] = await this.analyticsService.getRealtime(projectId, { interval: interval as string });
const response = AnalyticsMapper.realtimeResponse(data, projectId, queryOptions);
res.json(response);
return;
} catch (error) {
next(error);
}
}
async topPages(req: Request, res: Response, next: NextFunction): Promise<void> {
const { projectId } = req.params;
const { range, limit } = req.validatedQuery as AnalyticsQueryDTO;
const userPlan = req.user?.plan as string;
try {
const [data, queryOptions] = await this.analyticsService.getTopPages(projectId, userPlan, {
range: range as string,
limit: Number(limit || 30),
});
const response = AnalyticsMapper.topPagesResponse(data, projectId, queryOptions);
res.json(response);
return;
} catch (error) {
next(error);
}
}
async platformStats(req: Request, res: Response, next: NextFunction): Promise<void> {
const { projectId } = req.params;
const { range, limit } = req.validatedQuery as AnalyticsQueryDTO;
const userPlan = req.user?.plan as string;
try {
const [data, queryOptions] = await this.analyticsService.getPlatformStats(projectId, userPlan, {
range: range as string,
limit: Number(limit || 30),
});
const response = AnalyticsMapper.platformResponse(data, projectId, queryOptions);
res.json(response);
return;
} catch (error) {
next(error);
}
}
}
export default AnalyticsController;
|