Commit 580b0fec by liimin

完善接口

1 parent 4a826dbe
...@@ -2,59 +2,81 @@ ...@@ -2,59 +2,81 @@
* @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-12-02 12:15:42 * @Last Modified time: 2019-12-03 16:11:52
*/ */
const moment = require("moment"); const moment = require("moment");
const constant = require("../common/constant"); const constant = require("../common/constant");
/*
* string.replaceAll()---字符替换所有指定字符
* @parameter: FindText--原文本要替换值
* @parameter: RepText--将要替换值
*/
String.prototype.replaceAll = function (from, to) {
var regExp = new RegExp(from, "g");
return this.replace(regExp, to);
}
// exports.relativeTime = time => moment(new Date(time * 1000)).fromNow(); // exports.relativeTime = time => moment(new Date(time * 1000)).fromNow();
exports.date = (n, f = "YYYY-MM-DD") =>moment().add(n, "days").format(f); exports.date = (n, f = "YYYY-MM-DD") =>
moment()
.add(n, "days")
.format(f);
/** /**
* 结束日期格式化函数 * 结束日期格式化函数
*/ */
exports.formatEndDate =end=>`${end} 23:59:59`; exports.formatEndDate = end => `${end} 23:59:59`;
/** /**
* 字符串转json 如果是json字符串 返回json对象 如果不是JSON字符串 返回原始字符串 * 字符串转json 如果是json字符串 返回json对象 如果不是JSON字符串 返回原始字符串
*/ */
exports.jsonParser =str=>{ exports.jsonParser = str => {
var res; var res;
try { try {
res = JSON.parse(str) res = JSON.parse(str);
} catch (error) { } catch (error) {
res = str res = str;
} }
return res return res;
} };
/** /**
* json对象转字符串 * json对象转字符串
*/ */
exports.jsonStringify =obj=>{ exports.jsonStringify = obj => {
var res; var res;
if(typeof obj === 'string'){ if (typeof obj === "string") {
return obj return obj;
} }
try { try {
res = JSON.stringify(obj) res = JSON.stringify(obj);
} catch (error) { } catch (error) {
res = obj res = obj;
} }
return res return res;
} };
/** /**
* *
*/ */
exports.percent=(num,total,fixed =2 )=>((~~num / ~~total) * 100).toFixed(fixed)+ "%"; 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
}) {
let where = {}; let where = {};
const { between } = this.ctx.app.Sequelize.Op; const { between } = this.ctx.app.Sequelize.Op;
if (cid) { if (cid) {
where.city_code = cid; where.city_code = cid;
}else if (pid){ } else if (pid) {
where.prov_code = pid; where.prov_code = pid;
} }
if (day) { if (day) {
...@@ -70,12 +92,12 @@ exports.seqOption = function({ pid,cid, n, limit, order, day,beginDate,endDate } ...@@ -70,12 +92,12 @@ exports.seqOption = function({ pid,cid, n, limit, order, day,beginDate,endDate }
where.day = { where.day = {
[between]: [this.date(0), this.formatEndDate(this.date(0))] [between]: [this.date(0), this.formatEndDate(this.date(0))]
}; };
} else if(beginDate){ } else if (beginDate) {
if(!endDate){ if (!endDate) {
endDate = beginDate endDate = beginDate;
} }
if(!endDate.includes(':')){ if (!endDate.includes(":")) {
endDate = this.formatEndDate(endDate) endDate = this.formatEndDate(endDate);
} }
where.day = { where.day = {
[between]: [beginDate, endDate] [between]: [beginDate, endDate]
...@@ -85,7 +107,7 @@ exports.seqOption = function({ pid,cid, n, limit, order, day,beginDate,endDate } ...@@ -85,7 +107,7 @@ exports.seqOption = function({ pid,cid, n, limit, order, day,beginDate,endDate }
where where
}; };
if (limit) { if (limit) {
option.limit = limit; option.limit = +limit;
} }
if (order) { if (order) {
option.order = order; option.order = order;
......
...@@ -2,20 +2,14 @@ ...@@ -2,20 +2,14 @@
* @Author: limin * @Author: limin
* @Date: 2019-11-21 11:18:45 * @Date: 2019-11-21 11:18:45
* @Last Modified by: limin * @Last Modified by: limin
* @Last Modified time: 2019-11-21 17:13:29 * @Last Modified time: 2019-12-03 16:52:27
*/ */
module.exports = app => { module.exports = app => {
app.router.get('/screen/user/total', app.controller.screen.user.total); app.router.get('/screen/user/total', app.controller.screen.user.total);
app.router.get('/screen/user/statistics', app.controller.screen.user.statistics); app.router.get('/screen/user/statistics', app.controller.screen.user.statistics);
app.router.get('/screen/user/increase',app.controller.screen.user.increase); app.router.get('/screen/user/gender', app.controller.screen.user.gender);
app.router.get('/screen/user/lively',app.controller.screen.user.lively); app.router.get('/screen/user/city_range', app.controller.screen.user.city_range);
app.router.get('/screen/user/renew',app.controller.screen.user.renew); app.router.get('/screen/user/car_brand_range', app.controller.screen.user.car_brand_range);
app.router.get('/screen/user/journey',app.controller.screen.user.journey); app.router.get('/screen/user/drive_range', app.controller.screen.user.drive_range);
app.router.get('/screen/user/analysis',app.controller.screen.user.analysis);
app.router.get('/screen/user/carbrand',app.controller.screen.user.carbrand);
app.router.get('/screen/user/carprice',app.controller.screen.user.carprice);
app.router.get('/screen/user/city',app.controller.screen.user.city);
app.router.get('/screen/user/milesdaily',app.controller.screen.user.milesdaily);
app.router.get('/screen/user/drivedays',app.controller.screen.user.drivedays);
}; };
\ No newline at end of file \ No newline at end of file
...@@ -58,7 +58,7 @@ class UpdateCache extends Subscription { ...@@ -58,7 +58,7 @@ class UpdateCache extends Subscription {
cities.forEach(function (city) { cities.forEach(function (city) {
const prov_code = f(city); const prov_code = f(city);
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 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,''); day = day.replaceAll('-','');
data.date=Math.max(data.date,day) data.date=Math.max(data.date,day)
if(!data[day]){ if(!data[day]){
data[day] = { data[day] = {
...@@ -82,15 +82,12 @@ class UpdateCache extends Subscription { ...@@ -82,15 +82,12 @@ class UpdateCache extends Subscription {
// subscribe 是真正定时任务执行时被运行的函数 // subscribe 是真正定时任务执行时被运行的函数
async subscribe() { async subscribe() {
this.logger.info( "===============执行日常任务-缓存每日用户总数 begin=================="); this.logger.info( "===============执行日常任务-缓存每日统计数据(总30天) begin==================");
const _f = 'YYYYMMDD' const yesterday = this.helper.date(-1);
const yesterday = this.helper.date(-1,_f) const today = this.helper.date(0);
const today = this.helper.date(0,_f) const day30before = this.helper.date(-30);
const day30before = this.helper.date(-30)
// 查询昨天 所有城市 分别 对应的 用户总数 列表 // 查询昨天 所有城市 分别 对应的 用户总数 列表
const cities = await this.UserService.user_summary_city_by_day({beginDate:day30before,endDate: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});
if (cities.length) { if (cities.length) {
const data = this._group(cities, city=>city.prov_code); const data = this._group(cities, city=>city.prov_code);
const value = JSON.stringify(data); const value = JSON.stringify(data);
...@@ -112,7 +109,7 @@ class UpdateCache extends Subscription { ...@@ -112,7 +109,7 @@ class UpdateCache extends Subscription {
}, },
2 * 24 * 60 * 60 //两天过期 2 * 24 * 60 * 60 //两天过期
); );
this.logger.info("===============执行日常任务-缓存每日用户总数 end=================="); this.logger.info("===============执行日常任务-缓存每日统计数据(总30天) end==================");
} }
} }
......
...@@ -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-12-02 16:39:04 * @Last Modified time: 2019-12-03 17:06:34
*/ */
const Service = require("egg").Service; const Service = require("egg").Service;
...@@ -62,112 +62,26 @@ class UserService extends Service { ...@@ -62,112 +62,26 @@ class UserService extends Service {
* @param field {String} * @param field {String}
* @param value {String} * @param value {String}
* @return {Promise.<boolean>} * @return {Promise.<boolean>}
* @Description 用户城市排行
*/ */
async total(pid) { async city_range({ pid, limit }) {
const option = this.helper.seqOption({ pid }); const { app, helper, MCity, MUserDaily } = this;
const data = await this.MCityActive.sum("total", option); const { col } = app.Sequelize;
return data; const option = helper.seqOption({
}
/**
* @param field {String}
* @param value {String}
* @return {Promise.<boolean>}
* @Description 近N天 新增用户数
*/
async last_n_day_user_increased({pid,cid,n}) {
const option = this.helper.seqOption({ n, pid,cid });
const data = await this.MCityIncreased.sum("total", option);
return data;
}
/**
* @param field {String}
* @param value {String}
* @return {Promise.<boolean>}
* @Description 近N天 用户活跃度
*/
async last_n_day_user_lively({pid,cid,n}) {
const option = this.helper.seqOption({pid,cid,n});
const data = await this.MCityActive.sum("total", option);
return data;
}
/**
* @param field {String}
* @param value {String}
* @return {Promise.<boolean>}
* @Description 获取实时在线人数
*/
async online_total_reletime({pid,cid}) {
const option = this.helper.seqOption({pid,cid,n:0});
const data = await this.MCityOnline.sum("total", option);
return data;
}
/**
* @param field {String}
* @param value {String}
* @return {Promise.<boolean>}
* @Description 近N天 活跃 明细
*/
async last_n_day_user_lively_detail(pid, n) {
const option = this.helper.seqOption({ n, pid });
const data = await this.MCityActive.findAll(option);
return data;
}
/**
* @param field {String}
* @param value {String}
* @return {Promise.<boolean>}
* @Description 近N天 付费转换 明细
*/
async last_n_day_user_renew_detail(pid, n) {
const option = this.helper.seqOption({ n, pid });
const data = await this.MCityRenew.findAll(option);
return data;
}
/**
* @param field {String}
* @param value {String}
* @return {Promise.<boolean>}
* @Description 用户分析
*/
async analysis(pid) {
// let where = {}
// if(pid){
// where.prov_code = pid;
// }
// const data = await this.ModelGender.sum('total',{ where,group:['gender_range','']});
// return data;
}
/**
* @param field {String}
* @param value {String}
* @return {Promise.<boolean>}
* @Description 用户车辆品牌分布
*/
async carbrand(pid, limit) {
const option = this.helper.seqOption({
pid, pid,
limit, limit,
order: [["brand_cars", "DESC"]] order: [["total_user", "DESC"]]
}); });
const data = await this.MUserCarBrand.findAll(option);
return data;
}
/** Object.assign(option, {
* @param field {String} attributes: [
* @param value {String} col("total_user",'total'),
* @return {Promise.<boolean>} col("t_city.city_name",'city')
* @Description 用户车辆价格分布 ],
*/ include: { model: MCity, attributes: [] },
async carprice(pid) { raw: true
const option = this.helper.seqOption({ pid }); });
const data = await this.MUserCarPrice.findAll(option); const data = await MUserDaily.findAll(option);
return data; return data;
} }
...@@ -175,15 +89,26 @@ class UserService extends Service { ...@@ -175,15 +89,26 @@ class UserService extends Service {
* @param field {String} * @param field {String}
* @param value {String} * @param value {String}
* @return {Promise.<boolean>} * @return {Promise.<boolean>}
* @Description 用户城市排行 * @Description 用户车辆品牌分布
*/ */
async city(pid, limit) { async car_brand_range({ pid, cid, limit }) {
const option = this.helper.seqOption({ const { app, helper, MUserCarBrand } = this;
const { fn, col } = app.Sequelize;
const option = helper.seqOption({
pid, pid,
cid,
limit, limit,
order: [["brand_cars", "DESC"]] order: [["total", "DESC"]]
});
Object.assign(option, {
attributes: [
col("brand_name",'brand'),
[fn("SUM", col("brand_cars")), "total"],
],
group:'brand_name',
raw: true
}); });
const data = await this.MCity.findAll(option); const data = await MUserCarBrand.findAll(option);
return data; return data;
} }
...@@ -193,70 +118,34 @@ class UserService extends Service { ...@@ -193,70 +118,34 @@ class UserService extends Service {
* @return {Promise.<boolean>} * @return {Promise.<boolean>}
* @Description 用户日均里程分布 * @Description 用户日均里程分布
*/ */
async milesdaily(pid) { async drive_range({ pid, cid }) {
const option = this.helper.seqOption({ pid }); const { app, helper, MUserDriveInfo } = this;
const data = await this.MUserMilesDaily.findAll(option); const option = helper.seqOption({ pid, cid });
return data;
}
/**
* @param field {String}
* @param value {String}
* @return {Promise.<boolean>}
* @Description 用户日均里程分布
*/
async drivedays(pid) {
const option = this.helper.seqOption({ pid });
const data = await this.MUserDriveDaysMonth.findAll(option);
return data;
}
/**
* @param field {String}
* @param value {String}
* @return {Promise.<boolean>}
* @Description 近N天 用户出行明细
*/
async last_n_day_user_journey_detail(pid, n) {
const option = this.helper.seqOption({ pid, n });
const data = await this.MCityJourney.findAll(option);
return data;
}
/**
* @param field {String}
* @param value {String}
* @return {Promise.<boolean>}
* @Description 获取某生某天用户总数 不传pid 统计所有
*/
async user_summary_provice_by_day(beginDate, endDate, pid) {
const { app, helper, MProvice, MUserDaily: MUserCity } = this;
const { fn, col } = app.Sequelize; const { fn, col } = app.Sequelize;
var option = helper.seqOption({ beginDate, endDate, pid });
Object.assign(option, { Object.assign(option, {
attributes: [ attributes: [
"prov_code", [fn("SUM", col("d_0_5")),'d_0_5'],
"day", [fn("SUM", col("d_5_10")),'d_5_10'],
[fn("SUM", col("total")), "total"], [fn("SUM", col("d_10_15")),'d_10_15'],
col("t_province.prov_name") [fn("SUM", col("d_15_20")),'d_15_20'],
[fn("SUM", col("d_20_25")),'d_20_25'],
[fn("SUM", col("d_25_31")),'d_25_31'],
[fn("SUM", col("km_0_40")),'km_0_40'],
[fn("SUM", col("km_40_70")),'km_40_70'],
[fn("SUM", col("km_70_100")),'km_70_100'],
[fn("SUM", col("km_100_")),'km_100_'],
[fn("SUM", col("w_0_10")),'w_0_10'],
[fn("SUM", col("w_10_20")),'w_10_20'],
[fn("SUM", col("w_20_30")),'w_20_30'],
[fn("SUM", col("w_30_50")),'w_30_50'],
[fn("SUM", col("w_50_")),'w_50_']
], ],
group: ["prov_code", "day"],
include: { model: MProvice, attributes: [] },
rollup:true,
raw: true raw: true
}); });
const data = await MUserCity.findAll(option); const data = await MUserDriveInfo.findAll(option);
return data; return data;
} }
/** /**
* @param field {String} * @param field {String}
* @param value {String} * @param value {String}
...@@ -317,6 +206,30 @@ class UserService extends Service { ...@@ -317,6 +206,30 @@ class UserService extends Service {
const data = await MUserRealTime.findAll(option); const data = await MUserRealTime.findAll(option);
return data; return data;
} }
/**
* @param field {String}
* @param value {String}
* @return {Promise.<boolean>}
* @Description 获取用户性别年龄数量占比
*/
async user_gender({pid, cid}) {
const { app, helper, MUserGender } = this;
var option = helper.seqOption({ pid, cid });
const { fn, col } = app.Sequelize;
Object.assign(option, {
attributes: [
'gender_range',
[fn("SUM", col("total")), "total"],
[fn("SUM", col("male")), "male"],
[fn("SUM", col("female")), "female"]
],
group:'gender_range',
raw: true
});
const data = await MUserGender.findAll(option);
return data;
}
} }
module.exports = UserService; module.exports = UserService;
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!