@mongoosejs/studio 0.3.6 → 0.3.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,66 @@
1
+ 'use strict';
2
+
3
+ const Archetype = require('archetype');
4
+
5
+ const GetTasksOverTimeParams = new Archetype({
6
+ start: { $type: Date },
7
+ end: { $type: Date },
8
+ bucketSizeMs: { $type: 'number' }
9
+ }).compile('GetTasksOverTimeParams');
10
+
11
+ const TRACKED_STATUSES = ['succeeded', 'failed', 'cancelled'];
12
+
13
+ module.exports = ({ db }) => async function getTasksOverTime(params) {
14
+ params = new GetTasksOverTimeParams(params);
15
+ const { Task } = db.models;
16
+ const { start, end, bucketSizeMs } = params;
17
+
18
+ const bucketMs = (bucketSizeMs != null && bucketSizeMs > 0) ? bucketSizeMs : 5 * 60 * 1000;
19
+
20
+ const match = { status: { $in: TRACKED_STATUSES } };
21
+ if (start != null && end != null) {
22
+ match.scheduledAt = { $gte: start, $lt: end };
23
+ } else if (start != null) {
24
+ match.scheduledAt = { $gte: start };
25
+ }
26
+
27
+ const pipeline = [
28
+ { $match: match },
29
+ {
30
+ $project: {
31
+ status: 1,
32
+ bucket: {
33
+ $toDate: {
34
+ $multiply: [
35
+ { $floor: { $divide: [{ $toLong: '$scheduledAt' }, bucketMs] } },
36
+ bucketMs
37
+ ]
38
+ }
39
+ }
40
+ }
41
+ },
42
+ {
43
+ $group: {
44
+ _id: { bucket: '$bucket', status: '$status' },
45
+ count: { $sum: 1 }
46
+ }
47
+ },
48
+ {
49
+ $group: {
50
+ _id: '$_id.bucket',
51
+ counts: { $push: { status: '$_id.status', count: '$count' } }
52
+ }
53
+ },
54
+ { $sort: { _id: 1 } }
55
+ ];
56
+
57
+ const results = await Task.aggregate(pipeline);
58
+
59
+ return results.map(r => {
60
+ const bucket = { timestamp: r._id, succeeded: 0, failed: 0, cancelled: 0 };
61
+ for (const { status, count } of r.counts) {
62
+ if (status in bucket) bucket[status] = count;
63
+ }
64
+ return bucket;
65
+ });
66
+ };
@@ -4,5 +4,6 @@ exports.cancelTask = require('./cancelTask');
4
4
  exports.createTask = require('./createTask');
5
5
  exports.getTasks = require('./getTasks');
6
6
  exports.getTaskOverview = require('./getTaskOverview');
7
+ exports.getTasksOverTime = require('./getTasksOverTime');
7
8
  exports.rescheduleTask = require('./rescheduleTask');
8
9
  exports.runTask = require('./runTask');