Skip to content

Commit bc41b4e

Browse files
committed
core: add printTask metrics
1 parent d3007a5 commit bc41b4e

File tree

3 files changed

+21
-4
lines changed

3 files changed

+21
-4
lines changed

packages/server/handler/client.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ class ClientPrintConnectHandler extends Handler {
6666
logger.error(e);
6767
}
6868
this.response.body = { doc: code };
69-
if (params.printer && params.printer !== code.printer) this.response.body.setPrinter = code.printer;
69+
if (params.printer && params.printer !== client.printer) this.response.body.setPrinter = client.printer;
70+
await this.ctx.parallel('print/sendTask');
7071
logger.info(`Client ${client.name} connected, print task ${code.tid}#${code._id} sent.`);
7172
await this.ctx.db.code.updateOne({ _id: code._id }, { $set: { printer: params.cid, receivedAt: new Date().getTime() } });
7273
}
@@ -80,6 +81,7 @@ class ClientPrintDoneHandler extends Handler {
8081
if (!code) throw new ValidationError('Code', null, 'Code not found');
8182
if (code.printer !== params.cid) throw new BadRequestError('Client', null, 'Client not match');
8283
await this.ctx.db.code.updateOne({ _id: params.tid }, { $set: { done: 1, doneAt: new Date().getTime() } });
84+
await this.ctx.parallel('print/doneTask');
8385
this.response.body = { code: 1 };
8486
logger.info(`Client ${client.name} connected, print task ${code.tid}#${code._id} completed.`);
8587
}

packages/server/handler/printer.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ class CodeHandler extends Handler {
8181
fs.writeFileSync(path.resolve(process.cwd(), 'data/codes', `${team}#${res._id}`), code || fs.readFileSync(this.request.files.file.filepath));
8282
this.response.body = `The code has been submitted. Code Print ID: ${team}#${res._id}`;
8383
logger.info(`Team(${team}): ${tname} submitted code. Code Print ID: ${team}#${res._id}`);
84+
await this.ctx.parallel('print/newTask');
8485
if (tname.length > 40) {
8586
logger.warn(`Team ${tname} name is too long, may cause overflow!`);
8687
this.response.body += ', your team name is too long, may cause print failed!';

packages/server/utils/metrics.ts

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ declare module 'cordis' {
77
interface Context {
88
metrics: Registry;
99
}
10+
interface Events {
11+
'print/newTask': () => void;
12+
'print/sendTask': () => void;
13+
'print/doneTask': () => void;
14+
}
1015
}
1116

1217
export function createMetricsRegistry(ctx: Context) {
@@ -21,15 +26,24 @@ export function createMetricsRegistry(ctx: Context) {
2126
}
2227

2328
createMetric(Gauge, 'xcpc_machinecount', 'machinecount', {
29+
labelNames: ['status'],
2430
async collect() {
2531
const machines = await ctx.db.monitor.find({});
2632
const onlines = machines.filter((m) => m.updateAt > new Date().getTime() - 1000 * 60);
27-
this.set({ type: 'total' }, machines.length);
28-
this.set({ type: 'online' }, onlines.length);
29-
this.set({ type: 'offline' }, machines.length - onlines.length);
33+
this.set({ status: 'online' }, onlines.length);
34+
this.set({ status: 'offline' }, machines.length - onlines.length);
3035
},
3136
});
3237

38+
const printTaskCounter = createMetric(Counter, 'xcpc_printcount', 'printcount', {
39+
labelNames: ['status'],
40+
});
41+
ctx.on('print/newTask', () => printTaskCounter.inc({ status: 'new' }));
42+
43+
ctx.on('print/sendTask', () => printTaskCounter.inc({ status: 'sent' }));
44+
45+
ctx.on('print/doneTask', () => printTaskCounter.inc({ status: 'done' }));
46+
3347
collectDefaultMetrics({ register: registry });
3448

3549
ctx.set('metrics', registry);

0 commit comments

Comments
 (0)