Commit 534a0c9f by liimin

数据表调整

1 parent e9ab53f2
...@@ -2,10 +2,11 @@ ...@@ -2,10 +2,11 @@
* @Author: limin * @Author: limin
* @Date: 2019-11-21 11:20:15 * @Date: 2019-11-21 11:20:15
* @Last Modified by: limin * @Last Modified by: limin
* @Last Modified time: 2019-11-28 15:16:50 * @Last Modified time: 2019-11-29 09:43:28
*/ */
module.exports = { module.exports = {
PREFIX: '_glsx_data_screen_', PREFIX: '_glsx_data_screen_',
USER_SUMMARY_YESTERDAY:'user_summary_yesterday' USER_SUMMARY_YESTERDAY:'user_summary_yesterday',
DATES:'dates'
}; };
\ No newline at end of file \ No newline at end of file
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
* @Author: limin * @Author: limin
* @Date: 2019-11-21 11:20:01 * @Date: 2019-11-21 11:20:01
* @Last Modified by: limin * @Last Modified by: limin
* @Last Modified time: 2019-11-28 18:11:13 * @Last Modified time: 2019-11-29 14:12:17
*/ */
"use strict"; "use strict";
...@@ -25,18 +25,56 @@ class UserController extends Controller { ...@@ -25,18 +25,56 @@ class UserController extends Controller {
this.ServerResponse = ctx.response.ServerResponse; this.ServerResponse = ctx.response.ServerResponse;
} }
/** /**
* @Router GET /screen/user/total * 内部方法 从内存获取 昨天的统计数据
* @Description 数据大屏-用户总数统计.----参数pid 有 则返回对应的省份数据.没有则是全国数据的统计 * 比较数据时间 如果不是昨天的数据 则启动定时任务 更新一遍数据 再次获取数据并返回
* @Request query integer pid 省份id */
async _fetch_summary_yesterday(){
const { DATES, USER_SUMMARY_YESTERDAY } = this.helper.constant;
var yesterday_total = await this.CacheService.get(USER_SUMMARY_YESTERDAY);
const { date } = yesterday_total;
const { yesterday } = await this.CacheService.get(DATES);
if(date !== yesterday){ //如果 内存中的数据的日期不是昨天的 则手动启动定时器 更新缓存数据
await this.app.runSchedule('update_cache');
yesterday_total = await this.CacheService.get(USER_SUMMARY_YESTERDAY);
}
return yesterday_total
}
/**
* 内部方法 从内存获取 昨天的统计数据
*/ */
async total({ query: { pid,cid} }) { async _fetch_total({ pid, cid , field } ){
var yesterday_total = await this.CacheService.get(this.helper.constant.USER_SUMMARY_YESTERDAY); const _summary = await this._fetch_summary_yesterday();
var { total, province } = yesterday_total var { province } =_summary;
var total= 0;
function _set(target){
var total;
if(typeof field ==='string'){
total = target[field];
}else{
total = {}
for (const f of field) {
total[f] = target[f]
}
}
return total;
}
if (pid) { if (pid) {
var obj = cid ? province[pid]["city"][cid] : province[pid]; var obj = cid ? province[pid]["city"][cid] : province[pid];
total = obj ? obj.total : 0; obj && (total =_set(obj));
}else{
total = _set(_summary)
}
return total;
} }
const today_total = await this.UserService.last_n_day_user_increased({n:0,pid,cid}); /**
* @Router GET /screen/user/total
* @Description 数据大屏-用户总数统计.----参数pid 有 则返回对应的省份数据.没有则是全国数据的统计
* @Request query integer pid 省份id
*/
async total({ query: { pid,cid } }) {
var total = await this._fetch_total({ pid,cid,field:'total' })
const today_total = await this.UserService.last_n_day_user_increased({ n:0, pid, cid });
total += ~~today_total; total += ~~today_total;
// console.log(this.app.cache.today); // console.log(this.app.cache.today);
// `user_summary_${day}` // `user_summary_${day}`
...@@ -53,22 +91,25 @@ class UserController extends Controller { ...@@ -53,22 +91,25 @@ class UserController extends Controller {
* @Request query integer pid 省份id * @Request query integer pid 省份id
*/ */
async statistics({ query: { pid, n = -7,cid } }) { async statistics({ query: { pid, n = -7,cid } }) {
await this.app.runSchedule('update_cache'); const data = await this.UserService.last_n_day_user_increased_detail({ n, pid, cid });
//近7天新增用户数 // //近7天新增用户数
const increased7 = await this.UserService.last_n_day_user_increased({n,pid,cid}); // const increased7 = await this.UserService.last_n_day_user_increased({ n, pid, cid });
//近七天 活跃用户数 // //近七天 活跃用户数
const nLive7 = await this.UserService.last_n_day_user_lively({n,pid,cid}); // const nLive7 = await this.UserService.last_n_day_user_lively({ n, pid, cid });
// 近七天 用户活跃度 (近N天 活跃用户数 / 近N天新增用户数) // // 近七天 用户活跃度 (近N天 活跃用户数 / 近N天新增用户数)
const live7 = ((~~nLive7 / ~~increased7) * 100).toFixed(2)+ "%"; // const live7 = this.helper.percent(nLive7,increased7);
// //近30天新增用户数
// const increased30 = await this.UserService.last_n_day_user_increased({ n:-30, pid, cid });
// //近30天 活跃用户数
// const nLive30 = await this.UserService.last_n_day_user_lively({ n:-30, pid, cid });
// // 近30天 用户活跃度 (近N天 活跃用户数 / 近N天新增用户数)
// const live30 = this.helper.percent(nLive30,increased30);
//近30天新增用户数 // const { total ,renew } = await this._fetch_total({ pid,cid,field:['total','renew'] });
const increased30 = await this.UserService.last_n_day_user_increased({n:-30,pid,cid}); // // 昨天的付费转化率 (昨天的付费总人数 / 昨天用户总数)
//近30天 活跃用户数 // const renew_yesterday = this.helper.percent(renew,total);
const nLive30 = await this.UserService.last_n_day_user_lively({n:-30,pid,cid}); // const data = { increased7, live7,increased30,live30,renew_yesterday };
// 近30天 用户活跃度 (近N天 活跃用户数 / 近N天新增用户数)
const live30 = ((~~nLive30 / ~~increased30) * 100).toFixed(2)+ "%";
const data = { increased7, live7,increased30,live30 };
this.ctx.body = this.ServerResponse.Success(data); this.ctx.body = this.ServerResponse.Success(data);
} }
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
* @Author: limin * @Author: limin
* @Date: 2019-11-21 11:19:43 * @Date: 2019-11-21 11:19:43
* @Last Modified by: limin * @Last Modified by: limin
* @Last Modified time: 2019-11-28 16:22:53 * @Last Modified time: 2019-11-29 11:38:08
*/ */
const moment = require("moment"); const moment = require("moment");
...@@ -27,6 +27,26 @@ exports.jsonParser =str=>{ ...@@ -27,6 +27,26 @@ exports.jsonParser =str=>{
return res return res
} }
/** /**
* json对象转字符串
*/
exports.jsonStringify =obj=>{
var res;
if(typeof obj === 'string'){
return obj
}
try {
res = JSON.stringify(obj)
} catch (error) {
res = obj
}
return res
}
/**
*
*/
exports.percent=(num,total,fixed =2 )=>((~~num / ~~total) * 100).toFixed(fixed)+ "%";
/**
* 简单封装 sequelize 查询参数 * 简单封装 sequelize 查询参数
*/ */
exports.seqOption = function({ pid,cid, n, limit, order, day,beginDate,endDate }) { exports.seqOption = function({ pid,cid, n, limit, order, day,beginDate,endDate }) {
......
/* indent size: 2 */
module.exports = app => {
const DataTypes = app.Sequelize;
const Model = app.model.define('t_user_city_active_daily', {
id: {
type: DataTypes.INTEGER(10),
allowNull: false,
primaryKey: true,
autoIncrement: true
},
prov_code: {
type: DataTypes.STRING(32),
allowNull: false
},
city_code: {
type: DataTypes.STRING(32),
allowNull: false
},
day: {
type: DataTypes.DATE,
allowNull: false
},
total: {
type: DataTypes.INTEGER(10),
allowNull: false,
defaultValue: '0'
},
create_time: {
type: DataTypes.DATE,
allowNull: true
},
update_time: {
type: DataTypes.DATE,
allowNull: true
}
}, {
tableName: 't_user_city_active_daily'
});
Model.associate = function() {
}
return Model;
};
...@@ -18,19 +18,10 @@ module.exports = app => { ...@@ -18,19 +18,10 @@ module.exports = app => {
type: DataTypes.STRING(32), type: DataTypes.STRING(32),
allowNull: false allowNull: false
}, },
brand_code: {
type: DataTypes.STRING(32),
allowNull: false
},
brand_name: { brand_name: {
type: DataTypes.STRING(32), type: DataTypes.STRING(32),
allowNull: false allowNull: false
}, },
total_cars: {
type: DataTypes.INTEGER(10),
allowNull: true,
defaultValue: '0'
},
brand_cars: { brand_cars: {
type: DataTypes.INTEGER(10), type: DataTypes.INTEGER(10),
allowNull: true allowNull: true
......
...@@ -22,11 +22,6 @@ module.exports = app => { ...@@ -22,11 +22,6 @@ module.exports = app => {
type: DataTypes.STRING(32), type: DataTypes.STRING(32),
allowNull: false allowNull: false
}, },
total_cars: {
type: DataTypes.INTEGER(10),
allowNull: true,
defaultValue: '0'
},
range_cars: { range_cars: {
type: DataTypes.INTEGER(10), type: DataTypes.INTEGER(10),
allowNull: true allowNull: true
......
...@@ -3,13 +3,17 @@ ...@@ -3,13 +3,17 @@
module.exports = app => { module.exports = app => {
const DataTypes = app.Sequelize; const DataTypes = app.Sequelize;
const Model = app.model.define('t_user_city_journey_daily', { const Model = app.model.define('t_user_city_daily', {
id: { id: {
type: DataTypes.INTEGER(10), type: DataTypes.INTEGER(10),
allowNull: false, allowNull: false,
primaryKey: true, primaryKey: true,
autoIncrement: true autoIncrement: true
}, },
day: {
type: DataTypes.DATE,
allowNull: false
},
prov_code: { prov_code: {
type: DataTypes.STRING(32), type: DataTypes.STRING(32),
allowNull: false allowNull: false
...@@ -18,17 +22,40 @@ module.exports = app => { ...@@ -18,17 +22,40 @@ module.exports = app => {
type: DataTypes.STRING(32), type: DataTypes.STRING(32),
allowNull: false allowNull: false
}, },
day: { total_user: {
type: DataTypes.DATE, type: DataTypes.INTEGER(10),
allowNull: false allowNull: true,
defaultValue: '0'
},
renew: {
type: DataTypes.INTEGER(10),
allowNull: true,
defaultValue: '0'
},
active: {
type: DataTypes.INTEGER(10),
allowNull: true,
defaultValue: '0'
},
increase: {
type: DataTypes.INTEGER(10),
allowNull: true,
defaultValue: '0'
}, },
journey: { journey: {
type: DataTypes.INTEGER(10), type: DataTypes.INTEGER(10),
allowNull: true allowNull: true,
defaultValue: '0'
}, },
miles: { miles: {
type: "DOUBLE(12,2)", type: "DOUBLE(12,2)",
allowNull: true allowNull: true,
defaultValue: '0.00'
},
online_time: {
type: DataTypes.BIGINT,
allowNull: true,
defaultValue: '0'
}, },
create_time: { create_time: {
type: DataTypes.DATE, type: DataTypes.DATE,
...@@ -39,7 +66,7 @@ module.exports = app => { ...@@ -39,7 +66,7 @@ module.exports = app => {
allowNull: true allowNull: true
} }
}, { }, {
tableName: 't_user_city_journey_daily' tableName: 't_user_city_daily'
}); });
Model.associate = function() { Model.associate = function() {
......
/* indent size: 2 */
module.exports = app => {
const DataTypes = app.Sequelize;
const Model = app.model.define('t_user_city_increased_daily', {
id: {
type: DataTypes.INTEGER(10),
allowNull: false,
primaryKey: true,
autoIncrement: true
},
prov_code: {
type: DataTypes.STRING(32),
allowNull: false
},
city_code: {
type: DataTypes.STRING(32),
allowNull: false
},
day: {
type: DataTypes.DATE,
allowNull: false
},
total: {
type: DataTypes.INTEGER(10),
allowNull: false,
defaultValue: '0'
},
create_time: {
type: DataTypes.DATE,
allowNull: true
},
update_time: {
type: DataTypes.DATE,
allowNull: true
}
}, {
tableName: 't_user_city_increased_daily'
});
Model.associate = function() {
}
return Model;
};
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
module.exports = app => { module.exports = app => {
const DataTypes = app.Sequelize; const DataTypes = app.Sequelize;
const Model = app.model.define('t_user_city', { const Model = app.model.define('t_user_city_realtime', {
id: { id: {
type: DataTypes.INTEGER(10), type: DataTypes.INTEGER(10),
allowNull: false, allowNull: false,
...@@ -14,18 +14,34 @@ module.exports = app => { ...@@ -14,18 +14,34 @@ module.exports = app => {
type: DataTypes.STRING(32), type: DataTypes.STRING(32),
allowNull: false allowNull: false
}, },
day: {
type: DataTypes.DATE,
allowNull: false
},
city_code: { city_code: {
type: DataTypes.STRING(32), type: DataTypes.STRING(32),
allowNull: false, allowNull: false,
unique: true unique: true
}, },
total: { online_total: {
type: DataTypes.INTEGER(10), type: DataTypes.INTEGER(10),
allowNull: false, allowNull: true,
defaultValue: '0'
},
online_time: {
type: DataTypes.BIGINT,
allowNull: true,
defaultValue: '0'
},
increase: {
type: DataTypes.INTEGER(10),
allowNull: true,
defaultValue: '0'
},
active: {
type: DataTypes.INTEGER(10),
allowNull: true,
defaultValue: '0'
},
renew: {
type: DataTypes.INTEGER(10),
allowNull: true,
defaultValue: '0' defaultValue: '0'
}, },
create_time: { create_time: {
...@@ -37,7 +53,7 @@ module.exports = app => { ...@@ -37,7 +53,7 @@ module.exports = app => {
allowNull: true allowNull: true
} }
}, { }, {
tableName: 't_user_city' tableName: 't_user_city_realtime'
}); });
Model.associate = function() { Model.associate = function() {
......
/* indent size: 2 */
module.exports = app => {
const DataTypes = app.Sequelize;
const Model = app.model.define('t_user_city_renew_daily', {
id: {
type: DataTypes.INTEGER(10),
allowNull: false,
primaryKey: true,
autoIncrement: true
},
prov_code: {
type: DataTypes.STRING(32),
allowNull: false
},
city_code: {
type: DataTypes.STRING(32),
allowNull: false
},
day: {
type: DataTypes.DATE,
allowNull: false
},
total: {
type: DataTypes.INTEGER(10),
allowNull: false,
defaultValue: '0'
},
create_time: {
type: DataTypes.DATE,
allowNull: true
},
update_time: {
type: DataTypes.DATE,
allowNull: true
}
}, {
tableName: 't_user_city_renew_daily'
});
Model.associate = function() {
}
return Model;
};
/* indent size: 2 */
module.exports = app => {
const DataTypes = app.Sequelize;
const Model = app.model.define('t_user_province', {
id: {
type: DataTypes.INTEGER(10),
allowNull: false,
primaryKey: true,
autoIncrement: true
},
prov_code: {
type: DataTypes.STRING(32),
allowNull: false,
unique: true
},
total: {
type: DataTypes.INTEGER(10),
allowNull: false,
defaultValue: '0'
},
online_total: {
type: DataTypes.INTEGER(10),
allowNull: false,
defaultValue: '0'
},
create_time: {
type: DataTypes.DATE,
allowNull: true
},
update_time: {
type: DataTypes.DATE,
allowNull: true
}
}, {
tableName: 't_user_province'
});
Model.associate = function() {
}
return Model;
};
/* indent size: 2 */
module.exports = app => {
const DataTypes = app.Sequelize;
const Model = app.model.define('t_user_province_active_daily', {
id: {
type: DataTypes.INTEGER(10),
allowNull: false,
primaryKey: true,
autoIncrement: true
},
prov_code: {
type: DataTypes.STRING(32),
allowNull: false
},
day: {
type: DataTypes.DATE,
allowNull: false
},
total: {
type: DataTypes.INTEGER(10),
allowNull: false,
defaultValue: '0'
},
create_time: {
type: DataTypes.DATE,
allowNull: true
},
update_time: {
type: DataTypes.DATE,
allowNull: true
}
}, {
tableName: 't_user_province_active_daily'
});
Model.associate = function() {
}
return Model;
};
/* indent size: 2 */
module.exports = app => {
const DataTypes = app.Sequelize;
const Model = app.model.define('t_user_province_average_drive_month', {
id: {
type: DataTypes.INTEGER(10),
allowNull: false,
primaryKey: true,
autoIncrement: true
},
prov_code: {
type: DataTypes.STRING(32),
allowNull: false
},
days_range: {
type: DataTypes.STRING(32),
allowNull: false
},
total_users: {
type: DataTypes.INTEGER(10),
allowNull: true
},
range_users: {
type: DataTypes.INTEGER(10),
allowNull: true
},
create_time: {
type: DataTypes.DATE,
allowNull: true
},
update_time: {
type: DataTypes.DATE,
allowNull: true
}
}, {
tableName: 't_user_province_average_drive_month'
});
Model.associate = function() {
}
return Model;
};
/* indent size: 2 */
module.exports = app => {
const DataTypes = app.Sequelize;
const Model = app.model.define('t_user_province_average_miles_day', {
id: {
type: DataTypes.INTEGER(10),
allowNull: false,
primaryKey: true,
autoIncrement: true
},
prov_code: {
type: DataTypes.STRING(32),
allowNull: false
},
miles_range: {
type: DataTypes.STRING(32),
allowNull: false
},
total_users: {
type: DataTypes.INTEGER(10),
allowNull: true
},
range_users: {
type: DataTypes.INTEGER(10),
allowNull: true
},
create_time: {
type: DataTypes.DATE,
allowNull: true
},
update_time: {
type: DataTypes.DATE,
allowNull: true
}
}, {
tableName: 't_user_province_average_miles_day'
});
Model.associate = function() {
}
return Model;
};
/* indent size: 2 */
module.exports = app => {
const DataTypes = app.Sequelize;
const Model = app.model.define('t_user_province_carbrand', {
id: {
type: DataTypes.INTEGER(10),
allowNull: false,
primaryKey: true,
autoIncrement: true
},
prov_code: {
type: DataTypes.STRING(32),
allowNull: false
},
brand_code: {
type: DataTypes.STRING(32),
allowNull: false
},
brand_name: {
type: DataTypes.STRING(32),
allowNull: false
},
total_cars: {
type: DataTypes.INTEGER(10),
allowNull: true,
defaultValue: '0'
},
brand_cars: {
type: DataTypes.INTEGER(10),
allowNull: true
},
create_time: {
type: DataTypes.DATE,
allowNull: true
},
update_time: {
type: DataTypes.DATE,
allowNull: true
}
}, {
tableName: 't_user_province_carbrand'
});
Model.associate = function() {
}
return Model;
};
/* indent size: 2 */
module.exports = app => {
const DataTypes = app.Sequelize;
const Model = app.model.define('t_user_province_carprice', {
id: {
type: DataTypes.INTEGER(10),
allowNull: false,
primaryKey: true,
autoIncrement: true
},
prov_code: {
type: DataTypes.STRING(32),
allowNull: false
},
price_range: {
type: DataTypes.STRING(32),
allowNull: false
},
total_cars: {
type: DataTypes.INTEGER(10),
allowNull: true,
defaultValue: '0'
},
range_cars: {
type: DataTypes.INTEGER(10),
allowNull: true
},
create_time: {
type: DataTypes.DATE,
allowNull: true
},
update_time: {
type: DataTypes.DATE,
allowNull: true
}
}, {
tableName: 't_user_province_carprice'
});
Model.associate = function() {
}
return Model;
};
/* indent size: 2 */
module.exports = app => {
const DataTypes = app.Sequelize;
const Model = app.model.define('t_user_province_gender', {
id: {
type: DataTypes.INTEGER(10),
allowNull: false,
primaryKey: true,
autoIncrement: true
},
prov_code: {
type: DataTypes.STRING(32),
allowNull: false
},
gender_range: {
type: DataTypes.STRING(32),
allowNull: false
},
total: {
type: DataTypes.INTEGER(10),
allowNull: false,
defaultValue: '0'
},
male: {
type: DataTypes.INTEGER(11),
allowNull: false,
defaultValue: '0'
},
female: {
type: DataTypes.INTEGER(11),
allowNull: false,
defaultValue: '0'
},
create_time: {
type: DataTypes.DATE,
allowNull: true
},
update_time: {
type: DataTypes.DATE,
allowNull: true
}
}, {
tableName: 't_user_province_gender'
});
Model.associate = function() {
}
return Model;
};
/* indent size: 2 */
module.exports = app => {
const DataTypes = app.Sequelize;
const Model = app.model.define('t_user_province_increased_daily', {
id: {
type: DataTypes.INTEGER(10),
allowNull: false,
primaryKey: true,
autoIncrement: true
},
prov_code: {
type: DataTypes.STRING(32),
allowNull: false
},
day: {
type: DataTypes.DATE,
allowNull: false
},
total: {
type: DataTypes.INTEGER(10),
allowNull: false,
defaultValue: '0'
},
create_time: {
type: DataTypes.DATE,
allowNull: true
},
update_time: {
type: DataTypes.DATE,
allowNull: true
}
}, {
tableName: 't_user_province_increased_daily'
});
Model.associate = function() {
}
return Model;
};
/* indent size: 2 */
module.exports = app => {
const DataTypes = app.Sequelize;
const Model = app.model.define('t_user_province_journey_daily', {
id: {
type: DataTypes.INTEGER(10),
allowNull: false,
primaryKey: true,
autoIncrement: true
},
prov_code: {
type: DataTypes.STRING(32),
allowNull: false
},
day: {
type: DataTypes.DATE,
allowNull: false
},
journey: {
type: DataTypes.INTEGER(10),
allowNull: true
},
miles: {
type: "DOUBLE(12,2)",
allowNull: true
},
create_time: {
type: DataTypes.DATE,
allowNull: true
},
update_time: {
type: DataTypes.DATE,
allowNull: true
}
}, {
tableName: 't_user_province_journey_daily'
});
Model.associate = function() {
}
return Model;
};
/* indent size: 2 */
module.exports = app => {
const DataTypes = app.Sequelize;
const Model = app.model.define('t_user_province_renew_daily', {
id: {
type: DataTypes.INTEGER(10),
allowNull: false,
primaryKey: true,
autoIncrement: true
},
prov_code: {
type: DataTypes.STRING(32),
allowNull: false
},
day: {
type: DataTypes.DATE,
allowNull: false
},
total: {
type: DataTypes.INTEGER(10),
allowNull: false,
defaultValue: '0'
},
create_time: {
type: DataTypes.DATE,
allowNull: true
},
update_time: {
type: DataTypes.DATE,
allowNull: true
}
}, {
tableName: 't_user_province_renew_daily'
});
Model.associate = function() {
}
return Model;
};
/* indent size: 2 */
module.exports = app => {
const DataTypes = app.Sequelize;
const Model = app.model.define('t_user_summary', {
id: {
type: DataTypes.INTEGER(10),
allowNull: false,
primaryKey: true,
autoIncrement: true
},
day: {
type: DataTypes.DATE,
allowNull: false,
unique: true
},
total: {
type: DataTypes.INTEGER(10),
allowNull: true
},
create_time: {
type: DataTypes.DATE,
allowNull: true
},
update_time: {
type: DataTypes.DATE,
allowNull: true
}
}, {
tableName: 't_user_summary'
});
Model.associate = function() {
}
return Model;
};
...@@ -22,7 +22,7 @@ class UpdateCache extends Subscription { ...@@ -22,7 +22,7 @@ class UpdateCache extends Subscription {
static get schedule() { static get schedule() {
return { return {
// interval: '1m', // 定时任务 1 分钟间隔 // interval: '1m', // 定时任务 1 分钟间隔
cron: "0 33 9 * * *", // 定点任务 每天 凌晨 0点30 执行一次 cron: "0 30 6 * * *", // 定点任务 每天 凌晨 6点30 执行一次
type: "worker", // 随机 worker 执行 type: "worker", // 随机 worker 执行
immediate: true //启动立即执行一次 immediate: true //启动立即执行一次
// disable:true // 不启动 // disable:true // 不启动
...@@ -47,6 +47,7 @@ class UpdateCache extends Subscription { ...@@ -47,6 +47,7 @@ class UpdateCache extends Subscription {
if(!country['province'][prov_code]['name']){ if(!country['province'][prov_code]['name']){
country['province'][prov_code]['name']=prov_name; country['province'][prov_code]['name']=prov_name;
country['province'][prov_code]['total']= 0; country['province'][prov_code]['total']= 0;
country['province'][prov_code]['renew']= 0;
} }
country['province'][prov_code]['total'] += total; country['province'][prov_code]['total'] += total;
country['province'][prov_code]['renew'] += renew; country['province'][prov_code]['renew'] += renew;
...@@ -62,8 +63,13 @@ class UpdateCache extends Subscription { ...@@ -62,8 +63,13 @@ class UpdateCache extends Subscription {
async subscribe() { async subscribe() {
this.logger.info( "===============执行日常任务-缓存每日用户总数 begin=================="); this.logger.info( "===============执行日常任务-缓存每日用户总数 begin==================");
const yesterday = this.helper.date(-1) const yesterday = this.helper.date(-1)
const today = this.helper.date(0)
const day30before = this.helper.date(-30)
// 查询昨天 所有城市 分别 对应的 用户总数 列表 // 查询昨天 所有城市 分别 对应的 用户总数 列表
const cities = await this.UserService.user_summary_city_by_day({beginDate:yesterday}); const cities = await this.UserService.user_summary_city_by_day({beginDate:day30before,endDate:yesterday});
// 近 30天新增用户明细
// const increased30 = await this.UserService.user_increased_city_by_day({beginDate:day30before,endDate:yesterday});
console.log(cities);
if (cities.length) { if (cities.length) {
const data = this._group(cities, city=>city.prov_code); const data = this._group(cities, city=>city.prov_code);
data.date = yesterday; data.date = yesterday;
...@@ -77,10 +83,15 @@ class UpdateCache extends Subscription { ...@@ -77,10 +83,15 @@ class UpdateCache extends Subscription {
}else{ }else{
this.logger.warn("warnning=============user_summary_yesterday===========is empty!"); this.logger.warn("warnning=============user_summary_yesterday===========is empty!");
} }
this.app.cache = { //缓存日期.方便业务使用
await this.CacheService.set(
this.helper.constant.DATES,
{
today: this.helper.date(0), today: this.helper.date(0),
yesterday yesterday
}; },
2 * 24 * 60 * 60 //两天过期
);
this.logger.info("===============执行日常任务-缓存每日用户总数 end=================="); this.logger.info("===============执行日常任务-缓存每日用户总数 end==================");
} }
} }
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
* @Author: limin * @Author: limin
* @Date: 2019-11-21 14:32:51 * @Date: 2019-11-21 14:32:51
* @Last Modified by: limin * @Last Modified by: limin
* @Last Modified time: 2019-11-28 15:31:53 * @Last Modified time: 2019-11-29 09:47:18
*/ */
"use strict"; "use strict";
...@@ -19,7 +19,7 @@ class CacheService extends Service { ...@@ -19,7 +19,7 @@ class CacheService extends Service {
this.PREFIX = ctx.helper.constant.PREFIX; this.PREFIX = ctx.helper.constant.PREFIX;
} }
async set(key, value, ex = 60 * 60) { async set(key, value, ex = 60 * 60) {
this.app.redis && (await this.app.redis.set(`${this.PREFIX}${key}`, value, "EX", ex)); this.app.redis && (await this.app.redis.set(`${this.PREFIX}${key}`, this.helper.jsonStringify(value), "EX", ex));
} }
async get(key, fnGetFromDB, ex) { async get(key, fnGetFromDB, ex) {
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
* @Author: limin * @Author: limin
* @Date: 2019-11-21 11:18:37 * @Date: 2019-11-21 11:18:37
* @Last Modified by: limin * @Last Modified by: limin
* @Last Modified time: 2019-11-28 18:19:53 * @Last Modified time: 2019-11-29 17:53:54
*/ */
const Service = require("egg").Service; const Service = require("egg").Service;
...@@ -19,31 +19,23 @@ class UserService extends Service { ...@@ -19,31 +19,23 @@ class UserService extends Service {
*/ */
_reg_models_alias() { _reg_models_alias() {
const { const {
TUserCityActiveDaily,
TUserCityIncreasedDaily,
TUserCityRenewDaily,
TUserCityGender, TUserCityGender,
TUserCityCarbrand, TUserCityCarbrand,
TUserCityCarprice, TUserCityCarprice,
TUserCity, TUserCityDaily,
TUserCityAverageMilesDay, TUserCityAverageMilesDay,
TUserCityAverageDriveMonth, TUserCityAverageDriveMonth,
TUserCityJourneyDaily, TUserCityRealtime,
TUserSummary,
TProvince, TProvince,
TCity TCity
} = this.app.model; } = this.app.model;
this.MCityActive = TUserCityActiveDaily; this.MUserRealTime = TUserCityRealtime;
this.MCityIncreased = TUserCityIncreasedDaily; this.MUserGender = TUserCityGender;
this.MCityRenew = TUserCityRenewDaily; this.MUserCarBrand = TUserCityCarbrand;
this.MCityGender = TUserCityGender; this.MUserCarPrice = TUserCityCarprice;
this.MCityCarBrand = TUserCityCarbrand; this.MUserDaily = TUserCityDaily;
this.MCityCarPrice = TUserCityCarprice; this.MUserMilesDaily = TUserCityAverageMilesDay;
this.MUserCity = TUserCity; this.MUserDriveDaysMonth = TUserCityAverageDriveMonth;
this.MCityMilesDaily = TUserCityAverageMilesDay;
this.MCityDriveDaysMonth = TUserCityAverageDriveMonth;
this.MCityJourney = TUserCityJourneyDaily;
this.MUserSummary = TUserSummary;
this.MProvice = TProvince; this.MProvice = TProvince;
this.MCity = TCity; this.MCity = TCity;
} }
...@@ -52,15 +44,11 @@ class UserService extends Service { ...@@ -52,15 +44,11 @@ class UserService extends Service {
* 绑定表关系 * 绑定表关系
*/ */
_associate() { _associate() {
this.MUserCity.belongsTo(this.MProvice, { this.MUserDaily.belongsTo(this.MProvice, {
foreignKey: "prov_code", foreignKey: "prov_code",
targetKey: "prov_code" targetKey: "prov_code"
}); });
this.MUserCity.belongsTo(this.MCity, { this.MUserDaily.belongsTo(this.MCity, {
foreignKey: "city_code",
targetKey: "city_code"
});
this.MUserCity.belongsTo(this.MCityRenew, {
foreignKey: "city_code", foreignKey: "city_code",
targetKey: "city_code" targetKey: "city_code"
}); });
...@@ -92,11 +80,11 @@ class UserService extends Service { ...@@ -92,11 +80,11 @@ class UserService extends Service {
* @param field {String} * @param field {String}
* @param value {String} * @param value {String}
* @return {Promise.<boolean>} * @return {Promise.<boolean>}
* @Description 近N天 新增用户数 明细 * @Description 近N天 用户活跃度
*/ */
async last_n_day_user_increased_detail(pid, n) { async last_n_day_user_lively({pid,cid,n}) {
const option = this.helper.seqOption({ n, pid }); const option = this.helper.seqOption({pid,cid,n});
const data = await this.MCityIncreased.findAll(option); const data = await this.MCityActive.sum("total", option);
return data; return data;
} }
...@@ -104,11 +92,11 @@ class UserService extends Service { ...@@ -104,11 +92,11 @@ class UserService extends Service {
* @param field {String} * @param field {String}
* @param value {String} * @param value {String}
* @return {Promise.<boolean>} * @return {Promise.<boolean>}
* @Description 近N天 用户活跃度 * @Description 获取实时在线人数
*/ */
async last_n_day_user_lively({pid,cid,n}) { async online_total_reletime({pid,cid}) {
const option = this.helper.seqOption({pid,cid,n}); const option = this.helper.seqOption({pid,cid,n:0});
const data = await this.MCityActive.sum("total", option); const data = await this.MCityOnline.sum("total", option);
return data; return data;
} }
...@@ -163,7 +151,7 @@ class UserService extends Service { ...@@ -163,7 +151,7 @@ class UserService extends Service {
limit, limit,
order: [["brand_cars", "DESC"]] order: [["brand_cars", "DESC"]]
}); });
const data = await this.MCityCarBrand.findAll(option); const data = await this.MUserCarBrand.findAll(option);
return data; return data;
} }
...@@ -175,7 +163,7 @@ class UserService extends Service { ...@@ -175,7 +163,7 @@ class UserService extends Service {
*/ */
async carprice(pid) { async carprice(pid) {
const option = this.helper.seqOption({ pid }); const option = this.helper.seqOption({ pid });
const data = await this.MCityCarPrice.findAll(option); const data = await this.MUserCarPrice.findAll(option);
return data; return data;
} }
...@@ -203,7 +191,7 @@ class UserService extends Service { ...@@ -203,7 +191,7 @@ class UserService extends Service {
*/ */
async milesdaily(pid) { async milesdaily(pid) {
const option = this.helper.seqOption({ pid }); const option = this.helper.seqOption({ pid });
const data = await this.MCityMilesDaily.findAll(option); const data = await this.MUserMilesDaily.findAll(option);
return data; return data;
} }
/** /**
...@@ -214,7 +202,7 @@ class UserService extends Service { ...@@ -214,7 +202,7 @@ class UserService extends Service {
*/ */
async drivedays(pid) { async drivedays(pid) {
const option = this.helper.seqOption({ pid }); const option = this.helper.seqOption({ pid });
const data = await this.MCityDriveDaysMonth.findAll(option); const data = await this.MUserDriveDaysMonth.findAll(option);
return data; return data;
} }
...@@ -237,7 +225,7 @@ class UserService extends Service { ...@@ -237,7 +225,7 @@ class UserService extends Service {
* @Description 获取某生某天用户总数 不传pid 统计所有 * @Description 获取某生某天用户总数 不传pid 统计所有
*/ */
async user_summary_provice_by_day(beginDate, endDate, pid) { async user_summary_provice_by_day(beginDate, endDate, pid) {
const { app, helper, MProvice, MUserCity } = this; const { app, helper, MProvice, MUserDaily: MUserCity } = this;
const { fn, col } = app.Sequelize; const { fn, col } = app.Sequelize;
var option = helper.seqOption({ beginDate, endDate, pid }); var option = helper.seqOption({ beginDate, endDate, pid });
Object.assign(option, { Object.assign(option, {
...@@ -262,19 +250,25 @@ class UserService extends Service { ...@@ -262,19 +250,25 @@ class UserService extends Service {
* @Description 获取某天城市用户总数列表 * @Description 获取某天城市用户总数列表
*/ */
async user_summary_city_by_day({beginDate, endDate, pid, cid}) { async user_summary_city_by_day({beginDate, endDate, pid, cid}) {
const { app, helper, MUserCity, MCity, MProvice, MCityRenew } = this; const { app, helper, MUserDaily: MUserCity, MCity, MProvice } = this;
const { col } = app.Sequelize; const { col } = app.Sequelize;
var option = helper.seqOption({ beginDate, endDate, pid, cid }); var option = helper.seqOption({ beginDate, endDate, pid, cid });
Object.assign(option, { Object.assign(option, {
attributes: [ attributes: [
"city_code", "city_code",
'prov_code', 'prov_code',
"total", "total_user",
[col("t_user_city_renew_daily.total"),'renew'], 'renew',
'active',
'increase',
'journey',
'miles',
'online_time',
'day',
col("t_city.city_name"), col("t_city.city_name"),
col("t_province.prov_name") col("t_province.prov_name")
], ],
include: [{ model: MCity, attributes: [] },{ model: MProvice, attributes: []},{ model: MCityRenew, attributes: []}], include: [{ model: MCity, attributes: [] },{ model: MProvice, attributes: []}],
raw: true raw: true
}); });
const data = await MUserCity.findAll(option); const data = await MUserCity.findAll(option);
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!