Commit 4a826dbe by liimin

修改表

1 parent 534a0c9f
......@@ -51,7 +51,7 @@ npm install -g egg-sequelize-auto mysql2
-e 数据库类型
-a json定义文件路径,可以追加一些自定义配置,如{"timestamps": false}
- create model: $ egg-sequelize-auto -o "./app/model" -d test -h 192.168.0.39 -u root -p 3306 -x 123456 -t usedrow -e mysql
- create model: $ egg-sequelize-auto -o "./app/model" -d glsx_data_screen -h 192.168.0.39 -u root -p 3306 -x 123456 -t usedrow -e mysql
- 不指定-t 为所有表
-- shell
......
......@@ -2,11 +2,11 @@
* @Author: limin
* @Date: 2019-11-21 11:20:15
* @Last Modified by: limin
* @Last Modified time: 2019-11-29 09:43:28
* @Last Modified time: 2019-12-02 12:16:54
*/
module.exports = {
PREFIX: '_glsx_data_screen_',
USER_SUMMARY_YESTERDAY:'user_summary_yesterday',
USER_SUMMARY_30_DAY:'user_summary_30_day',
DATES:'dates'
};
\ No newline at end of file
......@@ -2,7 +2,7 @@
* @Author: limin
* @Date: 2019-11-21 11:20:01
* @Last Modified by: limin
* @Last Modified time: 2019-11-29 14:12:17
* @Last Modified time: 2019-12-02 18:10:30
*/
"use strict";
......@@ -24,28 +24,45 @@ class UserController extends Controller {
this.ServerResponseCode = ctx.response.ServerResponseCode;
this.ServerResponse = ctx.response.ServerResponse;
}
async _fetch_date_in_cache(){
const { DATES } = this.helper.constant;
return await this.CacheService.get(DATES);
}
/**
* 内部方法 从内存获取 昨天的统计数据
* 比较数据时间 如果不是昨天的数据 则启动定时任务 更新一遍数据 再次获取数据并返回
*/
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);
async _fetch_summary( arg ){
const { USER_SUMMARY_30_DAY } = this.helper.constant;
var summary_30_day = await this.CacheService.get(USER_SUMMARY_30_DAY);
const { date } = summary_30_day;
const { yesterday } = await this._fetch_date_in_cache();
if(date !== yesterday){ //如果 内存中的数据的日期不是昨天的 则手动启动定时器 更新缓存数据
await this.app.runSchedule('update_cache');
yesterday_total = await this.CacheService.get(USER_SUMMARY_YESTERDAY);
summary_30_day = await this.CacheService.get(USER_SUMMARY_30_DAY);
}
var result=null;
if(arg){
const { beginDate,endDate } = arg
result = []
for(const day in summary_30_day){
if(day >= +beginDate && day <= +endDate ){
result.push(summary_30_day[day])
}
}
}else{
result = summary_30_day[date]
}
return yesterday_total
return result
}
/**
* 内部方法 从内存获取 昨天的统计数据
* 内部方法 从内存获取 最后一天的统计数据
*/
async _fetch_total({ pid, cid , field } ){
const _summary = await this._fetch_summary_yesterday();
var { province } =_summary;
async _fetch_total_last_day({ pid, cid , field } ){
const _summary = await this._fetch_summary();
var { p } =_summary;
var total= 0;
function _set(target){
var total;
......@@ -60,28 +77,35 @@ class UserController extends Controller {
return total;
}
if (pid) {
var obj = cid ? province[pid]["city"][cid] : province[pid];
var obj = cid ? p[pid]["c"][cid] : p[pid];
obj && (total =_set(obj));
}else{
total = _set(_summary)
}
return total;
}
_reduce(obj,field){
return obj.reduce((pre,next)=>pre + +next[field],0)
}
/**
* 内部方法 从内存获取 范围时间内的数据
*/
async _fetch_total_by_range({ pid, cid , field, beginDate,endDate } ){
const _list = await this._fetch_summary({ beginDate, endDate });
console.log(_list);
}
/**
* @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;
// console.log(this.app.cache.today);
// `user_summary_${day}`
// const data = await this.CacheService.get("test", () =>
// this.DemoService.hello(this.request.body)
// );
// const statics = await this.UserService.total(pid);
var total = await this._fetch_total_last_day({ pid,cid,field:'u'})
const realtime = await this.UserService.user_realtime({ pid, cid });
const realtime_total = this._reduce(realtime,'increase');
total += ~~realtime_total;
const data = { total };
this.ctx.body = this.ServerResponse.Success(data);
}
......@@ -90,10 +114,11 @@ class UserController extends Controller {
* @Description 数据大屏-数据统计 1. 近七天新增用户数 2.近七天用户活跃度 3.近30天用户留存率 4.用户付费转化率
* @Request query integer pid 省份id
*/
async statistics({ query: { pid, n = -7,cid } }) {
const data = await this.UserService.last_n_day_user_increased_detail({ n, pid, cid });
// //近7天新增用户数
// const increased7 = await this.UserService.last_n_day_user_increased({ n, pid, cid });
async statistics({ query: { pid,cid } }) {
const beginDate7 = this.helper.date(-7,'YYYYMMDD');
const { yesterday } = await this._fetch_date_in_cache();
//近7天新增用户数
const increased7 = await this._fetch_total_by_range({ pid, cid, field:'i',beginDate:beginDate7,endDate:yesterday});
// //近七天 活跃用户数
// const nLive7 = await this.UserService.last_n_day_user_lively({ n, pid, cid });
// // 近七天 用户活跃度 (近N天 活跃用户数 / 近N天新增用户数)
......@@ -109,6 +134,7 @@ class UserController extends Controller {
// // 昨天的付费转化率 (昨天的付费总人数 / 昨天用户总数)
// const renew_yesterday = this.helper.percent(renew,total);
// const data = { increased7, live7,increased30,live30,renew_yesterday };
const data = { increased7 };
this.ctx.body = this.ServerResponse.Success(data);
}
......
......@@ -2,7 +2,7 @@
* @Author: limin
* @Date: 2019-11-21 11:19:43
* @Last Modified by: limin
* @Last Modified time: 2019-11-29 11:38:08
* @Last Modified time: 2019-12-02 12:15:42
*/
const moment = require("moment");
......
/* indent size: 2 */
module.exports = app => {
const DataTypes = app.Sequelize;
const Model = app.model.define('t_user_city_average_miles_day', {
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
},
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_city_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_city_carprice', {
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
},
price_range: {
type: DataTypes.STRING(32),
allowNull: false
},
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_city_carprice'
});
Model.associate = function() {
}
return Model;
};
......@@ -3,7 +3,7 @@
module.exports = app => {
const DataTypes = app.Sequelize;
const Model = app.model.define('t_user_city_average_drive_month', {
const Model = app.model.define('t_user_city_drive_info', {
id: {
type: DataTypes.INTEGER(10),
allowNull: false,
......@@ -16,17 +16,70 @@ module.exports = app => {
},
city_code: {
type: DataTypes.STRING(32),
allowNull: false
allowNull: false,
unique: true
},
days_range: {
type: DataTypes.STRING(32),
allowNull: false
total: {
type: DataTypes.INTEGER(10),
allowNull: true
},
d_0_5: {
type: DataTypes.INTEGER(10),
allowNull: true
},
d_5_10: {
type: DataTypes.INTEGER(10),
allowNull: true
},
d_10_15: {
type: DataTypes.INTEGER(10),
allowNull: true
},
d_15_20: {
type: DataTypes.INTEGER(10),
allowNull: true
},
d_20_25: {
type: DataTypes.INTEGER(10),
allowNull: true
},
d_25_31: {
type: DataTypes.INTEGER(10),
allowNull: true
},
km_0_40: {
type: DataTypes.INTEGER(10),
allowNull: true
},
km_40_70: {
type: DataTypes.INTEGER(10),
allowNull: true
},
km_70_100: {
type: DataTypes.INTEGER(10),
allowNull: true
},
km_100_: {
type: DataTypes.INTEGER(10),
allowNull: true
},
w_0_10: {
type: DataTypes.INTEGER(10),
allowNull: true
},
w_10_20: {
type: DataTypes.INTEGER(10),
allowNull: true
},
w_20_30: {
type: DataTypes.INTEGER(10),
allowNull: true
},
total_users: {
w_30_50: {
type: DataTypes.INTEGER(10),
allowNull: true
},
range_users: {
w_50_: {
type: DataTypes.INTEGER(10),
allowNull: true
},
......@@ -39,7 +92,7 @@ module.exports = app => {
allowNull: true
}
}, {
tableName: 't_user_city_average_drive_month'
tableName: 't_user_city_drive_info'
});
Model.associate = function() {
......
......@@ -30,64 +30,84 @@ class UpdateCache extends Subscription {
}
_group(cities, f) {
const country = {
total:0,
date:'',
renew:0,
province:{}
const data = {
date:'20000101',
};
function __push(target,obj){
target =target || {}
if(obj){
const {r,u,a,i,j,m,o} = obj;
target['r'] += +r;//续费 renew
target['u'] += +u;//用户总数 user
target['a'] += +a;// 活跃用户数 active
target['i'] += +i;// 新增用户数 increase
target['j'] += +j;// 出行 journey
target['m'] += +m;// 里程 miles
target['o'] += +o;// online 在线时间
}else{
target['r'] = 0;//续费 renew
target['u'] = 0;//用户总数 user
target['a'] = 0;// 活跃用户数 active
target['i'] = 0;// 新增用户数 increase
target['j'] = 0;// 出行 journey
target['m'] = 0.00;// 里程 miles
target['o'] = 0;// online 在线时间
}
return target;
};
cities.forEach(function (city) {
const prov_code = f(city);
let { city_code,total,city_name,prov_name,renew } = city
renew = +renew;
total = +total;
country.total += total;
country.renew += renew;
country['province'][prov_code] = country['province'][prov_code] || {}
if(!country['province'][prov_code]['name']){
country['province'][prov_code]['name']=prov_name;
country['province'][prov_code]['total']= 0;
country['province'][prov_code]['renew']= 0;
let { city_code,city_name,prov_name,total_user:u,renew:r,active:a,increase:i,journey:j,miles:m,online_time:o,day } = city
day = day.replace(/-/g,'');
data.date=Math.max(data.date,day)
if(!data[day]){
data[day] = {
p:{} //省 province
}
data[day] = __push(data[day])
}
data[day] = __push(data[day],{r,u,a,i,j,m,o})
data[day]['p'][prov_code] = data[day]['p'][prov_code] || {}
if(typeof data[day]['p'][prov_code]['u'] === 'undefined'){
data[day]['p'][prov_code] = __push(data[day]['p'][prov_code])
}
data[day]['p'][prov_code] = __push(data[day]['p'][prov_code],{r,u,a,i,j,m,o})
if(!data[day]['p'][prov_code]['c']){
data[day]['p'][prov_code]['c']={}
}
country['province'][prov_code]['total'] += total;
country['province'][prov_code]['renew'] += renew;
// country['province'][prov_code]['city'] = country['province'][prov_code]['city'] || [];
// country['province'][prov_code]['city'].push({ city_code,total,city_name});
country['province'][prov_code]['city'] = country['province'][prov_code]['city'] || {};
country['province'][prov_code]['city'][city_code]={ total,city_name,renew };
data[day]['p'][prov_code]['c'][city_code]={ u,r,a,i,j,m,o };
});
return country
return data
}
// subscribe 是真正定时任务执行时被运行的函数
async subscribe() {
this.logger.info( "===============执行日常任务-缓存每日用户总数 begin==================");
const yesterday = this.helper.date(-1)
const today = this.helper.date(0)
const _f = 'YYYYMMDD'
const yesterday = this.helper.date(-1,_f)
const today = this.helper.date(0,_f)
const day30before = this.helper.date(-30)
// 查询昨天 所有城市 分别 对应的 用户总数 列表
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) {
const data = this._group(cities, city=>city.prov_code);
data.date = yesterday;
const value = JSON.stringify(data);
await this.CacheService.set(
this.helper.constant.USER_SUMMARY_YESTERDAY,
this.helper.constant.USER_SUMMARY_30_DAY,
value,
2 * 24 * 60 * 60 //两天过期
);
this.logger.info("user_summary_%s:%s",yesterday,value);
this.logger.info("user_summary_%s:%s",data.date,value);
}else{
this.logger.warn("warnning=============user_summary_yesterday===========is empty!");
this.logger.warn("warnning=============user_summary_30_day===========is empty!");
}
//缓存日期.方便业务使用
await this.CacheService.set(
this.helper.constant.DATES,
{
today: this.helper.date(0),
today,
yesterday
},
2 * 24 * 60 * 60 //两天过期
......
......@@ -2,7 +2,7 @@
* @Author: limin
* @Date: 2019-11-21 11:18:37
* @Last Modified by: limin
* @Last Modified time: 2019-11-29 17:53:54
* @Last Modified time: 2019-12-02 16:39:04
*/
const Service = require("egg").Service;
......@@ -21,10 +21,8 @@ class UserService extends Service {
const {
TUserCityGender,
TUserCityCarbrand,
TUserCityCarprice,
TUserCityDaily,
TUserCityAverageMilesDay,
TUserCityAverageDriveMonth,
TUserCityDriveInfo,
TUserCityRealtime,
TProvince,
TCity
......@@ -32,10 +30,8 @@ class UserService extends Service {
this.MUserRealTime = TUserCityRealtime;
this.MUserGender = TUserCityGender;
this.MUserCarBrand = TUserCityCarbrand;
this.MUserCarPrice = TUserCityCarprice;
this.MUserDaily = TUserCityDaily;
this.MUserMilesDaily = TUserCityAverageMilesDay;
this.MUserDriveDaysMonth = TUserCityAverageDriveMonth;
this.MUserDriveInfo = TUserCityDriveInfo;
this.MProvice = TProvince;
this.MCity = TCity;
}
......@@ -52,6 +48,14 @@ class UserService extends Service {
foreignKey: "city_code",
targetKey: "city_code"
});
this.MUserRealTime.belongsTo(this.MProvice, {
foreignKey: "prov_code",
targetKey: "prov_code"
});
this.MUserRealTime.belongsTo(this.MCity, {
foreignKey: "city_code",
targetKey: "city_code"
});
}
/**
......@@ -243,6 +247,16 @@ class UserService extends Service {
const data = await MUserCity.findAll(option);
return data;
}
/**
* @param field {String}
* @param value {String}
......@@ -263,7 +277,7 @@ class UserService extends Service {
'increase',
'journey',
'miles',
'online_time',
"online_time",
'day',
col("t_city.city_name"),
col("t_province.prov_name")
......@@ -274,6 +288,35 @@ class UserService extends Service {
const data = await MUserCity.findAll(option);
return data;
}
/**
* @param field {String}
* @param value {String}
* @return {Promise.<boolean>}
* @Description 获取实时数据(详细)
*/
async user_realtime({pid, cid}) {
const { app, helper, MUserRealTime, MCity, MProvice } = this;
const { col } = app.Sequelize;
var option = helper.seqOption({ pid, cid });
Object.assign(option, {
attributes: [
"city_code",
'prov_code',
"online_total",
'renew',
'active',
'increase',
"online_time",
col("t_city.city_name"),
col("t_province.prov_name")
],
include: [{ model: MCity, attributes: [] },{ model: MProvice, attributes: []}],
raw: true
});
const data = await MUserRealTime.findAll(option);
return data;
}
}
module.exports = UserService;
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!