appBehaviorRecordService.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. 'use strict';
  2. const Service = require('egg').Service;
  3. class AppBehaviorRecordService extends Service {
  4. async duration({ startTime, endTime }) {
  5. const { ctx } = this;
  6. const agg = [
  7. { $match: ctx.helper.getTimeRangMatch(startTime, endTime) },
  8. { $unwind: '$appBehavior' },
  9. { $group: {
  10. _id: '$appBehavior._id',
  11. behavior_name: { $first: '$appBehavior.behavior_name' },
  12. duration: { $sum: '$appBehavior.duration' },
  13. } },
  14. {
  15. $sort: { duration: -1 },
  16. },
  17. ];
  18. return await ctx.model.Local.AppBehaviorRecordModel.aggregateFix(agg);
  19. }
  20. async count({ startTime, endTime }) {
  21. const { ctx } = this;
  22. const agg = [
  23. { $match: ctx.helper.getTimeRangMatch(startTime, endTime) },
  24. { $unwind: '$appBehavior' },
  25. { $group: {
  26. _id: '$appBehavior._id',
  27. behavior_name: { $first: '$appBehavior.behavior_name' },
  28. count: { $sum: '$appBehavior.count' },
  29. } },
  30. {
  31. $sort: { count: -1 },
  32. },
  33. ];
  34. return await ctx.model.Local.AppBehaviorRecordModel.aggregateFix(agg);
  35. }
  36. async ext() {
  37. const { ctx } = this;
  38. const durationPreAgg = [
  39. { $match: ctx.helper.getTimeRangMatch(ctx.helper.preMonth(), ctx.helper.getMonthTop()) },
  40. { $unwind: '$appBehavior' },
  41. { $group: {
  42. _id: null,
  43. duration: { $sum: '$appBehavior.duration' },
  44. count: { $sum: '$appBehavior.count' },
  45. } },
  46. ];
  47. const durationAgg = [
  48. { $match: { create_date: { $gte: ctx.helper.getMonthTop() } } },
  49. { $unwind: '$appBehavior' },
  50. { $group: {
  51. _id: null,
  52. duration: { $sum: '$appBehavior.duration' },
  53. count: { $sum: '$appBehavior.count' },
  54. } },
  55. ];
  56. const preResult = await ctx.model.Local.AppBehaviorRecordModel.aggregateNGroup(durationPreAgg);
  57. const result = await ctx.model.Local.AppBehaviorRecordModel.aggregateNGroup(durationAgg);
  58. const durationMonth = result.duration || 0;
  59. const countMonth = result.count || 0;
  60. const durationPreMonth = preResult.duration || 0;
  61. const countPreMonth = preResult.count || 0;
  62. return { countPreMonth, countMonth, durationPreMonth, durationMonth };
  63. }
  64. async rc({ type, startTime, endTime, rcType, rcResult }) {
  65. const { ctx } = this;
  66. const cond = [{ $match: {} }];
  67. if (ctx.helper.rcDict[rcType]) {
  68. const rc = ctx.helper.rcDict[rcType];
  69. Object.keys(rc).forEach(item => {
  70. cond[0].$match['remoteControl._id.' + item] = rc[item];
  71. });
  72. }
  73. if (ctx.helper.rcResult[rcResult]) {
  74. cond[0].$match['remoteControl._id.rc_execution_result'] = ctx.helper.rcResult[rcResult];
  75. }
  76. const agg = [
  77. { $match: ctx.helper.getTimeRangMatch(startTime, endTime) },
  78. { $unwind: '$remoteControl' },
  79. ...cond,
  80. { $group: ctx.helper.getTimeGroup(type, { count: { $sum: '$remoteControl.count' } }) },
  81. ];
  82. return await ctx.model.Local.AppBehaviorRecordModel.aggregateFix(agg);
  83. }
  84. async rcFail({ type, startTime, endTime, rcFailType }) {
  85. const { ctx } = this;
  86. const cond = [{ $match: {} }];
  87. if (rcFailType) {
  88. cond[0].$match['remoteControlFailure._id'] = rcFailType;
  89. }
  90. const agg = [
  91. { $match: ctx.helper.getTimeRangMatch(startTime, endTime) },
  92. { $unwind: '$remoteControlFailure' },
  93. ...cond,
  94. { $group: ctx.helper.getTimeGroup(type, { count: { $sum: '$remoteControlFailure.count' } }) },
  95. ];
  96. return await ctx.model.Local.AppBehaviorRecordModel.aggregateFix(agg);
  97. }
  98. async statistics({ timeRangData, initData, isForceUpdate }) {
  99. const { ctx } = this;
  100. const hasData = await ctx.service.statisticsService.saveBefore(ctx.model.Local.AppBehaviorRecordModel,
  101. { ...initData });
  102. if (hasData && !isForceUpdate) {
  103. return;
  104. }
  105. initData.start_time = new Date();
  106. const appBehavior = await this.behavior(timeRangData);
  107. ctx.logger.info('任务进行behavior');
  108. const remoteControl = await this.remoteControl(timeRangData);
  109. ctx.logger.info('任务进行remoteControl');
  110. const remoteControlFailure = await this.remoteControlFailure(timeRangData);
  111. ctx.logger.info('任务进行remoteControlFailure');
  112. await ctx.service.statisticsService.save(ctx.model.Local.AppBehaviorRecordModel,
  113. { ...initData, appBehavior, remoteControl, remoteControlFailure,
  114. }, isForceUpdate);
  115. }
  116. async behavior({ startTime, endTime }) {
  117. const { ctx } = this;
  118. const agg = [
  119. { $match: { ...ctx.helper.getTimeRangMatch(startTime, endTime, 'create_time') } },
  120. {
  121. $group: {
  122. _id: '$parent_behavior_id',
  123. behavior_name: { $first: '$parent_name' },
  124. duration: { $sum: { $cond: [{ $lt: [ '$use_duration', 0 ] }, 0, '$use_duration' ] } },
  125. count: { $sum: 1 },
  126. },
  127. },
  128. ];
  129. return await ctx.model.AppBehaviorRecordModel.aggregateFix(agg);
  130. }
  131. async remoteControl({ startTime, endTime }) {
  132. const { ctx } = this;
  133. const agg = [
  134. { $match: {
  135. ...ctx.helper.getTimeRangMatch(startTime, endTime, 'create_time'),
  136. parent_behavior_id: ctx.helper.rcBehaviorId,
  137. } },
  138. {
  139. $group: {
  140. _id: { rc_execution_result: '$rc_execution_result',
  141. behavior_id: '$behavior_id', op: '$op', opType: '$op_type', closeType: '$closeType' },
  142. count: { $sum: 1 },
  143. },
  144. },
  145. ];
  146. return await ctx.model.AppBehaviorRecordModel.aggregateFix(agg);
  147. }
  148. async remoteControlFailure({ startTime, endTime }) {
  149. const { ctx } = this;
  150. const agg = [
  151. { $match: {
  152. ...ctx.helper.getTimeRangMatch(startTime, endTime, 'create_time'),
  153. parent_behavior_id: ctx.helper.rcBehaviorId,
  154. rc_execution_result: ctx.helper.rcResult.FAILED,
  155. } },
  156. {
  157. $group: {
  158. _id: '$rc_fail_type',
  159. count: { $sum: 1 },
  160. },
  161. },
  162. ];
  163. return await ctx.model.AppBehaviorRecordModel.aggregateFix(agg);
  164. }
  165. }
  166. module.exports = AppBehaviorRecordService;