tBoxAutoTestService.js 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. 'use strict';
  2. const Service = require('egg').Service;
  3. class TBoxAutoTestService extends Service {
  4. async index({ ids }) {
  5. const { ctx } = this;
  6. let cond = [];
  7. if (ids) {
  8. cond = [{ $match: { 'autoTest._id': { $in: ids.split(',') } } }];
  9. } else {
  10. return [];
  11. }
  12. const agg = [
  13. { $unwind: '$autoTest' },
  14. ...cond,
  15. { $unwind: '$autoTest.data' },
  16. { $group: { _id: '$autoTest.data.applicationId',
  17. count: { $sum: '$autoTest.data.count' },
  18. failCount: { $sum: '$autoTest.data.failCount' } } },
  19. ];
  20. const result = await ctx.model.Local.TBoxAutoTestingStatsModel.aggregateFix(agg);
  21. return result.map(item => {
  22. return { ...item, name: ctx.helper.applicationDict[item._id] };
  23. }).filter(item => item.name);
  24. }
  25. async other() {
  26. const { ctx } = this;
  27. const agg = [
  28. { $unwind: '$autoTest' },
  29. { $unwind: '$autoTest.data' },
  30. { $group: { _id: '$autoTest.data.applicationId',
  31. count: { $sum: '$autoTest.data.count' },
  32. failCount: { $sum: '$autoTest.data.failCount' } } },
  33. ];
  34. const result = await ctx.model.Local.TBoxAutoTestingStatsModel.aggregateFix(agg);
  35. return result.map(item => {
  36. return { ...item, name: ctx.helper.applicationDict[item._id] };
  37. }).filter(item => item.name);
  38. }
  39. // ====================清洗分割线=========================================================================================
  40. async statistics({ timeRangData, initData, isForceUpdate }) {
  41. const { ctx } = this;
  42. const hasData = await ctx.service.statisticsService.saveBefore(ctx.model.Local.TBoxAutoTestingStatsModel,
  43. { ...initData });
  44. if (hasData && !isForceUpdate) {
  45. return;
  46. }
  47. initData.start_time = new Date();
  48. const autoTest = await this.group(timeRangData);
  49. ctx.logger.info('任务进行group');
  50. await ctx.service.statisticsService.save(ctx.model.Local.TBoxAutoTestingStatsModel,
  51. { ...initData, autoTest }, isForceUpdate);
  52. }
  53. async group({ startTime, endTime }) {
  54. const { ctx } = this;
  55. const agg = [
  56. { $match: { reqTime: { $gte: new Date(startTime), $lt: new Date(endTime) },
  57. reqSource: 'fawiovvgdebug' } },
  58. { $group: {
  59. _id: { relevancyId: { $ifNull: [ '$relevancyId', '$_id' ] }, serviceType: '$serviceType' },
  60. count: { $sum: 1 },
  61. failCount: { $sum: {
  62. $cond: [{ $eq: [ '$result', 'faild' ] }, 1, 0 ],
  63. } },
  64. } },
  65. { $group: {
  66. _id: '$_id.relevancyId',
  67. data: { $push: {
  68. applicationId: '$_id.serviceType',
  69. count: '$count',
  70. failCount: '$failCount',
  71. } },
  72. } },
  73. ];
  74. return await ctx.model.TBoxAutoTestingStatsModel.aggregateFix(agg);
  75. }
  76. }
  77. module.exports = TBoxAutoTestService;