123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191 |
- 'use strict';
- const Service = require('egg').Service;
- class AppBehaviorRecordService extends Service {
- async duration({ startTime, endTime }) {
- const { ctx } = this;
- const agg = [
- { $match: ctx.helper.getTimeRangMatch(startTime, endTime) },
- { $unwind: '$appBehavior' },
- { $group: {
- _id: '$appBehavior._id',
- behavior_name: { $first: '$appBehavior.behavior_name' },
- duration: { $sum: '$appBehavior.duration' },
- } },
- {
- $sort: { duration: -1 },
- },
- ];
- return await ctx.model.Local.AppBehaviorRecordModel.aggregateFix(agg);
- }
- async count({ startTime, endTime }) {
- const { ctx } = this;
- const agg = [
- { $match: ctx.helper.getTimeRangMatch(startTime, endTime) },
- { $unwind: '$appBehavior' },
- { $group: {
- _id: '$appBehavior._id',
- behavior_name: { $first: '$appBehavior.behavior_name' },
- count: { $sum: '$appBehavior.count' },
- } },
- {
- $sort: { count: -1 },
- },
- ];
- return await ctx.model.Local.AppBehaviorRecordModel.aggregateFix(agg);
- }
- async ext() {
- const { ctx } = this;
- const durationPreAgg = [
- { $match: ctx.helper.getTimeRangMatch(ctx.helper.preMonth(), ctx.helper.getMonthTop()) },
- { $unwind: '$appBehavior' },
- { $group: {
- _id: null,
- duration: { $sum: '$appBehavior.duration' },
- count: { $sum: '$appBehavior.count' },
- } },
- ];
- const durationAgg = [
- { $match: { create_date: { $gte: ctx.helper.getMonthTop() } } },
- { $unwind: '$appBehavior' },
- { $group: {
- _id: null,
- duration: { $sum: '$appBehavior.duration' },
- count: { $sum: '$appBehavior.count' },
- } },
- ];
- const preResult = await ctx.model.Local.AppBehaviorRecordModel.aggregateNGroup(durationPreAgg);
- const result = await ctx.model.Local.AppBehaviorRecordModel.aggregateNGroup(durationAgg);
- const durationMonth = result.duration || 0;
- const countMonth = result.count || 0;
- const durationPreMonth = preResult.duration || 0;
- const countPreMonth = preResult.count || 0;
- return { countPreMonth, countMonth, durationPreMonth, durationMonth };
- }
- async rc({ type, startTime, endTime, rcType, rcResult }) {
- const { ctx } = this;
- const cond = [{ $match: {} }];
- if (ctx.helper.rcDict[rcType]) {
- const rc = ctx.helper.rcDict[rcType];
- Object.keys(rc).forEach(item => {
- cond[0].$match['remoteControl._id.' + item] = rc[item];
- });
- }
- if (ctx.helper.rcResult[rcResult]) {
- cond[0].$match['remoteControl._id.rc_execution_result'] = ctx.helper.rcResult[rcResult];
- }
- const agg = [
- { $match: ctx.helper.getTimeRangMatch(startTime, endTime) },
- { $unwind: '$remoteControl' },
- ...cond,
- { $group: ctx.helper.getTimeGroup(type, { count: { $sum: '$remoteControl.count' } }) },
- ];
- return await ctx.model.Local.AppBehaviorRecordModel.aggregateFix(agg);
- }
- async rcFail({ type, startTime, endTime, rcFailType }) {
- const { ctx } = this;
- const cond = [{ $match: {} }];
- if (rcFailType) {
- cond[0].$match['remoteControlFailure._id'] = rcFailType;
- }
- const agg = [
- { $match: ctx.helper.getTimeRangMatch(startTime, endTime) },
- { $unwind: '$remoteControlFailure' },
- ...cond,
- { $group: ctx.helper.getTimeGroup(type, { count: { $sum: '$remoteControlFailure.count' } }) },
- ];
- return await ctx.model.Local.AppBehaviorRecordModel.aggregateFix(agg);
- }
- async statistics({ timeRangData, initData, isForceUpdate }) {
- const { ctx } = this;
- const hasData = await ctx.service.statisticsService.saveBefore(ctx.model.Local.AppBehaviorRecordModel,
- { ...initData });
- if (hasData && !isForceUpdate) {
- return;
- }
- initData.start_time = new Date();
- const appBehavior = await this.behavior(timeRangData);
- const remoteControl = await this.remoteControl(timeRangData);
- const remoteControlFailure = await this.remoteControlFailure(timeRangData);
- await ctx.service.statisticsService.save(ctx.model.Local.AppBehaviorRecordModel,
- { ...initData, appBehavior, remoteControl, remoteControlFailure,
- }, isForceUpdate);
- }
- async behavior({ startTime, endTime }) {
- const { ctx } = this;
- const agg = [
- { $match: { ...ctx.helper.getTimeRangMatch(startTime, endTime, 'create_time') } },
- {
- $group: {
- _id: '$parent_behavior_id',
- behavior_name: { $first: '$parent_name' },
- duration: { $sum: { $cond: [{ $lt: [ '$use_duration', 0 ] }, 0, '$use_duration' ] } },
- count: { $sum: 1 },
- },
- },
- ];
- return await ctx.model.AppBehaviorRecordModel.aggregateFix(agg);
- }
- async remoteControl({ startTime, endTime }) {
- const { ctx } = this;
- const agg = [
- { $match: {
- ...ctx.helper.getTimeRangMatch(startTime, endTime, 'create_time'),
- parent_behavior_id: ctx.helper.rcBehaviorId,
- } },
- {
- $group: {
- _id: { rc_execution_result: '$rc_execution_result',
- behavior_id: '$behavior_id', op: '$op', opType: '$op_type', closeType: '$closeType' },
- count: { $sum: 1 },
- },
- },
- ];
- return await ctx.model.AppBehaviorRecordModel.aggregateFix(agg);
- }
- async remoteControlFailure({ startTime, endTime }) {
- const { ctx } = this;
- const agg = [
- { $match: {
- ...ctx.helper.getTimeRangMatch(startTime, endTime, 'create_time'),
- parent_behavior_id: ctx.helper.rcBehaviorId,
- rc_execution_result: ctx.helper.rcResult.FAILED,
- } },
- {
- $group: {
- _id: '$rc_fail_type',
- count: { $sum: 1 },
- },
- },
- ];
- return await ctx.model.AppBehaviorRecordModel.aggregateFix(agg);
- }
- }
- module.exports = AppBehaviorRecordService;
|