@tiledesk/tiledesk-server 2.2.37 → 2.3.1

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.
Files changed (53) hide show
  1. package/CHANGELOG.md +8 -1
  2. package/LICENSE +14 -657
  3. package/README.md +21 -18
  4. package/app.js +27 -3
  5. package/config/labels/widget.json +320 -0
  6. package/event/subscriptionEvent.js +11 -0
  7. package/models/subscriptionEvent.js +11 -0
  8. package/models/subscriptionLog.js +34 -0
  9. package/models/tagLibrary.js +42 -0
  10. package/package.json +5 -11
  11. package/pubmodules/activities/activityArchiver.js +295 -0
  12. package/pubmodules/activities/index.js +3 -0
  13. package/pubmodules/activities/models/activity.js +88 -0
  14. package/pubmodules/activities/routes/activity.js +710 -0
  15. package/pubmodules/activities/test/activityRoute.js +85 -0
  16. package/pubmodules/analytics/analytics.js +1719 -0
  17. package/pubmodules/analytics/index.js +3 -0
  18. package/pubmodules/canned/cannedResponse.js +51 -0
  19. package/pubmodules/canned/cannedResponseRoute.js +157 -0
  20. package/pubmodules/canned/index.js +3 -0
  21. package/pubmodules/pubModulesManager.js +115 -7
  22. package/pubmodules/rasa/index.js +8 -1
  23. package/pubmodules/rasa/listener.js +28 -7
  24. package/pubmodules/scheduler/tasks/closeBotUnresponsiveRequestTask.js +2 -2
  25. package/pubmodules/trigger/default.js +271 -0
  26. package/pubmodules/trigger/event/actionEventEmitter.js +10 -0
  27. package/pubmodules/trigger/event/flowEventEmitter.js +10 -0
  28. package/pubmodules/trigger/event/triggerEventEmitter.js +10 -0
  29. package/pubmodules/trigger/index.js +3 -0
  30. package/pubmodules/trigger/models/trigger.js +149 -0
  31. package/pubmodules/trigger/rulesTrigger.js +1180 -0
  32. package/pubmodules/trigger/start.js +114 -0
  33. package/pubmodules/trigger/triggerRoute.js +150 -0
  34. package/routes/department.js +51 -0
  35. package/routes/group.js +140 -0
  36. package/routes/project.js +52 -0
  37. package/routes/request.js +3 -2
  38. package/routes/subscription.js +140 -0
  39. package/routes/tag.js +138 -0
  40. package/services/faqBotHandler.js +2 -2
  41. package/services/faqBotSupport.js +0 -1
  42. package/services/faqService.js +1 -1
  43. package/services/modulesManager.js +16 -182
  44. package/services/subscriptionNotifier.js +485 -0
  45. package/template/email/assignedEmailMessage.html +1 -1
  46. package/template/email/assignedRequest.html +1 -1
  47. package/template/email/newMessage.html +1 -1
  48. package/template/email/passwordChanged.html +1 -1
  49. package/template/email/pooledEmailMessage.html +1 -1
  50. package/template/email/pooledRequest.html +1 -1
  51. package/template/email/resetPassword.html +2 -2
  52. package/template/email/ticket.html +1 -1
  53. package/views/messages.jade +1 -1
