-
Notifications
You must be signed in to change notification settings - Fork 0
Island rhythms/task performance #167
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
4688202
4de389e
125c91b
c4b2a89
a30524d
4349b05
308e2e9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,119 @@ | ||
| 'use strict'; | ||
|
|
||
| const Archetype = require('archetype'); | ||
|
|
||
| const GetTaskOverviewParams = new Archetype({ | ||
| start: { $type: Date }, | ||
| end: { $type: Date }, | ||
| status: { $type: 'string' }, | ||
| name: { $type: 'string' } | ||
| }).compile('GetTaskOverviewParams'); | ||
|
|
||
| /** Statuses shown on the Task overview page. */ | ||
| const OVERVIEW_STATUSES = ['pending', 'succeeded', 'failed', 'cancelled']; | ||
|
|
||
| function ensureDate(value) { | ||
| if (value == null) return value; | ||
| if (value instanceof Date) return value; | ||
| if (typeof value === 'string' || typeof value === 'number') { | ||
| const d = new Date(value); | ||
| if (!Number.isNaN(d.getTime())) return d; | ||
| } | ||
| return value; | ||
| } | ||
IslandRhythms marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| function escapeRegex(str) { | ||
| return String(str).replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); | ||
IslandRhythms marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
IslandRhythms marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| function buildMatch(params) { | ||
| const { start, end, status, name } = params; | ||
| const match = {}; | ||
| const startDate = ensureDate(start); | ||
| const endDate = ensureDate(end); | ||
| if (startDate != null && endDate != null) { | ||
| match.scheduledAt = { $gte: startDate, $lt: endDate }; | ||
| } else if (startDate != null) { | ||
| match.scheduledAt = { $gte: startDate }; | ||
| } | ||
| const statusVal = typeof status === 'string' ? status.trim() : status; | ||
| if (statusVal != null && statusVal !== '') { | ||
| match.status = statusVal; | ||
| } else { | ||
| match.status = { $in: ['pending', 'in_progress', 'succeeded', 'failed', 'cancelled', 'unknown'] }; | ||
| } | ||
| if (name != null && name !== '') { | ||
| const nameStr = typeof name === 'string' ? name.trim() : String(name); | ||
| match.name = { $regex: escapeRegex(nameStr), $options: 'i' }; | ||
| } | ||
| return match; | ||
| } | ||
|
|
||
| module.exports = ({ db }) => async function getTaskOverview(params) { | ||
| params = new GetTaskOverviewParams(params); | ||
| params.start = ensureDate(params.start); | ||
| params.end = ensureDate(params.end); | ||
| if (typeof params.status === 'string') params.status = params.status.trim(); | ||
| if (typeof params.name === 'string') params.name = params.name.trim(); | ||
| const { Task } = db.models; | ||
| const match = buildMatch(params); | ||
|
|
||
vkarpov15 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| const defaultCounts = OVERVIEW_STATUSES.map(s => ({ k: s, v: 0 })); | ||
|
|
||
| const pipeline = [ | ||
| { $match: match }, | ||
| { | ||
| $facet: { | ||
| statusCounts: [ | ||
| { $group: { _id: { $ifNull: ['$status', 'unknown'] }, count: { $sum: 1 } } }, | ||
| { $group: { _id: null, counts: { $push: { k: '$_id', v: '$count' } } } }, | ||
| { | ||
| $project: { | ||
| statusCounts: { | ||
| $arrayToObject: { | ||
| $concatArrays: [{ $literal: defaultCounts }, '$counts'] | ||
| } | ||
| } | ||
| } | ||
| }, | ||
| { $replaceRoot: { newRoot: '$statusCounts' } } | ||
| ], | ||
| tasksByName: [ | ||
| { | ||
| $group: { | ||
| _id: '$name', | ||
| totalCount: { $sum: 1 }, | ||
| lastRun: { $max: '$scheduledAt' }, | ||
| pending: { $sum: { $cond: [{ $eq: ['$status', 'pending'] }, 1, 0] } }, | ||
| succeeded: { $sum: { $cond: [{ $eq: ['$status', 'succeeded'] }, 1, 0] } }, | ||
| failed: { $sum: { $cond: [{ $eq: ['$status', 'failed'] }, 1, 0] } }, | ||
| cancelled: { $sum: { $cond: [{ $eq: ['$status', 'cancelled'] }, 1, 0] } } | ||
| } | ||
| }, | ||
| { | ||
| $project: { | ||
| _id: 0, | ||
| name: '$_id', | ||
| totalCount: 1, | ||
| lastRun: 1, | ||
| statusCounts: { | ||
| pending: '$pending', | ||
| succeeded: '$succeeded', | ||
| failed: '$failed', | ||
| cancelled: '$cancelled' | ||
| } | ||
| } | ||
|
Comment on lines
+66
to
+88
|
||
| }, | ||
| { $sort: { name: 1 } } | ||
| ] | ||
| } | ||
| } | ||
| ]; | ||
|
|
||
| const [result] = await Task.aggregate(pipeline); | ||
|
|
||
| return { | ||
| statusCounts: result.statusCounts?.[0] ?? {}, | ||
| tasksByName: result.tasksByName || [] | ||
| }; | ||
| }; | ||
vkarpov15 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Uh oh!
There was an error while loading. Please reload this page.