Przeglądaj źródła

refactor: Report queued jobs in statistics

Owen Diffey 1 rok temu
rodzic
commit
4442a6382a
3 zmienionych plików z 37 dodań i 18 usunięć
  1. 12 6
      backend/src/Job.ts
  2. 3 0
      backend/src/JobQueue.ts
  3. 22 12
      backend/src/JobStatistics.ts

+ 12 - 6
backend/src/Job.ts

@@ -1,5 +1,5 @@
 import JobContext from "@/JobContext";
-import JobStatistics from "@/JobStatistics";
+import JobStatistics, { JobStatisticsType } from "@/JobStatistics";
 import LogBook, { Log } from "@/LogBook";
 import { JobOptions } from "@/types/JobOptions";
 import BaseModule from "./BaseModule";
@@ -84,7 +84,10 @@ export default abstract class Job {
 
 		this._context = new JobContext(this, contextOptions);
 
-		JobStatistics.updateStats(this.getPath(), "added");
+		JobStatistics.updateStats(
+			this.getPath(),
+			JobStatisticsType.CONSTRUCTED
+		);
 	}
 
 	/**
@@ -210,7 +213,10 @@ export default abstract class Job {
 				type: "success"
 			});
 
-			JobStatistics.updateStats(this.getPath(), "successful");
+			JobStatistics.updateStats(
+				this.getPath(),
+				JobStatisticsType.SUCCESSFUL
+			);
 
 			return data;
 		} catch (error: unknown) {
@@ -234,16 +240,16 @@ export default abstract class Job {
 				data: { error }
 			});
 
-			JobStatistics.updateStats(this.getPath(), "failed");
+			JobStatistics.updateStats(this.getPath(), JobStatisticsType.FAILED);
 
 			throw error;
 		} finally {
 			this._completedAt = performance.now();
-			JobStatistics.updateStats(this.getPath(), "total");
+			JobStatistics.updateStats(this.getPath(), JobStatisticsType.TOTAL);
 			if (this._startedAt)
 				JobStatistics.updateStats(
 					this.getPath(),
-					"duration",
+					JobStatisticsType.DURATION,
 					this._completedAt - this._startedAt
 				);
 			this._setStatus(JobStatus.COMPLETED);

+ 3 - 0
backend/src/JobQueue.ts

@@ -1,5 +1,6 @@
 import Job, { JobStatus } from "@/Job";
 import { JobOptions } from "@/types/JobOptions";
+import JobStatistics, { JobStatisticsType } from "./JobStatistics";
 
 export class JobQueue {
 	private _concurrency: number;
@@ -76,6 +77,8 @@ export class JobQueue {
 	): Promise<string> {
 		const job = new JobClass(payload, options);
 
+		JobStatistics.updateStats(job.getPath(), JobStatisticsType.QUEUED);
+
 		this._queue.push(job);
 		this._process();
 

+ 22 - 12
backend/src/JobStatistics.ts

@@ -1,14 +1,21 @@
+export enum JobStatisticsType {
+	SUCCESSFUL = "successful",
+	FAILED = "failed",
+	TOTAL = "total",
+	CONSTRUCTED = "constructed",
+	QUEUED = "queued",
+	DURATION = "duration"
+}
+
 export class JobStatistics {
 	private _stats: Record<
 		string,
-		{
-			successful: number;
-			failed: number;
-			total: number;
-			added: number;
-			averageTime: number;
-			totalTime: number;
-		}
+		Record<
+			| Exclude<JobStatisticsType, "duration">
+			| "averageTime"
+			| "totalTime",
+			number
+		>
 	>;
 
 	public constructor() {
@@ -26,7 +33,8 @@ export class JobStatistics {
 				successful: a.successful + b.successful,
 				failed: a.failed + b.failed,
 				total: a.total + b.total,
-				added: a.added + b.added,
+				constructed: a.constructed + b.constructed,
+				queued: a.queued + b.queued,
 				averageTime: -1,
 				totalTime: a.totalTime + b.totalTime
 			}),
@@ -34,7 +42,8 @@ export class JobStatistics {
 				successful: 0,
 				failed: 0,
 				total: 0,
-				added: 0,
+				constructed: 0,
+				queued: 0,
 				averageTime: -1,
 				totalTime: 0
 			}
@@ -56,7 +65,7 @@ export class JobStatistics {
 	 */
 	public updateStats(
 		jobName: string,
-		type: "successful" | "failed" | "total" | "added" | "duration",
+		type: JobStatisticsType,
 		duration?: number
 	) {
 		if (!this._stats[jobName])
@@ -64,7 +73,8 @@ export class JobStatistics {
 				successful: 0,
 				failed: 0,
 				total: 0,
-				added: 0,
+				constructed: 0,
+				queued: 0,
 				averageTime: 0,
 				totalTime: 0
 			};