Procházet zdrojové kódy

新增额外数据

z332406259 před 4 roky
rodič
revize
54617cc033

+ 26 - 27
app/extend/helper.js

@@ -164,33 +164,6 @@ exports.getTimeRangMatch = (startTime, endTime, dateStr = 'create_date') => {
   return match;
 };
 
-
-// 获取活跃car的条件
-exports.getCarActiveCond = ({ startTime, endTime }) => {
-  return [
-    { $lookup: {
-      from: 'driving_behavior_info',
-      let: { vin: '$vin' },
-      pipeline: [{ $match: { $expr: { $and:
-                [
-                  { $eq: [ '$vin', '$$vin' ] },
-                  { $gte: [ '$start_time', this.getMonthTop(startTime) ] },
-                  { $lt: [ '$start_time', endTime ] },
-                ] } } }], as: 'drive',
-    } },
-    { $unwind: { path: '$drive', preserveNullAndEmptyArrays: true } },
-    { $group: {
-      _id: '$vin',
-      mileage: { $sum: { $cond: [ '$drive.mileage', { $toDouble: '$drive.mileage' }, 0 ] } },
-      pro_code: { $first: '$pro_code' }, city_code: { $first: '$city_code' },
-      is_saled_car: { $first: '$is_saled_car' }, sale_date: { $first: '$sale_date' },
-      user_id: { $first: '$user_id' },
-      series_code: { $first: '$series_code' }, model_code: { $first: '$model_code' },
-    } },
-  ];
-};
-
-
 // 获取区域分组语句  (之前的前置语句或者结果必须存在 元素属性:city_name,area_name,provice_name)
 exports.getLocationMongo = () => {
   return [
@@ -548,3 +521,29 @@ exports.getAppActiveCond = ({ startTime, endTime }) => {
   ];
 };
 
+
+// 获取活跃car的条件
+exports.getCarActiveCond = ({ startTime, endTime }) => {
+  return [
+    { $lookup: {
+        from: 'driving_behavior_info',
+        let: { vin: '$vin' },
+        pipeline: [{ $match: { $expr: { $and:
+                  [
+                    { $eq: [ '$vin', '$$vin' ] },
+                    { $gte: [ '$start_time', this.getMonthTop(startTime) ] },
+                    { $lt: [ '$start_time', endTime ] },
+                  ] } } }], as: 'drive',
+      } },
+    { $unwind: { path: '$drive', preserveNullAndEmptyArrays: true } },
+    { $group: {
+        _id: '$vin',
+        mileage: { $sum: { $cond: [ '$drive.mileage', { $toDouble: '$drive.mileage' }, 0 ] } },
+        pro_code: { $first: '$pro_code' }, city_code: { $first: '$city_code' },
+        is_saled_car: { $first: '$is_saled_car' }, sale_date: { $first: '$sale_date' },
+        user_id: { $first: '$user_id' },
+        series_code: { $first: '$series_code' }, model_code: { $first: '$model_code' },
+      } },
+  ];
+};
+

+ 1 - 0
app/model/local/tVehicleRecordModel.js

@@ -14,6 +14,7 @@ module.exports = app => {
     end_time: { type: Date }, // 结束统计时间
 
     total: { type: Number }, // 车辆总数
+    saledTotal: { type: Number }, // 车辆总数
     car: [
       {
         _id: {

+ 18 - 18
app/schedule/updateData.js

@@ -9,7 +9,7 @@ class UpdateData extends Subscription {
       // interval: '24h', // 1 分钟间隔
       cron: '0 30 3 * * ? ', // 每天凌晨3点
       type: 'worker', //  all 指定所有的都需要执行  worker 其中一个
-      // immediate: true,
+      immediate: true,
       cronOptions: {
         tz: 'Asia/Shanghai',
       },
@@ -22,7 +22,7 @@ class UpdateData extends Subscription {
     // const result = await this.app.getHistoryModel('2006').find({}).limit(10);
     // this.app.logger.info(result);
     // await this.doTask();
-    // await this.roundTask();
+    await this.roundTask();
     await this.endTask();
   }
 
@@ -55,7 +55,7 @@ class UpdateData extends Subscription {
     this.isRunningTask = true;
 
     // 更新历史时间段数据 => 修改createBaseData的参数做循环请求即可
-    const satrtMoment = this.ctx.helper.momentDate('2020-05-01');
+    const satrtMoment = this.ctx.helper.momentDate('2019-01-01');
 
     // const satrtMoment = this.ctx.helper.momentDate('2020-06-01');
     // const endTime = this.ctx.helper.parse('2019-06-17');
@@ -65,21 +65,21 @@ class UpdateData extends Subscription {
     while (satrtMoment.valueOf() < endTime) {
       const startTime = satrtMoment.valueOf();
       const eTime = satrtMoment.add(1, 'days').valueOf();
-      // await this.ctx.service.tRbacUserService.statistics(this.createBaseData(startTime, eTime));
-      // await this.ctx.service.tRegisterInfoService.statistics(this.createBaseData(startTime, eTime));
-      // await this.ctx.service.onlineUserService2.statistics(this.createBaseData(startTime, eTime));
-      // await this.ctx.service.drivingBehaviorInfoService2.statistics(this.createBaseData(startTime, eTime));
-      // await this.ctx.service.statsBaseInfoService2.statistics(this.createBaseData(startTime, eTime));
-      // await this.ctx.service.tVehicleRecordService2.statistics(this.createBaseData(startTime, eTime));
-      // await this.ctx.service.tVehicleReportInfoService.statistics(this.createBaseData(startTime, eTime));
-      //
-      // await this.ctx.service.appBehaviorRecordService.statistics(this.createBaseData(startTime, eTime));
-      // await this.ctx.service.appExceptionRecordService.statistics(this.createBaseData(startTime, eTime));
-      // await this.ctx.service.appFeedbackRecordService.statistics(this.createBaseData(startTime, eTime));
-      // await this.ctx.service.iviBehaviorRecordService.statistics(this.createBaseData(startTime, eTime));
-      // await this.ctx.service.msgQueryRecordService.statistics(this.createBaseData(startTime, eTime));
-      // await this.ctx.service.msgCenterRecordService2.statistics(this.createBaseData(startTime, eTime));
-      // await this.ctx.service.tBoxAutoTestService.statistics(this.createBaseData(startTime, eTime));
+      await this.ctx.service.tRbacUserService.statistics(this.createBaseData(startTime, eTime));
+      await this.ctx.service.tRegisterInfoService.statistics(this.createBaseData(startTime, eTime));
+      await this.ctx.service.onlineUserService2.statistics(this.createBaseData(startTime, eTime));
+      await this.ctx.service.drivingBehaviorInfoService2.statistics(this.createBaseData(startTime, eTime));
+      await this.ctx.service.statsBaseInfoService2.statistics(this.createBaseData(startTime, eTime));
+      await this.ctx.service.tVehicleRecordService2.statistics(this.createBaseData(startTime, eTime));
+      await this.ctx.service.tVehicleReportInfoService.statistics(this.createBaseData(startTime, eTime));
+
+      await this.ctx.service.appBehaviorRecordService.statistics(this.createBaseData(startTime, eTime));
+      await this.ctx.service.appExceptionRecordService.statistics(this.createBaseData(startTime, eTime));
+      await this.ctx.service.appFeedbackRecordService.statistics(this.createBaseData(startTime, eTime));
+      await this.ctx.service.iviBehaviorRecordService.statistics(this.createBaseData(startTime, eTime));
+      await this.ctx.service.msgQueryRecordService.statistics(this.createBaseData(startTime, eTime));
+      await this.ctx.service.msgCenterRecordService2.statistics(this.createBaseData(startTime, eTime));
+      await this.ctx.service.tBoxAutoTestService.statistics(this.createBaseData(startTime, eTime));
     }
     this.isRunningTask = false;
 

+ 18 - 16
app/service/onlineUserService2.js

@@ -187,26 +187,26 @@ class OnlineUserService2 extends Service {
     if (al && al.length > 0) {
       appLocation = al[0].areas;
     }
-    const anl = await this.appNewLocation(timeRangData);
-    if (anl && anl.length > 0) {
-      appNewLocation = anl[0].areas;
-    }
-    const aal = await this.appActiveLocation(timeRangData);
-    if (aal && aal.length > 0) {
-      appActiveLocation = aal[0].areas;
-    }
+    // const anl = await this.appNewLocation(timeRangData);
+    // if (anl && anl.length > 0) {
+    //   appNewLocation = anl[0].areas;
+    // }
+    // const aal = await this.appActiveLocation(timeRangData);
+    // if (aal && aal.length > 0) {
+    //   appActiveLocation = aal[0].areas;
+    // }
     const il = await this.iviLocation(timeRangData);
     if (il && il.length > 0) {
       iviLocation = il[0].areas;
     }
-    const inl = await this.iviNewLocation(timeRangData);
-    if (inl && inl.length > 0) {
-      iviNewLocation = inl[0].areas;
-    }
-    const ial = await this.iviActiveLocation(timeRangData);
-    if (ial && ial.length > 0) {
-      iviActiveLocation = ial[0].areas;
-    }
+    // const inl = await this.iviNewLocation(timeRangData);
+    // if (inl && inl.length > 0) {
+    //   iviNewLocation = inl[0].areas;
+    // }
+    // const ial = await this.iviActiveLocation(timeRangData);
+    // if (ial && ial.length > 0) {
+    //   iviActiveLocation = ial[0].areas;
+    // }
     initData.end_time = new Date();
 
     await ctx.service.statisticsService.save(ctx.model.Local.OnlineUserModel,
@@ -433,6 +433,7 @@ class OnlineUserService2 extends Service {
         provice_name: 1,
         city_name: 1,
       } },
+      { $match: { area_name: { $exists: true } } },
       ...ctx.helper.getLocationMongo(),
     ];
     return await ctx.model.IviOnlineUserModel.aggregate(agg);
@@ -498,6 +499,7 @@ class OnlineUserService2 extends Service {
         provice_name: '$pro.province_name',
         city_name: '$city.city_name',
       } },
+      { $match: { area_name: { $exists: true } } },
       ...ctx.helper.getLocationMongo(),
     ];
     return await ctx.model.TBoxOnlineModel.aggregate(agg);

+ 5 - 4
app/service/tRbacUserService.js

@@ -51,11 +51,12 @@ class TRbacUserService extends Service {
     const newAppSexAndAgeTotal = await this.sex2Age({
       create_date: { $gte: timeRangData.startTime, $lt: timeRangData.endTime }, user_orgin: 'APP' });
     const newIviTotal = await this.total({
-      create_date: { $gte: timeRangData.startTime, $lt: timeRangData.endTime, user_orgin: 'IVI' } });
+      create_date: { $gte: timeRangData.startTime, $lt: timeRangData.endTime }, user_orgin: 'IVI' });
     const newIviSexAndAgeTotal = await this.sex2Age({
       create_date: { $gte: timeRangData.startTime, $lt: timeRangData.endTime }, user_orgin: 'IVI' });
     const activeAppTotal = await this.activeAppTotal(timeRangData);
-    const activeAppSexAndAgeTotal = await this.activeAppSex2Age(timeRangData);
+    const activeAppSexAndAgeTotal = [];
+    // const activeAppSexAndAgeTotal = await this.activeAppSex2Age(timeRangData);
     const activeIviTotal = await this.activeIviTotal(timeRangData);
     const activeIviSexAndAgeTotal = await this.activeIviSex2Age(timeRangData);
     initData.end_time = new Date();
@@ -135,7 +136,7 @@ class TRbacUserService extends Service {
   async activeIviTotal({ startTime, endTime }) {
     const { ctx } = this;
     const agg = [
-      { $match: ctx.helper.getTimeRangMatch(startTime, endTime, 'login_time'), login_state: 1 },
+      { $match: { ...ctx.helper.getTimeRangMatch(startTime, endTime, 'login_time'), login_state: 1 } },
       { $group: {
         _id: '$user_id' },
       },
@@ -151,7 +152,7 @@ class TRbacUserService extends Service {
   async activeIviSex2Age({ startTime, endTime }) {
     const { ctx } = this;
     const onlineAgg = [
-      { $match: ctx.helper.getTimeRangMatch(startTime, endTime, 'login_time'), login_state: 1 },
+      { $match: { ...ctx.helper.getTimeRangMatch(startTime, endTime, 'login_time'), login_state: 1 } },
       { $group: {
         _id: '$user_id',
       } },

+ 21 - 1
app/service/tRegisterInfoService.js

@@ -41,7 +41,27 @@ class TRegisterInfoService extends Service {
       { user_orgin: 'APP', role_id: ctx.helper.saledRoleId }).countDocuments();
     const saledIviTotal = await ctx.model.TRbacUserModel.find(
       { user_orgin: 'IVI', role_id: ctx.helper.saledRoleId }).countDocuments();
-    return { maxDay, maxMonth, saledTotal, saledAppTotal, saledIviTotal };
+    // const user = await ctx.model.Local.TRbacUserModel.find({}, { appTotal: 1, newAppTotal: 1, activeAppTotal: 1,
+    //   iviTotal: 1, newIviTotal: 1, activeIviTotal: 1 }).sort({ create_date: -1 });
+    let appTotal = 0;
+    let newAppTotal = 0;
+    let activeAppTotal = 0;
+    let iviTotal = 0;
+    let newIviTotal = 0;
+    let activeIviTotal = 0;
+    // if (user) {
+    //   appTotal = user.appTotal;
+    //   newAppTotal = user.newAppTotal;
+    //   activeAppTotal = user.activeAppTotal;
+    //   iviTotal = user.iviTotal;
+    //   newIviTotal = user.newIviTotal;
+    //   activeIviTotal = user.activeIviTotal;
+    // }
+    return { maxDay, maxMonth, saledTotal, saledAppTotal, saledIviTotal,
+      appTotal, newAppTotal, activeAppTotal,
+      iviTotal, newIviTotal, activeIviTotal,
+
+    };
 
   }
 

+ 21 - 12
app/service/tVehicleRecordService2.js

@@ -52,6 +52,7 @@ class TVehicleRecordService2 extends Service {
         area_name: { $arrayElemAt: [ '$area.area_name', 0 ] },
         provice_name: '$pro.province_name',
       } },
+      { $match: { area_name: { $exists: true } } },
       { $group: { _id: { area_name: '$area_name' },
         provinces: { $push: { provice_name: '$provice_name', count: '$count' } },
         count: { $sum: '$count' } } },
@@ -155,6 +156,7 @@ class TVehicleRecordService2 extends Service {
         provice_name: '$pro.province_name',
         city_name: '$city.city_name',
       } },
+      { $match: { area_name: { $exists: true } } },
       ...ctx.helper.getLocationMongo(),
     ];
     return await ctx.model.TVehicleRecordModel.aggregate(agg);
@@ -209,6 +211,7 @@ class TVehicleRecordService2 extends Service {
     }
     let maxMonthAgg = [];
     let maxYearAgg = [];
+    let saledTotal = 0;
     maxMonthAgg = [
       { $sort: { create_date: -1 } },
       { $group: ctx.helper.getTimeGroup('1', { car: { $first: '$car' } }) },
@@ -237,7 +240,12 @@ class TVehicleRecordService2 extends Service {
     if (maxYearResult && maxYearResult.length > 0) {
       maxYear = maxYearResult[0].data;
     }
-    return { maxMonth, maxYear };
+    const saled = await ctx.model.Local.TVehicleRecordModel.findOne({}, { saledTotal: 1 })
+      .sort({ create_date: -1 });
+    if (saled) {
+      saledTotal = saled.saledTotal;
+    }
+    return { maxMonth, maxYear, saledTotal };
   }
 
 
@@ -284,22 +292,22 @@ class TVehicleRecordService2 extends Service {
             { $lt: [ '$sale_date', timeRangData.endTime ] }] }, 1, 0 ],
         } },
       } },
-      { $group: { _id: null, car: { $push: '$$ROOT' }, total: { $sum: 'count' } } },
+      { $group: { _id: null, car: { $push: '$$ROOT' }, total: { $sum: '$count' }, saledTotal: { $sum: '$saledCount' } } },
     ];
     return await ctx.model.TVehicleRecordModel.aggregate(agg);
   }
 
   async activeCount(timeRangData) {
     const { ctx } = this;
-    const onlineAgg = [
-      { $match: { ...ctx.helper.getTimeRangMatch(ctx.helper.getMonthTop(timeRangData.startTime), timeRangData.endTime,
-        'online_time') } },
-      { $group: {
-        _id: '$vin',
-        login_count: { $sum: 1 },
-      } },
-      { $match: { login_count: { $gt: 3 } } },
-    ];
+    // const onlineAgg = [
+    //   { $match: { ...ctx.helper.getTimeRangMatch(ctx.helper.getMonthTop(timeRangData.startTime), timeRangData.endTime,
+    //     'online_time') } },
+    //   { $group: {
+    //     _id: '$vin',
+    //     login_count: { $sum: 1 },
+    //   } },
+    //   { $match: { login_count: { $gt: 3 } } },
+    // ];
     const driveAgg = [
       { $match: ctx.helper.getTimeRangMatch(ctx.helper.getMonthTop(timeRangData.startTime), timeRangData.endTime,
         'start_time') },
@@ -307,7 +315,8 @@ class TVehicleRecordService2 extends Service {
         mileage: { $sum: { $cond: [ '$mileage', { $toDouble: '$mileage' }, 0 ] } } } },
       { $match: { mileage: { $gt: 50 } } },
     ];
-    const result1 = await ctx.model.TBoxOnlineModel.aggregate(onlineAgg);
+    const result1 = [];
+    // const result1 = await ctx.model.TBoxOnlineModel.aggregate(onlineAgg);
     const result2 = await ctx.model.DrivingBehaviorInfoModel.aggregate(driveAgg);
     const unionArray = ctx.helper.unionArray(result1.map(item => item._id), result2.map(item => item._id));