12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- 'use strict';
- const Service = require('egg').Service;
- class TBoxAutoTestService extends Service {
- async index({ ids }) {
- const { ctx } = this;
- let cond = [];
- if (ids) {
- cond = [{ $match: { 'autoTest._id': { $in: ids.split(',') } } }];
- } else {
- return [];
- }
- const agg = [
- { $unwind: '$autoTest' },
- ...cond,
- { $unwind: '$autoTest.data' },
- { $group: { _id: '$autoTest.data.applicationId',
- count: { $sum: '$autoTest.data.count' },
- failCount: { $sum: '$autoTest.data.failCount' } } },
- ];
- const result = await ctx.model.Local.TBoxAutoTestingStatsModel.aggregateFix(agg);
- return result.map(item => {
- return { ...item, name: ctx.helper.applicationDict[item._id] };
- }).filter(item => item.name);
- }
- async other() {
- const { ctx } = this;
- const agg = [
- { $unwind: '$autoTest' },
- { $unwind: '$autoTest.data' },
- { $group: { _id: '$autoTest.data.applicationId',
- count: { $sum: '$autoTest.data.count' },
- failCount: { $sum: '$autoTest.data.failCount' } } },
- ];
- const result = await ctx.model.Local.TBoxAutoTestingStatsModel.aggregateFix(agg);
- return result.map(item => {
- return { ...item, name: ctx.helper.applicationDict[item._id] };
- }).filter(item => item.name);
- }
- // ====================清洗分割线=========================================================================================
- async statistics({ timeRangData, initData, isForceUpdate }) {
- const { ctx } = this;
- const hasData = await ctx.service.statisticsService.saveBefore(ctx.model.Local.TBoxAutoTestingStatsModel,
- { ...initData });
- if (hasData && !isForceUpdate) {
- return;
- }
- initData.start_time = new Date();
- const autoTest = await this.group(timeRangData);
- await ctx.service.statisticsService.save(ctx.model.Local.TBoxAutoTestingStatsModel,
- { ...initData, autoTest }, isForceUpdate);
- }
- async group({ startTime, endTime }) {
- const { ctx } = this;
- const agg = [
- { $match: { reqTime: { $gte: new Date(startTime), $lt: new Date(endTime) },
- reqSource: 'fawiovvgdebug' } },
- { $group: {
- _id: { relevancyId: { $ifNull: [ '$relevancyId', '$_id' ] }, serviceType: '$serviceType' },
- count: { $sum: 1 },
- failCount: { $sum: {
- $cond: [{ $eq: [ '$result', 'faild' ] }, 1, 0 ],
- } },
- } },
- { $group: {
- _id: '$_id.relevancyId',
- data: { $push: {
- applicationId: '$_id.serviceType',
- count: '$count',
- failCount: '$failCount',
- } },
- } },
- ];
- return await ctx.model.TBoxAutoTestingStatsModel.aggregateFix(agg);
- }
- }
- module.exports = TBoxAutoTestService;
|