Commit d795cca9 by liimin

数据逻辑调整

1 parent c698fed5
......@@ -2,7 +2,7 @@
* @Author: limin
* @Date: 2019-12-06 14:58:35
* @Last Modified by: limin
* @Last Modified time: 2019-12-08 21:19:20
* @Last Modified time: 2020-01-03 18:12:37
*/
// const items = new WeakMap(); //WeakMap对象是密钥/值对的集合,其中密钥被弱引用。键必须是对象,值可以是任意值。
......@@ -110,7 +110,7 @@ module.exports = class Queue {
this.enqueue(this._last_dequeue);
} else if (_result_size < this._size) {
// 从上一次返回的数据中 获取相差的几条数据 补充到最新数据的末尾 保证队列数据的数量 公式为: [data][_result_size]
const _last_data_list = data.filter((item, index) => index >= _result_size);
const _last_data_list = this._last_dequeue.filter((item, index) => index >= _result_size);
this.enqueue(...result, ..._last_data_list);
} else {
this.enqueue(result);
......
......@@ -2,15 +2,15 @@
* @Author: limin
* @Date: 2019-11-21 11:20:01
* @Last Modified by: limin
* @Last Modified time: 2019-12-26 18:25:56
* @Last Modified time: 2020-01-03 18:16:02
*/
"use strict";
const Controller = require("egg").Controller;
/**
* @Controller screen/user
* @Description 数据大屏-用户
* @Controller screen/merchant
* @Description 数据大屏-商户
*/
class MerchantController extends Controller {
constructor(ctx) {
......@@ -58,7 +58,7 @@ class MerchantController extends Controller {
var result=[];
if(isGetLast){
const { cache_data_last_date_merchant } = await this._fetch_date_in_cache_();
result = summary_30_day[cache_data_last_date_merchant];
result = [summary_30_day[cache_data_last_date_merchant]];
fn && fn(cache_data_last_date_merchant,summary_30_day[cache_data_last_date_merchant])
}else if(beginDate){
endDate = endDate || beginDate;
......@@ -87,7 +87,9 @@ class MerchantController extends Controller {
}
async _fetch_total_({ field,isGetLast } ){
const _summary = await this._fetch_summary_({isGetLast});
return this._set_(field,(f)=>_summary[f] || 0);
// return this._set_(field,async (f)=>_summary[f]||0);
const result = await this._set_(field,(f)=>_summary.reduce((pre,next)=>pre + +next[f],0));
return result
}
/**
......@@ -143,8 +145,8 @@ class MerchantController extends Controller {
* @Description 数据大屏-商户总数统计.---- 广联商户总数 商户累计订单数 当日订单数
*/
async total() {
var merchant_total = await this._fetch_total_({ field:'merchant_total', isGetLast:true})
var order_total = await this._fetch_total_({ field:'order_total', isGetLast:true})
var merchant_total = await this._fetch_total_({ field:'total_merchant',isGetLast:true})
var order_total = await this._fetch_total_({ field:'total_order',isGetLast:true})
const realtime = await this.MerchantService.merchant_realtime();
const realtime_new_order = realtime.reduce((pre,next)=>pre + +next['new_order'],0);
const realtime_new_merchant = realtime.reduce((pre,next)=>pre + +next['new_merchant'],0);
......@@ -207,47 +209,36 @@ class MerchantController extends Controller {
/**
* @Router GET /screen/merchant/trend_device
* @Description 数据大屏-设备订单统计 1.商户订单类型分布 2.订购设备类型分布
* @Description 数据大屏-设备订单统计 1.商户订单类型分布(order_type) 2.订购设备类型分布(device_type) 3商户类型分布(merchant_type) 4订单数量区域排名(order_sort) 5:商户渠道分布(channel)
* @Request query integer n 近N天数据 默认 30天
* @Request query string beginDate 查询开始时间 (2019-12-12)
* @Request query string endDate 查询结束时间 (2019-12-12)
*/
async device_order_stat() {
const q = this._fetch_total_({field:[ 'type_4s', 'type_weixiu', 'type_yingai', 'type_jingpin','type_meibao', 'maintain_order','repair_order','insurance_order','device_order','p' ],isGetLast:true})
const { type_4s, type_weixiu, type_yingai, type_jingpin,type_meibao,maintain_order,repair_order,insurance_order,device_order } =q
const data = {}
data.merchant_type = { type_4s, type_weixiu, type_yingai, type_jingpin,type_meibao } // 商户类型
data.order_type={ maintain_order,repair_order,insurance_order,device_order } // 订单类型
const { type_4s , type_weixiu , type_yingai , type_jingpin ,type_meibao ,maintain_order ,repair_order ,insurance_order ,device_order }
=
await this._fetch_total_({field:[ 'type_4s', 'type_weixiu', 'type_yingai', 'type_jingpin','type_meibao', 'maintain_order','repair_order','insurance_order','device_order' ],isGetLast:true})
//订单数量区域排名
// Object.values(p).sort(obj=>obj.)
this.ctx.body = this.ServerResponse.Success(data);
}
/**
* @Router GET /screen/user/activation
* @Description 实时激活数据播报
* @Request query integer pid 省份id
*/
async activation({ query: { pid } }) {
const data = this.queue.peek();
const { USER_ACTIVE_DATA_LASI_ID } = this.helper.constant;
const _last_id = await this.CacheService.get(USER_ACTIVE_DATA_LASI_ID);
this.queue.fill(async ()=>await this.UserService.activation({ pid, limit: this.queue.size(), last_id:_last_id }))
.then(last_id=>last_id && this.CacheService.set(USER_ACTIVE_DATA_LASI_ID,last_id))
this.ctx.body = this.ServerResponse.Success(data);
}
const last = await this._fetch_summary_({isGetLast:true});
const order_sort = []
const province = last[0].p
for (const key in province) {
order_sort.push({prov_code:key,value:province[key].total_order})
}
order_sort.sort((a,b)=>b.value-a.value).length = 10
const channel = await this.MerchantService.channel_stat({limit:6})
const device_type = await this.MerchantService.device_type()
const data = {
merchant_type : { type_4s, type_weixiu, type_yingai, type_jingpin,type_meibao }, // 商户类型
order_type: { maintain_order,repair_order,insurance_order,device_order }, // 商户订单类型分布
order_sort, //订单数量区域排名
channel, // 商户渠道分布
device_type //订购设备类型分布
}
/**
* @Router GET /screen/user/total_group_by_province
* @Description 获取每个省的总用户数
* @Request query integer pid 省份id
*/
async total_group_by_province({ query: { pid } }) {
const data = await this.UserService.total_group_by_province({ pid });
this.ctx.body = this.ServerResponse.Success(data);
}
}
module.exports = MerchantController;
......@@ -2,7 +2,7 @@
* @Author: limin
* @Date: 2019-11-21 11:20:01
* @Last Modified by: limin
* @Last Modified time: 2019-12-23 17:25:22
* @Last Modified time: 2020-01-03 18:23:32
*/
"use strict";
......@@ -165,7 +165,7 @@ class UserController extends Controller {
if(cid){
fn = async f=> value && value['p'][pid] && value['p'][pid]['c'][cid]?value['p'][pid]['c'][cid][f]:0
}else{
fn = async f=>value && value['p'][pid] ? value['p'][pid][f]:0
fn = async f=> value && value['p'][pid] ? value['p'][pid][f]:0
}
}else{
fn = async f=>value ? value[f]:0
......@@ -185,9 +185,9 @@ class UserController extends Controller {
const realtime = await this.UserService.user_realtime({ pid, cid });
const realtime_total = realtime.reduce((pre,next)=>pre + +next['increase'],0);
const online_total = realtime.reduce((pre,next)=>pre + +next['online_total'],0);
const online_time= realtime.reduce((pre,next)=>pre + +next['online_time'],0);
// const online_time= realtime.reduce((pre,next)=>pre + +next['online_time'],0);
total += ~~realtime_total;
const data = { total, online_total, online_time };
const data = { total, online_total };
this.ctx.body = this.ServerResponse.Success(data);
}
/**
......@@ -196,10 +196,9 @@ class UserController extends Controller {
* @Request query integer pid 省份id
*/
async statistics({ query: { pid,cid } }) {
var { r, u, rt, a7, a30 } = await this._fetch_total_last_day_({ pid,cid,field:['r','u', 'rt', 'a7', 'a30']});
console.log(a7,a30);
var { rr, rt, a7, a30, o } = await this._fetch_total_last_day_({ pid,cid,field:['rr', 'rt', 'a7', 'a30','o']});
// 昨天的付费转化率 (昨天的付费总人数 / 昨天用户总数)
const renew = this.helper.percent(r,u);
const renew = this.helper.percent(rr,rt);
const beginDate7 = this.helper.date(-7);
const { yesterday } = await this._fetch_date_in_cache_();
//近7天所有
......@@ -210,8 +209,9 @@ class UserController extends Controller {
const { i:increased30 } = await this._fetch_total_by_range_({ pid, cid, field:['i']});
// 近30天 用户活跃度 (昨天的数据: 近30天 活跃用户数 / 昨天用户数)
const live30 = this.helper.percent(a30,rt);
const data = { increased7, live7,increased30,live30,renew };
//在线时长 / 3600 / 近两年用户数
const online_time = o / 3600 / rt
const data = { increased7, live7,increased30,live30,renew, online_time };
this.ctx.body = this.ServerResponse.Success(data);
}
......@@ -280,9 +280,10 @@ class UserController extends Controller {
var { yesterday:endDate } = await this._fetch_date_in_cache_();
}
}
const { rt } = await this._fetch_total_last_day_({ pid,cid,field:['rt']});
type = type ? ( type.includes(',') ? type.split(',') : type ) : [ 'u', 'i', 'a', 'r', 'j' ];
const data = await this._fetch_detail_by_range_( { pid, cid, field:type,beginDate, endDate } )
this.ctx.body = this.ServerResponse.Success(data);
const list = await this._fetch_detail_by_range_( { pid, cid, field:type,beginDate, endDate } )
this.ctx.body = this.ServerResponse.Success({recent_total:rt,list});
}
/**
* @Router GET /screen/user/activation
......
......@@ -68,6 +68,51 @@ module.exports = app => {
allowNull: true,
defaultValue: '0'
},
maintain_order: {
type: DataTypes.INTEGER(10),
allowNull: true,
defaultValue: '0'
},
repair_order: {
type: DataTypes.INTEGER(10),
allowNull: true,
defaultValue: '0'
},
insurance_order: {
type: DataTypes.INTEGER(10),
allowNull: true,
defaultValue: '0'
},
device_order: {
type: DataTypes.INTEGER(10),
allowNull: true,
defaultValue: '0'
},
type_4s: {
type: DataTypes.INTEGER(10),
allowNull: true,
defaultValue: '0'
},
type_weixiu: {
type: DataTypes.INTEGER(10),
allowNull: true,
defaultValue: '0'
},
type_yingai: {
type: DataTypes.INTEGER(10),
allowNull: true,
defaultValue: '0'
},
type_jingpin: {
type: DataTypes.INTEGER(10),
allowNull: true,
defaultValue: '0'
},
type_meibao: {
type: DataTypes.INTEGER(10),
allowNull: true,
defaultValue: '0'
},
create_time: {
type: DataTypes.DATE,
allowNull: true
......
......@@ -27,6 +27,11 @@ module.exports = app => {
allowNull: true,
defaultValue: '0'
},
recent_renew: {
type: DataTypes.INTEGER(10),
allowNull: true,
defaultValue: '0'
},
renew: {
type: DataTypes.INTEGER(10),
allowNull: true,
......
......@@ -2,11 +2,13 @@
* @Author: limin
* @Date: 2019-11-21 11:18:45
* @Last Modified by: limin
* @Last Modified time: 2019-12-26 15:55:15
* @Last Modified time: 2019-12-27 15:51:21
*/
module.exports = app => {
app.router.get('/screen/merchant/total', app.controller.screen.merchant.total);
app.router.get('/screen/merchant/statistics', app.controller.screen.merchant.statistics);
app.router.get('/screen/merchant/trend', app.controller.screen.merchant.trend);
app.router.get('/screen/merchant/trend_device', app.controller.screen.merchant.trend_device);
app.router.get('/screen/merchant/device_order_stat', app.controller.screen.merchant.device_order_stat);
};
\ No newline at end of file
......@@ -37,8 +37,9 @@ class UpdateCache extends Subscription {
function __init_or_plus__(target, obj) {
target = target || {};
if (obj) {
const { r, u, a, i, j, m, o, rt, a7, a30 } = obj;
const { r, rr, u, a, i, j, m, o, rt, a7, a30 } = obj;
target["r"] += +r; //续费 renew
target["rr"] += +rr; //近年续费总数 recent_renew
target["u"] += +u; //用户总数 user
target["a"] += +a; // 活跃用户数 active
target["i"] += +i; // 新增用户数 increase
......@@ -50,13 +51,14 @@ class UpdateCache extends Subscription {
target["a30"] += +a30; // 近30天 活跃数
} else {
target["r"] = 0; //续费 renew
target["rr"] = 0; //近年续费总数 recent_renew
target["u"] = 0; //用户总数 user
target["a"] = 0; // 活跃用户数 active
target["i"] = 0; // 新增用户数 increase
target["j"] = 0; // 出行 journey
target["m"] = 0.0; // 里程 miles
target["o"] = 0; // online 在线时间
target["rt"] = 0; // 最近总数
target["rt"] = 0; // 近年活跃的用户总数 recent_total
target["a7"] = 0; // 近7天 活跃数
target["a30"] = 0; // 近30天 活跃数
}
......@@ -65,13 +67,13 @@ class UpdateCache extends Subscription {
cities.forEach(
function(city) {
const prov_code = f(city);
let { city_code, total_user: u, renew: r, active: a, increase: i, journey: j, miles: m, online_time: o, day,recent_total:rt, recent_7_day_active:a7, recent_30_day_active:a30} = city;
let { city_code, total_user: u, renew: r, recent_renew:rr, active: a, increase: i, journey: j, miles: m, online_time: o, day,recent_total:rt, recent_7_day_active:a7, recent_30_day_active:a30} = city;
date = this.helper.moment(day).isAfter(date) ? day : date;
data[day] = data[day] || Object.assign(__init_or_plus__(null),{'p':{}}) //如果day 节点不存在 则 初始化一个
data[day] = __init_or_plus__(data[day], { r, u, a, i, j, m, o, rt, a7, a30 }); // 累加全国总数值
data[day] = __init_or_plus__(data[day], { r,rr, u, a, i, j, m, o, rt, a7, a30 }); // 累加全国总数值
data[day]["p"][prov_code] = data[day]["p"][prov_code] || Object.assign(__init_or_plus__(null),{'c':{}}); //如果day 下的 省份 不存在 则 初始化一个
data[day]["p"][prov_code] = __init_or_plus__(data[day]["p"][prov_code], { r, u, a, i, j, m, o, rt, a7, a30 }); //累加 prov_code 省份总数据
data[day]["p"][prov_code]["c"][city_code] = { u, r, a, i, j, m, o, rt, a7, a30 };
data[day]["p"][prov_code] = __init_or_plus__(data[day]["p"][prov_code], { r, u, rr, a, i, j, m, o, rt, a7, a30 }); //累加 prov_code 省份总数据
data[day]["p"][prov_code]["c"][city_code] = { u, r, rr, a, i, j, m, o, rt, a7, a30 };
}.bind(this)
);
return { data, date };
......
......@@ -123,6 +123,7 @@ class UpdateCacheMerchant extends Subscription {
MERCHANT_SUMMARY_30_DAY,
data
);
cache_data_last_date_merchant = date
// STAT
const _last_month_ =moment().format('YYYY-MM')
const _last_month_7 =moment().subtract(7, 'months').format('YYYY-MM')
......
......@@ -2,7 +2,7 @@
* @Author: limin
* @Date: 2019-11-21 11:18:37
* @Last Modified by: limin
* @Last Modified time: 2019-12-26 18:32:25
* @Last Modified time: 2020-01-03 16:52:13
*/
const Service = require("egg").Service;
......@@ -137,8 +137,17 @@ class MerchantService extends Service {
* @Description 商户渠道分布
*/
async channel_stat({limit}) {
var option = this.__build__option__({limit,order:[["merchant_total", "DESC"]]});
const { col, fn } = this.app.Sequelize;
var option = this.__build__option__();
Object.assign(option, {
attributes: [
'channel',
[fn("SUM", col("merchant_total")), "total"],
],
group:'channel'
});
const data = await this.MCS.findAll(option);
data.sort((a,b)=>b.total-a.total).length = limit
return data;
}
......@@ -146,17 +155,18 @@ class MerchantService extends Service {
* @param field {String}
* @param value {String}
* @return {Promise.<boolean>}
* @Description 设备类型分布
* @Description 订购设备类型分布
*/
async device_type() {
const { col, fn } = this.app.Sequelize;
var option = this.__build__option__();
Object.assign(option, {
attributes: [
'prov_code',
"device_type",
'device_total'
]
})
'device_type',
[fn("SUM", col("device_total")), "total"],
],
group:'device_type'
});
const data = await this.MODS.findAll(option);
return data;
}
......
......@@ -2,7 +2,7 @@
* @Author: limin
* @Date: 2019-11-21 11:18:37
* @Last Modified by: limin
* @Last Modified time: 2019-12-25 10:44:44
* @Last Modified time: 2020-01-03 18:15:00
*/
const Service = require("egg").Service;
......@@ -176,6 +176,7 @@ class UserService extends Service {
'prov_code',
"total_user",
'renew',
'recent_renew',
'active',
'increase',
'journey',
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!