Skip to content

Commit

Permalink
feat: add durationAvg field
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
intpro and Boris Dorofeev authored May 25, 2020
1 parent 1013f4f commit 2df6aaf
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/types/job/Job.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export function getJobTC(sc: SchemaComposer<any>, opts: Options) {
data: jobDataTC,
progress: 'Int',
delay: 'Int',
timestamp: 'Date!',
timestamp: 'Date',
attemptsMade: 'Int',
failedReason: 'JSON',
stacktrace: '[String!]',
Expand Down
31 changes: 31 additions & 0 deletions src/types/queue/Queue.durationAvg.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { SchemaComposer, ObjectTypeComposerFieldConfigDefinition } from 'graphql-compose';
import { Queue } from 'bullmq';
import { Options } from '../../definitions';

export function createDurationAvgFC(
sc: SchemaComposer<any>,
opts: Options
): ObjectTypeComposerFieldConfigDefinition<any, any> {
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);
},
};
}
2 changes: 2 additions & 0 deletions src/types/queue/Queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -27,6 +28,7 @@ export function getQueueTC(sc: SchemaComposer<any>, opts: Options) {
jobsDelayed: createDelayedJobsFC(sc, opts),
jobsFailed: createFailedJobsFC(sc, opts),
activeWorkers: createWorkersTC(sc, opts),
durationAvg: createDurationAvgFC(sc, opts),
});
});
}

0 comments on commit 2df6aaf

Please sign in to comment.