appExceptionRecordService.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. await ctx.service.statisticsService.save(ctx.model.Local.AppExceptionRecordModel,
  27. { ...initData, ...obj }, isForceUpdate);
  28. }
  29. async total({ startTime, endTime }) {
  30. const { ctx } = this;
  31. const agg = [
  32. { $match: ctx.helper.getTimeRangMatch(startTime, endTime, 'create_time') },
  33. { $group: {
  34. _id: null,
  35. sys_count: { $sum: '$sys_count' },
  36. interface_exception_count: { $sum: '$interface_exception_count' },
  37. } },
  38. {
  39. $addFields: {
  40. total: { $add: [ '$sys_count', '$interface_exception_count' ] },
  41. },
  42. },
  43. ];
  44. return await ctx.model.AppExceptionRecordModel.aggregateNGroup(agg);
  45. }
  46. }
  47. module.exports = AppExceptionRecordService;