appExceptionRecordService.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. 'use strict';
  2. const Service = require('egg').Service;
  3. class AppExceptionRecordService extends Service {
  4. async exception({ type, startTime, endTime }) {
  5. const { ctx } = this;
  6. const agg = [
  7. { $match: ctx.helper.getTimeRangMatch(startTime, endTime) },
  8. { $group: ctx.helper.getTimeGroup(type,
  9. { total: { $sum: '$total' },
  10. sys_count: { $sum: '$sys_count' },
  11. interface_exception_count: { $sum: '$interface_exception_count' },
  12. }),
  13. },
  14. ];
  15. return await ctx.model.Local.AppExceptionRecordModel.aggregateFix(agg);
  16. }
  17. async statistics({ timeRangData, initData, isForceUpdate }) {
  18. const { ctx } = this;
  19. const hasData = await ctx.service.statisticsService.saveBefore(ctx.model.Local.AppExceptionRecordModel,
  20. { ...initData });
  21. if (hasData && !isForceUpdate) {
  22. return;
  23. }
  24. initData.start_time = new Date();
  25. const obj = await this.total(timeRangData);
  26. ctx.logger.info('任务进行total');
  27. const sys_count = obj.sys_count || 0;
  28. const interface_exception_count = obj.interface_exception_count || 0;
  29. const total = obj.total || 0;
  30. await ctx.service.statisticsService.save(ctx.model.Local.AppExceptionRecordModel,
  31. { ...initData, sys_count, interface_exception_count, total }, isForceUpdate);
  32. }
  33. async total({ startTime, endTime }) {
  34. const { ctx } = this;
  35. const agg = [
  36. { $match: ctx.helper.getTimeRangMatch(startTime, endTime, 'create_time') },
  37. { $group: {
  38. _id: null,
  39. sys_count: { $sum: '$sys_count' },
  40. interface_exception_count: { $sum: '$interface_exception_count' },
  41. } },
  42. {
  43. $addFields: {
  44. total: { $add: [ '$sys_count', '$interface_exception_count' ] },
  45. },
  46. },
  47. ];
  48. return await ctx.model.AppExceptionRecordModel.aggregateNGroup(agg);
  49. }
  50. }
  51. module.exports = AppExceptionRecordService;