msgQueryRecordService.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. let successTotal = 0;// 成功数
  25. let failTotal = 0;// 失败数
  26. let total = 0;// 总数
  27. result.forEach(item => {
  28. switch (item._id) {
  29. case 0:
  30. failTotal = item.count;
  31. break;
  32. case 1:
  33. successTotal = item.count;
  34. break;
  35. default:
  36. break;
  37. }
  38. total += item.count;
  39. });
  40. await ctx.service.statisticsService.save(ctx.model.Local.MsgQueryRecordModel,
  41. { ...initData, successTotal, failTotal, total }, isForceUpdate);
  42. }
  43. async group({ startTime, endTime }) {
  44. const { ctx } = this;
  45. const agg = [
  46. { $match: ctx.helper.getTimeRangMatch(startTime, endTime, 'create_time') },
  47. { $group: {
  48. _id: '$is_success',
  49. count: { $sum: 1 },
  50. } },
  51. ];
  52. return await ctx.model.MsgQueryRecordModel.aggregateFix(agg);
  53. }
  54. }
  55. module.exports = MsgQueryRecordService;