Advertisement
Guest User

Untitled

a guest
Jul 16th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.17 KB | None | 0 0
  1. const job = require('../parts/jobs/job.js');
  2. const async = require('async');
  3. const _ = require('underscore');
  4. const crypto = require('crypto');
  5. const common = require('../utils/common.js');
  6. const countlyApi = {
  7. data: {
  8. fetch: require('../parts/data/fetch.js'),
  9. lib: require('../lib/countly.model.js')
  10. }
  11. };
  12. const countlyEvents = countlyApi.data.lib.load("event");
  13.  
  14. /** Class for job of top events widget **/
  15. class TopEventsJob extends job.Job {
  16. /**
  17. * Run the job
  18. * @param {Db} db connection
  19. * @param {done} done callback
  20. */
  21. run(db, done) {
  22. common.db.collection('apps').find({}, {}).toArray(function(err, result) {
  23. async.eachSeries(result, function(givenApps, eachSeriesCallback) {
  24. const app_id = givenApps._id;
  25. common.db.collection('events').findOne({
  26. '_id': app_id
  27. }, function(err, result) {
  28. result.list = _.filter(result.list, function(l) {
  29. return l.indexOf('[CLY]') !== 0;
  30. });
  31. let eventMap = [];
  32. for (let index = 0; index < result.list.length; index++) {
  33. if (result.map) {
  34. let mapKey = result.list[index];
  35. if (typeof result.map[mapKey] === "undefined") {
  36. result.map[mapKey] = true;
  37. }
  38. if (typeof result.map[mapKey] !== 'object' && result.map[mapKey]) {
  39. eventMap.push(result.list[index]);
  40. }
  41. } else {
  42. eventMap.push(result.list[index]);
  43. }
  44. }
  45. if (Array.isArray(eventMap)) {
  46. const arrPeriod = ['30days', 'hour'];
  47. const appID = String(app_id.toString());
  48. const collectionName = "top_events" + crypto.createHash('sha1').update(appID).digest('hex');
  49. common.db.collection(collectionName).drop();
  50. async.eachSeries(arrPeriod, function(givenPeriod, eachCallback) {
  51. var data = {};
  52. var ob = {
  53. app_id,
  54. qstring: {
  55. period: givenPeriod
  56. }
  57. }
  58. setTimeout(function() {
  59. async.each(eventMap, function(event, done) {
  60. var collectionName = "events" + crypto.createHash('sha1').update(event + app_id).digest('hex');
  61. countlyApi.data.fetch.getTimeObjForEvents(collectionName, ob, function(doc) {
  62. countlyEvents.setDb(doc || {});
  63. var countProp = countlyEvents.getNumber("c");
  64. data[event] = {};
  65. data[event].data = {
  66. "count": countProp
  67. };
  68. done();
  69. });
  70. },
  71. function() {
  72. givenPeriod === "hour" ? givenPeriod = "today" : givenPeriod;
  73. common.db.collection(collectionName).insert({
  74. ts: Math.round(new Date().getTime() / 1000),
  75. period: givenPeriod,
  76. data
  77. });
  78. });
  79. eachCallback();
  80. }, 2000);
  81. })
  82. }
  83. });
  84. eachSeriesCallback();
  85. });
  86. });
  87. done();
  88. }
  89. }
  90.  
  91. module.exports = TopEventsJob;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement