123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796 |
- 'use strict';
- const Service = require('egg').Service;
- class DrivingBehaviorInfoService2 extends Service {
- // 基本方法 计算匹配的时间段下 过滤车型车系后时间分组下 维度分布分组数据之和
- async index({ type, startTime, endTime, seriesCode, modelCode }, vString) {
- const { ctx } = this;
- const cond = [{ $match: {} }];
- if (seriesCode) {
- cond[0].$match[`${vString}._id.series_code`] = seriesCode;
- }
- if (modelCode) {
- cond[0].$match[`${vString}._id.model_code`] = modelCode;
- }
- const agg = [
- { $match: ctx.helper.getTimeRangMatch(startTime, endTime) },
- { $unwind: `$${vString}` },
- ...cond,
- ...ctx.helper.getCarTimeGroupMongo(type, vString),
- ];
- return await ctx.model.Local.DrivingBehaviorInfoModel2.aggregateFix(agg);
- }
- // 月累计占比算法
- async mixMY({ type, startTime, endTime, seriesCode, modelCode }, vString) {
- const { ctx } = this;
- const cond = [{ $match: {} }];
- if (seriesCode) {
- cond[0].$match['count._id.series_code'] = seriesCode;
- }
- if (modelCode) {
- cond[0].$match['count._id.model_code'] = modelCode;
- }
- // {
- // ...baseCond, _id: { year: '$year', month: '$month' },
- // }
- const agg = [
- ...ctx.helper.getCommonAggMax({ type, startTime, endTime, value: vString }),
- { $unwind: '$count' },
- ...cond,
- ...ctx.helper.getCarTimeGroupMongo2(type),
- ];
- return await ctx.model.Local.DrivingBehaviorInfoModel2.aggregateFix(agg);
- }
- // 计算车辆出行和疲劳驾驶次数 两个维度count和dsmCount(类似index)
- async mileageStartTimeAndDsm({ type, startTime, endTime, seriesCode, modelCode }) {
- const { ctx } = this;
- const cond = [{ $match: {} }];
- if (seriesCode) {
- cond[0].$match['mileageStartTimeAndDsm._id.series_code'] = seriesCode;
- }
- if (modelCode) {
- cond[0].$match['mileageStartTimeAndDsm._id.model_code'] = modelCode;
- }
- const agg = [
- { $match: ctx.helper.getTimeRangMatch(startTime, endTime) },
- { $unwind: '$mileageStartTimeAndDsm' },
- ...cond,
- ...ctx.helper.getCarDSMTimeGroupMongo(type),
- ];
- return await ctx.model.Local.DrivingBehaviorInfoModel2.aggregateFix(agg);
- }
- // 带有周末时间过滤(类似index)
- async date({ type, startTime, endTime, seriesCode, modelCode, week = 0 }, vString) {
- const { ctx } = this;
- const cond = [{ $match: {} }];
- const weekCond = [{ $match: {} }];
- const project = { $project: { year: 1, month: 1, day: 1,
- week: { $dayOfWeek: { date: { $toDate: '$create_date' }, timezone: 'Asia/Shanghai' } } } };
- project.$project[vString] = 1;
- if (seriesCode) {
- cond[0].$match[`${vString}._id.series_code`] = seriesCode;
- }
- if (modelCode) {
- cond[0].$match[`${vString}._id.model_code`] = modelCode;
- }
- switch (week) {
- case '1':
- weekCond[0].$match.week = { $in: [ 2, 3, 4, 5, 6 ] };
- break;
- case '2':
- weekCond[0].$match.week = { $in: [ 1, 7 ] };
- break;
- default:
- break;
- }
- const agg = [
- { $match: ctx.helper.getTimeRangMatch(startTime, endTime) },
- project,
- ...weekCond,
- { $unwind: `$${vString}` },
- ...cond,
- ...ctx.helper.getCarTimeGroupMongo(type, vString),
- ];
- return await ctx.model.Local.DrivingBehaviorInfoModel2.aggregateFix(agg);
- }
- // 求多个空调维度数据(无时间分组)
- async air({ startTime, endTime, seriesCode, modelCode }) {
- const { ctx } = this;
- const cond = [{ $match: {} }];
- if (seriesCode) {
- cond[0].$match['data._id.series_code'] = seriesCode;
- }
- if (modelCode) {
- cond[0].$match['data._id.model_code'] = modelCode;
- }
- const agg = [
- { $match: ctx.helper.getTimeRangMatch(startTime, endTime) },
- { $unwind: '$data' },
- ...cond,
- { $group: { _id: null, count: { $sum: '$data.count' },
- airDuration: { $sum: '$data.airDuration' },
- onCompressorDuration: { $sum: '$data.onCompressorDuration' },
- offCompressorDuration: { $sum: '$data.offCompressorDuration' },
- onAutoDuration: { $sum: '$data.onAutoDuration' },
- offAutoDuration: { $sum: '$data.offAutoDuration' },
- blowerRatingOneDuration: { $sum: '$data.blowerRatingOneDuration' },
- blowerRatingTwoDuration: { $sum: '$data.blowerRatingTwoDuration' },
- blowerRatingThreeDuration: { $sum: '$data.blowerRatingThreeDuration' },
- blowerRatingFourDuration: { $sum: '$data.blowerRatingFourDuration' },
- blowerRatingOtherDuration: { $sum: '$data.blowerRatingOtherDuration' } } },
- { $project: {
- airDuration: {
- $cond: {
- if: { $eq: [ '$count', 0 ] },
- then: 0, else: { $divide: [ '$airDuration', '$count' ] },
- },
- },
- onCompressorDuration: {
- $cond: {
- if: { $eq: [ '$count', 0 ] },
- then: 0, else: { $divide: [ '$onCompressorDuration', '$count' ] },
- },
- },
- offCompressorDuration: {
- $cond: {
- if: { $eq: [ '$count', 0 ] },
- then: 0, else: { $divide: [ '$offCompressorDuration', '$count' ] },
- },
- },
- onAutoDuration: {
- $cond: {
- if: { $eq: [ '$count', 0 ] },
- then: 0, else: { $divide: [ '$onAutoDuration', '$count' ] },
- },
- },
- offAutoDuration: {
- $cond: {
- if: { $eq: [ '$count', 0 ] },
- then: 0, else: { $divide: [ '$offAutoDuration', '$count' ] },
- },
- },
- blowerRatingOneDuration: {
- $cond: {
- if: { $eq: [ '$count', 0 ] },
- then: 0, else: { $divide: [ '$blowerRatingOneDuration', '$count' ] },
- },
- },
- blowerRatingTwoDuration: {
- $cond: {
- if: { $eq: [ '$count', 0 ] },
- then: 0, else: { $divide: [ '$blowerRatingTwoDuration', '$count' ] },
- },
- },
- blowerRatingThreeDuration: {
- $cond: {
- if: { $eq: [ '$count', 0 ] },
- then: 0, else: { $divide: [ '$blowerRatingThreeDuration', '$count' ] },
- },
- },
- blowerRatingFourDuration: {
- $cond: {
- if: { $eq: [ '$count', 0 ] },
- then: 0, else: { $divide: [ '$blowerRatingFourDuration', '$count' ] },
- },
- },
- blowerRatingOtherDuration: {
- $cond: {
- if: { $eq: [ '$count', 0 ] },
- then: 0, else: { $divide: [ '$blowerRatingOtherDuration', '$count' ] },
- },
- },
- } },
- ];
- const result = await ctx.model.Local.DrivingBehaviorInfoModel2.aggregateNGroup(agg);
- return Object.assign({ airDuration: 0, onCompressorDuration: 0, offCompressorDuration: 0,
- onAutoDuration: 0, offAutoDuration: 0,
- blowerRatingOneDuration: 0, blowerRatingTwoDuration: 0, blowerRatingThreeDuration: 0,
- blowerRatingFourDuration: 0, blowerRatingOtherDuration: 0 }, result);
- }
- // 求位置维度数据(无时间分组)油耗平均
- async avgOil({ startTime, endTime, seriesCode, modelCode, level }) {
- const { ctx } = this;
- const obj = `oil${level.charAt(0).toUpperCase()}${level.slice(1)}`;
- const cond = [{ $match: {} }];
- if (seriesCode) {
- cond[0].$match[`${obj}._id.series_code`] = seriesCode;
- }
- if (modelCode) {
- cond[0].$match[`${obj}._id.model_code`] = modelCode;
- }
- const agg = [
- { $match: ctx.helper.getTimeRangMatch(startTime, endTime) },
- { $unwind: `$${obj}` },
- ...cond,
- { $group: { _id: `$${obj}._id._id`, name: { $first: `$${obj}.name` },
- oil: { $sum: `$${obj}.oil` }, // 总油耗
- mileage: { $sum: `$${obj}.mileage` } } }, // 总公里
- { $sort: { count: -1 } },
- { $project:
- {
- name: 1,
- count: { $cond: [{ $eq: [ '$mileage', 0 ] }, 0, { $divide: [{ $trunc: { $multiply: [{ $divide: [ '$oil', '$mileage' ] }, 100 * 100 ] } }, 100 ] }] },
- },
- },
- ];
- return await ctx.model.Local.DrivingBehaviorInfoModel2.aggregateFix(agg);
- }
- // 求车辆某个维度数据之和(无时间分组)
- async sum({ startTime, endTime, seriesCode, modelCode }, vString) {
- const { ctx } = this;
- const cond = [{ $match: {} }];
- if (seriesCode) {
- cond[0].$match[`${vString}._id.series_code`] = seriesCode;
- }
- if (modelCode) {
- cond[0].$match[`${vString}._id.model_code`] = modelCode;
- }
- const agg = [
- { $match: ctx.helper.getTimeRangMatch(startTime, endTime) },
- { $unwind: `$${vString}` },
- ...cond,
- { $group: { _id: `$${vString}._id._id`, count: { $sum: `$${vString}.count` } } },
- ];
- return await ctx.model.Local.DrivingBehaviorInfoModel2.aggregateFix(agg);
- }
- // 基本方法 计算匹配的时间段下 过滤车型车系后时间分组下 维度分布分组数据平均值
- async avg({ type, startTime, endTime, seriesCode, modelCode }, vString) {
- const { ctx } = this;
- const cond = [{ $match: {} }];
- if (seriesCode) {
- cond[0].$match[`${vString}._id.series_code`] = seriesCode;
- }
- if (modelCode) {
- cond[0].$match[`${vString}._id.model_code`] = modelCode;
- }
- const agg = [
- { $match: ctx.helper.getTimeRangMatch(startTime, endTime) },
- { $unwind: `$${vString}` },
- ...cond,
- ...ctx.helper.getCarAvgTimeGroupMongo(type, vString),
- ];
- return await ctx.model.Local.DrivingBehaviorInfoModel2.aggregateFix(agg);
- }
- // 求平均值数据下某个维度 对应时间段的平均值
- async count({ type, startTime, endTime, seriesCode, modelCode }, vString) {
- const { ctx } = this;
- const cond = [{ $match: {} }];
- if (seriesCode) {
- cond[0].$match['data._id.series_code'] = seriesCode;
- }
- if (modelCode) {
- cond[0].$match['data._id.model_code'] = modelCode;
- }
- const agg = [
- { $match: ctx.helper.getTimeRangMatch(startTime, endTime) },
- { $unwind: '$data' },
- ...cond,
- { $group: ctx.helper.getTimeGroup(type, {
- data: { $sum: `$data.${vString}` }, count: { $sum: '$data.count' },
- }) },
- { $project: {
- count:
- { $cond: { if: { $eq: [ '$count', 0 ] },
- then: 0, else: { $divide: [{ $trunc: { $multiply: [{ $divide: [ '$data', '$count' ] }, 100 ] } }, 100 ] } } },
- } },
- ];
- return await ctx.model.Local.DrivingBehaviorInfoModel2.aggregateFix(agg);
- }
- // 求平均值数据下某个不是平均值(是总和)维度的数据 对应时间段的总和
- async count2({ type, startTime, endTime, seriesCode, modelCode }, vString) {
- const { ctx } = this;
- const cond = [{ $match: {} }];
- if (seriesCode) {
- cond[0].$match['data._id.series_code'] = seriesCode;
- }
- if (modelCode) {
- cond[0].$match['data._id.model_code'] = modelCode;
- }
- const agg = [
- { $match: ctx.helper.getTimeRangMatch(startTime, endTime) },
- { $unwind: '$data' },
- ...cond,
- { $group: ctx.helper.getTimeGroup(type, { count: { $sum: `$data.${vString}` } }) },
- { $project: {
- count: { $divide: [{ $trunc: { $multiply: [ '$count', 100 ] } }, 100 ] },
- } },
- ];
- return await ctx.model.Local.DrivingBehaviorInfoModel2.aggregateFix(agg);
- }
- // 统计清洗
- async statistics({ timeRangData, initData, isForceUpdate }) {
- const { ctx } = this;
- const hasData = await ctx.service.statisticsService.saveBefore(ctx.model.Local.DrivingBehaviorInfoModel2,
- { ...initData });
- if (hasData && !isForceUpdate) {
- return;
- }
- initData.start_time = new Date();
- const dHour = { $add: [{ $multiply: [
- { $hour: { date: { $toDate: '$$this.start_time' }, timezone: 'Asia/Shanghai' } },
- 60 ] },
- { $minute: { $toDate: '$$this.start_time' } }] };
- const mileage2 = await this.bucketGroup(timeRangData,
- { $toDouble: '$mileage' },
- [ 0, 5, 10, 20, 30, 50, 100, 200, 10000 * 10000 ]);
- ctx.logger.info('任务进行mileage2');
- const mileageAvg = await this.bucketGroup(timeRangData,
- { $toDouble: '$mileage_list.mileage' },
- [ 0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 10 * 10000 ],
- [{ $unwind: '$mileage_list' }]
- );
- ctx.logger.info('任务进行mileageAvg');
- const supplementaryCnt = await this.bucketGroup(timeRangData,
- { $toDouble: '$supplementary_cnt' }, [ 0, 1, 20, 30, 40, 50, 10 * 10000 ]);
- ctx.logger.info('任务进行supplementaryCnt');
- const morningStart = 420;
- const morningEnd = 570;
- const eveningStart = 17 * 60;
- const eveningEnd = 19 * 60 + 30;
- const data = await this.avgGroup(timeRangData,
- {
- batterySoc: { $sum: { $toDouble: '$battery_soc' } },
- engineSpeed: { $sum: { $toDouble: '$engine_speed' } },
- insideTemperature: { $sum: { $toDouble: '$inside_temperature' } },
- outsideTemperature: { $sum: { $toDouble: '$outside_temperature' } },
- powerBatterySoc: { $sum: { $toDouble: '$power_battery_soc' } },
- chargeDuration: { $sum: { $divide: [{ $toDouble: '$charge_duration' }, 60 * 60 * 1000 ] } },
- chargeCnt: { $sum: { $toDouble: '$charge_cnt' } },
- skyDuration: { $sum: { $divide: [{ $toDouble: '$sky_duration' }, 60 * 1000 ] } },
- headlightCnt: { $sum: { $toDouble: '$headlight_cnt' } },
- headlightDuration: { $sum: { $divide: [{ $toDouble: '$headlight_duration' }, 60 * 1000 ] } },
- highBeamCnt: { $sum: { $toDouble: '$high_beam_cnt' } },
- highBeamDuration: { $sum: { $divide: [{ $toDouble: '$high_beam_duration' }, 60 * 1000 ] } },
- airDuration: { $sum: { $toDouble: '$air_duration' } },
- onCompressorDuration: { $sum: { $toDouble: '$on_compressor_duration' } },
- offCompressorDuration: { $sum: { $toDouble: '$off_compressor_duration' } },
- onAutoDuration: { $sum: { $toDouble: '$on_auto_duration' } },
- offAutoDuration: { $sum: { $toDouble: '$off_auto_duration' } },
- blowerRatingOneDuration: { $sum: { $toDouble: '$blower_rating_duration.one' } },
- blowerRatingTwoDuration: { $sum: { $toDouble: '$blower_rating_duration.two' } },
- blowerRatingThreeDuration: { $sum: { $toDouble: '$blower_rating_duration.three' } },
- blowerRatingFourDuration: { $sum: { $toDouble: '$blower_rating_duration.four' } },
- blowerRatingOtherDuration: { $sum: { $add: [{ $toDouble: '$blower_rating_duration.five' },
- { $toDouble: '$blower_rating_duration.six' }, { $toDouble: '$blower_rating_duration.seven' }] } },
- dsmCnt: { $sum: { $toDouble: '$dsm_cnt' } },
- atNightDriveCnt: { $sum: { $toDouble: '$at_night_drive_cnt' } },
- atNightDriveDuration: { $sum: { $divide: [{ $toDouble: '$at_night_drive_duration' }, 60 * 60 * 1000 ] } },
- atNightDriveMileage: { $sum: { $toDouble: '$at_night_drive_mileage' } },
- morningPeakDuration: { $sum: '$morningPeakDuration' },
- morningPeakMileage: { $sum: '$morningPeakMileage' },
- eveningPeakDuration: { $sum: '$eveningPeakDuration' },
- eveningPeakMileage: { $sum: '$eveningPeakMileage' },
- },
- [{ $addFields: {
- morningPeakDuration: { $ifNull: [
- { $sum: { $map: { input: '$mileage_list',
- in: { $cond: { if: { $and: [{ $gte: [ dHour, morningStart ] }, { $lt: [ dHour, morningEnd ] }] },
- then: { $divide: [{ $toDouble: '$$this.drive_duration' }, 60 * 60 * 1000 ] }, else: 0 } } } } }, 0 ] },
- morningPeakMileage: { $ifNull: [
- { $sum: { $map: { input: '$mileage_list',
- in: { $cond: { if: { $and: [{ $gte: [ dHour, morningStart ] }, { $lt: [ dHour, morningEnd ] }] },
- then: { $toDouble: '$$this.mileage' }, else: 0 } } } } }, 0 ] },
- eveningPeakDuration: { $ifNull: [
- { $sum: { $map: { input: '$mileage_list',
- in: { $cond: { if: { $and: [{ $gte: [ dHour, eveningStart ] }, { $lt: [ dHour, eveningEnd ] }] },
- then: { $divide: [{ $toDouble: '$$this.drive_duration' }, 60 * 60 * 1000 ] }, else: 0 } } } } }, 0 ] },
- eveningPeakMileage: { $ifNull: [
- { $sum: { $map: { input: '$mileage_list',
- in: { $cond: { if: { $and: [{ $gte: [ dHour, eveningStart ] }, { $lt: [ dHour, eveningEnd ] }] },
- then: { $toDouble: '$$this.mileage' }, else: 0 } } } } }, 0 ] },
- } }]
- );
- ctx.logger.info('任务进行avgGroup');
- const batterySoc = await this.bucketGroup(timeRangData,
- { $toDouble: '$battery_soc' }, [ 0, 60, 70, 80, 90, 101 ]);
- ctx.logger.info('任务进行batterySoc');
- const engineSpeed = await this.bucketGroup(timeRangData,
- { $toDouble: '$engine_speed' }, [ 0, 2000, 3000, 4000, 5000, 10000 * 10000 ]);
- ctx.logger.info('任务进行engineSpeed');
- const insideTemperature = await this.bucketGroup(timeRangData,
- { $toDouble: '$inside_temperature' }, [ -1000, 30, 35, 40, 45, 50 ]);
- ctx.logger.info('任务进行insideTemperature');
- const outsideTemperature = await this.bucketGroup(timeRangData,
- { $toDouble: '$outside_temperature' }, [ -1000, 0, 10, 20, 30, 1000 ]);
- ctx.logger.info('任务进行outsideTemperature');
- const powerBatterySoc = await this.bucketGroup(timeRangData,
- { $toDouble: '$power_battery_soc' }, [ 0, 60, 70, 80, 90, 101 ]);
- ctx.logger.info('任务进行powerBatterySoc');
- const chargeDuration = await this.bucketGroup(timeRangData,
- { $divide: [{ $toDouble: '$charge_duration' }, 60 * 60 * 1000 ] }, [ 0, 1, 2, 3, 4, 24 ]);
- ctx.logger.info('任务进行chargeDuration');
- const chargeStartSoc = await this.bucketGroup(timeRangData, '$chargeStartSoc', [ 0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 101 ],
- [{ $addFields: { chargeStartSoc: { $ifNull: [
- { $avg: { $map: { input: '$charge_list', in: { $toDouble: '$$this.start_soc' } } } }, 0 ] } } }]);
- ctx.logger.info('任务进行chargeStartSoc');
- const chargeEndSoc = await this.bucketGroup(timeRangData, '$chargeEndSoc', [ 0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 101 ],
- [{ $addFields: { chargeEndSoc: { $ifNull: [
- { $avg: { $map: { input: '$charge_list', in: { $toDouble: '$$this.end_soc' } } } }, 0 ] } } }]);
- ctx.logger.info('任务进行chargeEndSoc');
- const chargePreStartMileage = await this.bucketGroup(timeRangData, '$chargePreStartMileage',
- [ 0, 50, 100, 150, 200, 250, 300, 350, 400 ],
- [{ $addFields: { chargePreStartMileage: { $ifNull: [
- { $avg: { $map: { input: '$charge_list', in: { $toDouble: '$$this.pre_start_mileage' } } } }, 0 ] } } }]);
- ctx.logger.info('任务进行chargePreStartMileage');
- const chargeStartTime = await this.group(timeRangData,
- { $hour: { date: { $toDate: '$charge_list.start_time' }, timezone: 'Asia/Shanghai' } },
- [{ $unwind: '$charge_list' }]
- );
- ctx.logger.info('任务进行chargeStartTime');
- const chargeType = await this.group(timeRangData, { $toDouble: '$charge_list.charge_type' },
- [{ $unwind: '$charge_list' }]
- );
- ctx.logger.info('任务进行chargeType');
- const chargeMileage = await this.bucketGroup(timeRangData, '$chargeMileage',
- [ 0, 50, 100, 150, 200, 250, 300, 10000 * 10000 ],
- [{ $addFields: { chargeMileage: { $ifNull: [
- { $avg: { $map: { input: '$charge_list', in: { $toDouble: '$$this.mileage' } } } }, 0 ] } } }]);
- ctx.logger.info('任务进行chargeMileage');
- const avgSpeedPower = await this.bucketGroup(timeRangData,
- { $toDouble: '$avg_speed' }, [ 0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 1000 ],
- [], { $sum: { $toDouble: '$v.avg_power_consumption' } },
- { avg_power_consumption: '$avg_power_consumption' });
- ctx.logger.info('任务进行avgSpeedPower');
- const actualMileage = await this.bucketGroup(timeRangData, '$actualMileage',
- [ 0, 100, 120, 140, 160, 180, 200, 220, 240, 260, 280, 300, 10000 * 10000 ],
- [{ $addFields: { actualMileage: { $ifNull: [
- { $avg: { $map: { input: '$actual_mileage_list', in: { $toDouble: '$$this.mileage' } } } }, 0 ] } } }]);
- ctx.logger.info('任务进行actualMileage');
- const mileageCnt = await this.bucketGroup(timeRangData,
- { $toDouble: '$mileage_cnt' }, [ 0, 30, 60, 90, 120, 10 * 10000 ]);
- ctx.logger.info('任务进行mileageCnt');
- const mileageCntMonth = await this.bucketDupGroupMonth(
- { startTime: ctx.helper.getMonthTop(timeRangData.startTime), endTime: timeRangData.endTime },
- { $toDouble: '$mileage_cnt' }, [ 0, 30, 60, 90, 120, 10 * 10000 ]);
- ctx.logger.info('任务进行mileageCntMonth');
- const mileageCntYear = await this.bucketDupGroupYear(
- { startTime: ctx.helper.nowYear(timeRangData.startTime), endTime: timeRangData.endTime },
- { $toDouble: '$mileage_cnt' }, [ 0, 300, 600, 900, 1200, 10 * 10000 ]);
- ctx.logger.info('任务进行mileageCntYear');
- // 0-500,500-1000,1000-1500,1500-2000,2000-3000,3000-4000,4000-5000,5000以上
- // 0-0.5万,0.5-1万,1万-1.5万,1.5-2万,2万-3万,3万-4万,4万-5万,5万以上
- const mileage = await this.bucketGroup(timeRangData,
- { $toDouble: '$mileage' }, [ 0, 50, 100, 300, 500, 1000, 1500, 20000, 10000 * 10000 ]);
- ctx.logger.info('任务进行mileage');
- const mileageMonth = await this.bucketDupGroupMonth(
- { startTime: ctx.helper.getMonthTop(timeRangData.startTime), endTime: timeRangData.endTime },
- { $toDouble: '$mileage' }, [ 0, 500, 1000, 1500, 2000, 3000, 4000, 5000, 10000 * 10000 ]);
- ctx.logger.info('任务进行mileageMonth');
- const mileageYear = await this.bucketDupGroupYear(
- { startTime: ctx.helper.nowYear(timeRangData.startTime), endTime: timeRangData.endTime },
- { $toDouble: '$mileage' }, [ 0, 5000, 10000, 15000, 20000, 30000, 40000, 50000, 10000 * 10000 ]);
- ctx.logger.info('任务进行mileageYear');
- const chargeCnt = await this.bucketGroup(timeRangData,
- { $toDouble: '$charge_cnt' }, [ 0, 50, 100, 150, 200, 10 * 10000 ]);
- ctx.logger.info('任务进行chargeCnt');
- const chargeCntMonth = await this.bucketDupGroupMonth(
- { startTime: ctx.helper.getMonthTop(timeRangData.startTime), endTime: timeRangData.endTime },
- { $toDouble: '$charge_cnt' }, [ 0, 5, 10, 15, 20, 10 * 10000 ]);
- ctx.logger.info('任务进行chargeCntMonth');
- const chargeCntYear = await this.bucketDupGroupYear(
- { startTime: ctx.helper.nowYear(timeRangData.startTime), endTime: timeRangData.endTime },
- { $toDouble: '$charge_cnt' }, [ 0, 50, 100, 150, 200, 10 * 10000 ]);
- ctx.logger.info('任务进行chargeCntYear');
- const skyDuration = await this.bucketGroup(timeRangData,
- { $divide: [{ $toDouble: '$sky_duration' }, 60 * 1000 ] },
- [ 0, 30, 60, 120, 180, 24 * 60 ]);
- ctx.logger.info('任务进行skyDuration');
- const skyDurationMonth = await this.bucketDupGroupMonth(
- { startTime: ctx.helper.getMonthTop(timeRangData.startTime), endTime: timeRangData.endTime },
- { $divide: [{ $toDouble: '$sky_duration' }, 60 * 60 * 1000 ] },
- [ 0, 0.5, 1, 2, 3, 24 * 60 ]);
- ctx.logger.info('任务进行skyDurationMonth');
- const skyDurationYear = await this.bucketDupGroupYear(
- { startTime: ctx.helper.nowYear(timeRangData.startTime), endTime: timeRangData.endTime },
- { $divide: [{ $toDouble: '$sky_duration' }, 60 * 60 * 1000 ] },
- [ 0, 5, 10, 20, 30, 24 * 60 ]);
- ctx.logger.info('任务进行skyDurationYear');
- const headlightCnt = await this.bucketGroup(timeRangData, { $toDouble: '$headlight_cnt' },
- [ 0, 30, 60, 90, 120, 150, 180, 210, 240, 270, 300, 10 * 10000 ]);
- ctx.logger.info('任务进行headlightCnt');
- const headlightCntMonth = await this.bucketDupGroupMonth(
- { startTime: ctx.helper.getMonthTop(timeRangData.startTime), endTime: timeRangData.endTime },
- { $toDouble: '$headlight_cnt' },
- [ 0, 30, 60, 90, 120, 150, 180, 210, 240, 270, 300, 10 * 10000 ]);
- ctx.logger.info('任务进行headlightCntMonth');
- const headlightCntYear = await this.bucketDupGroupYear(
- { startTime: ctx.helper.nowYear(timeRangData.startTime), endTime: timeRangData.endTime },
- { $toDouble: '$headlight_cnt' },
- [ 0, 300, 600, 900, 1200, 1500, 1800, 2100, 2400, 2700, 3000, 10 * 10000 ]);
- ctx.logger.info('任务进行headlightCntYear');
- const headlightDuration = await this.bucketGroup(timeRangData,
- { $divide: [{ $toDouble: '$headlight_duration' }, 60 * 1000 ] },
- [ 0, 0.5, 1, 3, 5, 8, 10, 15, 20, 25, 30, 40, 50, 24 * 60 ]);
- ctx.logger.info('任务进行headlightDuration');
- const headlightDurationMonth = await this.bucketDupGroupMonth(
- { startTime: ctx.helper.getMonthTop(timeRangData.startTime), endTime: timeRangData.endTime },
- { $divide: [{ $toDouble: '$headlight_duration' }, 60 * 60 * 1000 ] },
- [ 0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5, 5, 5.5, 6, 24 * 60 ]);
- ctx.logger.info('任务进行headlightDurationMonth');
- const headlightDurationYear = await this.bucketDupGroupYear(
- { startTime: ctx.helper.nowYear(timeRangData.startTime), endTime: timeRangData.endTime },
- { $divide: [{ $toDouble: '$headlight_duration' }, 60 * 60 * 1000 ] },
- [ 0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 24 * 60 ]);
- ctx.logger.info('任务进行headlightDurationYear');
- const highBeamCnt = await this.bucketGroup(timeRangData, { $toDouble: '$high_beam_cnt' },
- [ 0, 30, 60, 90, 120, 150, 180, 210, 240, 270, 300, 10 * 10000 ]);
- ctx.logger.info('任务进行highBeamCnt');
- const highBeamCntMonth = await this.bucketDupGroupMonth(
- { startTime: ctx.helper.getMonthTop(timeRangData.startTime), endTime: timeRangData.endTime },
- { $toDouble: '$high_beam_cnt' },
- [ 0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 10 * 10000 ]);
- ctx.logger.info('任务进行highBeamCntMonth');
- const highBeamCntYear = await this.bucketDupGroupYear(
- { startTime: ctx.helper.nowYear(timeRangData.startTime), endTime: timeRangData.endTime },
- { $toDouble: '$high_beam_cnt' },
- [ 0, 100, 200, 300, 400, 500, 600, 700, 800, 900, 1000, 10 * 10000 ]);
- ctx.logger.info('任务进行highBeamCntYear');
- const highBeamDuration = await this.bucketGroup(timeRangData,
- { $divide: [{ $toDouble: '$high_beam_duration' }, 60 * 1000 ] },
- [ 0, 0.5, 1, 3, 5, 8, 10, 15, 20, 25, 30, 40, 50, 24 * 60 ]);
- ctx.logger.info('任务进行highBeamDuration');
- const highBeamDurationMonth = await this.bucketDupGroupMonth(
- { startTime: ctx.helper.getMonthTop(timeRangData.startTime), endTime: timeRangData.endTime },
- { $divide: [{ $toDouble: '$high_beam_duration' }, 60 * 60 * 1000 ] },
- [ 0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5, 5, 5.5, 6, 24 * 60 ]);
- ctx.logger.info('任务进行highBeamDurationMonth');
- const highBeamDurationYear = await this.bucketDupGroupYear(
- { startTime: ctx.helper.nowYear(timeRangData.startTime), endTime: timeRangData.endTime },
- { $divide: [{ $toDouble: '$high_beam_duration' }, 60 * 60 * 1000 ] },
- [ 0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 24 * 60 ]);
- ctx.logger.info('任务进行highBeamDurationYear');
- const mileageStartTimeAndDsm = await this.group(timeRangData, { $hour: { date: { $toDate: '$mileage_list.start_time' }, timezone: 'Asia/Shanghai' } },
- [{ $unwind: '$mileage_list' }],
- { count: { $sum: 1 }, dsmCount: { $sum: { $cond: [{ $eq: [ '$mileage_list.dsm_status', 1 ] }, 1, 0 ] } } }
- );
- ctx.logger.info('任务进行mileageStartTimeAndDsm');
- const mileageCnt2 = await this.bucketGroup(timeRangData, { $toDouble: '$mileage_cnt' },
- [ 1, 2, 3, 4, 5, 6, 7, 10 * 10000 ]
- );
- ctx.logger.info('任务进行mileageCnt2');
- const avgSpeed = await this.bucketGroup(timeRangData, { $toDouble: '$avg_speed' },
- [ 0, 20, 30, 40, 50, 1000 ]
- );
- ctx.logger.info('任务进行avgSpeed');
- const driveDuration = await this.bucketGroup(timeRangData,
- { $divide: [{ $toDouble: '$drive_duration' }, 60 * 60 * 1000 ] },
- [ 0, 1, 2, 3, 4, 24 ]
- );
- ctx.logger.info('任务进行driveDuration');
- const continuousDrive = await this.bucketGroup(timeRangData,
- { $divide: [{ $toDouble: '$mileage_list.drive_duration' }, 60 * 60 * 1000 ] },
- [ 0, 1, 2, 3, 4, 24 ],
- [{ $unwind: '$mileage_list' }]
- );
- ctx.logger.info('任务进行continuousDrive');
- const driveStyle = await this.group(timeRangData, '$mileage_list.drive_style',
- [{ $unwind: '$mileage_list' }],
- { count: { $sum: 1 } }
- );
- ctx.logger.info('任务进行driveStyle');
- const drivingSafetyScore = await this.bucketGroup(timeRangData, { $toDouble: '$driving_safety_score' },
- [ 0, 60, 75, 90, 101 ]
- );
- ctx.logger.info('任务进行drivingSafetyScore');
- const energyConservationScore = await this.bucketGroup(timeRangData, { $toDouble: '$energy_conservation_score' },
- [ 0, 60, 75, 90, 101 ]
- );
- ctx.logger.info('任务进行energyConservationScore');
- const mileageSingle = await this.bucketGroup(timeRangData,
- { $toDouble: '$mileage_list.mileage' },
- [ 0, 10, 20, 30, 40, 50 ],
- [{ $unwind: '$mileage_list' }]
- );
- ctx.logger.info('任务进行mileageSingle');
- const maxAcce = await this.bucketGroup(timeRangData, { $toDouble: '$mileage_list.max_acce' },
- [ 0, 0.05, 0.1, 0.15, 0.2, 0.25, 0.3, 0.35, 0.4, 0.45, 0.5 ],
- [{ $unwind: '$mileage_list' }]
- );
- ctx.logger.info('任务进行maxAcce');
- const maxDece = await this.bucketGroup(timeRangData, { $toDouble: '$mileage_list.max_dece' },
- [ 0, 0.05, 0.1, 0.15, 0.2, 0.25, 0.3, 0.35, 0.4, 0.45, 0.5, 0.55, 0.6, 0.65, 0.7, 0.75, 0.8 ],
- [{ $unwind: '$mileage_list' }]
- );
- ctx.logger.info('任务进行maxDece');
- const sideAcce = await this.bucketGroup(timeRangData, { $toDouble: '$mileage_list.side_acce' },
- [ 0, 0.05, 0.1, 0.15, 0.2, 0.25, 0.3, 0.35, 0.4, 0.45, 0.5 ],
- [{ $unwind: '$mileage_list' }]
- );
- ctx.logger.info('任务进行sideAcce');
- const oilProvince = await this.oilLocation(timeRangData, { _id: '$car.pro_code' },
- [
- { $lookup: { from: 't_sync_province', localField: '_id._id', foreignField: 'out_pro_code', as: 'pro' } },
- { $unwind: '$pro' },
- { $project: { oil: 1, mileage: 1, name: '$pro.province_name'/* ,avgOil:1*/ } },
- ]);
- ctx.logger.info('任务进行oilProvince');
- const oilCity = await this.oilLocation(timeRangData, { fid: '$car.pro_code', _id: '$car.city_code' },
- [
- { $lookup: { from: 't_sync_province', localField: '_id.fid', foreignField: 'out_pro_code', as: 'pro' } },
- { $unwind: '$pro' },
- { $lookup: {
- from: 't_sync_city',
- let: { city_code: '$_id._id', province_id: '$pro.province_id' },
- pipeline: [{ $match: { $expr: { $and:
- [
- { $eq: [ '$out_city_code', '$$city_code' ] },
- { $eq: [ '$province_id', '$$province_id' ] },
- ] } } }], as: 'city',
- } },
- { $unwind: '$city' },
- { $project: { oil: 1, mileage: 1, name: '$city.city_name'/* , avgOil:1 */ } },
- ]);
- ctx.logger.info('任务进行oilCity');
- await this.ctx.service.statisticsService.save(this.ctx.model.Local.DrivingBehaviorInfoModel2,
- { ...initData,
- mileage2, supplementaryCnt, batterySoc, engineSpeed, insideTemperature, outsideTemperature,
- powerBatterySoc, chargeDuration, chargeStartSoc, chargeEndSoc, chargePreStartMileage, chargeStartTime,
- chargeType, chargeMileage, avgSpeedPower, actualMileage,
- mileageCnt, mileageCntMonth, mileageCntYear, mileage, mileageMonth, mileageYear,
- chargeCnt, chargeCntMonth, chargeCntYear,
- skyDuration, skyDurationMonth, skyDurationYear,
- headlightCnt, headlightCntMonth, headlightCntYear,
- headlightDuration, headlightDurationMonth, headlightDurationYear,
- highBeamCnt, highBeamCntMonth, highBeamCntYear,
- highBeamDuration, highBeamDurationMonth, highBeamDurationYear,
- mileageStartTimeAndDsm, driveDuration, mileageCnt2, avgSpeed,
- continuousDrive, driveStyle, mileageSingle, mileageAvg,
- drivingSafetyScore, energyConservationScore,
- maxAcce, maxDece, sideAcce,
- data, oilProvince, oilCity,
- }, isForceUpdate);
- }
- async oilLocation({ startTime, endTime }, group, lookups) {
- const { ctx } = this;
- const agg = [
- { $match: ctx.helper.getTimeRangMatch(startTime, endTime, 'start_time') },
- { $lookup: { from: 't_vehicle_record', localField: 'vin', foreignField: 'vin', as: 'car' } },
- { $unwind: { path: '$car', preserveNullAndEmptyArrays: true } },
- { $addFields: { oil: { $multiply: [{ $divide: [{ $toDouble: '$avg_oil_consumption' }, 100 ] }, { $toDouble: '$mileage' }] } } },
- { $group: {
- _id: { series_code: '$car.series_code', model_code: '$car.model_code', ...group },
- oil: { $sum: '$oil' },
- mileage: { $sum: { $toDouble: '$mileage' } },
- // avgOil: { $avg: { $toDouble: '$avg_oil_consumption' } },
- } },
- ...lookups,
- ];
- return await ctx.model.DrivingBehaviorInfoModel.aggregateFix(agg);
- }
- async bucketGroup({ startTime, endTime }, group, boundaries, otherCond = [],
- sum = { $sum: 1 }, bucketF = {}) {
- const { ctx } = this;
- const agg = [
- { $match: ctx.helper.getTimeRangMatch(startTime, endTime, 'start_time') },
- ...otherCond,
- ...ctx.helper.getBucketMongo(group, boundaries, sum, bucketF), // 保证在这步骤前必须有条件字段和vin即可
- ];
- return await ctx.model.DrivingBehaviorInfoModel.aggregateFix(agg);
- }
- async bucketDupGroupMonth({ startTime, endTime }, group, boundaries) {
- const { ctx } = this;
- const agg = [
- { $match: ctx.helper.getTimeRangMatch(startTime, endTime, 'start_time') },
- { $group: {
- _id: { vin: '$vin',
- month: { $month: { date: { $toDate: '$start_time' }, timezone: 'Asia/Shanghai' } },
- year: { $year: { date: { $toDate: '$start_time' }, timezone: 'Asia/Shanghai' } },
- },
- count: { $sum: group },
- } },
- { $project: { vin: '$_id.vin', count: 1 } },
- ...ctx.helper.getBucketMongo('$count', boundaries), // 保证在这步骤前必须有条件字段和vin即可
- ];
- return await ctx.model.DrivingBehaviorInfoModel.aggregateFix(agg);
- }
- async bucketDupGroupYear({ startTime, endTime }, group, boundaries) {
- const { ctx } = this;
- const agg = [
- { $match: ctx.helper.getTimeRangMatch(startTime, endTime, 'start_time') },
- { $group: {
- _id: { vin: '$vin',
- year: { $year: { date: { $toDate: '$start_time' }, timezone: 'Asia/Shanghai' } },
- },
- count: { $sum: group },
- } },
- { $project: { vin: '$_id.vin', count: 1 } },
- ...ctx.helper.getBucketMongo('$count', boundaries), // 保证在这步骤前必须有条件字段和vin即可
- ];
- return await ctx.model.DrivingBehaviorInfoModel.aggregateFix(agg);
- }
- async group({ startTime, endTime }, group, otherCond, cond = { count: { $sum: 1 } }) {
- const { ctx } = this;
- const agg = [
- { $match: ctx.helper.getTimeRangMatch(startTime, endTime, 'start_time') },
- ...otherCond,
- { $lookup: { from: 't_vehicle_record', localField: 'vin', foreignField: 'vin', as: 'car' } },
- { $unwind: { path: '$car', preserveNullAndEmptyArrays: true } },
- { $group: {
- _id: { series_code: '$car.series_code', model_code: '$car.model_code', _id: group },
- ...cond,
- } },
- ];
- return await ctx.model.DrivingBehaviorInfoModel.aggregateFix(agg);
- }
- async avgGroup({ startTime, endTime }, cond, otherCond) {
- const { ctx } = this;
- const agg = [
- { $match: ctx.helper.getTimeRangMatch(startTime, endTime, 'start_time') },
- ...otherCond,
- { $lookup: { from: 't_vehicle_record', localField: 'vin', foreignField: 'vin', as: 'car' } },
- { $unwind: { path: '$car', preserveNullAndEmptyArrays: true } },
- { $group: { _id: { series_code: '$car.series_code', model_code: '$car.model_code' },
- ...cond, count: { $sum: 1 } } },
- ];
- return await ctx.model.DrivingBehaviorInfoModel.aggregateFix(agg);
- }
- }
- module.exports = DrivingBehaviorInfoService2;
|