Spaces:
Sleeping

File size: 5,269 Bytes
4e83772
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
446bc13
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import { DEPLOYMENT_ERRORS } from "@/constants/errors.js";
import KafkaEventConsumer from "@/events/consumers.js";
import { IDeploymentRepository } from "@/interfaces/repository/IDeploymentRepository.js";
import { ILogRepository, LogModel } from "@/interfaces/repository/ILogRepository.js";
import { ILogsService } from "@/interfaces/service/ILogsService.js";
import { ILogs } from "@/models/Logs.js";
import AppError from "@/utils/AppError.js";
import { STATUS_CODES } from "@/utils/statusCodes.js";
import { ResponseJSON, ResultSet } from "@clickhouse/client";

class LogsService implements ILogsService {
	private logsRepository: ILogRepository;
	private deploymentRepository: IDeploymentRepository;
	private consumer?: KafkaEventConsumer;

	private logsBuffer: ILogs[] = [];
	private readonly BATCH_SIZE = 2000;
	private readonly MAX_BUFFER_SIZE = 10000;
	private isSaving = false;
	private isPaused = false;

	constructor(logRepo: ILogRepository, depRepo: IDeploymentRepository) {
		this.logsRepository = logRepo;
		this.deploymentRepository = depRepo;
	}
	public setConsumer(consumer: KafkaEventConsumer) {
		this.consumer = consumer;
		console.log("Consumer added ...");
	}
	async getDeploymentLog(
		deploymentId: string,
		userId: string,
		pagination: { page?: number; limit: number },
	): Promise<{
		logs: ResponseJSON<unknown>["data"];
		total: number;
		page: number;
		limit: number;
	}> {
		const deployment = await this.deploymentRepository.findDeploymentById(deploymentId, userId);
		if (!deployment) {
			throw new AppError(DEPLOYMENT_ERRORS.NOT_FOUND, STATUS_CODES.NOT_FOUND);
		}
		const { limit, page } = pagination;

		const data = await this.logsRepository.getLogs(deploymentId, page || 1, limit || 100);
		return {
			logs: data.data,
			total: Number((data.data[0] as { total: number })?.total || 0),
			page: page || 1,
			limit: limit || 100,
		};
	}
	async getProjectsLogs(
		projectId: string,
		userId: string,
		pagination: { page?: number; limit?: number },
	): Promise<{
		logs: ResponseJSON<unknown>["data"];
		total: number;
		page: number;
		limit: number;
	}> {
		const { limit, page } = pagination; // projet check herer ------------------------------------------------------
		const data = await this.logsRepository.getProjectLogs(projectId, page || 1, limit || 100);
		return {
			logs: data.data,
			total: Number((data.data[0] as { total: number })?.total || 0),
			page: page || 1,
			limit: limit || 100,
		};
	}
	async deleteProjectLogs(projectId: string): Promise<void> {
		return this.logsRepository.deleteProjectLogs(projectId);
	}
	async deleteDeploymentLogs(deploymentId: string): Promise<void> {
		return this.logsRepository.deletedeploymentLogs(deploymentId);
	}

	async saveBatch(): Promise<void> {
		if (this.isSaving || this.logsBuffer.length === 0) {
			// process.stdout.write(" '");
			return;
		}
		this.isSaving = true;
		const batch = this.logsBuffer.splice(0, this.BATCH_SIZE);
		try {
			await this.__saveLogAsBatch(batch);
			console.log(`Saved ${batch.length} logs `);
		} catch (error) {
			console.error("save logs error:", error, "Discarding data");
		} finally {
			this.isSaving = false;
			if (this.logsBuffer.length >= this.BATCH_SIZE) {
				setImmediate(() => this.saveBatch());
			}
			if (this.isPaused && this.logsBuffer.length < 2000) {
				console.log("Buffer healthy, resuming...");
				this.consumer?.resumeLogs();
				this.isPaused = false;
			}
		}
	}

	async flushLogs(): Promise<void> {
		if (this.isSaving || this.logsBuffer.length === 0) {
			process.stdout.write(" '");
			return;
		}
		console.log("saving ....", this.logsBuffer.length);
		this.isSaving = true;
		try {
			while (this.logsBuffer.length > 0) {
				const logsSlice = this.logsBuffer.splice(0, 200);
				await this.__saveLogAsBatch(logsSlice);
				console.log(`Successfully flushed ${logsSlice.length} logs to DB.`);
			}
		} catch (error) {
			console.error("Failed to flush log batch:", error);
		} finally {
			this.isSaving = false;
		}
	}

	__insertToBatch(logs: ILogs[]): void {
		this.logsBuffer.push(...logs);

		if (this.logsBuffer.length > this.MAX_BUFFER_SIZE) {
			console.warn("Buffer full! Pausing consumer.");
			this.consumer?.pauseLogs();
			this.isPaused = true;
			setImmediate(() => this.saveBatch());
			return;
		}
		// this.flushLogs()     // save to db on arrival instead of interval, use this to save on each message incoming. This will stop once message saving finishes
		if (this.logsBuffer.length >= this.BATCH_SIZE) {
			this.saveBatch();
		}
	}

	async __saveLog(log: string, projectId: string, deploymentId: string, reportTime: Date, info: string, sequence?: number): Promise<void> {
		await this.logsRepository.__insertLog({ deploymentId, log, projectId, reportTime, info, sequence });
	}

	async __saveLogAsBatch(logs: ILogs[]): Promise<void> {
		// await new Promise((res) => setTimeout(res, 2000))
		console.log(logs[0].event_id);
		await this.logsRepository.__insertLogsAsBatch(logs);
	}

	async exitService(): Promise<void> {
		console.log("service cleaning logs....");
		while (this.logsBuffer.length > 0) {
			if (this.isSaving) {
				await new Promise((resolve) => setTimeout(resolve, 500));
				continue;
			}
			await this.saveBatch();
		}
	}
}

export default LogsService;