'use strict'; const Service = require('egg').Service; class AppExceptionRecordService extends Service { async exception({ type, startTime, endTime }) { const { ctx } = this; const agg = [ { $match: ctx.helper.getTimeRangMatch(startTime, endTime) }, { $group: ctx.helper.getTimeGroup(type, { total: { $sum: '$total' }, sys_count: { $sum: '$sys_count' }, interface_exception_count: { $sum: '$interface_exception_count' }, }), }, ]; return await ctx.model.Local.AppExceptionRecordModel.aggregateFix(agg); } async statistics({ timeRangData, initData, isForceUpdate }) { const { ctx } = this; const hasData = await ctx.service.statisticsService.saveBefore(ctx.model.Local.AppExceptionRecordModel, { ...initData }); if (hasData && !isForceUpdate) { return; } initData.start_time = new Date(); const obj = await this.total(timeRangData); ctx.logger.info('任务进行total'); const sys_count = obj.sys_count || 0; const interface_exception_count = obj.interface_exception_count || 0; const total = obj.total || 0; await ctx.service.statisticsService.save(ctx.model.Local.AppExceptionRecordModel, { ...initData, sys_count, interface_exception_count, total }, isForceUpdate); } async total({ startTime, endTime }) { const { ctx } = this; const agg = [ { $match: ctx.helper.getTimeRangMatch(startTime, endTime, 'create_time') }, { $group: { _id: null, sys_count: { $sum: '$sys_count' }, interface_exception_count: { $sum: '$interface_exception_count' }, } }, { $addFields: { total: { $add: [ '$sys_count', '$interface_exception_count' ] }, }, }, ]; return await ctx.model.AppExceptionRecordModel.aggregateNGroup(agg); } } module.exports = AppExceptionRecordService;