@@ -0,0 +1,1719 @@
1
+ var express = require('express');
2
+ var router = express.Router();
3
+ var AnalyticResult = require("../../models/analyticResult");
4
+ var AnalyticProject_UsersResult = require("../../models/analyticProject_usersResult");
5
+ var AnalyticMessagesResult = require("../../models/analyticMessagesResult");
6
+ var AnalyticEventsResult = require("../../pubmodules/events/analyticEventsResult");
7
+
8
+ var RoleConstants = require("../../models/roleConstants");
9
+
10
+ var mongoose = require('mongoose');
11
+ var winston = require('../../config/winston');
12
+ var ObjectId = require('mongodb').ObjectId;
13
+
14
+
15
+
16
+ // mongoose.set('debug', true);
17
+
18
+
19
+
20
+
21
+ router.get('/requests/count', function(req, res) {
22
+
23
+ winston.debug(req.params);
24
+ winston.debug("req.projectid", req.projectid);
25
+
26
+
27
+ AnalyticResult.aggregate([
28
+ // { "$match": {"id_project": req.projectid } },
29
+ // { "$match": {} },
30
+ { "$match": {"id_project":req.projectid, $or:[ {preflight:false}, { preflight : { $exists: false } } ], "createdAt" : { $gte : new Date((new Date().getTime() - (30 * 24 * 60 * 60 * 1000))) }} },
31
+ { "$count": "totalCount" }
32
+
33
+ ])
34
+ // .exec((err, result) => {
35
+ .exec(function(err, result) {
36
+
37
+
38
+ //, function (err, result) {
39
+ if (err) {
40
+ winston.debug(err);
41
+ return res.status(500).send({success: false, msg: 'Error getting analytics.'});
42
+ }
43
+ winston.debug(result);
44
+
45
+ res.json(result);
46
+ });
47
+
48
+ });
49
+
50
+
51
+
52
+ router.get('/requests/aggregate/status', function(req, res) {
53
+
54
+ winston.debug(req.params);
55
+ winston.debug("req.projectid", req.projectid);
56
+
57
+
58
+ AnalyticResult.aggregate([
59
+ // { "$match": {"id_project": req.projectid } },
60
+ // { "$match": {} },
61
+ { "$match": {"id_project":req.projectid, $or:[ {preflight:false}, { preflight : { $exists: false } } ], "createdAt" : { $gte : new Date((new Date().getTime() - (30 * 24 * 60 * 60 * 1000))) }} },
62
+ { "$count": "totalCount" }
63
+ // DA IMPLEMNTARE
64
+ ])
65
+ // .exec((err, result) => {
66
+ .exec(function(err, result) {
67
+
68
+
69
+ //, function (err, result) {
70
+ if (err) {
71
+ winston.debug(err);
72
+ return res.status(500).send({success: false, msg: 'Error getting analytics.'});
73
+ }
74
+ winston.debug(result);
75
+
76
+ res.json(result);
77
+ });
78
+
79
+ });
80
+
81
+
82
+
83
+ // https://stackoverflow.com/questions/22516514/mongodb-return-the-count-of-documents-for-each-day-for-the-last-one-month
84
+ // https://stackoverflow.com/questions/15938859/mongodb-aggregate-within-daily-grouping
85
+ // db.requests.aggregate(
86
+ // [
87
+ // // Get only records created in the last 30 days
88
+ // { $match: {"id_project":"5ad5bd52c975820014ba900a","createdAt" : { $gte : new Date(ISODate().getTime() - 1000*60*60*24*30) }} },
89
+ // // Get the year, month and day from the createdTimeStamp
90
+ // {$project:{
91
+ // "year":{$year:"$createdAt"},
92
+ // "month":{$month:"$createdAt"},
93
+ // "day": {$dayOfMonth:"$createdAt"}
94
+ // }},
95
+ // // Group by year, month and day and get the count
96
+ // {$group:{
97
+ // _id:{year:"$year", month:"$month", day:"$day"},
98
+ // "count":{$sum:1}
99
+ // }},
100
+ // {$sort:{_id:1}},
101
+ // ]
102
+ // )
103
+
104
+ // router.get('/requests/aggregate/day', function(req, res) {
105
+
106
+ // //set default value for lastdays&department_id
107
+ // let lastdays=7
108
+
109
+
110
+ // //check for lastdays&dep_id parameters
111
+ // if(req.query.lastdays){
112
+ // lastdays=req.query.lastdays
113
+ // }
114
+
115
+ // let query={"id_project":req.projectid, "createdAt" : { $gte : new Date((new Date().getTime() - (lastdays * 24 * 60 * 60 * 1000))) }}
116
+
117
+ // if(req.query.department_id){
118
+ // //add field departmentid to query if req.query.department_id exist
119
+ // query.department=req.query.department_id;
120
+
121
+ // }
122
+
123
+
124
+
125
+ // winston.debug("QueryParams:", lastdays,req.query.department_id)
126
+ // winston.debug("Query", query)
127
+
128
+ // winston.debug(req.params);
129
+ // winston.debug("req.projectid", req.projectid);
130
+
131
+ // AnalyticResult.aggregate([
132
+ // // { "$match": {"id_project": req.projectid } },
133
+ // // { "$match": {} },
134
+ // { $match: query },
135
+ // { "$project":{
136
+ // "year":{"$year":"$createdAt"},
137
+ // "month":{"$month":"$createdAt"},
138
+ // "day": {"$dayOfMonth":"$createdAt"}
139
+ // }},
140
+ // // // Group by year, month and day and get the count
141
+ // { "$group":{
142
+ // "_id":{"year":"$year", "month":"$month", "day":"$day"},
143
+ // "count":{"$sum":1}
144
+ // }},
145
+ // { "$sort": {"_id":1}},
146
+ // // { "$limit": 7 },
147
+ // ])
148
+ // // .exec((err, result) => {
149
+ // .exec(function(err, result) {
150
+
151
+
152
+ // //, function (err, result) {
153
+ // if (err) {
154
+ // winston.debug(err);
155
+ // winston.debug("ERR",err)
156
+ // return res.status(500).send({success: false, msg: 'Error getting analytics.'});
157
+ // }
158
+ // winston.debug(result);
159
+ // winston.debug("RES",result)
160
+ // res.json(result);
161
+ // });
162
+
163
+ // });
164
+
165
+ router.get('/requests/aggregate/day', function(req, res) {
166
+
167
+ //set default value for lastdays&department_id
168
+ let lastdays=7
169
+ //let department_id='';
170
+
171
+
172
+ //check for lastdays&dep_id parameters
173
+ if(req.query.lastdays){
174
+ lastdays=req.query.lastdays
175
+ }
176
+ let query={"id_project":req.projectid, $or:[ {preflight:false}, { preflight : { $exists: false } } ], "createdAt" : { $gte : new Date((new Date().getTime() - (lastdays * 24 * 60 * 60 * 1000))) }}
177
+
178
+ if(req.query.department_id){
179
+ //department_id=req.query.department_id;
180
+ //add field departmentid to query if req.query.department_id exist
181
+ query.department= new ObjectId(req.query.department_id);
182
+
183
+ }
184
+
185
+ if (req.query.participant) {
186
+ winston.debug('req.query.participant', req.query.participant);
187
+ query.participants = req.query.participant;
188
+ }
189
+
190
+ winston.debug("QueryParams_LastDayCHART:", lastdays,req.query.department_id)
191
+ winston.debug("Query_LastDayCHART", query)
192
+
193
+ winston.debug(req.params);
194
+ winston.debug("req.projectid", req.projectid);
195
+
196
+ AnalyticResult.aggregate([
197
+ // { "$match": {"id_project": req.projectid } },
198
+ // { "$match": {} },
199
+ { $match: query },
200
+ { "$project":{
201
+ "year":{"$year":"$createdAt"},
202
+ "month":{"$month":"$createdAt"},
203
+ "day": {"$dayOfMonth":"$createdAt"}
204
+ }},
205
+ // // Group by year, month and day and get the count
206
+ { "$group":{
207
+ "_id":{"year":"$year", "month":"$month", "day":"$day"},
208
+ "count":{"$sum":1}
209
+ }},
210
+ { "$sort": {"_id":1}},
211
+ // { "$limit": 7 },
212
+ ])
213
+ // .exec((err, result) => {
214
+ .exec(function(err, result) {
215
+
216
+
217
+ //, function (err, result) {
218
+ if (err) {
219
+ winston.debug(err);
220
+ winston.debug("ERR",err)
221
+ return res.status(500).send({success: false, msg: 'Error getting analytics.'});
222
+ }
223
+ winston.debug(result);
224
+ winston.debug("RESULT",result)
225
+ res.json(result);
226
+ });
227
+
228
+ });
229
+
230
+
231
+
232
+ router.get('/requests/aggregate/status/day', function(req, res) {
233
+
234
+ //set default value for lastdays&department_id
235
+ let lastdays=7
236
+ //let department_id='';
237
+
238
+
239
+ //check for lastdays&dep_id parameters
240
+ if(req.query.lastdays){
241
+ lastdays=req.query.lastdays
242
+ }
243
+ let query={"id_project":req.projectid, $or:[ {preflight:false}, { preflight : { $exists: false } } ], "createdAt" : { $gte : new Date((new Date().getTime() - (lastdays * 24 * 60 * 60 * 1000))) }}
244
+
245
+ if(req.query.department_id){
246
+ //department_id=req.query.department_id;
247
+ //add field departmentid to query if req.query.department_id exist
248
+ query.department= new ObjectId(req.query.department_id);
249
+
250
+ }
251
+
252
+ if (req.query.participant) {
253
+ winston.debug('req.query.participant', req.query.participant);
254
+ query.participants = req.query.participant;
255
+ }
256
+
257
+ winston.debug("QueryParams_LastDayCHART:", lastdays,req.query.department_id)
258
+ winston.debug("Query_LastDayCHART", query)
259
+
260
+ winston.debug(req.params);
261
+ winston.debug("req.projectid", req.projectid);
262
+
263
+ AnalyticResult.aggregate([
264
+ // { "$match": {"id_project": req.projectid } },
265
+ // { "$match": {} },
266
+ { $match: query },
267
+ { "$project":{
268
+ "year":{"$year":"$createdAt"},
269
+ "month":{"$month":"$createdAt"},
270
+ "day": {"$dayOfMonth":"$createdAt"},
271
+ "status": "$status"
272
+ }},
273
+ // // Group by year, month and day and get the count
274
+ { "$group":{
275
+ "_id":{"year":"$year", "month":"$month", "day":"$day","status": "$status"},
276
+ "count":{"$sum":1}
277
+ }},
278
+ { "$sort": {"_id":1}},
279
+ // { "$limit": 7 },
280
+ ])
281
+ // .exec((err, result) => {
282
+ .exec(function(err, result) {
283
+
284
+
285
+ //, function (err, result) {
286
+ if (err) {
287
+ winston.debug(err);
288
+ winston.debug("ERR",err)
289
+ return res.status(500).send({success: false, msg: 'Error getting analytics.'});
290
+ }
291
+ winston.debug(result);
292
+ winston.debug("RESULT",result)
293
+ res.json(result);
294
+ });
295
+
296
+ });
297
+
298
+
299
+ router.get('/requests/aggregate/month', function(req, res) {
300
+
301
+
302
+ let query={"id_project": req.projectid , $or:[ {preflight:false}, { preflight : { $exists: false } } ]}
303
+
304
+ if(req.query.department_id){
305
+ //department_id=req.query.department_id;
306
+ //add field departmentid to query if req.query.department_id exist
307
+ query.department= new ObjectId(req.query.department_id);
308
+
309
+ }
310
+
311
+ winston.debug("QueryParams_MonthCHART:", req.query.department_id)
312
+ winston.debug("Query_LastDayCHART", query)
313
+
314
+ winston.debug(req.params);
315
+ winston.debug("req.projectid", req.projectid);
316
+
317
+ AnalyticResult.aggregate([
318
+ { "$match": query},
319
+ // { "$match": {} },
320
+ //{ $match: query },
321
+ { "$project":{
322
+ "year":{"$year":"$createdAt"},
323
+ "month":{"$month":"$createdAt"},
324
+
325
+ }},
326
+ // // Group by year and month and get the count
327
+ { "$group":{
328
+ "_id":{"year":"$year", "month":"$month"},
329
+ "count":{"$sum":1}
330
+ }},
331
+ { "$sort": {"_id":1}},
332
+ // { "$limit": 7 },
333
+ ])
334
+ // .exec((err, result) => {
335
+ .exec(function(err, result) {
336
+
337
+
338
+ //, function (err, result) {
339
+ if (err) {
340
+ winston.debug(err);
341
+ winston.debug("ERR",err)
342
+ return res.status(500).send({success: false, msg: 'Error getting analytics.'});
343
+ }
344
+ winston.debug(result);
345
+ winston.debug("RESULT",result)
346
+ res.json(result);
347
+ });
348
+
349
+ });
350
+
351
+ router.get('/requests/aggregate/week', function(req, res) {
352
+
353
+
354
+ //let query={"id_project":req.projectid, "createdAt" : { $gte : new Date((new Date().getTime() - (lastdays * 24 * 60 * 60 * 1000))) }}
355
+
356
+ if(req.query.department_id){
357
+ //department_id=req.query.department_id;
358
+ //add field departmentid to query if req.query.department_id exist
359
+ //query.department= new ObjectId(req.query.department_id);
360
+
361
+ }
362
+
363
+ //winston.debug("QueryParams_WeekCHART:", lastdays,req.query.department_id)
364
+ //winston.debug("Query_LastDayCHART", query)
365
+
366
+ winston.debug(req.params);
367
+ winston.debug("req.projectid", req.projectid);
368
+
369
+ AnalyticResult.aggregate([
370
+ { "$match": {"id_project": req.projectid, $or:[ {preflight:false}, { preflight : { $exists: false } } ] } },
371
+ // { "$match": {} },
372
+ //{ $match: query },
373
+ { "$project":{
374
+ "year":{"$year":"$createdAt"},
375
+ "month":{"$month":"$createdAt"},
376
+ "day": {"$dayOfMonth":"$createdAt"},
377
+ "week":{"$week":"$createdAt"},
378
+
379
+
380
+ }},
381
+ // // Group by year and month and get the count
382
+ { "$group":{
383
+ "_id":{"year":"$year", "month":"$month","week":"$week", },
384
+ "count":{"$sum":1}
385
+ }},
386
+ { "$sort": {"_id":1}},
387
+ // { "$limit": 7 },
388
+ ])
389
+ // .exec((err, result) => {
390
+ .exec(function(err, result) {
391
+
392
+
393
+ //, function (err, result) {
394
+ if (err) {
395
+ winston.debug(err);
396
+ winston.debug("ERR",err)
397
+ return res.status(500).send({success: false, msg: 'Error getting analytics.'});
398
+ }
399
+ winston.debug(result);
400
+ winston.debug("RESULT",result)
401
+ res.json(result);
402
+ });
403
+
404
+ });
405
+
406
+
407
+
408
+ // db.requests.aggregate([
409
+ // // Get only records created in the last 30 days
410
+ // { $match: {"id_project":"5ad5bd52c975820014ba900a","createdAt" : { $gte : new Date(ISODate().getTime() - 1000*60*60*24*30) }} },
411
+ // // Get the year, month and day from the createdTimeStamp
412
+ // {$project:{
413
+ // "hour":{$hour:"$createdAt"}
414
+ // }},
415
+ // // Group by year, month and day and get the count
416
+ // {$group:{
417
+ // _id:{hour:"$hour"},
418
+ // "count":{$sum:1}
419
+ // }},
420
+ // {$sort:{_id:-1}},
421
+ // ])
422
+
423
+ router.get('/requests/aggregate/hours', function(req, res) {
424
+ winston.debug(req.params);
425
+ winston.debug("req.projectid", req.projectid);
426
+
427
+ let timezone = req.query.timezone || "+00:00";
428
+ winston.debug("timezone", timezone);
429
+
430
+ AnalyticResult.aggregate([
431
+ // { "$match": {"id_project": req.projectid } },
432
+ // { "$match": {} },
433
+ { $match: {"id_project":req.projectid, $or:[ {preflight:false}, { preflight : { $exists: false } } ], "createdAt" : { $gte : new Date((new Date().getTime() - (30 * 24 * 60 * 60 * 1000))) }} },
434
+ { "$project":{
435
+ "hour":{"$hour": {date: "$createdAt", timezone: timezone} }
436
+ }},
437
+ // // Group by year, month and day and get the count
438
+ { "$group":{
439
+ "_id":{"hour":"$hour"},
440
+ "count":{"$sum":1}
441
+ }},
442
+ { "$sort": {"_id":-1}}
443
+ ])
444
+ // .exec((err, result) => {
445
+ .exec(function(err, result) {
446
+
447
+
448
+ //, function (err, result) {
449
+ if (err) {
450
+ winston.debug(err);
451
+ return res.status(500).send({success: false, msg: 'Error getting analytics.'});
452
+ }
453
+ winston.debug(result);
454
+
455
+ res.json(result);
456
+ });
457
+
458
+ });
459
+
460
+
461
+ //test importante qui decommenta per vedere
462
+ // AnalyticResult.aggregate([
463
+ // // { "$match": {"id_project": req.projectid } },
464
+ // // { "$match": {} },
465
+ // { $match: {"id_project":req.projectid, "createdAt" : { $gte : new Date((new Date().getTime() - (30 * 24 * 60 * 60 * 1000))) }} },
466
+ // { "$project":{
467
+ // "hour":{"$hour":"$createdAt"},
468
+ // "weekday":{"$dayoftheweek":"$createdAt"}
469
+ // }},
470
+ // // // Group by year, month and day and get the count
471
+ // { "$group":{
472
+ // "_id":{"hour":"$hour","weekday":"$weekday"},
473
+ // "count":{"$sum":1}
474
+ // }},
475
+ // { "$sort": {"_id":-1}}
476
+ // ])
477
+ router.get('/requests/aggregate/dayoftheweek/hours', function(req, res) {
478
+ winston.debug(req.params);
479
+ winston.debug("req.projectid", req.projectid);
480
+
481
+ let timezone = req.query.timezone || "+00:00";
482
+ winston.debug("timezone", timezone);
483
+
484
+ AnalyticResult.aggregate([
485
+ // { "$match": {"id_project": req.projectid } },
486
+ // { "$match": {} },
487
+ { $match: {"id_project":req.projectid, $or:[ {preflight:false}, { preflight : { $exists: false } } ], "createdAt" : { $gte : new Date((new Date().getTime() - (30 * 24 * 60 * 60 * 1000))) }} },
488
+ { "$project":{
489
+ "hour":{"$hour":{date: "$createdAt", timezone: timezone} },
490
+ "weekday":{"$dayOfWeek":"$createdAt"}
491
+ }},
492
+ // // Group by year, month and day and get the count
493
+ { "$group":{
494
+ "_id":{"hour":"$hour","weekday":"$weekday"},
495
+ "count":{"$sum":1}
496
+ }},
497
+ { "$sort": {"_id":-1}}
498
+ ])
499
+ .exec(function(err, result) {
500
+
501
+
502
+ if (err) {
503
+ winston.debug(err);
504
+ return res.status(500).send({success: false, msg: 'Error getting analytics.'});
505
+ }
506
+ winston.debug(result);
507
+
508
+ res.json(result);
509
+ });
510
+
511
+ });
512
+
513
+
514
+
515
+
516
+
517
+
518
+ // db.requests.aggregate([
519
+ // { $match: {"id_project":"5ad5bd52c975820014ba900a"} },
520
+ // { $group: { _id: "$id_project",
521
+ // "waiting_time_avg":{$avg: "$waiting_time"}
522
+ // }
523
+ // },
524
+ // { "$sort": {"_id":-1}},
525
+ // ])
526
+
527
+
528
+
529
+ router.get('/requests/waiting', function(req, res) {
530
+
531
+ winston.debug(req.params);
532
+ winston.debug("req.projectid", req.projectid);
533
+
534
+ var last = 30 * 24 * 60 * 60;
535
+
536
+ if (req.query.last) {
537
+ last = req.query.last;
538
+ }
539
+
540
+ AnalyticResult.aggregate([
541
+ { $match: {"id_project":req.projectid, $or:[ {preflight:false}, { preflight : { $exists: false } } ], "createdAt" : { $gte : new Date((new Date().getTime() - ( last * 1000))) }} },
542
+ { "$group": {
543
+ "_id": "$id_project",
544
+ "waiting_time_avg":{"$avg": "$waiting_time"}
545
+ }
546
+ },
547
+
548
+ ])
549
+ .exec(function(err, result) {
550
+
551
+ if (err) {
552
+ winston.debug(err);
553
+ return res.status(500).send({success: false, msg: 'Error getting analytics.'});
554
+ }
555
+ winston.debug(result);
556
+
557
+ res.json(result);
558
+ });
559
+
560
+ });
561
+
562
+
563
+ // db.requests.aggregate([
564
+ // { $match: {"id_project":"5ad5bd52c975820014ba900a"} },
565
+ // { $project:{
566
+
567
+ // "month":{$month:"$createdAt"},
568
+ // "year":{$year:"$createdAt"},
569
+ // "waiting_time_project" : "$waiting_time"
570
+ // }},
571
+ // { $group: { _id: {month:"$month", year: "$year"},
572
+ // "waiting_time_avg":{$avg: "$waiting_time_project"}
573
+ // }
574
+ // },
575
+ // { "$sort": {"_id":-1}},
576
+ // ])
577
+
578
+
579
+ router.get('/requests/waiting/day/last', function(req, res) {
580
+
581
+ winston.debug(req.params);
582
+ winston.debug("req.projectid", req.projectid);
583
+
584
+
585
+ AnalyticResult.aggregate([
586
+ //{ "$match": {"id_project":req.projectid }},
587
+ { $match: {"id_project":req.projectid, $or:[ {preflight:false}, { preflight : { $exists: false } } ], "createdAt" : { $gte : new Date((new Date().getTime() - (1 * 24 * 60 * 60 * 1000))) }} },
588
+ { "$group": {
589
+ "_id": "$id_project",
590
+ "waiting_time_avg":{"$avg": "$waiting_time"}
591
+ }
592
+ },
593
+
594
+ ])
595
+ .exec(function(err, result) {
596
+
597
+ if (err) {
598
+ winston.debug(err);
599
+ return res.status(500).send({success: false, msg: 'Error getting analytics.'});
600
+ }
601
+ winston.debug(result);
602
+
603
+ res.json(result);
604
+ });
605
+
606
+ });
607
+
608
+
609
+
610
+ router.get('/requests/waiting/day', function(req, res) {
611
+
612
+ //set default value for lastdays&department_id
613
+ let lastdays=7
614
+ //let department_id='';
615
+
616
+
617
+ //check for lastdays&dep_id parameters
618
+ if(req.query.lastdays){
619
+ lastdays=req.query.lastdays
620
+ }
621
+ let query={"id_project":req.projectid, $or:[ {preflight:false}, { preflight : { $exists: false } } ], "createdAt" : { $gte : new Date((new Date().getTime() - (lastdays * 24 * 60 * 60 * 1000))) }}
622
+
623
+ if(req.query.department_id){
624
+ //department_id=req.query.department_id;
625
+ //add field departmentid to query if req.query.department_id exist
626
+ query.department= new ObjectId(req.query.department_id);
627
+
628
+ }
629
+
630
+ if (req.query.participant) {
631
+ winston.debug('req.query.participant', req.query.participant);
632
+ query.participants = req.query.participant;
633
+ }
634
+
635
+
636
+ winston.debug("QueryParams_AvgTime:", lastdays,req.query.department_id)
637
+ winston.debug("Query_AvgTIME", query)
638
+
639
+ winston.debug(req.params);
640
+ winston.debug("req.projectid", req.projectid);
641
+
642
+
643
+ AnalyticResult.aggregate([
644
+ { $match: query },
645
+ { "$project":{
646
+ "year":{"$year":"$createdAt"},
647
+ "month":{"$month":"$createdAt"},
648
+ "day":{"$dayOfMonth":"$createdAt"},
649
+ "waiting_time_project" : "$waiting_time"
650
+ }},
651
+ { "$group": {
652
+ "_id": {"day":"$day","month":"$month", "year": "$year"},
653
+ "waiting_time_avg":{"$avg": "$waiting_time_project"}
654
+ }
655
+ },
656
+ { "$sort": {"_id":-1}}
657
+
658
+ ])
659
+ .exec(function(err, result) {
660
+
661
+ if (err) {
662
+ winston.debug(err);
663
+ return res.status(500).send({success: false, msg: 'Error getting analytics.'});
664
+ }
665
+ winston.debug(result);
666
+ winston.debug("RES",result)
667
+ res.json(result);
668
+ });
669
+
670
+ });
671
+
672
+
673
+
674
+ router.get('/requests/waiting/month', function(req, res) {
675
+
676
+ winston.debug(req.params);
677
+ winston.debug("req.projectid", req.projectid);
678
+
679
+
680
+ AnalyticResult.aggregate([
681
+ { "$match": {"id_project":req.projectid,$or:[ {preflight:false}, { preflight : { $exists: false } } ] }},
682
+ { "$project":{
683
+ "year":{"$year":"$createdAt"},
684
+ "month":{"$month":"$createdAt"},
685
+ "waiting_time_project" : "$waiting_time"
686
+ }},
687
+ { "$group": {
688
+ "_id": {"month":"$month", "year": "$year"},
689
+ "waiting_time_avg":{"$avg": "$waiting_time_project"}
690
+ }
691
+ },
692
+ { "$sort": {"_id":-1}}
693
+ ])
694
+ .exec(function(err, result) {
695
+
696
+ if (err) {
697
+ winston.debug(err);
698
+ return res.status(500).send({success: false, msg: 'Error getting analytics.'});
699
+ }
700
+ winston.debug(result);
701
+
702
+ res.json(result);
703
+ });
704
+
705
+ });
706
+
707
+
708
+
709
+ //duration
710
+
711
+
712
+
713
+ // db.getCollection('requests').aggregate( [
714
+ // { $match: {"closed_at":{$exists:true}, "id_project":"5ad5bd52c975820014ba900a","createdAt" : { $gte : new Date(ISODate().getTime() - 1000*60*60*24*1) }} },
715
+ // {$project:{
716
+ // "duration": {$subtract: ["$closed_at","$createdAt"]},
717
+ // "id_project":1
718
+ // }},
719
+ // { "$group": {
720
+ // "_id": "$id_project",
721
+ // "duration_avg":{"$avg": "$duration"}
722
+ // }
723
+ // }
724
+ // ] )
725
+
726
+
727
+ router.get('/requests/duration', function(req, res) {
728
+
729
+ winston.debug(req.params);
730
+ winston.debug("req.projectid", req.projectid);
731
+
732
+
733
+ AnalyticResult.aggregate([
734
+ { $match: {"id_project":req.projectid, $or:[ {preflight:false}, { preflight : { $exists: false } } ], "createdAt" : { $gte : new Date((new Date().getTime() - (30 * 24 * 60 * 60 * 1000))) }} },
735
+ {$project:{
736
+ "duration": {$subtract: ["$closed_at","$createdAt"]},
737
+ "id_project":1
738
+ }},
739
+ { "$group": {
740
+ "_id": "$id_project",
741
+ "duration_avg":{"$avg": "$duration"}
742
+ }
743
+ },
744
+
745
+ ])
746
+ .exec(function(err, result) {
747
+
748
+ if (err) {
749
+ winston.debug(err);
750
+ return res.status(500).send({success: false, msg: 'Error getting analytics.'});
751
+ }
752
+ winston.debug(result);
753
+
754
+ res.json(result);
755
+ });
756
+
757
+ });
758
+
759
+ /*
760
+ db.getCollection('requests').aggregate( [
761
+ { $match: {"closed_at":{$exists:true}, "id_project":"5ad5bd52c975820014ba900a","createdAt" : { $gte : new Date(ISODate().getTime() - 1000*60*60*24*30) }} },
762
+ { "$project":{
763
+ "year":{"$year":"$createdAt"},
764
+ "month":{"$month":"$createdAt"},
765
+ "day":{"$dayOfMonth":"$createdAt"},
766
+ "duration": {$subtract: ["$closed_at","$createdAt"]},
767
+ }},
768
+ { "$group": {
769
+ "_id": {"day":"$day","month":"$month", "year": "$year"},
770
+ "duration_avg":{"$avg": "$duration"}
771
+ }
772
+ },
773
+ { "$sort": {"_id":-1}}
774
+ ] )
775
+ */
776
+
777
+
778
+ router.get('/requests/duration/day', function(req, res) {
779
+
780
+ //set default value for lastdays&department_id
781
+ let lastdays=7
782
+ //let department_id='';
783
+
784
+
785
+ //check for lastdays&dep_id parameters
786
+ if(req.query.lastdays){
787
+ lastdays=req.query.lastdays
788
+ }
789
+
790
+ let query={"id_project":req.projectid, $or:[ {preflight:false}, { preflight : { $exists: false } } ], "createdAt" : { $gte : new Date((new Date().getTime() - (lastdays * 24 * 60 * 60 * 1000))) }}
791
+
792
+ if(req.query.department_id){
793
+ //department_id=req.query.department_id;
794
+ //add field departmentid to query if req.query.department_id exist
795
+ query.department= new ObjectId(req.query.department_id);
796
+
797
+ }
798
+
799
+ if (req.query.participant) {
800
+ winston.debug('req.query.participant', req.query.participant);
801
+ query.participants = req.query.participant;
802
+ }
803
+
804
+
805
+ winston.debug("QueryParams_DurationTIME:", lastdays,req.query.department_id)
806
+ winston.debug("Query_DurationTIME", query)
807
+
808
+ winston.debug(req.params);
809
+ winston.debug("req.projectid", req.projectid);
810
+
811
+
812
+ AnalyticResult.aggregate([
813
+ { $match: query },
814
+ { "$project":{
815
+ "year":{"$year":"$createdAt"},
816
+ "month":{"$month":"$createdAt"},
817
+ "day":{"$dayOfMonth":"$createdAt"},
818
+ "duration": {"$subtract": ["$closed_at","$createdAt"]},
819
+ }},
820
+ { "$group": {
821
+ "_id": {"day":"$day","month":"$month", "year": "$year"},
822
+ "duration_avg":{"$avg": "$duration"}
823
+ }
824
+ },
825
+ { "$sort": {"_id":-1}}
826
+
827
+ ])
828
+ .exec(function(err, result) {
829
+
830
+ if (err) {
831
+ winston.debug(err);
832
+ return res.status(500).send({success: false, msg: 'Error getting analytics.'});
833
+ }
834
+ winston.debug(result);
835
+
836
+ res.json(result);
837
+ });
838
+
839
+ });
840
+
841
+
842
+ router.get('/requests/duration/month', function(req, res) {
843
+
844
+ winston.debug(req.params);
845
+ winston.debug("req.projectid", req.projectid);
846
+
847
+
848
+ AnalyticResult.aggregate([
849
+ { $match: {"id_project":req.projectid, $or:[ {preflight:false}, { preflight : { $exists: false } } ], "createdAt" : { $gte : new Date((new Date().getTime() - (30 * 24 * 60 * 60 * 1000))) }} },
850
+ { "$project":{
851
+ "year":{"$year":"$createdAt"},
852
+ "month":{"$month":"$createdAt"},
853
+ "duration": {"$subtract": ["$closed_at","$createdAt"]},
854
+ }},
855
+ { "$group": {
856
+ "_id": {"month":"$month", "year": "$year"},
857
+ "duration_avg":{"$avg": "$duration"}
858
+ }
859
+ },
860
+ { "$sort": {"_id":-1}}
861
+
862
+ ])
863
+ .exec(function(err, result) {
864
+
865
+ if (err) {
866
+ winston.debug(err);
867
+ return res.status(500).send({success: false, msg: 'Error getting analytics.'});
868
+ }
869
+ winston.debug(result);
870
+
871
+ res.json(result);
872
+ });
873
+
874
+ });
875
+
876
+
877
+
878
+
879
+
880
+ router.get('/requests/satisfaction', function(req, res) {
881
+
882
+ winston.debug(req.params);
883
+ winston.debug("req.projectid", req.projectid);
884
+
885
+
886
+ AnalyticResult.aggregate([
887
+ { "$match": {"id_project":req.projectid, $or:[ {preflight:false}, { preflight : { $exists: false } } ]}},
888
+ { "$group": {
889
+ "_id": "$id_project",
890
+ "satisfaction_avg":{"$avg": "$rating"}
891
+ }
892
+ },
893
+
894
+ ])
895
+ .exec(function(err, result) {
896
+
897
+ if (err) {
898
+ winston.debug(err);
899
+ return res.status(500).send({success: false, msg: 'Error getting analytics.'});
900
+ }
901
+ winston.debug(result);
902
+
903
+ res.json(result);
904
+ });
905
+
906
+ });
907
+
908
+ router.get('/requests/satisfaction/day', function(req, res) {
909
+
910
+ //set default value for lastdays&department_id
911
+ let lastdays=7
912
+ //let department_id='';
913
+
914
+ //check for lastdays&dep_id parameters
915
+ if(req.query.lastdays){
916
+ lastdays=req.query.lastdays
917
+ }
918
+
919
+ let query={"id_project":req.projectid, $or:[ {preflight:false}, { preflight : { $exists: false } } ], "createdAt" : { $gte : new Date((new Date().getTime() - (lastdays * 24 * 60 * 60 * 1000))) }}
920
+
921
+ if(req.query.department_id){
922
+ //department_id=req.query.department_id;
923
+ //add field departmentid to query if req.query.department_id exist
924
+ query.department= new ObjectId(req.query.department_id);
925
+
926
+ }
927
+
928
+ if (req.query.participant) {
929
+ winston.debug('req.query.participant', req.query.participant);
930
+ query.participants = req.query.participant;
931
+ }
932
+
933
+
934
+
935
+ winston.debug("QueryParams_SatisfactionTIME:", lastdays,req.query.department_id)
936
+ winston.debug("Query_SatisfactionTIME", query)
937
+
938
+ winston.debug(req.params);
939
+ winston.debug("req.projectid", req.projectid);
940
+
941
+
942
+ AnalyticResult.aggregate([
943
+ { $match: query },
944
+ { "$project":{
945
+ "year":{"$year":"$createdAt"},
946
+ "month":{"$month":"$createdAt"},
947
+ "day":{"$dayOfMonth":"$createdAt"},
948
+ "satisfaction_project" : "$rating"
949
+ }},
950
+ { "$group": {
951
+ "_id": {"day":"$day","month":"$month", "year": "$year"},
952
+ "satisfaction_avg":{"$avg": "$satisfaction_project"}
953
+ }
954
+ },
955
+ { "$sort": {"_id":-1}}
956
+
957
+ ])
958
+ .exec(function(err, result) {
959
+
960
+ if (err) {
961
+ winston.debug(err);
962
+ return res.status(500).send({success: false, msg: 'Error getting analytics.'});
963
+ }
964
+ winston.debug(result);
965
+
966
+ res.json(result);
967
+ });
968
+
969
+ });
970
+
971
+
972
+ router.get('/requests/satisfaction/month', function(req, res) {
973
+
974
+ winston.debug(req.params);
975
+ winston.debug("req.projectid", req.projectid);
976
+
977
+
978
+ AnalyticResult.aggregate([
979
+ { "$match": {"id_project":req.projectid, $or:[ {preflight:false}, { preflight : { $exists: false } } ] }},
980
+ { "$project":{
981
+ "year":{"$year":"$createdAt"},
982
+ "month":{"$month":"$createdAt"},
983
+ "satisfaction_project" : "$rating"
984
+ }},
985
+ { "$group": {
986
+ "_id": {"month":"$month", "year": "$year"},
987
+ "satisfaction_avg":{"$avg": "$satisfaction_project"}
988
+ }
989
+ },
990
+
991
+ ])
992
+ .exec(function(err, result) {
993
+
994
+ if (err) {
995
+ winston.debug(err);
996
+ return res.status(500).send({success: false, msg: 'Error getting analytics.'});
997
+ }
998
+ winston.debug(result);
999
+
1000
+ res.json(result);
1001
+ });
1002
+
1003
+ });
1004
+
1005
+
1006
+
1007
+
1008
+ router.get('/requests/hasBot/count', function(req, res) {
1009
+
1010
+ winston.debug(req.params);
1011
+ winston.debug("req.projectid", req.projectid);
1012
+
1013
+
1014
+ AnalyticResult.aggregate([
1015
+ // { "$match": {"id_project": req.projectid } },
1016
+ // { "$match": {} },
1017
+ { "$match": {"id_project":req.projectid, hasBot:true, $or:[ {preflight:false}, { preflight : { $exists: false } } ], "createdAt" : { $gte : new Date((new Date().getTime() - (30 * 24 * 60 * 60 * 1000))) }} },
1018
+ { "$count": "totalCount" }
1019
+
1020
+ ])
1021
+ // .exec((err, result) => {
1022
+ .exec(function(err, result) {
1023
+
1024
+
1025
+ //, function (err, result) {
1026
+ if (err) {
1027
+ winston.debug(err);
1028
+ return res.status(500).send({success: false, msg: 'Error getting analytics.'});
1029
+ }
1030
+ winston.debug(result);
1031
+
1032
+ res.json(result);
1033
+ });
1034
+
1035
+ });
1036
+
1037
+
1038
+
1039
+
1040
+ router.get('/requests/aggregate/hasBot/day', function(req, res) {
1041
+
1042
+ //set default value for lastdays&department_id
1043
+ let lastdays=7
1044
+ //let department_id='';
1045
+
1046
+
1047
+ //check for lastdays&dep_id parameters
1048
+ if(req.query.lastdays){
1049
+ lastdays=req.query.lastdays
1050
+ }
1051
+ let query={"id_project":req.projectid, $or:[ {preflight:false}, { preflight : { $exists: false } } ], "createdAt" : { $gte : new Date((new Date().getTime() - (lastdays * 24 * 60 * 60 * 1000))) }}
1052
+
1053
+ if(req.query.department_id){
1054
+ //department_id=req.query.department_id;
1055
+ //add field departmentid to query if req.query.department_id exist
1056
+ query.department= new ObjectId(req.query.department_id);
1057
+
1058
+ }
1059
+
1060
+ winston.debug("QueryParams_LastDayCHART:", lastdays,req.query.department_id)
1061
+ winston.debug("Query_LastDayCHART", query)
1062
+
1063
+ winston.debug(req.params);
1064
+ winston.debug("req.projectid", req.projectid);
1065
+
1066
+ AnalyticResult.aggregate([
1067
+ // { "$match": {"id_project": req.projectid } },
1068
+ // { "$match": {} },
1069
+ { $match: query },
1070
+ { "$project":{
1071
+ "year":{"$year":"$createdAt"},
1072
+ "month":{"$month":"$createdAt"},
1073
+ "day": {"$dayOfMonth":"$createdAt"},
1074
+ "hasBot": "$hasBot"
1075
+ }},
1076
+ // // Group by year, month and day and get the count
1077
+ { "$group":{
1078
+ "_id":{"year":"$year", "month":"$month", "day":"$day", "hasBot": "$hasBot"},
1079
+ "count":{"$sum":1}
1080
+ }},
1081
+ { "$sort": {"_id":1}},
1082
+ // { "$limit": 7 },
1083
+ ])
1084
+ // .exec((err, result) => {
1085
+ .exec(function(err, result) {
1086
+
1087
+
1088
+ //, function (err, result) {
1089
+ if (err) {
1090
+ winston.debug(err);
1091
+ winston.debug("ERR",err)
1092
+ return res.status(500).send({success: false, msg: 'Error getting analytics.'});
1093
+ }
1094
+ winston.debug(result);
1095
+ winston.debug("RESULT",result)
1096
+ res.json(result);
1097
+ });
1098
+
1099
+ });
1100
+
1101
+
1102
+ router.get('/requests/aggregate/sourcePage', function(req, res) {
1103
+ //set default value for lastdays&department_id
1104
+ let lastdays=30
1105
+ //let department_id='';
1106
+
1107
+
1108
+ //check for lastdays&dep_id parameters
1109
+ if(req.query.lastdays){
1110
+ lastdays=req.query.lastdays
1111
+ }
1112
+ let query={"id_project":req.projectid, $or:[ {preflight:false}, { preflight : { $exists: false } } ], "createdAt" : { $gte : new Date((new Date().getTime() - (lastdays * 24 * 60 * 60 * 1000))) }}
1113
+
1114
+ if(req.query.department_id){
1115
+ //department_id=req.query.department_id;
1116
+ //add field departmentid to query if req.query.department_id exist
1117
+ query.department= new ObjectId(req.query.department_id);
1118
+
1119
+ }
1120
+
1121
+ winston.debug("QueryParams_LastDayCHART:", lastdays,req.query.department_id)
1122
+ winston.debug("Query_LastDayCHART", query)
1123
+
1124
+ winston.debug(req.params);
1125
+ winston.debug("req.projectid", req.projectid);
1126
+
1127
+ AnalyticResult.aggregate([
1128
+ // { "$match": {"id_project": req.projectid } },
1129
+ // { "$match": {} },
1130
+ { $match: query },
1131
+ { "$project":{
1132
+ "sourcePage":"$sourcePage",
1133
+ }},
1134
+ // // Group by year, month and day and get the count
1135
+ { "$group":{
1136
+ "_id":{"sourcePage":"$sourcePage"},
1137
+ "count":{"$sum":1}
1138
+ }},
1139
+ { "$sort": {"_id":1}},
1140
+ // { "$limit": 7 },
1141
+ ])
1142
+ // .exec((err, result) => {
1143
+ .exec(function(err, result) {
1144
+
1145
+
1146
+ //, function (err, result) {
1147
+ if (err) {
1148
+ winston.debug(err);
1149
+ winston.debug("ERR",err)
1150
+ return res.status(500).send({success: false, msg: 'Error getting analytics.'});
1151
+ }
1152
+ winston.debug(result);
1153
+ winston.debug("RESULT",result)
1154
+ res.json(result);
1155
+ });
1156
+
1157
+ });
1158
+
1159
+
1160
+
1161
+
1162
+
1163
+
1164
+
1165
+
1166
+
1167
+ router.get('/project_users/count', function(req, res) {
1168
+
1169
+ winston.debug(req.params);
1170
+ winston.debug("req.projectid: "+ req.projectid);
1171
+
1172
+ AnalyticProject_UsersResult.aggregate([
1173
+ { "$match": {"id_project":new ObjectId(req.projectid), role: { $in : [RoleConstants.GUEST, RoleConstants.USER]}, status: "active", "createdAt" : { $gte : new Date((new Date().getTime() - (30 * 24 * 60 * 60 * 1000))) }} },
1174
+ { "$count": "totalCount" }
1175
+
1176
+ ])
1177
+ .exec(function(err, result) {
1178
+ if (err) {
1179
+ winston.debug(err);
1180
+ return res.status(500).send({success: false, msg: 'Error getting analytics.'});
1181
+ }
1182
+ winston.debug(result);
1183
+
1184
+ res.json(result);
1185
+ });
1186
+
1187
+ });
1188
+
1189
+
1190
+
1191
+
1192
+ router.get('/project_users/aggregate/day', function(req, res) {
1193
+
1194
+ //set default value for lastdays&department_id
1195
+ let lastdays=7
1196
+ //let department_id='';
1197
+
1198
+
1199
+ //check for lastdays&dep_id parameters
1200
+ if(req.query.lastdays){
1201
+ lastdays=req.query.lastdays
1202
+ }
1203
+ let query={"id_project":new ObjectId(req.projectid), role: { $in : [RoleConstants.GUEST, RoleConstants.USER]}, status: "active", "createdAt" : { $gte : new Date((new Date().getTime() - (lastdays * 24 * 60 * 60 * 1000))) }}
1204
+
1205
+
1206
+ winston.debug("Query_LastDayCHART", query)
1207
+
1208
+ winston.debug(req.params);
1209
+ winston.debug("req.projectid", req.projectid);
1210
+
1211
+ AnalyticProject_UsersResult.aggregate([
1212
+ // { "$match": {"id_project": req.projectid } },
1213
+ // { "$match": {} },
1214
+ { $match: query },
1215
+ { "$project":{
1216
+ "year":{"$year":"$createdAt"},
1217
+ "month":{"$month":"$createdAt"},
1218
+ "day": {"$dayOfMonth":"$createdAt"}
1219
+ }},
1220
+ // // Group by year, month and day and get the count
1221
+ { "$group":{
1222
+ "_id":{"year":"$year", "month":"$month", "day":"$day"},
1223
+ "count":{"$sum":1}
1224
+ }},
1225
+ { "$sort": {"_id":1}},
1226
+ // { "$limit": 7 },
1227
+ ])
1228
+ // .exec((err, result) => {
1229
+ .exec(function(err, result) {
1230
+
1231
+
1232
+ //, function (err, result) {
1233
+ if (err) {
1234
+ winston.debug(err);
1235
+ winston.debug("ERR",err)
1236
+ return res.status(500).send({success: false, msg: 'Error getting analytics.'});
1237
+ }
1238
+ winston.debug(result);
1239
+ winston.debug("RESULT",result)
1240
+ res.json(result);
1241
+ });
1242
+
1243
+ });
1244
+
1245
+ router.get('/project_users/aggregate/month', function(req, res) {
1246
+
1247
+
1248
+ let query={"id_project": new ObjectId(req.projectid), role: { $in : [RoleConstants.GUEST, RoleConstants.USER]}, status: "active"}
1249
+
1250
+ winston.debug("Query_LastDayCHART", query)
1251
+
1252
+ winston.debug(req.params);
1253
+ winston.debug("req.projectid", req.projectid);
1254
+
1255
+ AnalyticProject_UsersResult.aggregate([
1256
+ { "$match": query},
1257
+ // { "$match": {} },
1258
+ //{ $match: query },
1259
+ { "$project":{
1260
+ "year":{"$year":"$createdAt"},
1261
+ "month":{"$month":"$createdAt"},
1262
+
1263
+ }},
1264
+ // // Group by year and month and get the count
1265
+ { "$group":{
1266
+ "_id":{"year":"$year", "month":"$month"},
1267
+ "count":{"$sum":1}
1268
+ }},
1269
+ { "$sort": {"_id":1}},
1270
+ // { "$limit": 7 },
1271
+ ])
1272
+ // .exec((err, result) => {
1273
+ .exec(function(err, result) {
1274
+
1275
+
1276
+ //, function (err, result) {
1277
+ if (err) {
1278
+ winston.debug(err);
1279
+ winston.debug("ERR",err)
1280
+ return res.status(500).send({success: false, msg: 'Error getting analytics.'});
1281
+ }
1282
+ winston.debug(result);
1283
+ winston.debug("RESULT",result)
1284
+ res.json(result);
1285
+ });
1286
+
1287
+ });
1288
+
1289
+ router.get('/project_users/aggregate/week', function(req, res) {
1290
+
1291
+
1292
+ winston.debug(req.params);
1293
+ winston.debug("req.projectid", req.projectid);
1294
+
1295
+ AnalyticProject_UsersResult.aggregate([
1296
+ { "$match": {"id_project": new ObjectId(req.projectid), role: { $in : [RoleConstants.GUEST, RoleConstants.USER]}, status: "active" } },
1297
+ // { "$match": {} },
1298
+ //{ $match: query },
1299
+ { "$project":{
1300
+ "year":{"$year":"$createdAt"},
1301
+ "month":{"$month":"$createdAt"},
1302
+ "day": {"$dayOfMonth":"$createdAt"},
1303
+ "week":{"$week":"$createdAt"},
1304
+
1305
+
1306
+ }},
1307
+ // // Group by year and month and get the count
1308
+ { "$group":{
1309
+ "_id":{"year":"$year", "month":"$month","week":"$week", },
1310
+ "count":{"$sum":1}
1311
+ }},
1312
+ { "$sort": {"_id":1}},
1313
+ // { "$limit": 7 },
1314
+ ])
1315
+ // .exec((err, result) => {
1316
+ .exec(function(err, result) {
1317
+
1318
+
1319
+ //, function (err, result) {
1320
+ if (err) {
1321
+ winston.debug(err);
1322
+ winston.debug("ERR",err)
1323
+ return res.status(500).send({success: false, msg: 'Error getting analytics.'});
1324
+ }
1325
+ winston.debug(result);
1326
+ winston.debug("RESULT",result)
1327
+ res.json(result);
1328
+ });
1329
+
1330
+ });
1331
+
1332
+
1333
+
1334
+
1335
+
1336
+
1337
+
1338
+
1339
+
1340
+
1341
+
1342
+
1343
+
1344
+
1345
+
1346
+ router.get('/messages/count', function(req, res) {
1347
+
1348
+ winston.debug(req.params);
1349
+ winston.debug("req.projectid: "+ req.projectid);
1350
+
1351
+ var query = {"id_project":req.projectid, "createdAt" : { $gte : new Date((new Date().getTime() - (30 * 24 * 60 * 60 * 1000))) }};
1352
+
1353
+ if (req.query.sender) {
1354
+ query.sender = req.query.sender;
1355
+ }
1356
+
1357
+ if (req.query.recipient) {
1358
+ query.recipient = req.query.recipient;
1359
+ }
1360
+
1361
+ if (req.query.attributes) {
1362
+ query.attributes = req.query.attributes;
1363
+ }
1364
+
1365
+ if (req.query.attributes_answerid) {
1366
+ query["attributes._answerid"] = req.query.attributes_answerid;
1367
+ }
1368
+
1369
+
1370
+ winston.debug("query ", query);
1371
+
1372
+ AnalyticMessagesResult.aggregate([
1373
+ { "$match":query},
1374
+ { "$count": "totalCount" }
1375
+
1376
+ ])
1377
+ .exec(function(err, result) {
1378
+ if (err) {
1379
+ winston.debug(err);
1380
+ return res.status(500).send({success: false, msg: 'Error getting analytics.'});
1381
+ }
1382
+ winston.debug(result);
1383
+
1384
+ res.json(result);
1385
+ });
1386
+
1387
+ });
1388
+
1389
+
1390
+
1391
+
1392
+ router.get('/messages/aggregate/day', function(req, res) {
1393
+
1394
+ //set default value for lastdays&department_id
1395
+ let lastdays=7
1396
+ //let department_id='';
1397
+
1398
+
1399
+ //check for lastdays&dep_id parameters
1400
+ if(req.query.lastdays){
1401
+ lastdays=req.query.lastdays
1402
+ }
1403
+ let query={"id_project":req.projectid, "createdAt" : { $gte : new Date((new Date().getTime() - (lastdays * 24 * 60 * 60 * 1000))) }}
1404
+
1405
+ if (req.query.sender) {
1406
+ query.sender = req.query.sender;
1407
+ }
1408
+
1409
+ if (req.query.recipient) {
1410
+ query.recipient = req.query.recipient;
1411
+ }
1412
+
1413
+
1414
+ winston.debug("Query_LastDayCHART", query)
1415
+
1416
+ winston.debug(req.params);
1417
+ winston.debug("req.projectid", req.projectid);
1418
+
1419
+ AnalyticMessagesResult.aggregate([
1420
+ { $match: query },
1421
+ { "$project":{
1422
+ "year":{"$year":"$createdAt"},
1423
+ "month":{"$month":"$createdAt"},
1424
+ "day": {"$dayOfMonth":"$createdAt"}
1425
+ }},
1426
+ // // Group by year, month and day and get the count
1427
+ { "$group":{
1428
+ "_id":{"year":"$year", "month":"$month", "day":"$day"},
1429
+ "count":{"$sum":1}
1430
+ }},
1431
+ { "$sort": {"_id":1}},
1432
+ ])
1433
+ .exec(function(err, result) {
1434
+
1435
+
1436
+ if (err) {
1437
+ winston.debug(err);
1438
+ winston.debug("ERR",err)
1439
+ return res.status(500).send({success: false, msg: 'Error getting analytics.'});
1440
+ }
1441
+ winston.debug(result);
1442
+ winston.debug("RESULT",result)
1443
+ res.json(result);
1444
+ });
1445
+
1446
+ });
1447
+
1448
+ router.get('/messages/aggregate/month', function(req, res) {
1449
+
1450
+
1451
+ let query={"id_project": req.projectid}
1452
+
1453
+
1454
+ if (req.query.sender) {
1455
+ query.sender = req.query.sender;
1456
+ }
1457
+
1458
+ if (req.query.recipient) {
1459
+ query.recipient = req.query.recipient;
1460
+ }
1461
+
1462
+ winston.debug("Query_LastDayCHART", query)
1463
+
1464
+ winston.debug(req.params);
1465
+ winston.debug("req.projectid", req.projectid);
1466
+
1467
+ AnalyticMessagesResult.aggregate([
1468
+ { "$match": query},
1469
+ { "$project":{
1470
+ "year":{"$year":"$createdAt"},
1471
+ "month":{"$month":"$createdAt"},
1472
+
1473
+ }},
1474
+ // // Group by year and month and get the count
1475
+ { "$group":{
1476
+ "_id":{"year":"$year", "month":"$month"},
1477
+ "count":{"$sum":1}
1478
+ }},
1479
+ { "$sort": {"_id":1}},
1480
+ ])
1481
+ .exec(function(err, result) {
1482
+ if (err) {
1483
+ winston.debug(err);
1484
+ winston.debug("ERR",err)
1485
+ return res.status(500).send({success: false, msg: 'Error getting analytics.'});
1486
+ }
1487
+ winston.debug(result);
1488
+ winston.debug("RESULT",result)
1489
+ res.json(result);
1490
+ });
1491
+
1492
+ });
1493
+
1494
+ router.get('/messages/aggregate/week', function(req, res) {
1495
+
1496
+
1497
+ winston.debug(req.params);
1498
+ winston.debug("req.projectid", req.projectid);
1499
+
1500
+ let query={"id_project": req.projectid}
1501
+
1502
+ if (req.query.sender) {
1503
+ query.sender = req.query.sender;
1504
+ }
1505
+
1506
+ if (req.query.recipient) {
1507
+ query.recipient = req.query.recipient;
1508
+ }
1509
+
1510
+
1511
+ AnalyticMessagesResult.aggregate([
1512
+ { "$match":query },
1513
+ { "$project":{
1514
+ "year":{"$year":"$createdAt"},
1515
+ "month":{"$month":"$createdAt"},
1516
+ "day": {"$dayOfMonth":"$createdAt"},
1517
+ "week":{"$week":"$createdAt"},
1518
+
1519
+
1520
+ }},
1521
+ // // Group by year and month and get the count
1522
+ { "$group":{
1523
+ "_id":{"year":"$year", "month":"$month","week":"$week", },
1524
+ "count":{"$sum":1}
1525
+ }},
1526
+ { "$sort": {"_id":1}},
1527
+ ])
1528
+ .exec(function(err, result) {
1529
+ if (err) {
1530
+ winston.debug(err);
1531
+ winston.debug("ERR",err)
1532
+ return res.status(500).send({success: false, msg: 'Error getting analytics.'});
1533
+ }
1534
+ winston.debug(result);
1535
+ winston.debug("RESULT",result)
1536
+ res.json(result);
1537
+ });
1538
+
1539
+ });
1540
+
1541
+
1542
+
1543
+
1544
+ router.get('/messages/aggregate/sender', function(req, res) {
1545
+
1546
+ //set default value for lastdays&department_id
1547
+ let lastdays=7
1548
+ //let department_id='';
1549
+
1550
+
1551
+ //check for lastdays&dep_id parameters
1552
+ if(req.query.lastdays){
1553
+ lastdays=req.query.lastdays
1554
+ }
1555
+ let query={"id_project":req.projectid, "createdAt" : { $gte : new Date((new Date().getTime() - (lastdays * 24 * 60 * 60 * 1000))) }}
1556
+
1557
+ if (req.query.sender) {
1558
+ query.sender = req.query.sender;
1559
+ }
1560
+
1561
+ if (req.query.recipient) {
1562
+ query.recipient = req.query.recipient;
1563
+ }
1564
+
1565
+
1566
+ winston.debug("Query_LastDayCHART", query)
1567
+
1568
+ winston.debug(req.params);
1569
+ winston.debug("req.projectid", req.projectid);
1570
+
1571
+ AnalyticMessagesResult.aggregate([
1572
+ { $match: query },
1573
+ { "$project":{
1574
+ "sender":"$sender"
1575
+ }},
1576
+ // // Group by year, month and day and get the count
1577
+ { "$group":{
1578
+ "_id":{"sender":"$sender"},
1579
+ "count":{"$sum":1}
1580
+ }},
1581
+ { "$sort": {"_id":1}},
1582
+ ])
1583
+ .exec(function(err, result) {
1584
+
1585
+
1586
+ if (err) {
1587
+ winston.debug(err);
1588
+ winston.debug("ERR",err)
1589
+ return res.status(500).send({success: false, msg: 'Error getting analytics.'});
1590
+ }
1591
+ winston.debug(result);
1592
+ winston.debug("RESULT",result)
1593
+ res.json(result);
1594
+ });
1595
+
1596
+ });
1597
+
1598
+
1599
+
1600
+ router.get('/requests/aggregate/attributes/_answerid', function(req, res) {
1601
+ //set default value for lastdays&department_id
1602
+ let lastdays=7
1603
+ //let department_id='';
1604
+
1605
+
1606
+ //check for lastdays&dep_id parameters
1607
+ if(req.query.lastdays){
1608
+ lastdays=req.query.lastdays
1609
+ }
1610
+ let query={"id_project":req.projectid, "createdAt" : { $gte : new Date((new Date().getTime() - (lastdays * 24 * 60 * 60 * 1000))) }}
1611
+
1612
+ if (req.query.sender) {
1613
+ query.sender = req.query.sender;
1614
+ }
1615
+
1616
+ if (req.query.recipient) {
1617
+ query.recipient = req.query.recipient;
1618
+ }
1619
+
1620
+
1621
+ winston.debug("Query_LastDayCHART", query)
1622
+
1623
+ winston.debug(req.params);
1624
+ winston.debug("req.projectid", req.projectid);
1625
+
1626
+ AnalyticMessagesResult.aggregate([
1627
+ { $match: query },
1628
+ { "$project":{
1629
+ "_answerid":"$attributes._answerid",
1630
+ }},
1631
+ // // Group by year, month and day and get the count
1632
+ { "$group":{
1633
+ "_id":{"_answerid":"$_answerid"},
1634
+ "count":{"$sum":1}
1635
+ }},
1636
+ { "$sort": {"_id":1}},
1637
+ ])
1638
+ .exec(function(err, result) {
1639
+
1640
+
1641
+ if (err) {
1642
+ winston.debug(err);
1643
+ winston.debug("ERR",err)
1644
+ return res.status(500).send({success: false, msg: 'Error getting analytics.'});
1645
+ }
1646
+ winston.debug(result);
1647
+ winston.debug("RESULT",result)
1648
+ res.json(result);
1649
+ });
1650
+
1651
+ });
1652
+
1653
+
1654
+
1655
+
1656
+
1657
+
1658
+
1659
+
1660
+
1661
+
1662
+ router.get('/events/aggregate/day', function(req, res) {
1663
+
1664
+ //set default value for lastdays&department_id
1665
+ let lastdays=7
1666
+ //let department_id='';
1667
+
1668
+
1669
+ //check for lastdays&dep_id parameters
1670
+ if(req.query.lastdays){
1671
+ lastdays=req.query.lastdays
1672
+ }
1673
+ let query={"id_project":req.projectid, "createdAt" : { $gte : new Date((new Date().getTime() - (lastdays * 24 * 60 * 60 * 1000))) }}
1674
+
1675
+ if (req.query.name) {
1676
+ query.name = req.query.name;
1677
+ } else {
1678
+ query.name = {$ne : "typing.start"};
1679
+ }
1680
+
1681
+
1682
+
1683
+ winston.debug("Query_LastDayCHART", query)
1684
+
1685
+ winston.debug(req.params);
1686
+ winston.debug("req.projectid", req.projectid);
1687
+
1688
+ AnalyticEventsResult.aggregate([
1689
+ { $match: query },
1690
+ { "$project":{
1691
+ "year":{"$year":"$createdAt"},
1692
+ "month":{"$month":"$createdAt"},
1693
+ "day": {"$dayOfMonth":"$createdAt"},
1694
+ "name": "$name"
1695
+ }},
1696
+ // // Group by year, month and day and get the count
1697
+ { "$group":{
1698
+ "_id":{"year":"$year", "month":"$month", "day":"$day","name":"$name"},
1699
+ "count":{"$sum":1}
1700
+ }},
1701
+ { "$sort": {"_id":1}},
1702
+ ])
1703
+ .exec(function(err, result) {
1704
+
1705
+
1706
+ if (err) {
1707
+ winston.debug(err);
1708
+ winston.debug("ERR",err)
1709
+ return res.status(500).send({success: false, msg: 'Error getting analytics.'});
1710
+ }
1711
+ winston.debug(result);
1712
+ winston.debug("RESULT",result)
1713
+ res.json(result);
1714
+ });
1715
+
1716
+ });
1717
+
1718
+
1719
+ module.exports = router;