Skip to content

Commit 2df6aaf

Browse files
intproBoris Dorofeev
andauthored
feat: add durationAvg field
* fix: job name not necessarily * fix: job timestamp not necessary * feat: completed jobs duration average field Co-authored-by: Boris Dorofeev <bdorofeev@bdorofeev-laptop.corp.ps.kz>
1 parent 1013f4f commit 2df6aaf

File tree

3 files changed

+34
-1
lines changed

3 files changed

+34
-1
lines changed

src/types/job/Job.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export function getJobTC(sc: SchemaComposer<any>, opts: Options) {
1414
data: jobDataTC,
1515
progress: 'Int',
1616
delay: 'Int',
17-
timestamp: 'Date!',
17+
timestamp: 'Date',
1818
attemptsMade: 'Int',
1919
failedReason: 'JSON',
2020
stacktrace: '[String!]',
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { SchemaComposer, ObjectTypeComposerFieldConfigDefinition } from 'graphql-compose';
2+
import { Queue } from 'bullmq';
3+
import { Options } from '../../definitions';
4+
5+
export function createDurationAvgFC(
6+
sc: SchemaComposer<any>,
7+
opts: Options
8+
): ObjectTypeComposerFieldConfigDefinition<any, any> {
9+
return {
10+
type: 'Int!',
11+
args: {
12+
limit: {
13+
type: 'Int',
14+
defaultValue: 1000,
15+
},
16+
},
17+
resolve: async (queue: Queue, { limit }) => {
18+
const jobs = await queue.getCompleted(0, limit);
19+
20+
const amount = jobs.reduce((acc, job) => {
21+
if (job?.finishedOn && job?.processedOn) {
22+
return acc + job.finishedOn - job?.processedOn;
23+
}
24+
25+
return acc;
26+
}, 0);
27+
28+
return (amount / jobs.length).toFixed(0);
29+
},
30+
};
31+
}

src/types/queue/Queue.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { createActiveJobsFC } from './Queue.activeJobs';
88
import { createDelayedJobsFC } from './Queue.delayedJobs';
99
import { createFailedJobsFC } from './Queue.failedJobs';
1010
import { createWorkersTC } from './Queue.workers';
11+
import { createDurationAvgFC } from './Queue.durationAvg';
1112
import { SchemaComposer } from 'graphql-compose';
1213
import { Options } from '../../definitions';
1314

@@ -27,6 +28,7 @@ export function getQueueTC(sc: SchemaComposer<any>, opts: Options) {
2728
jobsDelayed: createDelayedJobsFC(sc, opts),
2829
jobsFailed: createFailedJobsFC(sc, opts),
2930
activeWorkers: createWorkersTC(sc, opts),
31+
durationAvg: createDurationAvgFC(sc, opts),
3032
});
3133
});
3234
}

0 commit comments

Comments
 (0)