msgQueryRecordService.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. 'use strict';
  2. const Service = require('egg').Service;
  3. class MsgQueryRecordService extends Service {
  4. async index({ type, startTime, endTime }) {
  5. const { ctx } = this;
  6. const agg = [
  7. { $match: ctx.helper.getTimeRangMatch(startTime, endTime) },
  8. { $group: ctx.helper.getTimeGroup(type, { total: { $sum: '$total' },
  9. successTotal: { $sum: '$successTotal' }, failTotal: { $sum: '$failTotal' },
  10. }) },
  11. ];
  12. return await ctx.model.Local.MsgQueryRecordModel.aggregateFix(agg);
  13. }
  14. // ====================清洗分割线==================================================================
  15. async statistics({ timeRangData, initData, isForceUpdate }) {
  16. const { ctx } = this;
  17. const hasData = await ctx.service.statisticsService.saveBefore(ctx.model.Local.MsgQueryRecordModel,
  18. { ...initData });
  19. if (hasData && !isForceUpdate) {
  20. return;
  21. }
  22. initData.start_time = new Date();
  23. const result = await this.group(timeRangData);
  24. ctx.logger.info('任务进行group');
  25. let successTotal = 0;// 成功数
  26. let failTotal = 0;// 失败数
  27. let total = 0;// 总数
  28. result.forEach(item => {
  29. switch (item._id) {
  30. case 0:
  31. failTotal = item.count;
  32. break;
  33. case 1:
  34. successTotal = item.count;
  35. break;
  36. default:
  37. break;
  38. }
  39. total += item.count;
  40. });
  41. await ctx.service.statisticsService.save(ctx.model.Local.MsgQueryRecordModel,
  42. { ...initData, successTotal, failTotal, total }, isForceUpdate);
  43. }
  44. async group({ startTime, endTime }) {
  45. const { ctx } = this;
  46. const agg = [
  47. { $match: ctx.helper.getTimeRangMatch(startTime, endTime, 'create_time') },
  48. { $group: {
  49. _id: '$is_success',
  50. count: { $sum: 1 },
  51. } },
  52. ];
  53. return await ctx.model.MsgQueryRecordModel.aggregateFix(agg);
  54. }
  55. }
  56. module.exports = MsgQueryRecordService;