@resolveio/server-lib 4.5.12 → 4.5.13
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.
- package/methods/report-builder.js +926 -582
- package/package.json +1 -1
|
@@ -195,7 +195,7 @@ function loadReportBuilderMethods(methodManager) {
|
|
|
195
195
|
if (date_field === void 0) { date_field = ''; }
|
|
196
196
|
if (date_interval === void 0) { date_interval = ''; }
|
|
197
197
|
return new Promise(function (resolve, reject) { return __awaiter(_this, void 0, void 0, function () {
|
|
198
|
-
var uniqueSelectedFieldCollections, modelCollection, query, initialQueryMatchCondition,
|
|
198
|
+
var uniqueSelectedFieldCollections, modelCollection, query, initialQueryMatchCondition, sizes, divFields, queryGroup, res, err_1, tmpTotals, tmpRes;
|
|
199
199
|
var _this = this;
|
|
200
200
|
return __generator(this, function (_a) {
|
|
201
201
|
switch (_a.label) {
|
|
@@ -233,637 +233,162 @@ function loadReportBuilderMethods(methodManager) {
|
|
|
233
233
|
$match: initialQueryMatchCondition
|
|
234
234
|
});
|
|
235
235
|
}
|
|
236
|
-
|
|
236
|
+
sizes = [];
|
|
237
|
+
// Groups - Lookups / Sizes / Unwinds
|
|
237
238
|
groupsRow.forEach(function (row) {
|
|
239
|
+
var _a, _b;
|
|
238
240
|
if (row.field.includes('$')) {
|
|
239
241
|
var fieldData = row.field.split('.$');
|
|
240
|
-
var
|
|
241
|
-
var
|
|
242
|
-
|
|
243
|
-
if (
|
|
244
|
-
|
|
242
|
+
var fieldPath = '';
|
|
243
|
+
for (var i = 0; i < fieldData.length - 1; i++) {
|
|
244
|
+
fieldPath += fieldData[i];
|
|
245
|
+
if (fieldPath.split('.$')[fieldPath.split('.$').length - 1].includes('(Lookup)')) {
|
|
246
|
+
query.push({ $lookup: {
|
|
245
247
|
from: row.treeItem.lookup_collection,
|
|
246
248
|
localField: row.treeItem.lookup_local_key,
|
|
247
249
|
foreignField: row.treeItem.lookup_foreign_key,
|
|
248
250
|
as: row.treeItem.lookup_as.replace(/\.\$/g, '')
|
|
249
251
|
} });
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
fieldsTotal.forEach(function (total) {
|
|
254
|
-
selectedFields.filter(function (a) { return !a.fieldPath.include('.$') && a.fieldType === 'Number'; }).forEach(function (field) {
|
|
255
|
-
addFields_1.$addFields[total.id + '_' + field.id] = {
|
|
256
|
-
$divide: ['$' + field.fieldPath.replace(/\.\$/g, ''), { $multiply: [{ $cond: { if: { $gt: [{ $size: '$' + row.treeItem.lookup_as.replace(/\.\$/g, '') }, 0] }, then: { $size: '$' + row.treeItem.lookup_as.replace(/\.\$/g, '') }, else: 1 } }] }]
|
|
257
|
-
};
|
|
258
|
-
});
|
|
259
|
-
});
|
|
260
|
-
groupQueries.push(addFields_1);
|
|
261
|
-
}
|
|
262
|
-
}
|
|
263
|
-
// total_1_f_0: {
|
|
264
|
-
// $divide: ['$loaded_miles.loaded_miles', {$multiply: [{$size: '$chemicals'}] } ]
|
|
265
|
-
// },
|
|
266
|
-
// total_1_f_1: {
|
|
267
|
-
// $divide: ['$wo_number', {$size: '$chemicals'}]
|
|
268
|
-
// }
|
|
269
|
-
groupQueries.push({ '$unwind': { path: '$' + row.treeItem.lookup_as.replace(/\.\$/g, ''), preserveNullAndEmptyArrays: true } });
|
|
270
|
-
}
|
|
271
|
-
if (i === 0) {
|
|
272
|
-
if (fieldsTotal.length) {
|
|
273
|
-
var addFields_2 = { $addFields: {} };
|
|
274
|
-
fieldsTotal.forEach(function (total) {
|
|
275
|
-
selectedFields.filter(function (a) { return !a.fieldPath.includes('.$') && a.fieldType === 'Number'; }).forEach(function (field) {
|
|
276
|
-
addFields_2.$addFields[total.id + '_' + field.id] = {
|
|
277
|
-
$divide: ['$' + field.fieldPath.replace(/\.\$/g, ''), { $multiply: [{ $cond: { if: { $gt: [{ $size: '$' + fieldPath_1 }, 0] }, then: { $size: '$' + fieldPath_1 }, else: 1 } }] }]
|
|
278
|
-
};
|
|
279
|
-
});
|
|
280
|
-
});
|
|
281
|
-
groupQueries.push(addFields_2);
|
|
282
|
-
}
|
|
252
|
+
sizes.push(row.treeItem.lookup_as.replace(/\.\$\./g, '_'));
|
|
253
|
+
query.push({ $addFields: (_a = {}, _a['size_' + row.treeItem.lookup_as.replace(/\.\$\./g, '_')] = { $size: '$' + row.treeItem.lookup_as.replace(/\.\$/g, '') }, _a) });
|
|
254
|
+
query.push({ '$unwind': { path: '$' + row.treeItem.lookup_as.replace(/\.\$/g, ''), preserveNullAndEmptyArrays: true } });
|
|
283
255
|
}
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
selectedFields.filter(function (a) { return a.fieldPath.replace(/\.\$/g, '').includes(fieldPath_1) && a.fieldPath.split('.$').length === 2 && a.fieldType === 'Number'; }).forEach(function (field) {
|
|
289
|
-
addFields_3.$addFields[total.id + '_' + field.id] = '$' + field.fieldPath.replace(/\.\$/g, '');
|
|
290
|
-
});
|
|
291
|
-
});
|
|
292
|
-
groupQueries.push(addFields_3);
|
|
256
|
+
else {
|
|
257
|
+
sizes.push(fieldPath.replace(/\./g, '_'));
|
|
258
|
+
query.push({ $addFields: (_b = {}, _b['size_' + fieldPath.replace(/\./g, '_')] = { $size: '$' + fieldPath }, _b) });
|
|
259
|
+
query.push({ '$unwind': { path: '$' + fieldPath, preserveNullAndEmptyArrays: true } });
|
|
293
260
|
}
|
|
294
|
-
};
|
|
295
|
-
for (var i = 0; i < fieldData.length - 1; i++) {
|
|
296
|
-
_loop_1(i);
|
|
297
261
|
}
|
|
298
262
|
}
|
|
299
263
|
});
|
|
300
|
-
|
|
301
|
-
|
|
264
|
+
// Selected Fields - Lookups / Sizes / Unwinds
|
|
265
|
+
selectedFields.forEach(function (field) {
|
|
266
|
+
var _a, _b;
|
|
267
|
+
if (field.fieldPath.includes('$')) {
|
|
302
268
|
var fieldData = field.fieldPath.split('.$');
|
|
303
269
|
var fieldPath = '';
|
|
304
|
-
var
|
|
270
|
+
for (var i = 0; i < fieldData.length - 1; i++) {
|
|
305
271
|
fieldPath += fieldData[i];
|
|
306
|
-
if (fieldPath.includes('(Lookup)')) {
|
|
307
|
-
|
|
272
|
+
if (fieldPath.split('.$')[fieldPath.split('.$').length - 1].includes('(Lookup)')) {
|
|
273
|
+
query.push({ $lookup: {
|
|
308
274
|
from: field.lookup_collection,
|
|
309
275
|
localField: field.lookup_local_key,
|
|
310
276
|
foreignField: field.lookup_foreign_key,
|
|
311
277
|
as: field.lookup_as.replace(/\.\$/g, '')
|
|
312
278
|
} });
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
fieldsTotal.forEach(function (total) {
|
|
317
|
-
selectedFields.filter(function (a) { return !a.fieldPath.include('.$') && a.fieldType === 'Number'; }).forEach(function (field) {
|
|
318
|
-
addFields_4.$addFields[total.id + '_' + field.id] = {
|
|
319
|
-
$divide: ['$' + field.fieldPath.replace(/\.\$/g, ''), { $multiply: [{ $cond: { if: { $gt: [{ $size: '$' + field.lookup_as.replace(/\.\$/g, '') }, 0] }, then: { $size: '$' + field.lookup_as.replace(/\.\$/g, '') }, else: 1 } }] }]
|
|
320
|
-
};
|
|
321
|
-
});
|
|
322
|
-
});
|
|
323
|
-
groupQueries.push(addFields_4);
|
|
324
|
-
}
|
|
325
|
-
}
|
|
326
|
-
// total_1_f_0: {
|
|
327
|
-
// $divide: ['$loaded_miles.loaded_miles', {$multiply: [{$size: '$chemicals'}] } ]
|
|
328
|
-
// },
|
|
329
|
-
// total_1_f_1: {
|
|
330
|
-
// $divide: ['$wo_number', {$size: '$chemicals'}]
|
|
331
|
-
// }
|
|
332
|
-
groupQueries.push({ '$unwind': { path: '$' + field.lookup_as.replace(/\.\$/g, ''), preserveNullAndEmptyArrays: true } });
|
|
333
|
-
}
|
|
334
|
-
if (i === 0) {
|
|
335
|
-
if (fieldsTotal.length) {
|
|
336
|
-
var addFields_5 = { $addFields: {} };
|
|
337
|
-
fieldsTotal.forEach(function (total) {
|
|
338
|
-
selectedFields.filter(function (a) { return !a.fieldPath.includes('.$') && a.fieldType === 'Number'; }).forEach(function (field) {
|
|
339
|
-
addFields_5.$addFields[total.id + '_' + field.id] = {
|
|
340
|
-
$divide: ['$' + field.fieldPath.replace(/\.\$/g, ''), { $multiply: [{ $cond: { if: { $gt: [{ $size: '$' + fieldPath }, 0] }, then: { $size: '$' + fieldPath }, else: 1 } }] }]
|
|
341
|
-
};
|
|
342
|
-
});
|
|
343
|
-
});
|
|
344
|
-
groupQueries.push(addFields_5);
|
|
345
|
-
}
|
|
346
|
-
}
|
|
347
|
-
groupQueries.push({ '$unwind': { path: '$' + fieldPath, preserveNullAndEmptyArrays: true } });
|
|
348
|
-
if (fieldsTotal.length) {
|
|
349
|
-
var addFields_6 = { $addFields: {} };
|
|
350
|
-
fieldsTotal.forEach(function (total) {
|
|
351
|
-
selectedFields.filter(function (a) { return a.fieldPath.replace(/\.\$/g, '').includes(fieldPath) && a.fieldPath.split('.$').length === 2 && a.fieldType === 'Number'; }).forEach(function (field) {
|
|
352
|
-
addFields_6.$addFields[total.id + '_' + field.id] = '$' + field.fieldPath.replace(/\.\$/g, '');
|
|
353
|
-
});
|
|
354
|
-
});
|
|
355
|
-
groupQueries.push(addFields_6);
|
|
279
|
+
sizes.push(field.lookup_as.replace(/\.\$\./g, '_'));
|
|
280
|
+
query.push({ $addFields: (_a = {}, _a['size_' + field.lookup_as.replace(/\.\$\./g, '_')] = { $size: '$' + field.lookup_as.replace(/\.\$/g, '') }, _a) });
|
|
281
|
+
query.push({ '$unwind': { path: '$' + field.lookup_as.replace(/\.\$/g, ''), preserveNullAndEmptyArrays: true } });
|
|
356
282
|
}
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
});
|
|
362
|
-
}
|
|
363
|
-
// Add group lookups and unwinds to query
|
|
364
|
-
query = query.concat(groupQueries);
|
|
365
|
-
// GROUP
|
|
366
|
-
if (groupsRow.length) {
|
|
367
|
-
queryGroup_1 = {
|
|
368
|
-
_id: {}
|
|
369
|
-
};
|
|
370
|
-
groupsRow.forEach(function (row) {
|
|
371
|
-
queryGroup_1._id[row.id] = '$' + row.field.replace(/\.\$/g, '');
|
|
372
|
-
if (date_field && date_interval) {
|
|
373
|
-
if (date_interval === 'Daily') {
|
|
374
|
-
queryGroup_1._id['day'] = { '$dayOfMonth': { 'date': '$' + date_field.replace(/\.\$/g, ''), 'timezone': _this.serverConfig['TIMEZONE'] } };
|
|
375
|
-
queryGroup_1._id['month'] = { '$month': { 'date': '$' + date_field.replace(/\.\$/g, ''), 'timezone': _this.serverConfig['TIMEZONE'] } };
|
|
376
|
-
queryGroup_1._id['year'] = { '$year': { 'date': '$' + date_field.replace(/\.\$/g, ''), 'timezone': _this.serverConfig['TIMEZONE'] } };
|
|
377
|
-
}
|
|
378
|
-
else if (date_interval === 'Weekly') {
|
|
379
|
-
queryGroup_1._id['week'] = { '$week': { 'date': '$' + date_field.replace(/\.\$/g, ''), 'timezone': _this.serverConfig['TIMEZONE'] } };
|
|
380
|
-
// queryGroup._id['month'] = {'$month' : '$' + date_field.replace(/\.\$/g, '')};
|
|
381
|
-
queryGroup_1._id['year'] = { '$year': { 'date': '$' + date_field.replace(/\.\$/g, ''), 'timezone': _this.serverConfig['TIMEZONE'] } };
|
|
382
|
-
}
|
|
383
|
-
else if (date_interval === 'Monthly') {
|
|
384
|
-
queryGroup_1._id['month'] = { '$month': { 'date': '$' + date_field.replace(/\.\$/g, ''), 'timezone': _this.serverConfig['TIMEZONE'] } };
|
|
385
|
-
queryGroup_1._id['year'] = { '$year': { 'date': '$' + date_field.replace(/\.\$/g, ''), 'timezone': _this.serverConfig['TIMEZONE'] } };
|
|
386
|
-
}
|
|
387
|
-
else if (date_interval === 'Quarterly') {
|
|
388
|
-
queryGroup_1._id['month'] = { '$month': { 'date': '$' + date_field.replace(/\.\$/g, ''), 'timezone': _this.serverConfig['TIMEZONE'] } };
|
|
389
|
-
queryGroup_1._id['year'] = { '$year': { 'date': '$' + date_field.replace(/\.\$/g, ''), 'timezone': _this.serverConfig['TIMEZONE'] } };
|
|
390
|
-
}
|
|
391
|
-
else if (date_interval === 'Yearly') {
|
|
392
|
-
queryGroup_1._id['year'] = { '$year': { 'date': '$' + date_field.replace(/\.\$/g, ''), 'timezone': _this.serverConfig['TIMEZONE'] } };
|
|
283
|
+
else {
|
|
284
|
+
sizes.push(fieldPath.replace(/\./g, '_'));
|
|
285
|
+
query.push({ $addFields: (_b = {}, _b['size_' + fieldPath.replace(/\./g, '_')] = { $size: '$' + fieldPath }, _b) });
|
|
286
|
+
query.push({ '$unwind': { path: '$' + fieldPath, preserveNullAndEmptyArrays: true } });
|
|
393
287
|
}
|
|
394
|
-
// queryGroup[date_field.replace(/\.\$/g, '')] = {$first: '$' + date_field.replace(/\.\$/g, '')};
|
|
395
|
-
}
|
|
396
|
-
});
|
|
397
|
-
selectedFields.forEach(function (field) {
|
|
398
|
-
if (field.leafValueType === 'Average') {
|
|
399
|
-
queryGroup_1[field.id] = { '$avg': '$' + field.fieldPath.replace(/\.\$/g, '') };
|
|
400
|
-
}
|
|
401
|
-
else if (field.leafValueType === 'Sum') {
|
|
402
|
-
queryGroup_1[field.id] = { '$sum': '$' + field.fieldPath.replace(/\.\$/g, '') };
|
|
403
|
-
}
|
|
404
|
-
else if (field.leafValueType === 'Count') {
|
|
405
|
-
queryGroup_1[field.id] = { '$sum': 1 };
|
|
406
|
-
}
|
|
407
|
-
else if (field.leafValueType === 'Minimum') {
|
|
408
|
-
queryGroup_1[field.id] = { '$min': '$' + field.fieldPath.replace(/\.\$/g, '') };
|
|
409
|
-
}
|
|
410
|
-
else if (field.leafValueType === 'Maximum') {
|
|
411
|
-
queryGroup_1[field.id] = { '$max': '$' + field.fieldPath.replace(/\.\$/g, '') };
|
|
412
288
|
}
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
289
|
+
}
|
|
290
|
+
});
|
|
291
|
+
divFields = { $addFields: {} };
|
|
292
|
+
fieldsTotal.filter(function (a) { return a.type === 'sum'; }).forEach(function (total) {
|
|
293
|
+
selectedFields.filter(function (a) { return !a.fieldPath.includes('.$') && a.fieldType === 'Number'; }).forEach(function (field) {
|
|
294
|
+
var multFields = [];
|
|
295
|
+
sizes.forEach(function (size) {
|
|
296
|
+
multFields.push({ $cond: { if: { $gt: ['$size_' + size, 0] }, then: '$size_' + size, else: 1 } });
|
|
297
|
+
});
|
|
298
|
+
if (multFields.length) {
|
|
299
|
+
divFields.$addFields[total.id + '_' + field.id] = {
|
|
300
|
+
$divide: ['$' + field.fieldPath.replace(/\.\$/g, ''), { $multiply: multFields }]
|
|
301
|
+
};
|
|
421
302
|
}
|
|
422
|
-
// if (field.fieldPath.includes('.$') && ) {
|
|
423
|
-
// }
|
|
424
303
|
});
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
var _a;
|
|
430
|
-
queryGroup_1[total.id + '_' + field.id] = (_a = {}, _a['$' + total.type] = '$' + total.id + '_' + field.id, _a);
|
|
431
|
-
});
|
|
304
|
+
selectedFields.filter(function (a) { return a.fieldPath.includes('.$') && a.fieldType === 'Number'; }).forEach(function (field) {
|
|
305
|
+
var multFields = [];
|
|
306
|
+
sizes.filter(function (a) { return !field.fieldPath.includes(a); }).forEach(function (size) {
|
|
307
|
+
multFields.push({ $cond: { if: { $gt: ['$size_' + size, 0] }, then: '$size_' + size, else: 1 } });
|
|
432
308
|
});
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
});
|
|
438
|
-
selectedFields.filter(function (a) { return a.fieldPath.includes('.$'); }).forEach(function (field) {
|
|
439
|
-
if (!queryGroup_1[field.fieldPath.split('.$')[0]]) {
|
|
440
|
-
queryGroup_1[field.fieldPath.split('.$')[0]] = { $push: '$' + field.fieldPath.split('.$')[0] };
|
|
309
|
+
if (multFields.length) {
|
|
310
|
+
divFields.$addFields[total.id + '_' + field.id] = {
|
|
311
|
+
$divide: ['$' + field.fieldPath.replace(/\.\$/g, ''), { $multiply: multFields }]
|
|
312
|
+
};
|
|
441
313
|
}
|
|
442
314
|
});
|
|
443
|
-
|
|
315
|
+
});
|
|
316
|
+
if (Object.keys(divFields.$addFields).length > 0) {
|
|
317
|
+
query.push(divFields);
|
|
444
318
|
}
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
}
|
|
459
|
-
else if (field.leafValueType === 'Minimum') {
|
|
460
|
-
queryGroup_2[field.id] = { '$min': '$' + field.fieldPath.replace(/\.\$/g, '') };
|
|
319
|
+
queryGroup = {
|
|
320
|
+
_id: {}
|
|
321
|
+
};
|
|
322
|
+
if (!groupsRow.length) {
|
|
323
|
+
queryGroup._id = '$_id';
|
|
324
|
+
}
|
|
325
|
+
groupsRow.forEach(function (row) {
|
|
326
|
+
queryGroup._id[row.id] = '$' + row.field.replace(/\.\$/g, '');
|
|
327
|
+
if (date_field && date_interval) {
|
|
328
|
+
if (date_interval === 'Daily') {
|
|
329
|
+
queryGroup._id['day'] = { '$dayOfMonth': { 'date': '$' + date_field.replace(/\.\$/g, ''), 'timezone': _this.serverConfig['TIMEZONE'] } };
|
|
330
|
+
queryGroup._id['month'] = { '$month': { 'date': '$' + date_field.replace(/\.\$/g, ''), 'timezone': _this.serverConfig['TIMEZONE'] } };
|
|
331
|
+
queryGroup._id['year'] = { '$year': { 'date': '$' + date_field.replace(/\.\$/g, ''), 'timezone': _this.serverConfig['TIMEZONE'] } };
|
|
461
332
|
}
|
|
462
|
-
else if (
|
|
463
|
-
|
|
333
|
+
else if (date_interval === 'Weekly') {
|
|
334
|
+
queryGroup._id['week'] = { '$week': { 'date': '$' + date_field.replace(/\.\$/g, ''), 'timezone': _this.serverConfig['TIMEZONE'] } };
|
|
335
|
+
queryGroup._id['year'] = { '$year': { 'date': '$' + date_field.replace(/\.\$/g, ''), 'timezone': _this.serverConfig['TIMEZONE'] } };
|
|
464
336
|
}
|
|
465
|
-
else if (
|
|
466
|
-
|
|
337
|
+
else if (date_interval === 'Monthly') {
|
|
338
|
+
queryGroup._id['month'] = { '$month': { 'date': '$' + date_field.replace(/\.\$/g, ''), 'timezone': _this.serverConfig['TIMEZONE'] } };
|
|
339
|
+
queryGroup._id['year'] = { '$year': { 'date': '$' + date_field.replace(/\.\$/g, ''), 'timezone': _this.serverConfig['TIMEZONE'] } };
|
|
467
340
|
}
|
|
468
|
-
else if (
|
|
469
|
-
|
|
341
|
+
else if (date_interval === 'Quarterly') {
|
|
342
|
+
queryGroup._id['month'] = { '$month': { 'date': '$' + date_field.replace(/\.\$/g, ''), 'timezone': _this.serverConfig['TIMEZONE'] } };
|
|
343
|
+
queryGroup._id['year'] = { '$year': { 'date': '$' + date_field.replace(/\.\$/g, ''), 'timezone': _this.serverConfig['TIMEZONE'] } };
|
|
470
344
|
}
|
|
471
|
-
else {
|
|
472
|
-
|
|
345
|
+
else if (date_interval === 'Yearly') {
|
|
346
|
+
queryGroup._id['year'] = { '$year': { 'date': '$' + date_field.replace(/\.\$/g, ''), 'timezone': _this.serverConfig['TIMEZONE'] } };
|
|
473
347
|
}
|
|
474
|
-
});
|
|
475
|
-
//Totals
|
|
476
|
-
if (fieldsTotal.length) {
|
|
477
|
-
fieldsTotal.forEach(function (total) {
|
|
478
|
-
selectedFields.filter(function (a) { return a.fieldType === 'Number'; }).forEach(function (field) {
|
|
479
|
-
var _a;
|
|
480
|
-
queryGroup_2[total.id + '_' + field.id] = (_a = {}, _a['$' + total.type] = '$' + total.id + '_' + field.id, _a);
|
|
481
|
-
});
|
|
482
|
-
});
|
|
483
348
|
}
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
selectedFields.filter(function (a) { return a.fieldPath.includes('.$'); }).forEach(function (field) {
|
|
489
|
-
if (!queryGroup_2[field.fieldPath.split('.$')[0]]) {
|
|
490
|
-
queryGroup_2[field.fieldPath.split('.$')[0]] = { $push: '$' + field.fieldPath.split('.$')[0] };
|
|
491
|
-
}
|
|
492
|
-
});
|
|
493
|
-
query.push({ $group: queryGroup_2 });
|
|
494
|
-
}
|
|
495
|
-
/* --------------------- END IF GROUP ---------------------- */
|
|
496
|
-
// Lookup Selected Fields
|
|
497
|
-
selectedFields.filter(function (a) { return a.fieldPath.includes('.$'); }).forEach(function (field) {
|
|
498
|
-
// Unwind if from group
|
|
499
|
-
if (groupsRow.length && !groupsRow.some(function (a) { return a.field.split('.$')[0] === field.fieldPath.split('.$')[0]; })) {
|
|
500
|
-
query.push({ $unwind: {
|
|
501
|
-
path: '$' + field.fieldPath.split('.$')[0],
|
|
502
|
-
preserveNullAndEmptyArrays: true
|
|
503
|
-
} });
|
|
349
|
+
});
|
|
350
|
+
selectedFields.forEach(function (field) {
|
|
351
|
+
if (field.leafValueType === 'Average') {
|
|
352
|
+
queryGroup[field.id] = { '$avg': '$' + field.fieldPath.replace(/\.\$/g, '') };
|
|
504
353
|
}
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
for (var i = 0; i < field.fieldPath.split('.$').length; i++) {
|
|
508
|
-
path += field.fieldPath.split('.$')[i];
|
|
509
|
-
// Unwind
|
|
510
|
-
if (i < field.fieldPath.split('.$').length - 1 && !groupsRow.some(function (a) { return a.field.split('.$')[0] === field.fieldPath.split('.$')[0]; })) {
|
|
511
|
-
query.push({ $unwind: {
|
|
512
|
-
path: path,
|
|
513
|
-
preserveNullAndEmptyArrays: true
|
|
514
|
-
} });
|
|
515
|
-
}
|
|
516
|
-
// Link
|
|
517
|
-
if (!groupsRow.some(function (a) { return a.field.split('.$')[0] === field.fieldPath.split('.$')[0]; })) {
|
|
518
|
-
fieldsLink.filter(function (a) { return a.field_first && a.field_second; }).forEach(function (link) {
|
|
519
|
-
var _a, _b;
|
|
520
|
-
var firstLink = link.field_first.split('.$.');
|
|
521
|
-
firstLink.pop();
|
|
522
|
-
firstLink = firstLink.join('.');
|
|
523
|
-
var secondLink = link.field_second.split('.$.');
|
|
524
|
-
secondLink.pop();
|
|
525
|
-
secondLink = secondLink.join('.');
|
|
526
|
-
if ('$' + firstLink === path || '$' + secondLink === path) {
|
|
527
|
-
query.push({
|
|
528
|
-
$addFields: (_a = {},
|
|
529
|
-
_a[link.id] = {
|
|
530
|
-
$cmp: [
|
|
531
|
-
'$' + link.field_first.replace(/\.\$/g, ''),
|
|
532
|
-
'$' + link.field_second.replace(/\.\$/g, '')
|
|
533
|
-
]
|
|
534
|
-
},
|
|
535
|
-
_a)
|
|
536
|
-
}, {
|
|
537
|
-
$match: (_b = {},
|
|
538
|
-
_b[link.id] = 0,
|
|
539
|
-
_b)
|
|
540
|
-
});
|
|
541
|
-
}
|
|
542
|
-
});
|
|
543
|
-
}
|
|
544
|
-
//Totals
|
|
545
|
-
if (i === field.fieldPath.split('.$').length - 1 && field.fieldType === 'Number') {
|
|
546
|
-
fieldsTotal.forEach(function (total) {
|
|
547
|
-
var _a;
|
|
548
|
-
query.push({ $addFields: (_a = {}, _a[total.id + '_' + field.id] = path, _a) });
|
|
549
|
-
});
|
|
550
|
-
}
|
|
354
|
+
else if (field.leafValueType === 'Sum') {
|
|
355
|
+
queryGroup[field.id] = { '$sum': '$' + field.fieldPath.replace(/\.\$/g, '') };
|
|
551
356
|
}
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
_id: {}
|
|
555
|
-
};
|
|
556
|
-
if (!groupsRow.length) {
|
|
557
|
-
queryLoopGroup._id = '$_id';
|
|
357
|
+
else if (field.leafValueType === 'Count') {
|
|
358
|
+
queryGroup[field.id] = { '$sum': 1 };
|
|
558
359
|
}
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
queryLoopGroup._id['month'] = '$_id.month';
|
|
574
|
-
queryLoopGroup._id['year'] = '$_id.year';
|
|
575
|
-
}
|
|
576
|
-
else if (date_interval === 'Quarterly') {
|
|
577
|
-
queryLoopGroup._id['month'] = '$_id.month';
|
|
578
|
-
queryLoopGroup._id['year'] = '$_id.year';
|
|
579
|
-
}
|
|
580
|
-
else if (date_interval === 'Yearly') {
|
|
581
|
-
queryLoopGroup._id['year'] = '$_id.year';
|
|
582
|
-
}
|
|
583
|
-
}
|
|
584
|
-
});
|
|
585
|
-
selectedFields.forEach(function (selField) {
|
|
586
|
-
if (selField.id === field.id) {
|
|
587
|
-
if (selField.leafValueType === 'Average') {
|
|
588
|
-
queryLoopGroup[selField.id] = { '$avg': '$' + (selField.collection_name === rootCollectionName ? selField.fieldPath.replace(/\.\$/g, '') : selField.fieldPath.replace(/\.\$/g, '')) };
|
|
589
|
-
}
|
|
590
|
-
else if (selField.leafValueType === 'Sum') {
|
|
591
|
-
queryLoopGroup[selField.id] = { '$sum': '$' + (selField.collection_name === rootCollectionName ? selField.fieldPath.replace(/\.\$/g, '') : selField.fieldPath.replace(/\.\$/g, '')) };
|
|
592
|
-
}
|
|
593
|
-
else if (selField.leafValueType === 'Count') {
|
|
594
|
-
queryLoopGroup[selField.id] = { '$sum': 1 };
|
|
595
|
-
}
|
|
596
|
-
else if (selField.leafValueType === 'Minimum') {
|
|
597
|
-
queryLoopGroup[selField.id] = { '$min': '$' + (selField.collection_name === rootCollectionName ? selField.fieldPath.replace(/\.\$/g, '') : selField.fieldPath.replace(/\.\$/g, '')) };
|
|
598
|
-
}
|
|
599
|
-
else if (selField.leafValueType === 'Maximum') {
|
|
600
|
-
queryLoopGroup[selField.id] = { '$max': '$' + (selField.collection_name === rootCollectionName ? selField.fieldPath.replace(/\.\$/g, '') : selField.fieldPath.replace(/\.\$/g, '')) };
|
|
601
|
-
}
|
|
602
|
-
else if (selField.leafValueType === 'First') {
|
|
603
|
-
queryLoopGroup[selField.id] = { '$first': '$' + (selField.collection_name === rootCollectionName ? selField.fieldPath.replace(/\.\$/g, '') : selField.fieldPath.replace(/\.\$/g, '')) };
|
|
604
|
-
}
|
|
605
|
-
else if (selField.leafValueType === 'Last') {
|
|
606
|
-
queryLoopGroup[selField.id] = { '$last': '$' + (selField.collection_name === rootCollectionName ? selField.fieldPath.replace(/\.\$/g, '') : selField.fieldPath.replace(/\.\$/g, '')) };
|
|
607
|
-
}
|
|
608
|
-
else {
|
|
609
|
-
queryLoopGroup[selField.id] = { '$push': '$' + (selField.collection_name === rootCollectionName ? selField.fieldPath.replace(/\.\$/g, '') : selField.fieldPath.replace(/\.\$/g, '')) };
|
|
610
|
-
}
|
|
611
|
-
}
|
|
612
|
-
else {
|
|
613
|
-
queryLoopGroup[selField.id] = { '$first': '$' + selField.id };
|
|
614
|
-
}
|
|
615
|
-
});
|
|
616
|
-
//Totals
|
|
617
|
-
if (fieldsTotal.length) {
|
|
618
|
-
fieldsTotal.forEach(function (total) {
|
|
619
|
-
selectedFields.filter(function (a) { return a.fieldType === 'Number'; }).forEach(function (selField) {
|
|
620
|
-
var _a;
|
|
621
|
-
if (selField.id === field.id) {
|
|
622
|
-
queryLoopGroup[total.id + '_' + selField.id] = (_a = {}, _a['$' + total.type] = '$' + total.id + '_' + selField.id, _a);
|
|
623
|
-
}
|
|
624
|
-
else {
|
|
625
|
-
queryLoopGroup[total.id + '_' + selField.id] = { $first: '$' + total.id + '_' + selField.id };
|
|
626
|
-
}
|
|
627
|
-
});
|
|
628
|
-
});
|
|
360
|
+
else if (field.leafValueType === 'Minimum') {
|
|
361
|
+
queryGroup[field.id] = { '$min': '$' + field.fieldPath.replace(/\.\$/g, '') };
|
|
362
|
+
}
|
|
363
|
+
else if (field.leafValueType === 'Maximum') {
|
|
364
|
+
queryGroup[field.id] = { '$max': '$' + field.fieldPath.replace(/\.\$/g, '') };
|
|
365
|
+
}
|
|
366
|
+
else if (field.leafValueType === 'First') {
|
|
367
|
+
queryGroup[field.id] = { '$first': '$' + field.fieldPath.replace(/\.\$/g, '') };
|
|
368
|
+
}
|
|
369
|
+
else if (field.leafValueType === 'Last') {
|
|
370
|
+
queryGroup[field.id] = { '$last': '$' + field.fieldPath.replace(/\.\$/g, '') };
|
|
371
|
+
}
|
|
372
|
+
else {
|
|
373
|
+
queryGroup[field.id] = { '$push': '$' + field.fieldPath.replace(/\.\$/g, '') };
|
|
629
374
|
}
|
|
630
|
-
// Copy Group Arrays Moving Forward
|
|
631
|
-
groupsRow.filter(function (a) { return a.field.includes('.$'); }).forEach(function (row) {
|
|
632
|
-
queryLoopGroup[row.field.split('.$')[0]] = { $first: '$' + row.field.split('.$')[0] };
|
|
633
|
-
});
|
|
634
|
-
selectedFields.filter(function (a) { return a.fieldPath.includes('.$'); }).forEach(function (selField) {
|
|
635
|
-
if (selField.id === field.id) {
|
|
636
|
-
if (!queryLoopGroup[selField.fieldPath.split('.$')[0]]) {
|
|
637
|
-
queryLoopGroup[selField.fieldPath.split('.$')[0]] = { $push: '$' + selField.fieldPath.split('.$')[0] };
|
|
638
|
-
}
|
|
639
|
-
}
|
|
640
|
-
else {
|
|
641
|
-
if (!queryLoopGroup[selField.fieldPath.split('.$')[0]]) {
|
|
642
|
-
queryLoopGroup[selField.fieldPath.split('.$')[0]] = { $first: '$' + selField.fieldPath.split('.$')[0] };
|
|
643
|
-
}
|
|
644
|
-
}
|
|
645
|
-
});
|
|
646
|
-
query.push({ $group: queryLoopGroup });
|
|
647
|
-
// Sort
|
|
648
375
|
});
|
|
649
|
-
|
|
650
|
-
if (fieldsTotal.length
|
|
376
|
+
// Totals
|
|
377
|
+
if (fieldsTotal.length) {
|
|
651
378
|
fieldsTotal.forEach(function (total) {
|
|
652
379
|
selectedFields.filter(function (a) { return a.fieldType === 'Number'; }).forEach(function (field) {
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
queryTotals = {};
|
|
656
|
-
}
|
|
657
|
-
queryTotals[total.id + '_' + field.id] = (_a = {}, _a['$' + total.type] = '$results.' + total.id + '_' + field.id, _a);
|
|
380
|
+
// queryGroup[total.id + '_' + field.id] = {['$' + total.type]: '$' + total.id + '_' + field.id};
|
|
381
|
+
queryGroup[total.id + '_' + field.id] = { $sum: '$' + total.id + '_' + field.id };
|
|
658
382
|
});
|
|
659
383
|
});
|
|
660
384
|
}
|
|
661
|
-
|
|
662
|
-
//
|
|
663
|
-
// queryUnwindCollectionPaths.filter(a => !a.includes('(Lookup)')).forEach(path => {
|
|
664
|
-
// query.push({
|
|
665
|
-
// $unwind: {
|
|
666
|
-
// path: path,
|
|
667
|
-
// preserveNullAndEmptyArrays: true
|
|
668
|
-
// }
|
|
669
|
-
// });
|
|
670
|
-
// });
|
|
671
|
-
// queryGroupUnwindCollectionPaths.filter(a => !a.includes('(Lookup)')).forEach(path => {
|
|
672
|
-
// query.push({
|
|
673
|
-
// $unwind: {
|
|
674
|
-
// path: path,
|
|
675
|
-
// preserveNullAndEmptyArrays: false
|
|
676
|
-
// }
|
|
677
|
-
// });
|
|
678
|
-
// });
|
|
679
|
-
// // Unwind Lookups
|
|
680
|
-
// queryUnwindCollectionPaths.filter(a => a.includes('(Lookup)')).forEach(path => {
|
|
681
|
-
// if (!query.filter(a => a.$unwind && a.$unwind.path === path).length) {
|
|
682
|
-
// query.push({
|
|
683
|
-
// $unwind: {
|
|
684
|
-
// path: path,
|
|
685
|
-
// preserveNullAndEmptyArrays: true
|
|
686
|
-
// }
|
|
687
|
-
// });
|
|
688
|
-
// }
|
|
689
|
-
// });
|
|
690
|
-
// queryGroupUnwindCollectionPaths.filter(a => a.includes('(Lookup)')).forEach(path => {
|
|
691
|
-
// if (!query.filter(a => a.$unwind && a.$unwind.path === path).length) {
|
|
692
|
-
// query.push({
|
|
693
|
-
// $unwind: {
|
|
694
|
-
// path: path,
|
|
695
|
-
// preserveNullAndEmptyArrays: false
|
|
696
|
-
// }
|
|
697
|
-
// });
|
|
698
|
-
// }
|
|
699
|
-
// });
|
|
700
|
-
// // Group Unwinds
|
|
701
|
-
// if (groupsRow.length) {
|
|
702
|
-
// query = query.concat(queryGroupUnwind.filter(a => !query.some(b => b.$unwind && (b.$unwind.path === a.$unwind.path))));
|
|
703
|
-
// }
|
|
704
|
-
// // Array and Lookup Filters
|
|
705
|
-
// let queryMatchConditionLookup = {
|
|
706
|
-
// $and: []
|
|
707
|
-
// };
|
|
708
|
-
// if (filters.filter(a => (!a['$or'] && Object.keys(a).some(b => b.includes('(Lookup)'))) || (a['$or'] && a['$or'].some(b => Object.keys(b).some(c => c.includes('(Lookup)'))))).length) {
|
|
709
|
-
// filters.filter(a => (!a['$or'] && Object.keys(a).some(b => b.includes('(Lookup)'))) || (a['$or'] && a['$or'].some(b => Object.keys(b).some(c => c.includes('(Lookup)'))))).forEach(filter => {
|
|
710
|
-
// queryMatchConditionLookup['$and'].push(filter);
|
|
711
|
-
// });
|
|
712
|
-
// }
|
|
713
|
-
// filterArrays.forEach(filter => {
|
|
714
|
-
// queryMatchConditionLookup['$and'].push(filter);
|
|
715
|
-
// });
|
|
716
|
-
// if (queryMatchConditionLookup.$and.some(a => !a['$or'] || (a['$or'] && a['$or'].length)) || queryMatchConditionLookup.$and.length) {
|
|
717
|
-
// query.push({
|
|
718
|
-
// $match: queryMatchConditionLookup
|
|
719
|
-
// });
|
|
720
|
-
// }
|
|
721
|
-
// // Group Queries and Projection
|
|
722
|
-
// if (groupsRow.length) {
|
|
723
|
-
// queryGroups.forEach(group => {
|
|
724
|
-
// query.push(group);
|
|
725
|
-
// });
|
|
726
|
-
// if (reportType === 'Tabular Group') {
|
|
727
|
-
// let tmpProj = deepCopy(queryGroups[queryGroups.length - 2].$group);
|
|
728
|
-
// if (groupsRow.length === 1) {
|
|
729
|
-
// tmpProj._id.gr_1 = '$_id.gr_1';
|
|
730
|
-
// }
|
|
731
|
-
// Object.keys(tmpProj).filter(a => a !== '_id').forEach(key => {
|
|
732
|
-
// tmpProj[key] = 1;
|
|
733
|
-
// });
|
|
734
|
-
// Object.assign(tmpProj, tmpProj['_id']);
|
|
735
|
-
// tmpProj['_id'] = 0;
|
|
736
|
-
// query.push({
|
|
737
|
-
// $project: tmpProj
|
|
738
|
-
// });
|
|
739
|
-
// }
|
|
740
|
-
// }
|
|
741
|
-
// else {
|
|
742
|
-
// query.push({
|
|
743
|
-
// $project: queryProjection
|
|
744
|
-
// });
|
|
745
|
-
// }
|
|
746
|
-
// // Links
|
|
747
|
-
// if (fieldsLink.length) {
|
|
748
|
-
// if (fieldsLink.length === 1 && fieldsLink[0].field_first && fieldsLink[0].field_second) {
|
|
749
|
-
// query.push({$match: {[fieldsLink[0].id]: 0}});
|
|
750
|
-
// }
|
|
751
|
-
// else {
|
|
752
|
-
// let tmpAnds = [];
|
|
753
|
-
// fieldsLink.filter(a => a.field_first && a.field_second).forEach(link => {
|
|
754
|
-
// tmpAnds.push({[link.id]: 0});
|
|
755
|
-
// });
|
|
756
|
-
// if (tmpAnds.length) {
|
|
757
|
-
// query.push({$match: {$and: tmpAnds}});
|
|
758
|
-
// }
|
|
759
|
-
// }
|
|
760
|
-
// }
|
|
761
|
-
// // SORT
|
|
385
|
+
query.push({ $group: queryGroup });
|
|
386
|
+
// SORT
|
|
762
387
|
if (rootOptions.sort && reportType !== 'Tabular Group') {
|
|
763
388
|
query.push({
|
|
764
389
|
$sort: rootOptions.sort
|
|
765
390
|
});
|
|
766
391
|
}
|
|
767
|
-
// Dated Grouping
|
|
768
|
-
// if (date_field && date_interval) {
|
|
769
|
-
// if (date_interval === 'Daily') {
|
|
770
|
-
// query.push({
|
|
771
|
-
// $group: {
|
|
772
|
-
// _id: {
|
|
773
|
-
// day: '$_id.day',
|
|
774
|
-
// month: '$_id.month',
|
|
775
|
-
// year: '$_id.year'
|
|
776
|
-
// },
|
|
777
|
-
// results: {
|
|
778
|
-
// '$push': '$$ROOT'
|
|
779
|
-
// }
|
|
780
|
-
// }
|
|
781
|
-
// });
|
|
782
|
-
// }
|
|
783
|
-
// else if (date_interval === 'Weekly') {
|
|
784
|
-
// query.push({
|
|
785
|
-
// $group: {
|
|
786
|
-
// _id: {
|
|
787
|
-
// week: '$_id.week',
|
|
788
|
-
// // month: '$_id.month',
|
|
789
|
-
// year: '$_id.year'
|
|
790
|
-
// },
|
|
791
|
-
// results: {
|
|
792
|
-
// '$push': '$$ROOT'
|
|
793
|
-
// }
|
|
794
|
-
// }
|
|
795
|
-
// });
|
|
796
|
-
// }
|
|
797
|
-
// else if (date_interval === 'Monthly') {
|
|
798
|
-
// query.push({
|
|
799
|
-
// $group: {
|
|
800
|
-
// _id: {
|
|
801
|
-
// month: '$_id.month',
|
|
802
|
-
// year: '$_id.year'
|
|
803
|
-
// },
|
|
804
|
-
// results: {
|
|
805
|
-
// '$push': '$$ROOT'
|
|
806
|
-
// }
|
|
807
|
-
// }
|
|
808
|
-
// });
|
|
809
|
-
// }
|
|
810
|
-
// else if (date_interval === 'Quarterly') {
|
|
811
|
-
// query.push({
|
|
812
|
-
// $group: {
|
|
813
|
-
// _id: {
|
|
814
|
-
// month: '$_id.month',
|
|
815
|
-
// year: '$_id.year'
|
|
816
|
-
// },
|
|
817
|
-
// results: {
|
|
818
|
-
// '$push': '$$ROOT'
|
|
819
|
-
// }
|
|
820
|
-
// }
|
|
821
|
-
// });
|
|
822
|
-
// }
|
|
823
|
-
// else if (date_interval === 'Yearly') {
|
|
824
|
-
// query.push({
|
|
825
|
-
// $group: {
|
|
826
|
-
// _id: {
|
|
827
|
-
// year: '$_id.year'
|
|
828
|
-
// },
|
|
829
|
-
// results: {
|
|
830
|
-
// '$push': '$$ROOT'
|
|
831
|
-
// }
|
|
832
|
-
// }
|
|
833
|
-
// });
|
|
834
|
-
// }
|
|
835
|
-
// }
|
|
836
|
-
// GROUP TO GET TOTAL COUNT
|
|
837
|
-
query.push({
|
|
838
|
-
$group: {
|
|
839
|
-
_id: null,
|
|
840
|
-
count: { $sum: 1 },
|
|
841
|
-
results: { $push: '$$ROOT' }
|
|
842
|
-
}
|
|
843
|
-
});
|
|
844
|
-
if (rootOptions.limit) {
|
|
845
|
-
// PROJECT TO SKIP/LIMIT
|
|
846
|
-
query.push({
|
|
847
|
-
$project: {
|
|
848
|
-
count: 1,
|
|
849
|
-
results: {
|
|
850
|
-
$slice: ['$results', rootOptions.skip, rootOptions.limit]
|
|
851
|
-
}
|
|
852
|
-
}
|
|
853
|
-
});
|
|
854
|
-
if (queryTotals !== null) {
|
|
855
|
-
query[query.length - 1].$project['totals'] = queryTotals;
|
|
856
|
-
}
|
|
857
|
-
}
|
|
858
|
-
else if (queryTotals !== null) {
|
|
859
|
-
query.push({
|
|
860
|
-
$project: {
|
|
861
|
-
count: 1,
|
|
862
|
-
results: 1,
|
|
863
|
-
totals: queryTotals
|
|
864
|
-
}
|
|
865
|
-
});
|
|
866
|
-
}
|
|
867
392
|
console.log('_____________ query __________');
|
|
868
393
|
console.log(query);
|
|
869
394
|
console.log(JSON.stringify(query, null, 2));
|
|
@@ -880,7 +405,23 @@ function loadReportBuilderMethods(methodManager) {
|
|
|
880
405
|
console.log(err_1);
|
|
881
406
|
return [3 /*break*/, 4];
|
|
882
407
|
case 4:
|
|
883
|
-
|
|
408
|
+
tmpTotals = {};
|
|
409
|
+
fieldsTotal.forEach(function (totField) {
|
|
410
|
+
selectedFields.filter(function (b) { return b.fieldType === 'Number'; }).forEach(function (selField) {
|
|
411
|
+
if (totField.type === 'sum') {
|
|
412
|
+
tmpTotals[totField.id + '_' + selField.id] = res.reduce(function (a, b) { return (a[totField.id + '_' + selField.id] || 0) + (b[totField.id + '_' + selField.id] || 0); }, 0);
|
|
413
|
+
}
|
|
414
|
+
else if (totField.type === 'avg') {
|
|
415
|
+
tmpTotals[totField.id + '_' + selField.id] = Math.round(res.reduce(function (a, b) { return (a[totField.id + '_' + selField.id] || 0) + (b[totField.id + '_' + selField.id] || 0); }, 0) / res.length);
|
|
416
|
+
}
|
|
417
|
+
});
|
|
418
|
+
});
|
|
419
|
+
tmpRes = [{
|
|
420
|
+
count: res.length,
|
|
421
|
+
results: rootOptions.limit ? res.splice(0, rootOptions.limit) : res,
|
|
422
|
+
totals: tmpTotals
|
|
423
|
+
}];
|
|
424
|
+
resolve(tmpRes);
|
|
884
425
|
return [2 /*return*/];
|
|
885
426
|
}
|
|
886
427
|
});
|
|
@@ -890,3 +431,806 @@ function loadReportBuilderMethods(methodManager) {
|
|
|
890
431
|
});
|
|
891
432
|
}
|
|
892
433
|
exports.loadReportBuilderMethods = loadReportBuilderMethods;
|
|
434
|
+
// let queryGroupUnwindCollectionPaths = [];
|
|
435
|
+
// groupsRow.filter(a => a.treeItem.fieldPath.includes('$')).forEach(field => {
|
|
436
|
+
// let arrayPartData = field.treeItem.fieldPath.split('.$');
|
|
437
|
+
// let path = '$';
|
|
438
|
+
// for (let i = 0; i < arrayPartData.length - 1; i++) {
|
|
439
|
+
// path += arrayPartData[i];
|
|
440
|
+
// if (!queryGroupUnwindCollectionPaths.filter(a => a === path)[0]) {
|
|
441
|
+
// queryGroupUnwindCollectionPaths.push(path);
|
|
442
|
+
// }
|
|
443
|
+
// }
|
|
444
|
+
// });
|
|
445
|
+
// // Unwind
|
|
446
|
+
// let queryUnwindCollectionPaths = [];
|
|
447
|
+
// selectedFields.filter(a => a.fieldPath.includes('$')).forEach(field => {
|
|
448
|
+
// let arrayPartData = field.fieldPath.split('.$');
|
|
449
|
+
// let path = '$';
|
|
450
|
+
// for (let i = 0; i < arrayPartData.length - 1; i++) {
|
|
451
|
+
// path += arrayPartData[i];
|
|
452
|
+
// if (!queryUnwindCollectionPaths.some(a => a === path) && !queryGroupUnwindCollectionPaths.some(a => a === path)) {
|
|
453
|
+
// queryUnwindCollectionPaths.push(path);
|
|
454
|
+
// }
|
|
455
|
+
// }
|
|
456
|
+
// });
|
|
457
|
+
// //Links
|
|
458
|
+
// if (fieldsLink.length) {
|
|
459
|
+
// fieldsLink.filter(a => a.field_first && a.field_second).forEach(link => {
|
|
460
|
+
// queryGroup[link.id] = {$cmp: ['$' + link.field_first.replace(/\.\$/g, ''), '$' + link.field_second.replace(/\.\$/g, '')]};
|
|
461
|
+
// });
|
|
462
|
+
// }
|
|
463
|
+
// if (rootOptions.sort && reportType === 'Tabular Group') {
|
|
464
|
+
// let correctSort = {};
|
|
465
|
+
// Object.keys(rootOptions.sort).forEach(key => {
|
|
466
|
+
// correctSort[selectedFields.filter(a => a.id === key)[0].fieldPath.replace(/\.\$/g, '')] = rootOptions.sort[key];
|
|
467
|
+
// });
|
|
468
|
+
// queryGroups.push({
|
|
469
|
+
// $sort: correctSort
|
|
470
|
+
// });
|
|
471
|
+
// }
|
|
472
|
+
/* --------------------- START IF GROUP ---------------------- */
|
|
473
|
+
// Lookups for Groups
|
|
474
|
+
// let groupQueries = [];
|
|
475
|
+
// groupsRow.forEach(row => {
|
|
476
|
+
// if (row.field.includes('$')) {
|
|
477
|
+
// let fieldData = row.field.split('.$');
|
|
478
|
+
// let fieldPath = '';
|
|
479
|
+
// for (let i = 0; i < fieldData.length - 1; i++) {
|
|
480
|
+
// fieldPath += fieldData[i];
|
|
481
|
+
// if (fieldPath.includes('(Lookup)')) {
|
|
482
|
+
// groupQueries.push({$lookup: {
|
|
483
|
+
// from: row.treeItem.lookup_collection,
|
|
484
|
+
// localField: row.treeItem.lookup_local_key,
|
|
485
|
+
// foreignField: row.treeItem.lookup_foreign_key,
|
|
486
|
+
// as: row.treeItem.lookup_as.replace(/\.\$/g, '')
|
|
487
|
+
// }});
|
|
488
|
+
// if (i === 0) {
|
|
489
|
+
// if (fieldsTotal.length) {
|
|
490
|
+
// let addFields = {$addFields: {}};
|
|
491
|
+
// fieldsTotal.forEach(total => {
|
|
492
|
+
// selectedFields.filter(a => !a.fieldPath.include('.$') && a.fieldType === 'Number').forEach(field => {
|
|
493
|
+
// addFields.$addFields[total.id + '_' + field.id] = {
|
|
494
|
+
// $divide: ['$' + field.fieldPath.replace(/\.\$/g, ''), {$multiply: [{$cond: { if: { $gt: [{$size: '$' + row.treeItem.lookup_as.replace(/\.\$/g, '')} , 0 ] }, then: {$size: '$' + row.treeItem.lookup_as.replace(/\.\$/g, '')}, else: 1 }}]}]
|
|
495
|
+
// };
|
|
496
|
+
// });
|
|
497
|
+
// });
|
|
498
|
+
// if (Object.entries(addFields.$addFields).length > 0) {
|
|
499
|
+
// groupQueries.push(addFields);
|
|
500
|
+
// }
|
|
501
|
+
// }
|
|
502
|
+
// }
|
|
503
|
+
// // total_1_f_0: {
|
|
504
|
+
// // $divide: ['$loaded_miles.loaded_miles', {$multiply: [{$size: '$chemicals'}] } ]
|
|
505
|
+
// // },
|
|
506
|
+
// // total_1_f_1: {
|
|
507
|
+
// // $divide: ['$wo_number', {$size: '$chemicals'}]
|
|
508
|
+
// // }
|
|
509
|
+
// // groupQueries.push({'$unwind': {path: '$' + row.treeItem.lookup_as.replace(/\.\$/g, ''), preserveNullAndEmptyArrays: true}});
|
|
510
|
+
// }
|
|
511
|
+
// if (i === 0) {
|
|
512
|
+
// if (fieldsTotal.length) {
|
|
513
|
+
// let addFields = {$addFields: {}};
|
|
514
|
+
// fieldsTotal.forEach(total => {
|
|
515
|
+
// selectedFields.filter(a => !a.fieldPath.includes('.$') && a.fieldType === 'Number' && !groupQueries.some(b => b.$addFields && Object.keys(b.$addFields).includes(total.id + '_' + a.id))).forEach(field => {
|
|
516
|
+
// addFields.$addFields[total.id + '_' + field.id] = {
|
|
517
|
+
// $divide: ['$' + field.fieldPath.replace(/\.\$/g, ''), {$multiply: [{$cond: { if: { $gt: [{$size: '$' + fieldPath} , 0 ] }, then: {$size: '$' + fieldPath}, else: 1 }}]}]
|
|
518
|
+
// };
|
|
519
|
+
// });
|
|
520
|
+
// });
|
|
521
|
+
// if (Object.entries(addFields.$addFields).length > 0) {
|
|
522
|
+
// groupQueries.push(addFields);
|
|
523
|
+
// }
|
|
524
|
+
// }
|
|
525
|
+
// }
|
|
526
|
+
// groupQueries.push({'$unwind': {path: '$' + fieldPath, preserveNullAndEmptyArrays: true}});
|
|
527
|
+
// if (fieldsTotal.length) {
|
|
528
|
+
// let addFields = {$addFields: {}};
|
|
529
|
+
// fieldsTotal.forEach(total => {
|
|
530
|
+
// selectedFields.filter(a => a.fieldPath.replace(/\.\$/g, '').includes(fieldPath) && a.fieldPath.split('.$').length === 2 && a.fieldType === 'Number').forEach(field => {
|
|
531
|
+
// addFields.$addFields[total.id + '_' + field.id] = '$' + field.fieldPath.replace(/\.\$/g, '');
|
|
532
|
+
// });
|
|
533
|
+
// });
|
|
534
|
+
// if (Object.entries(addFields.$addFields).length > 0) {
|
|
535
|
+
// groupQueries.push(addFields);
|
|
536
|
+
// }
|
|
537
|
+
// }
|
|
538
|
+
// }
|
|
539
|
+
// }
|
|
540
|
+
// });
|
|
541
|
+
// if (!groupsRow.length) {
|
|
542
|
+
// selectedFields.forEach(field => {
|
|
543
|
+
// let fieldData = field.fieldPath.split('.$');
|
|
544
|
+
// let fieldPath = '';
|
|
545
|
+
// for (let i = 0; i < fieldData.length - 1; i++) {
|
|
546
|
+
// fieldPath += fieldData[i];
|
|
547
|
+
// if (fieldPath.includes('(Lookup)')) {
|
|
548
|
+
// groupQueries.push({$lookup: {
|
|
549
|
+
// from: field.lookup_collection,
|
|
550
|
+
// localField: field.lookup_local_key,
|
|
551
|
+
// foreignField: field.lookup_foreign_key,
|
|
552
|
+
// as: field.lookup_as.replace(/\.\$/g, '')
|
|
553
|
+
// }});
|
|
554
|
+
// if (i === 0) {
|
|
555
|
+
// if (fieldsTotal.length) {
|
|
556
|
+
// let addFields = {$addFields: {}};
|
|
557
|
+
// fieldsTotal.forEach(total => {
|
|
558
|
+
// selectedFields.filter(a => !a.fieldPath.include('.$') && a.fieldType === 'Number').forEach(field => {
|
|
559
|
+
// addFields.$addFields[total.id + '_' + field.id] = {
|
|
560
|
+
// $divide: ['$' + field.fieldPath.replace(/\.\$/g, ''), {$multiply: [{$cond: { if: { $gt: [{$size: '$' + field.lookup_as.replace(/\.\$/g, '')} , 0 ] }, then: {$size: '$' + field.lookup_as.replace(/\.\$/g, '')}, else: 1 }}]}]
|
|
561
|
+
// };
|
|
562
|
+
// });
|
|
563
|
+
// });
|
|
564
|
+
// if (Object.entries(addFields.$addFields).length > 0) {
|
|
565
|
+
// groupQueries.push(addFields);
|
|
566
|
+
// }
|
|
567
|
+
// }
|
|
568
|
+
// }
|
|
569
|
+
// // total_1_f_0: {
|
|
570
|
+
// // $divide: ['$loaded_miles.loaded_miles', {$multiply: [{$size: '$chemicals'}] } ]
|
|
571
|
+
// // },
|
|
572
|
+
// // total_1_f_1: {
|
|
573
|
+
// // $divide: ['$wo_number', {$size: '$chemicals'}]
|
|
574
|
+
// // }
|
|
575
|
+
// // groupQueries.push({'$unwind': {path: '$' + field.lookup_as.replace(/\.\$/g, ''), preserveNullAndEmptyArrays: true}});
|
|
576
|
+
// }
|
|
577
|
+
// if (i === 0) {
|
|
578
|
+
// if (fieldsTotal.length) {
|
|
579
|
+
// let addFields = {$addFields: {}};
|
|
580
|
+
// fieldsTotal.forEach(total => {
|
|
581
|
+
// selectedFields.filter(a => !a.fieldPath.includes('.$') && a.fieldType === 'Number' && !groupQueries.some(b => b.$addFields && Object.keys(b.$addFields).includes(total.id + '_' + a.id))).forEach(field => {
|
|
582
|
+
// addFields.$addFields[total.id + '_' + field.id] = {
|
|
583
|
+
// $divide: ['$' + field.fieldPath.replace(/\.\$/g, ''), {$multiply: [{$cond: { if: { $gt: [{$size: '$' + fieldPath} , 0 ] }, then: {$size: '$' + fieldPath}, else: 1 }}]}]
|
|
584
|
+
// };
|
|
585
|
+
// });
|
|
586
|
+
// });
|
|
587
|
+
// if (Object.entries(addFields.$addFields).length > 0) {
|
|
588
|
+
// groupQueries.push(addFields);
|
|
589
|
+
// }
|
|
590
|
+
// }
|
|
591
|
+
// }
|
|
592
|
+
// groupQueries.push({'$unwind': {path: '$' + fieldPath, preserveNullAndEmptyArrays: true}});
|
|
593
|
+
// if (fieldsTotal.length) {
|
|
594
|
+
// let addFields = {$addFields: {}};
|
|
595
|
+
// fieldsTotal.forEach(total => {
|
|
596
|
+
// selectedFields.filter(a => a.fieldPath.replace(/\.\$/g, '').includes(fieldPath) && a.fieldPath.split('.$').length === 2 && a.fieldType === 'Number').forEach(field => {
|
|
597
|
+
// addFields.$addFields[total.id + '_' + field.id] = '$' + field.fieldPath.replace(/\.\$/g, '');
|
|
598
|
+
// });
|
|
599
|
+
// });
|
|
600
|
+
// if (Object.entries(addFields.$addFields).length > 0) {
|
|
601
|
+
// groupQueries.push(addFields);
|
|
602
|
+
// }
|
|
603
|
+
// }
|
|
604
|
+
// }
|
|
605
|
+
// });
|
|
606
|
+
// }
|
|
607
|
+
// // Add group lookups and unwinds to query
|
|
608
|
+
// query = query.concat(groupQueries);
|
|
609
|
+
// // GROUP
|
|
610
|
+
// if (groupsRow.length) {
|
|
611
|
+
// let queryGroup = {
|
|
612
|
+
// _id: {}
|
|
613
|
+
// };
|
|
614
|
+
// groupsRow.forEach(row => {
|
|
615
|
+
// queryGroup._id[row.id] = '$' + row.field.replace(/\.\$/g, '');
|
|
616
|
+
// if (date_field && date_interval) {
|
|
617
|
+
// if (date_interval === 'Daily') {
|
|
618
|
+
// queryGroup._id['day'] = {'$dayOfMonth' : {'date': '$' + date_field.replace(/\.\$/g, ''), 'timezone': this.serverConfig['TIMEZONE']}};
|
|
619
|
+
// queryGroup._id['month'] = {'$month' : {'date': '$' + date_field.replace(/\.\$/g, ''), 'timezone': this.serverConfig['TIMEZONE']}};
|
|
620
|
+
// queryGroup._id['year'] = {'$year' : {'date': '$' + date_field.replace(/\.\$/g, ''), 'timezone': this.serverConfig['TIMEZONE']}};
|
|
621
|
+
// }
|
|
622
|
+
// else if (date_interval === 'Weekly') {
|
|
623
|
+
// queryGroup._id['week'] = {'$week' : {'date': '$' + date_field.replace(/\.\$/g, ''), 'timezone': this.serverConfig['TIMEZONE']}};
|
|
624
|
+
// // queryGroup._id['month'] = {'$month' : '$' + date_field.replace(/\.\$/g, '')};
|
|
625
|
+
// queryGroup._id['year'] = {'$year' : {'date': '$' + date_field.replace(/\.\$/g, ''), 'timezone': this.serverConfig['TIMEZONE']}};
|
|
626
|
+
// }
|
|
627
|
+
// else if (date_interval === 'Monthly') {
|
|
628
|
+
// queryGroup._id['month'] = {'$month' : {'date': '$' + date_field.replace(/\.\$/g, ''), 'timezone': this.serverConfig['TIMEZONE']}};
|
|
629
|
+
// queryGroup._id['year'] = {'$year' : {'date': '$' + date_field.replace(/\.\$/g, ''), 'timezone': this.serverConfig['TIMEZONE']}};
|
|
630
|
+
// }
|
|
631
|
+
// else if (date_interval === 'Quarterly') {
|
|
632
|
+
// queryGroup._id['month'] = {'$month' : {'date': '$' + date_field.replace(/\.\$/g, ''), 'timezone': this.serverConfig['TIMEZONE']}};
|
|
633
|
+
// queryGroup._id['year'] = {'$year' : {'date': '$' + date_field.replace(/\.\$/g, ''), 'timezone': this.serverConfig['TIMEZONE']}};
|
|
634
|
+
// }
|
|
635
|
+
// else if (date_interval === 'Yearly') {
|
|
636
|
+
// queryGroup._id['year'] = {'$year' : {'date': '$' + date_field.replace(/\.\$/g, ''), 'timezone': this.serverConfig['TIMEZONE']}};
|
|
637
|
+
// }
|
|
638
|
+
// // queryGroup[date_field.replace(/\.\$/g, '')] = {$first: '$' + date_field.replace(/\.\$/g, '')};
|
|
639
|
+
// }
|
|
640
|
+
// });
|
|
641
|
+
// selectedFields.forEach(field => {
|
|
642
|
+
// if (field.leafValueType === 'Average') {
|
|
643
|
+
// queryGroup[field.id] = {'$avg': '$' + field.fieldPath.replace(/\.\$/g, '')};
|
|
644
|
+
// }
|
|
645
|
+
// else if (field.leafValueType === 'Sum') {
|
|
646
|
+
// queryGroup[field.id] = {'$sum': '$' + field.fieldPath.replace(/\.\$/g, '')};
|
|
647
|
+
// }
|
|
648
|
+
// else if (field.leafValueType === 'Count') {
|
|
649
|
+
// queryGroup[field.id] = {'$sum': 1};
|
|
650
|
+
// }
|
|
651
|
+
// else if (field.leafValueType === 'Minimum') {
|
|
652
|
+
// queryGroup[field.id] = {'$min': '$' + field.fieldPath.replace(/\.\$/g, '')};
|
|
653
|
+
// }
|
|
654
|
+
// else if (field.leafValueType === 'Maximum') {
|
|
655
|
+
// queryGroup[field.id] = {'$max': '$' + field.fieldPath.replace(/\.\$/g, '')};
|
|
656
|
+
// }
|
|
657
|
+
// else if (field.leafValueType === 'First') {
|
|
658
|
+
// queryGroup[field.id] = {'$first': '$' + field.fieldPath.replace(/\.\$/g, '')};
|
|
659
|
+
// }
|
|
660
|
+
// else if (field.leafValueType === 'Last') {
|
|
661
|
+
// queryGroup[field.id] = {'$last': '$' + field.fieldPath.replace(/\.\$/g, '')};
|
|
662
|
+
// }
|
|
663
|
+
// else {
|
|
664
|
+
// queryGroup[field.id] = {'$push': '$' + field.fieldPath.replace(/\.\$/g, '')};
|
|
665
|
+
// }
|
|
666
|
+
// // if (field.fieldPath.includes('.$') && ) {
|
|
667
|
+
// // }
|
|
668
|
+
// });
|
|
669
|
+
// //Totals
|
|
670
|
+
// if (fieldsTotal.length) {
|
|
671
|
+
// fieldsTotal.forEach(total => {
|
|
672
|
+
// selectedFields.filter(a => a.fieldType === 'Number').forEach(field => {
|
|
673
|
+
// queryGroup[total.id + '_' + field.id] = {['$' + total.type]: '$' + total.id + '_' + field.id};
|
|
674
|
+
// });
|
|
675
|
+
// });
|
|
676
|
+
// }
|
|
677
|
+
// // Copy Group Arrays Moving Forward
|
|
678
|
+
// groupsRow.filter(a => a.field.includes('.$')).forEach(row => {
|
|
679
|
+
// queryGroup[row.field.split('.$')[0]] = {$first: '$' + row.field.split('.$')[0]};
|
|
680
|
+
// });
|
|
681
|
+
// selectedFields.filter(a => a.fieldPath.includes('.$')).forEach(field => {
|
|
682
|
+
// if (!queryGroup[field.fieldPath.split('.$')[0]]) {
|
|
683
|
+
// queryGroup[field.fieldPath.split('.$')[0]] = {$push: '$' + field.fieldPath.split('.$')[0]};
|
|
684
|
+
// }
|
|
685
|
+
// });
|
|
686
|
+
// query.push({$group: queryGroup});
|
|
687
|
+
// }
|
|
688
|
+
// else {
|
|
689
|
+
// let queryGroup = {
|
|
690
|
+
// _id: '$_id'
|
|
691
|
+
// };
|
|
692
|
+
// selectedFields.filter(a => !a.fieldPath.includes('.$')).forEach(field => {
|
|
693
|
+
// if (field.leafValueType === 'Average') {
|
|
694
|
+
// queryGroup[field.id] = {'$avg': '$' + field.fieldPath.replace(/\.\$/g, '')};
|
|
695
|
+
// }
|
|
696
|
+
// else if (field.leafValueType === 'Sum') {
|
|
697
|
+
// queryGroup[field.id] = {'$sum': '$' + field.fieldPath.replace(/\.\$/g, '')};
|
|
698
|
+
// }
|
|
699
|
+
// else if (field.leafValueType === 'Count') {
|
|
700
|
+
// queryGroup[field.id] = {'$sum': 1};
|
|
701
|
+
// }
|
|
702
|
+
// else if (field.leafValueType === 'Minimum') {
|
|
703
|
+
// queryGroup[field.id] = {'$min': '$' + field.fieldPath.replace(/\.\$/g, '')};
|
|
704
|
+
// }
|
|
705
|
+
// else if (field.leafValueType === 'Maximum') {
|
|
706
|
+
// queryGroup[field.id] = {'$max': '$' + field.fieldPath.replace(/\.\$/g, '')};
|
|
707
|
+
// }
|
|
708
|
+
// else if (field.leafValueType === 'First') {
|
|
709
|
+
// queryGroup[field.id] = {'$first': '$' + field.fieldPath.replace(/\.\$/g, '')};
|
|
710
|
+
// }
|
|
711
|
+
// else if (field.leafValueType === 'Last') {
|
|
712
|
+
// queryGroup[field.id] = {'$last': '$' + field.fieldPath.replace(/\.\$/g, '')};
|
|
713
|
+
// }
|
|
714
|
+
// else {
|
|
715
|
+
// queryGroup[field.id] = {'$push': '$' + field.fieldPath.replace(/\.\$/g, '')};
|
|
716
|
+
// }
|
|
717
|
+
// });
|
|
718
|
+
// //Totals
|
|
719
|
+
// if (fieldsTotal.length) {
|
|
720
|
+
// fieldsTotal.forEach(total => {
|
|
721
|
+
// selectedFields.filter(a => a.fieldType === 'Number').forEach(field => {
|
|
722
|
+
// queryGroup[total.id + '_' + field.id] = {['$' + total.type]: '$' + total.id + '_' + field.id};
|
|
723
|
+
// });
|
|
724
|
+
// });
|
|
725
|
+
// }
|
|
726
|
+
// // Copy Group Arrays Moving Forward
|
|
727
|
+
// groupsRow.filter(a => a.field.includes('.$')).forEach(row => {
|
|
728
|
+
// queryGroup[row.field.split('.$')[0]] = {$first: '$' + row.field.split('.$')[0]};
|
|
729
|
+
// });
|
|
730
|
+
// selectedFields.filter(a => a.fieldPath.includes('.$')).forEach(field => {
|
|
731
|
+
// if (!queryGroup[field.fieldPath.split('.$')[0]]) {
|
|
732
|
+
// queryGroup[field.fieldPath.split('.$')[0]] = {$push: '$' + field.fieldPath.split('.$')[0]};
|
|
733
|
+
// }
|
|
734
|
+
// });
|
|
735
|
+
// query.push({$group: queryGroup});
|
|
736
|
+
// }
|
|
737
|
+
// /* --------------------- END IF GROUP ---------------------- */
|
|
738
|
+
// // Lookup Selected Fields
|
|
739
|
+
// selectedFields.filter(a => a.fieldPath.includes('.$')).forEach(field => {
|
|
740
|
+
// // Unwind if from group
|
|
741
|
+
// if (groupsRow.length && !groupsRow.some(a => a.field.split('.$')[0] === field.fieldPath.split('.$')[0])) {
|
|
742
|
+
// query.push({$unwind: {
|
|
743
|
+
// path: '$' + field.fieldPath.split('.$')[0],
|
|
744
|
+
// preserveNullAndEmptyArrays: true
|
|
745
|
+
// }});
|
|
746
|
+
// }
|
|
747
|
+
// let path = '$';
|
|
748
|
+
// // Loop for each array value (Not Grouped!)
|
|
749
|
+
// for (let i = 0; i < field.fieldPath.split('.$').length; i++) {
|
|
750
|
+
// path += field.fieldPath.split('.$')[i];
|
|
751
|
+
// // Unwind
|
|
752
|
+
// if (i < field.fieldPath.split('.$').length - 1 && !groupsRow.some(a => a.field.split('.$')[0] === field.fieldPath.split('.$')[0])) {
|
|
753
|
+
// query.push({$unwind: {
|
|
754
|
+
// path: path,
|
|
755
|
+
// preserveNullAndEmptyArrays: true
|
|
756
|
+
// }});
|
|
757
|
+
// }
|
|
758
|
+
// // Link
|
|
759
|
+
// if (!groupsRow.some(a => a.field.split('.$')[0] === field.fieldPath.split('.$')[0])) {
|
|
760
|
+
// fieldsLink.filter(a => a.field_first && a.field_second).forEach(link => {
|
|
761
|
+
// let firstLink = link.field_first.split('.$.');
|
|
762
|
+
// firstLink.pop();
|
|
763
|
+
// firstLink = firstLink.join('.');
|
|
764
|
+
// let secondLink = link.field_second.split('.$.');
|
|
765
|
+
// secondLink.pop();
|
|
766
|
+
// secondLink = secondLink.join('.');
|
|
767
|
+
// if ('$' + firstLink === path || '$' + secondLink === path) {
|
|
768
|
+
// query.push(
|
|
769
|
+
// {
|
|
770
|
+
// $addFields: {
|
|
771
|
+
// [link.id]: {
|
|
772
|
+
// $cmp: [
|
|
773
|
+
// '$' + link.field_first.replace(/\.\$/g, ''),
|
|
774
|
+
// '$' + link.field_second.replace(/\.\$/g, '')
|
|
775
|
+
// ]
|
|
776
|
+
// }
|
|
777
|
+
// }
|
|
778
|
+
// },
|
|
779
|
+
// {
|
|
780
|
+
// $match: {
|
|
781
|
+
// [link.id]: 0
|
|
782
|
+
// }
|
|
783
|
+
// }
|
|
784
|
+
// );
|
|
785
|
+
// }
|
|
786
|
+
// });
|
|
787
|
+
// }
|
|
788
|
+
// //Totals
|
|
789
|
+
// if (i === field.fieldPath.split('.$').length - 1 && field.fieldType === 'Number') {
|
|
790
|
+
// fieldsTotal.forEach(total => {
|
|
791
|
+
// query.push({$addFields: {[total.id + '_' + field.id]: path}});
|
|
792
|
+
// });
|
|
793
|
+
// }
|
|
794
|
+
// }
|
|
795
|
+
// // Regroup
|
|
796
|
+
// let queryLoopGroup = {
|
|
797
|
+
// _id: {}
|
|
798
|
+
// };
|
|
799
|
+
// if (!groupsRow.length) {
|
|
800
|
+
// queryLoopGroup._id = '$_id';
|
|
801
|
+
// }
|
|
802
|
+
// groupsRow.forEach(row => {
|
|
803
|
+
// queryLoopGroup._id[row.id] = '$_id.' + row.id;
|
|
804
|
+
// if (date_field && date_interval) {
|
|
805
|
+
// if (date_interval === 'Daily') {
|
|
806
|
+
// queryLoopGroup._id['day'] = '$_id.day';
|
|
807
|
+
// queryLoopGroup._id['month'] = '$_id.month';
|
|
808
|
+
// queryLoopGroup._id['year'] = '$_id.year';
|
|
809
|
+
// }
|
|
810
|
+
// else if (date_interval === 'Weekly') {
|
|
811
|
+
// queryLoopGroup._id['week'] = '$_id.week';
|
|
812
|
+
// // queryGroup._id['month'] = {'$month' : '$' + date_field.replace(/\.\$/g, '')};
|
|
813
|
+
// queryLoopGroup._id['year'] = '$_id.year';
|
|
814
|
+
// }
|
|
815
|
+
// else if (date_interval === 'Monthly') {
|
|
816
|
+
// queryLoopGroup._id['month'] = '$_id.month';
|
|
817
|
+
// queryLoopGroup._id['year'] = '$_id.year';
|
|
818
|
+
// }
|
|
819
|
+
// else if (date_interval === 'Quarterly') {
|
|
820
|
+
// queryLoopGroup._id['month'] = '$_id.month';
|
|
821
|
+
// queryLoopGroup._id['year'] = '$_id.year';
|
|
822
|
+
// }
|
|
823
|
+
// else if (date_interval === 'Yearly') {
|
|
824
|
+
// queryLoopGroup._id['year'] = '$_id.year';
|
|
825
|
+
// }
|
|
826
|
+
// }
|
|
827
|
+
// });
|
|
828
|
+
// selectedFields.forEach(selField => {
|
|
829
|
+
// if (selField.id === field.id) {
|
|
830
|
+
// if (selField.leafValueType === 'Average') {
|
|
831
|
+
// queryLoopGroup[selField.id] = {'$avg': '$' + (selField.collection_name === rootCollectionName ? selField.fieldPath.replace(/\.\$/g, '') : selField.fieldPath.replace(/\.\$/g, ''))};
|
|
832
|
+
// }
|
|
833
|
+
// else if (selField.leafValueType === 'Sum') {
|
|
834
|
+
// queryLoopGroup[selField.id] = {'$sum': '$' + (selField.collection_name === rootCollectionName ? selField.fieldPath.replace(/\.\$/g, '') : selField.fieldPath.replace(/\.\$/g, ''))};
|
|
835
|
+
// }
|
|
836
|
+
// else if (selField.leafValueType === 'Count') {
|
|
837
|
+
// queryLoopGroup[selField.id] = {'$sum': 1};
|
|
838
|
+
// }
|
|
839
|
+
// else if (selField.leafValueType === 'Minimum') {
|
|
840
|
+
// queryLoopGroup[selField.id] = {'$min': '$' + (selField.collection_name === rootCollectionName ? selField.fieldPath.replace(/\.\$/g, '') : selField.fieldPath.replace(/\.\$/g, ''))};
|
|
841
|
+
// }
|
|
842
|
+
// else if (selField.leafValueType === 'Maximum') {
|
|
843
|
+
// queryLoopGroup[selField.id] = {'$max': '$' + (selField.collection_name === rootCollectionName ? selField.fieldPath.replace(/\.\$/g, '') : selField.fieldPath.replace(/\.\$/g, ''))};
|
|
844
|
+
// }
|
|
845
|
+
// else if (selField.leafValueType === 'First') {
|
|
846
|
+
// queryLoopGroup[selField.id] = {'$first': '$' + (selField.collection_name === rootCollectionName ? selField.fieldPath.replace(/\.\$/g, '') : selField.fieldPath.replace(/\.\$/g, ''))};
|
|
847
|
+
// }
|
|
848
|
+
// else if (selField.leafValueType === 'Last') {
|
|
849
|
+
// queryLoopGroup[selField.id] = {'$last': '$' + (selField.collection_name === rootCollectionName ? selField.fieldPath.replace(/\.\$/g, '') : selField.fieldPath.replace(/\.\$/g, ''))};
|
|
850
|
+
// }
|
|
851
|
+
// else {
|
|
852
|
+
// queryLoopGroup[selField.id] = {'$push': '$' + (selField.collection_name === rootCollectionName ? selField.fieldPath.replace(/\.\$/g, '') : selField.fieldPath.replace(/\.\$/g, ''))};
|
|
853
|
+
// }
|
|
854
|
+
// }
|
|
855
|
+
// else {
|
|
856
|
+
// queryLoopGroup[selField.id] = {'$first': '$' + selField.id};
|
|
857
|
+
// }
|
|
858
|
+
// });
|
|
859
|
+
// //Totals
|
|
860
|
+
// if (fieldsTotal.length) {
|
|
861
|
+
// fieldsTotal.forEach(total => {
|
|
862
|
+
// selectedFields.filter(a => a.fieldType === 'Number').forEach(selField => {
|
|
863
|
+
// if (selField.id === field.id) {
|
|
864
|
+
// queryLoopGroup[total.id + '_' + selField.id] = {['$' + total.type]: '$' + total.id + '_' + selField.id};
|
|
865
|
+
// }
|
|
866
|
+
// else {
|
|
867
|
+
// queryLoopGroup[total.id + '_' + selField.id] = {$first: '$' + total.id + '_' + selField.id};
|
|
868
|
+
// }
|
|
869
|
+
// });
|
|
870
|
+
// });
|
|
871
|
+
// }
|
|
872
|
+
// // Copy Group Arrays Moving Forward
|
|
873
|
+
// groupsRow.filter(a => a.field.includes('.$')).forEach(row => {
|
|
874
|
+
// queryLoopGroup[row.field.split('.$')[0]] = {$first: '$' + row.field.split('.$')[0]};
|
|
875
|
+
// });
|
|
876
|
+
// selectedFields.filter(a => a.fieldPath.includes('.$')).forEach(selField => {
|
|
877
|
+
// if (selField.id === field.id) {
|
|
878
|
+
// if (!queryLoopGroup[selField.fieldPath.split('.$')[0]]) {
|
|
879
|
+
// queryLoopGroup[selField.fieldPath.split('.$')[0]] = {$push: '$' + selField.fieldPath.split('.$')[0]};
|
|
880
|
+
// }
|
|
881
|
+
// }
|
|
882
|
+
// else {
|
|
883
|
+
// if (!queryLoopGroup[selField.fieldPath.split('.$')[0]]) {
|
|
884
|
+
// queryLoopGroup[selField.fieldPath.split('.$')[0]] = {$first: '$' + selField.fieldPath.split('.$')[0]};
|
|
885
|
+
// }
|
|
886
|
+
// }
|
|
887
|
+
// });
|
|
888
|
+
// query.push({$group: queryLoopGroup});
|
|
889
|
+
// // Sort
|
|
890
|
+
// });
|
|
891
|
+
// uniqueSelectedFieldCollections.filter(a => a !== rootCollectionName).forEach(uniqueCollection => {
|
|
892
|
+
// selectedFields.filter(a => a.collection_name === uniqueCollection).forEach(field => {
|
|
893
|
+
// if (!queryLookups.filter(a => a.as === field.lookup_as.replace(/\.\$/g, '')).length) {
|
|
894
|
+
// queryLookups.push({
|
|
895
|
+
// from: field.lookup_collection,
|
|
896
|
+
// localField: field.lookup_local_key,
|
|
897
|
+
// foreignField: field.lookup_foreign_key,
|
|
898
|
+
// as: field.lookup_as.replace(/\.\$/g, '')
|
|
899
|
+
// });
|
|
900
|
+
// }
|
|
901
|
+
// });
|
|
902
|
+
// });
|
|
903
|
+
// let queryGroupUnwindCollectionPaths = [];
|
|
904
|
+
// groupsRow.filter(a => a.treeItem.fieldPath.includes('$')).forEach(field => {
|
|
905
|
+
// let arrayPartData = field.treeItem.fieldPath.split('.$');
|
|
906
|
+
// let path = '$';
|
|
907
|
+
// for (let i = 0; i < arrayPartData.length - 1; i++) {
|
|
908
|
+
// path += arrayPartData[i];
|
|
909
|
+
// if (!queryGroupUnwindCollectionPaths.filter(a => a === path)[0]) {
|
|
910
|
+
// queryGroupUnwindCollectionPaths.push(path);
|
|
911
|
+
// }
|
|
912
|
+
// }
|
|
913
|
+
// });
|
|
914
|
+
// // Unwind
|
|
915
|
+
// let queryUnwindCollectionPaths = [];
|
|
916
|
+
// selectedFields.filter(a => a.fieldPath.includes('$')).forEach(field => {
|
|
917
|
+
// let arrayPartData = field.fieldPath.split('.$');
|
|
918
|
+
// let path = '$';
|
|
919
|
+
// for (let i = 0; i < arrayPartData.length - 1; i++) {
|
|
920
|
+
// path += arrayPartData[i];
|
|
921
|
+
// if (!queryUnwindCollectionPaths.some(a => a === path) && !queryGroupUnwindCollectionPaths.some(a => a === path)) {
|
|
922
|
+
// queryUnwindCollectionPaths.push(path);
|
|
923
|
+
// }
|
|
924
|
+
// }
|
|
925
|
+
// });
|
|
926
|
+
// //Links
|
|
927
|
+
// if (fieldsLink.length) {
|
|
928
|
+
// fieldsLink.filter(a => a.field_first && a.field_second).forEach(link => {
|
|
929
|
+
// queryGroup[link.id] = {$cmp: ['$' + link.field_first.replace(/\.\$/g, ''), '$' + link.field_second.replace(/\.\$/g, '')]};
|
|
930
|
+
// });
|
|
931
|
+
// }
|
|
932
|
+
// if (rootOptions.sort && reportType === 'Tabular Group') {
|
|
933
|
+
// let correctSort = {};
|
|
934
|
+
// Object.keys(rootOptions.sort).forEach(key => {
|
|
935
|
+
// correctSort[selectedFields.filter(a => a.id === key)[0].fieldPath.replace(/\.\$/g, '')] = rootOptions.sort[key];
|
|
936
|
+
// });
|
|
937
|
+
// queryGroups.push({
|
|
938
|
+
// $sort: correctSort
|
|
939
|
+
// });
|
|
940
|
+
// }
|
|
941
|
+
// queryGroups.push({$group: queryGroup});
|
|
942
|
+
// if (reportType === 'Tabular Group') {
|
|
943
|
+
// for (let i = groupsRow.length - 1; i > 0; i--) {
|
|
944
|
+
// queryGroups.push({$sort: {['_id.' + groupsRow[i].id]: 1}});
|
|
945
|
+
// let tmpQueryGroup = {
|
|
946
|
+
// _id: {}
|
|
947
|
+
// };
|
|
948
|
+
// for (let j = 0; j < i; j++) {
|
|
949
|
+
// tmpQueryGroup._id[groupsRow[j].id] = '$_id.' + groupsRow[j].id;
|
|
950
|
+
// }
|
|
951
|
+
// let tmpQueryGroupObj = {
|
|
952
|
+
// [groupsRow[i].id]: '$_id.' + [groupsRow[i].id]
|
|
953
|
+
// };
|
|
954
|
+
// if (i === groupsRow.length - 1) {
|
|
955
|
+
// selectedFields.forEach(field => {
|
|
956
|
+
// tmpQueryGroupObj[field.id] = '$' + field.id;
|
|
957
|
+
// });
|
|
958
|
+
// }
|
|
959
|
+
// else {
|
|
960
|
+
// tmpQueryGroupObj[groupsRow[i + 1].id] = '$' + [groupsRow[i + 1].id];
|
|
961
|
+
// }
|
|
962
|
+
// fieldsTotal.forEach(total => {
|
|
963
|
+
// selectedFields.filter(a => a.fieldType === 'Number').forEach(field => {
|
|
964
|
+
// tmpQueryGroupObj[total.id + '_' + field.id] = '$' + total.id + '_' + field.id;
|
|
965
|
+
// tmpQueryGroup[total.id + '_' + field.id] = {['$' + total.type]: '$' + total.id + '_' + field.id};
|
|
966
|
+
// });
|
|
967
|
+
// });
|
|
968
|
+
// tmpQueryGroup[groupsRow[i].id] = {'$push': tmpQueryGroupObj};
|
|
969
|
+
// queryGroups.push({$group: tmpQueryGroup});
|
|
970
|
+
// }
|
|
971
|
+
// queryGroups.push({$sort: {['_id.' + groupsRow[0].id]: 1}});
|
|
972
|
+
// }
|
|
973
|
+
// else if (reportType === 'Dated') {
|
|
974
|
+
// // db.collection.aggregate([
|
|
975
|
+
// // { "$group": {
|
|
976
|
+
// // "_id": {
|
|
977
|
+
// // "$toDate": {
|
|
978
|
+
// // "$subtract": [
|
|
979
|
+
// // { "$toLong": "$created_at" },
|
|
980
|
+
// // { "$mod": [ { "$toLong": "$created_at" }, 1000 * 60 * 15 ] }
|
|
981
|
+
// // ]
|
|
982
|
+
// // }
|
|
983
|
+
// // },
|
|
984
|
+
// // "count": { "$sum": 1 }
|
|
985
|
+
// // }}
|
|
986
|
+
// // ])
|
|
987
|
+
// }
|
|
988
|
+
// }
|
|
989
|
+
// //Projection
|
|
990
|
+
// let queryProjection = {
|
|
991
|
+
// _id: 1
|
|
992
|
+
// };
|
|
993
|
+
// selectedFields.forEach(field => {
|
|
994
|
+
// if (field.leafValueType === 'Average') {
|
|
995
|
+
// queryProjection[field.id] = {$avg: '$' + field.fieldPath.replace(/\.\$/g, '')};
|
|
996
|
+
// }
|
|
997
|
+
// else if (field.leafValueType === 'Sum') {
|
|
998
|
+
// queryProjection[field.id] = {$sum: '$' + field.fieldPath.replace(/\.\$/g, '')};
|
|
999
|
+
// }
|
|
1000
|
+
// else if (field.leafValueType === 'Count') {
|
|
1001
|
+
// queryProjection[field.id] = {$sum: 1};
|
|
1002
|
+
// }
|
|
1003
|
+
// else if (field.leafValueType === 'Minimum') {
|
|
1004
|
+
// queryProjection[field.id] = {$min: '$' + field.fieldPath.replace(/\.\$/g, '')};
|
|
1005
|
+
// }
|
|
1006
|
+
// else if (field.leafValueType === 'Maximum') {
|
|
1007
|
+
// queryProjection[field.id] = {$max: '$' + field.fieldPath.replace(/\.\$/g, '')};
|
|
1008
|
+
// }
|
|
1009
|
+
// else if (field.leafValueType === 'First') {
|
|
1010
|
+
// queryProjection[field.id] = {$slice: ['$' + field.fieldPath.replace(/\.\$/g, ''), 0]};
|
|
1011
|
+
// }
|
|
1012
|
+
// else if (field.leafValueType === 'Last') {
|
|
1013
|
+
// queryProjection[field.id] = {$slice: ['$' + field.fieldPath.replace(/\.\$/g, ''), -1]};
|
|
1014
|
+
// }
|
|
1015
|
+
// else {
|
|
1016
|
+
// queryProjection[field.id] = '$' + field.fieldPath.replace(/\.\$/g, '');
|
|
1017
|
+
// }
|
|
1018
|
+
// });
|
|
1019
|
+
// //Links
|
|
1020
|
+
// if (fieldsLink.length) {
|
|
1021
|
+
// fieldsLink.filter(a => a.field_first && a.field_second).forEach(link => {
|
|
1022
|
+
// queryProjection[link.id] = {$cmp: ['$' + link.field_first.replace(/\.\$/g, ''), '$' + link.field_second.replace(/\.\$/g, '')]};
|
|
1023
|
+
// });
|
|
1024
|
+
// }
|
|
1025
|
+
// //Totals
|
|
1026
|
+
// let queryTotals = null;
|
|
1027
|
+
// if (fieldsTotal.length && (reportType === 'Tabular' || reportType === 'Dated')) {
|
|
1028
|
+
// fieldsTotal.forEach(total => {
|
|
1029
|
+
// selectedFields.filter(a => a.fieldType === 'Number').forEach(field => {
|
|
1030
|
+
// if (!queryTotals) {
|
|
1031
|
+
// queryTotals = {};
|
|
1032
|
+
// }
|
|
1033
|
+
// queryTotals[total.id + '_' + field.id] = {['$' + total.type]: '$results.' + total.id + '_' + field.id};
|
|
1034
|
+
// });
|
|
1035
|
+
// });
|
|
1036
|
+
// }
|
|
1037
|
+
// // ---------------------------------------------------------------
|
|
1038
|
+
// // Unwind Pre-Lookup Arrays
|
|
1039
|
+
// queryUnwindCollectionPaths.filter(a => !a.includes('(Lookup)')).forEach(path => {
|
|
1040
|
+
// query.push({
|
|
1041
|
+
// $unwind: {
|
|
1042
|
+
// path: path,
|
|
1043
|
+
// preserveNullAndEmptyArrays: true
|
|
1044
|
+
// }
|
|
1045
|
+
// });
|
|
1046
|
+
// });
|
|
1047
|
+
// queryGroupUnwindCollectionPaths.filter(a => !a.includes('(Lookup)')).forEach(path => {
|
|
1048
|
+
// query.push({
|
|
1049
|
+
// $unwind: {
|
|
1050
|
+
// path: path,
|
|
1051
|
+
// preserveNullAndEmptyArrays: false
|
|
1052
|
+
// }
|
|
1053
|
+
// });
|
|
1054
|
+
// });
|
|
1055
|
+
// // Unwind Lookups
|
|
1056
|
+
// queryUnwindCollectionPaths.filter(a => a.includes('(Lookup)')).forEach(path => {
|
|
1057
|
+
// if (!query.filter(a => a.$unwind && a.$unwind.path === path).length) {
|
|
1058
|
+
// query.push({
|
|
1059
|
+
// $unwind: {
|
|
1060
|
+
// path: path,
|
|
1061
|
+
// preserveNullAndEmptyArrays: true
|
|
1062
|
+
// }
|
|
1063
|
+
// });
|
|
1064
|
+
// }
|
|
1065
|
+
// });
|
|
1066
|
+
// queryGroupUnwindCollectionPaths.filter(a => a.includes('(Lookup)')).forEach(path => {
|
|
1067
|
+
// if (!query.filter(a => a.$unwind && a.$unwind.path === path).length) {
|
|
1068
|
+
// query.push({
|
|
1069
|
+
// $unwind: {
|
|
1070
|
+
// path: path,
|
|
1071
|
+
// preserveNullAndEmptyArrays: false
|
|
1072
|
+
// }
|
|
1073
|
+
// });
|
|
1074
|
+
// }
|
|
1075
|
+
// });
|
|
1076
|
+
// // Group Unwinds
|
|
1077
|
+
// if (groupsRow.length) {
|
|
1078
|
+
// query = query.concat(queryGroupUnwind.filter(a => !query.some(b => b.$unwind && (b.$unwind.path === a.$unwind.path))));
|
|
1079
|
+
// }
|
|
1080
|
+
// // Array and Lookup Filters
|
|
1081
|
+
// let queryMatchConditionLookup = {
|
|
1082
|
+
// $and: []
|
|
1083
|
+
// };
|
|
1084
|
+
// if (filters.filter(a => (!a['$or'] && Object.keys(a).some(b => b.includes('(Lookup)'))) || (a['$or'] && a['$or'].some(b => Object.keys(b).some(c => c.includes('(Lookup)'))))).length) {
|
|
1085
|
+
// filters.filter(a => (!a['$or'] && Object.keys(a).some(b => b.includes('(Lookup)'))) || (a['$or'] && a['$or'].some(b => Object.keys(b).some(c => c.includes('(Lookup)'))))).forEach(filter => {
|
|
1086
|
+
// queryMatchConditionLookup['$and'].push(filter);
|
|
1087
|
+
// });
|
|
1088
|
+
// }
|
|
1089
|
+
// filterArrays.forEach(filter => {
|
|
1090
|
+
// queryMatchConditionLookup['$and'].push(filter);
|
|
1091
|
+
// });
|
|
1092
|
+
// if (queryMatchConditionLookup.$and.some(a => !a['$or'] || (a['$or'] && a['$or'].length)) || queryMatchConditionLookup.$and.length) {
|
|
1093
|
+
// query.push({
|
|
1094
|
+
// $match: queryMatchConditionLookup
|
|
1095
|
+
// });
|
|
1096
|
+
// }
|
|
1097
|
+
// // Group Queries and Projection
|
|
1098
|
+
// if (groupsRow.length) {
|
|
1099
|
+
// queryGroups.forEach(group => {
|
|
1100
|
+
// query.push(group);
|
|
1101
|
+
// });
|
|
1102
|
+
// if (reportType === 'Tabular Group') {
|
|
1103
|
+
// let tmpProj = deepCopy(queryGroups[queryGroups.length - 2].$group);
|
|
1104
|
+
// if (groupsRow.length === 1) {
|
|
1105
|
+
// tmpProj._id.gr_1 = '$_id.gr_1';
|
|
1106
|
+
// }
|
|
1107
|
+
// Object.keys(tmpProj).filter(a => a !== '_id').forEach(key => {
|
|
1108
|
+
// tmpProj[key] = 1;
|
|
1109
|
+
// });
|
|
1110
|
+
// Object.assign(tmpProj, tmpProj['_id']);
|
|
1111
|
+
// tmpProj['_id'] = 0;
|
|
1112
|
+
// query.push({
|
|
1113
|
+
// $project: tmpProj
|
|
1114
|
+
// });
|
|
1115
|
+
// }
|
|
1116
|
+
// }
|
|
1117
|
+
// else {
|
|
1118
|
+
// query.push({
|
|
1119
|
+
// $project: queryProjection
|
|
1120
|
+
// });
|
|
1121
|
+
// }
|
|
1122
|
+
// // Links
|
|
1123
|
+
// if (fieldsLink.length) {
|
|
1124
|
+
// if (fieldsLink.length === 1 && fieldsLink[0].field_first && fieldsLink[0].field_second) {
|
|
1125
|
+
// query.push({$match: {[fieldsLink[0].id]: 0}});
|
|
1126
|
+
// }
|
|
1127
|
+
// else {
|
|
1128
|
+
// let tmpAnds = [];
|
|
1129
|
+
// fieldsLink.filter(a => a.field_first && a.field_second).forEach(link => {
|
|
1130
|
+
// tmpAnds.push({[link.id]: 0});
|
|
1131
|
+
// });
|
|
1132
|
+
// if (tmpAnds.length) {
|
|
1133
|
+
// query.push({$match: {$and: tmpAnds}});
|
|
1134
|
+
// }
|
|
1135
|
+
// }
|
|
1136
|
+
// }
|
|
1137
|
+
// Dated Grouping
|
|
1138
|
+
// if (date_field && date_interval) {
|
|
1139
|
+
// if (date_interval === 'Daily') {
|
|
1140
|
+
// query.push({
|
|
1141
|
+
// $group: {
|
|
1142
|
+
// _id: {
|
|
1143
|
+
// day: '$_id.day',
|
|
1144
|
+
// month: '$_id.month',
|
|
1145
|
+
// year: '$_id.year'
|
|
1146
|
+
// },
|
|
1147
|
+
// results: {
|
|
1148
|
+
// '$push': '$$ROOT'
|
|
1149
|
+
// }
|
|
1150
|
+
// }
|
|
1151
|
+
// });
|
|
1152
|
+
// }
|
|
1153
|
+
// else if (date_interval === 'Weekly') {
|
|
1154
|
+
// query.push({
|
|
1155
|
+
// $group: {
|
|
1156
|
+
// _id: {
|
|
1157
|
+
// week: '$_id.week',
|
|
1158
|
+
// // month: '$_id.month',
|
|
1159
|
+
// year: '$_id.year'
|
|
1160
|
+
// },
|
|
1161
|
+
// results: {
|
|
1162
|
+
// '$push': '$$ROOT'
|
|
1163
|
+
// }
|
|
1164
|
+
// }
|
|
1165
|
+
// });
|
|
1166
|
+
// }
|
|
1167
|
+
// else if (date_interval === 'Monthly') {
|
|
1168
|
+
// query.push({
|
|
1169
|
+
// $group: {
|
|
1170
|
+
// _id: {
|
|
1171
|
+
// month: '$_id.month',
|
|
1172
|
+
// year: '$_id.year'
|
|
1173
|
+
// },
|
|
1174
|
+
// results: {
|
|
1175
|
+
// '$push': '$$ROOT'
|
|
1176
|
+
// }
|
|
1177
|
+
// }
|
|
1178
|
+
// });
|
|
1179
|
+
// }
|
|
1180
|
+
// else if (date_interval === 'Quarterly') {
|
|
1181
|
+
// query.push({
|
|
1182
|
+
// $group: {
|
|
1183
|
+
// _id: {
|
|
1184
|
+
// month: '$_id.month',
|
|
1185
|
+
// year: '$_id.year'
|
|
1186
|
+
// },
|
|
1187
|
+
// results: {
|
|
1188
|
+
// '$push': '$$ROOT'
|
|
1189
|
+
// }
|
|
1190
|
+
// }
|
|
1191
|
+
// });
|
|
1192
|
+
// }
|
|
1193
|
+
// else if (date_interval === 'Yearly') {
|
|
1194
|
+
// query.push({
|
|
1195
|
+
// $group: {
|
|
1196
|
+
// _id: {
|
|
1197
|
+
// year: '$_id.year'
|
|
1198
|
+
// },
|
|
1199
|
+
// results: {
|
|
1200
|
+
// '$push': '$$ROOT'
|
|
1201
|
+
// }
|
|
1202
|
+
// }
|
|
1203
|
+
// });
|
|
1204
|
+
// }
|
|
1205
|
+
// }
|
|
1206
|
+
// GROUP TO GET TOTAL COUNT
|
|
1207
|
+
// query.push({
|
|
1208
|
+
// $group: {
|
|
1209
|
+
// _id: null,
|
|
1210
|
+
// count: {$sum: 1},
|
|
1211
|
+
// results: {$push: '$$ROOT'}
|
|
1212
|
+
// }
|
|
1213
|
+
// });
|
|
1214
|
+
// if (rootOptions.limit) {
|
|
1215
|
+
// // PROJECT TO SKIP/LIMIT
|
|
1216
|
+
// query.push({
|
|
1217
|
+
// $project: {
|
|
1218
|
+
// count: 1,
|
|
1219
|
+
// results: {
|
|
1220
|
+
// $slice: ['$results', rootOptions.skip, rootOptions.limit]
|
|
1221
|
+
// }
|
|
1222
|
+
// }
|
|
1223
|
+
// });
|
|
1224
|
+
// if (queryTotals !== null) {
|
|
1225
|
+
// query[query.length - 1].$project['totals'] = queryTotals;
|
|
1226
|
+
// }
|
|
1227
|
+
// }
|
|
1228
|
+
// else if (queryTotals !== null) {
|
|
1229
|
+
// query.push({
|
|
1230
|
+
// $project: {
|
|
1231
|
+
// count: 1,
|
|
1232
|
+
// results: 1,
|
|
1233
|
+
// totals: queryTotals
|
|
1234
|
+
// }
|
|
1235
|
+
// });
|
|
1236
|
+
// }
|