appBehaviorRecordService.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. const remoteControl = await this.remoteControl(timeRangData);
  108. const remoteControlFailure = await this.remoteControlFailure(timeRangData);
  109. await ctx.service.statisticsService.save(ctx.model.Local.AppBehaviorRecordModel,
  110. { ...initData, appBehavior, remoteControl, remoteControlFailure,
  111. }, isForceUpdate);
  112. }
  113. async behavior({ startTime, endTime }) {
  114. const { ctx } = this;
  115. const agg = [
  116. { $match: { ...ctx.helper.getTimeRangMatch(startTime, endTime, 'create_time') } },
  117. {
  118. $group: {
  119. _id: '$parent_behavior_id',
  120. behavior_name: { $first: '$parent_name' },
  121. duration: { $sum: { $cond: [{ $lt: [ '$use_duration', 0 ] }, 0, '$use_duration' ] } },
  122. count: { $sum: 1 },
  123. },
  124. },
  125. ];
  126. return await ctx.model.AppBehaviorRecordModel.aggregateFix(agg);
  127. }
  128. async remoteControl({ startTime, endTime }) {
  129. const { ctx } = this;
  130. const agg = [
  131. { $match: {
  132. ...ctx.helper.getTimeRangMatch(startTime, endTime, 'create_time'),
  133. parent_behavior_id: ctx.helper.rcBehaviorId,
  134. } },
  135. {
  136. $group: {
  137. _id: { rc_execution_result: '$rc_execution_result',
  138. behavior_id: '$behavior_id', op: '$op', opType: '$op_type', closeType: '$closeType' },
  139. count: { $sum: 1 },
  140. },
  141. },
  142. ];
  143. return await ctx.model.AppBehaviorRecordModel.aggregateFix(agg);
  144. }
  145. async remoteControlFailure({ startTime, endTime }) {
  146. const { ctx } = this;
  147. const agg = [
  148. { $match: {
  149. ...ctx.helper.getTimeRangMatch(startTime, endTime, 'create_time'),
  150. parent_behavior_id: ctx.helper.rcBehaviorId,
  151. rc_execution_result: ctx.helper.rcResult.FAILED,
  152. } },
  153. {
  154. $group: {
  155. _id: '$rc_fail_type',
  156. count: { $sum: 1 },
  157. },
  158. },
  159. ];
  160. return await ctx.model.AppBehaviorRecordModel.aggregateFix(agg);
  161. }
  162. }
  163. module.exports = AppBehaviorRecordService;