user.js
11.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/*
* @Author: limin
* @Date: 2019-11-21 11:20:01
* @Last Modified by: limin
* @Last Modified time: 2020-01-03 18:23:32
*/
"use strict";
const Controller = require("egg").Controller;
/**
* @Controller screen/user
* @Description 数据大屏-用户
*/
class UserController extends Controller {
constructor(ctx) {
super(ctx);
this.validate = ctx.app.validator.validate;
this.request = ctx.request;
this.helper = ctx.helper;
this.redis = ctx.app.redis;
this.UserService = ctx.service.screen.user;
this.CacheService = ctx.service.cache; //redis 缓存服务
this.ServerResponseCode = ctx.response.ServerResponseCode;
this.ServerResponse = ctx.response.ServerResponse;
this.queue = ctx.app.queue;
}
/**
* 从内存获取日期(昨天,今天)
*/
async _fetch_date_in_cache_(){
const { DATES } = this.helper.constant;
return await this.CacheService.get(DATES);
}
/**
* 对比和获取内存中的数据(总共30天,每天通过定时任务更新)
* 如果获取的时候数据还是最新则手动触发定时任务更新一次
*/
async _compare_get_(isRunTask){
var { yesterday, cache_data_last_date } = await this._fetch_date_in_cache_();
if(!this.helper.moment(cache_data_last_date).isSame(yesterday) && isRunTask){ //如果 内存中的数据的日期不是昨天的 则手动启动定时器 更新缓存数据
await this.app.runSchedule('update_cache');
}
const { USER_SUMMARY_30_DAY } = this.helper.constant;
const summary_30_day = await this.CacheService.get(USER_SUMMARY_30_DAY);
return summary_30_day
}
/**
* 内部方法 从内存获取 昨天的统计数据
* 比较数据时间 如果不是昨天的数据 则启动定时任务 更新一遍数据 再次获取数据并返回
*/
async _fetch_summary_( { beginDate,endDate,isGetLast }, fn ){
var summary_30_day= await this._compare_get_(isGetLast);
const { moment } =this.helper
var result=[];
if(isGetLast){
const { cache_data_last_date } = await this._fetch_date_in_cache_();;
result = summary_30_day[cache_data_last_date];
fn && fn(cache_data_last_date,summary_30_day[cache_data_last_date])
}else if(beginDate){
endDate = endDate || beginDate;
if(moment(beginDate).isSame(endDate) ){
result.push(summary_30_day[beginDate])
fn && fn(beginDate,summary_30_day[beginDate])
}else{
for(const day in summary_30_day){
if(moment(day).isBetween(beginDate,endDate)
|| moment(day).isSame(beginDate)
|| moment(day).isSame(endDate)){
result.push(summary_30_day[day])
fn && fn(day,summary_30_day[day])
}
}
}
}else{
if(fn){
for(const day in summary_30_day){
fn(day,summary_30_day[day])
}
}
result = Object.values(summary_30_day)
}
return result
}
/**
* 内部帮助方法
* @param {*} field
* @param {*} fn
*/
async _set_(field,fn){
var total;
if(typeof field ==='string'){
total = await fn(field)
}else{
total = {}
for (const f of field) {
total[f] = await fn(f)
}
}
return total;
}
/**
* 内部方法 从内存获取 最后一天的统计数据
*/
async _fetch_total_last_day_({ pid, cid , field } ){
const _summary = await this._fetch_summary_({isGetLast:true});
var { p } =_summary;
var total= 0;
if (pid) {
var obj = cid ? p[pid]["c"][cid] : p[pid];
obj && (total =await this._set_(field,async f=>obj[f]));
}else{
total =await this._set_(field,async f=>_summary[f])
}
return total;
}
/**
* 内部方法 从内存获取 范围时间内的数据
*/
async _fetch_total_by_range_({ pid, cid , field, beginDate,endDate } ){
var res = []
const _list = await this._fetch_summary_({ beginDate, endDate });
if( _list && _list.length ){
var fn = null;
var num=0;
if (pid) {
if(cid){
fn = async f=>_list.reduce((pre,next)=>{
num = next && next['p'][pid] && next['p'][pid]['c'][cid]?next['p'][pid]['c'][cid][f]:0
return pre + num
},0)
}else{
fn = async f=>_list.reduce((pre,next)=>{
num = next && next['p'][pid] ? next['p'][pid][f] :0
return pre + num
},0)
}
}else{
fn = async f=>_list.reduce((pre,next)=>{
num = next ? next[f] :0
return pre+num
},0)
}
res = await this._set_(field,fn)
}
return res;
}
/**
* 内部方法 从内存获取 范围时间内的数据
*/
async _fetch_detail_by_range_({ pid, cid , field, beginDate,endDate } ){
var res = []
var _set = this._set_
var data = null
await this._fetch_summary_({ beginDate, endDate },async (day,value)=>{
var fn = null;
if (pid) {
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
}
}else{
fn = async f=>value ? value[f]:0
}
data = await _set(field,fn);
res.push({ day, data })
});
return res;
}
/**
* @Router GET /screen/user/total
* @Description 数据大屏-用户总数统计.----参数pid 有 则返回对应的省份数据.没有则是全国数据的统计
* @Request query integer pid 省份id
*/
async total({ query: { pid,cid } }) {
var total = await this._fetch_total_last_day_({ pid,cid,field:'u'})
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);
total += ~~realtime_total;
const data = { total, online_total };
this.ctx.body = this.ServerResponse.Success(data);
}
/**
* @Router GET /screen/user/statistics
* @Description 数据大屏-数据统计 1. 近七天新增用户数 2.近七天用户活跃度 3.近30天用户留存率 4.用户付费转化率
* @Request query integer pid 省份id
*/
async statistics({ query: { pid,cid } }) {
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(rr,rt);
const beginDate7 = this.helper.date(-7);
const { yesterday } = await this._fetch_date_in_cache_();
//近7天所有
const { i:increased7 } = await this._fetch_total_by_range_({ pid, cid, field:['i'],beginDate:beginDate7,endDate:yesterday});
// 近七天 用户活跃度 (昨天的数据: 近7天 活跃用户数 / 昨天用户数)
const live7 = this.helper.percent(a7,rt);
//近30天所有
const { i:increased30 } = await this._fetch_total_by_range_({ pid, cid, field:['i']});
// 近30天 用户活跃度 (昨天的数据: 近30天 活跃用户数 / 昨天用户数)
const live30 = this.helper.percent(a30,rt);
//在线时长 / 3600 / 近两年用户数
const online_time = o / 3600 / rt
const data = { increased7, live7,increased30,live30,renew, online_time };
this.ctx.body = this.ServerResponse.Success(data);
}
/**
* @Router GET /screen/user/gender
* @Description 数据大屏-用户性别、年龄段、以及数量的占比 2: 00后,3: 90后,4: 80后,5: 70后,6: 60后,7:其他
* @Request query integer pid 省份id
* @Request query integer cid 城市id
*/
async gender({ query: { pid,cid } }) {
const data = await this.UserService.user_gender({ pid,cid })
this.ctx.body = this.ServerResponse.Success(data);
}
/**
* @Router GET /screen/user/city_range
* @Description 数据大屏-城市排行
* @Request query integer pid 省份id
* @Request query integer n 前 n名 默认 5
*/
async city_range({ query: { pid, n = 5 } }) {
const data = await this.UserService.city_range( { pid, limit:n })
this.ctx.body = this.ServerResponse.Success(data);
}
/**
* @Router GET /screen/user/city_range
* @Description 数据大屏-车辆品牌分布
* @Request query integer pid 省份id
* @Request query integer cid 城市id
* @Request query integer n 前 n名 默认 6
*/
async car_brand_range({ query: { pid, cid, n = 6 } }) {
const data = await this.UserService.car_brand_range( { pid, cid, limit:n })
this.ctx.body = this.ServerResponse.Success(data);
}
/**
* @Router GET /screen/user/drive_range
* @Description 数据大屏-出行分布
* @Request query integer pid 省份id
* @Request query integer cid 城市id
*/
async drive_range({ query: { pid, cid } }) {
const data = await this.UserService.drive_range( { pid, cid })
this.ctx.body = this.ServerResponse.Success(data);
}
/**
* @Router GET /screen/user/trend
* @Description 数据大屏-1 用户新增趋势 2.用户活跃趋势, 3.用户付费趋势 4.用户车辆出行趋势
* @Request query integer pid 省份id
* @Request query integer cid 城市id
* @Request query string type 趋势类别 u:用户总数 i: 用户新增趋势 a: 用户活跃趋势, r:用户付费趋势 j:用户车辆出行趋势(多个',' 隔开 默认:['u','i','a','r','j'])
* @Request query integer n 近N天数据 默认 30天
* @Request query string beginDate 查询开始时间 (2019-12-12)
* @Request query string endDate 查询结束时间 (2019-12-12)
*/
async trend({ query: { pid, cid, type, n = 30, beginDate , endDate } }) {
if(n){
if(n===30){
beginDate = endDate = null;
}else{
beginDate = this.helper.date(-n);
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 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
* @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);
}
/**
* @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 = UserController;