Spaces:
Sleeping

File size: 2,838 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
import { Request, Response, NextFunction } from "express";
import { v4 } from "uuid";

import { ILogsController } from "@/interfaces/controller/ILogsController.js";
import { ILogsService } from "@/interfaces/service/ILogsService.js";
import { LogMapper } from "@/mappers/LogsMapper.js";
import { sseManager } from "@/events/deploymentEmitter.js";
import { IDeploymentService } from "@/interfaces/service/IDeploymentService.js";
import { STATUS_CODES } from "@/utils/statusCodes.js";
import { DEPLOYMENT_ERRORS } from "@/constants/errors.js";
import { DeploymentStatus } from "@/models/Deployment.js";

class LogsController implements ILogsController {
	private logsService: ILogsService;
	private deploymentService: IDeploymentService;
	constructor(logsService: ILogsService, deplyService: IDeploymentService) {
		this.logsService = logsService;
		this.deploymentService = deplyService;
	}

	async getLogsByProject(req: Request, res: Response, next: NextFunction): Promise<void> {
		try {
			const projectId = req.params.projectId;
			const user = req.user?.id as string;
			const result = await this.logsService.getProjectsLogs(projectId, user, {});

			const response = LogMapper.toLogsResponse(result.logs, result.total);
			res.json(response);
			return;
		} catch (error) {
			next(error);
		}
	}
	async getLogsByDeployment(req: Request, res: Response, next: NextFunction): Promise<void> {
		try {
			const deploymentId = req.params.deploymentId;
			const user = req.user?.id as string;
			const result = await this.logsService.getDeploymentLog(deploymentId, user, {});

			const response = LogMapper.toLogsResponse(result.logs, result.total);
			res.json(response);
			return;
		} catch (error) {
			next(error);
		}
	}

	async streamLogs(req: Request, res: Response, next: NextFunction): Promise<void> {
		const id = req.params.deploymentId;
		const user = req.user?.id as string;
		try {
			const deplomentExist = await this.deploymentService.getDeploymentById(id, user);
			if (!deplomentExist) {
				res.status(STATUS_CODES.NOT_FOUND).json({
					status: 404,
					error: "not_found",
					message: DEPLOYMENT_ERRORS.NOT_FOUND,
				});
				return;
			}
			if (!(deplomentExist.status === DeploymentStatus.BUILDING || deplomentExist.status === DeploymentStatus.QUEUED)) {
				res.status(STATUS_CODES.CONFLICT).json({
					status: STATUS_CODES.CONFLICT,
					error: "Cant connect to deployment",
					message: DEPLOYMENT_ERRORS.NO_ACTIVE_DEPLOYMENT,
				});
				return;
			}
			sseManager.addClient(v4(), id, res, req);
		} catch (error) {
			next(error);
		}
	}

	async getData(req: Request, res: Response, next: NextFunction): Promise<void> {
		res.json({
			clientCount: sseManager.getClientCount(),
			listerCount: sseManager.getListeners(),
			listerners: sseManager.getEventFns(),
		});
	}
}

export default LogsController;