From 2df6aaffc3881703e503a16779ac81b358b531a0 Mon Sep 17 00:00:00 2001 From: intpro Date: Mon, 25 May 2020 17:10:16 +0600 Subject: [PATCH] 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 --- src/types/job/Job.ts | 2 +- src/types/queue/Queue.durationAvg.ts | 31 ++++++++++++++++++++++++++++ src/types/queue/Queue.ts | 2 ++ 3 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 src/types/queue/Queue.durationAvg.ts diff --git a/src/types/job/Job.ts b/src/types/job/Job.ts index e8357e4..6f01a1d 100644 --- a/src/types/job/Job.ts +++ b/src/types/job/Job.ts @@ -14,7 +14,7 @@ export function getJobTC(sc: SchemaComposer, opts: Options) { data: jobDataTC, progress: 'Int', delay: 'Int', - timestamp: 'Date!', + timestamp: 'Date', attemptsMade: 'Int', failedReason: 'JSON', stacktrace: '[String!]', diff --git a/src/types/queue/Queue.durationAvg.ts b/src/types/queue/Queue.durationAvg.ts new file mode 100644 index 0000000..ef15419 --- /dev/null +++ b/src/types/queue/Queue.durationAvg.ts @@ -0,0 +1,31 @@ +import { SchemaComposer, ObjectTypeComposerFieldConfigDefinition } from 'graphql-compose'; +import { Queue } from 'bullmq'; +import { Options } from '../../definitions'; + +export function createDurationAvgFC( + sc: SchemaComposer, + opts: Options +): ObjectTypeComposerFieldConfigDefinition { + return { + type: 'Int!', + args: { + limit: { + type: 'Int', + defaultValue: 1000, + }, + }, + resolve: async (queue: Queue, { limit }) => { + const jobs = await queue.getCompleted(0, limit); + + const amount = jobs.reduce((acc, job) => { + if (job?.finishedOn && job?.processedOn) { + return acc + job.finishedOn - job?.processedOn; + } + + return acc; + }, 0); + + return (amount / jobs.length).toFixed(0); + }, + }; +} diff --git a/src/types/queue/Queue.ts b/src/types/queue/Queue.ts index 4078e56..0828773 100644 --- a/src/types/queue/Queue.ts +++ b/src/types/queue/Queue.ts @@ -8,6 +8,7 @@ import { createActiveJobsFC } from './Queue.activeJobs'; import { createDelayedJobsFC } from './Queue.delayedJobs'; import { createFailedJobsFC } from './Queue.failedJobs'; import { createWorkersTC } from './Queue.workers'; +import { createDurationAvgFC } from './Queue.durationAvg'; import { SchemaComposer } from 'graphql-compose'; import { Options } from '../../definitions'; @@ -27,6 +28,7 @@ export function getQueueTC(sc: SchemaComposer, opts: Options) { jobsDelayed: createDelayedJobsFC(sc, opts), jobsFailed: createFailedJobsFC(sc, opts), activeWorkers: createWorkersTC(sc, opts), + durationAvg: createDurationAvgFC(sc, opts), }); }); }