update_cache.js
5.57 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
const Subscription = require("egg").Subscription;
class UpdateCache extends Subscription {
constructor(ctx) {
super(ctx);
this.UserService = ctx.service.screen.user;
this.helper = ctx.helper;
this.CacheService = ctx.service.cache; //redis 缓存服务
}
/*
* * * * * *
┬ ┬ ┬ ┬ ┬ ┬
│ │ │ │ │ |
│ │ │ │ │ └ 星期 (0 - 7) (0或7都是星期日)
│ │ │ │ └───── 月份 (1 - 12)
│ │ │ └────────── 日期 (1 - 31)
│ │ └─────────────── 小时 (0 - 23)
│ └──────────────────── 分钟 (0 - 59)
└───────────────────────── 秒 (0 - 59, optional)
*/
// 通过 schedule 属性来设置定时任务的执行间隔等配置
static get schedule() {
return {
// interval: '1m', // 定时任务 1 分钟间隔
cron: "0 30 6 * * *", // 定点任务 每天 凌晨 6点30 执行一次
type: "worker", // 随机 worker 执行
immediate: true //启动立即执行一次
// disable:true // 不启动
};
}
_group(cities, f) {
const data = {
};
var date= this.app.config.sys.init_date;
function __init_or_plus__(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.0; // 里程 miles
target["o"] = 0; // online 在线时间
}
return target;
}
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 } = 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 }); // 累加全国总数值
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 }); //累加 prov_code 省份总数据
data[day]["p"][prov_code]["c"][city_code] = { u, r, a, i, j, m, o };
}.bind(this)
);
return { data, date };
}
// task_total_country_cache(){
// const gender = await this.UserService.user_gender({});
// const city_range = await this.UserService.city_range( { limit:5 });
// const car_brand_range = await this.UserService.car_brand_range( { limit:6 })
// const drive_range = await this.UserService.drive_range( { })
// return { gender,city_range,car_brand_range,drive_range }
// }
// subscribe 是真正定时任务执行时被运行的函数
async subscribe() {
this.logger.info("===============执行日常任务-缓存每日统计数据(总30天) begin==================");
const { moment, constant } = this.helper;
const { DATES, USER_SUMMARY_30_DAY } = constant;
var { cache_update_time, cache_data_last_date } = await this.CacheService.get(DATES);
const { user_cache_update_time_diff } = this.app.config.sys;
if( moment().diff(moment(cache_update_time)) < user_cache_update_time_diff ) {
this.logger.info( "=============== 距离上一次更新不到 %s 毫秒.不执行更新操作 ==================",user_cache_update_time_diff );
return ;
}
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: day30before, endDate: yesterday});
if (cities.length) {
var {data, date } = this._group(cities, city => city.prov_code);
if( moment(date).isAfter( cache_data_last_date) ){ //当前数据最新时间 大于 上一次缓存数据的最新时间 才写入缓存
const value = JSON.stringify(data);
await this.CacheService.set(
USER_SUMMARY_30_DAY,
value
);
cache_data_last_date = date; //更新缓存数据最新时间
this.logger.info("user_summary_%s([%s]):%s",data.date,Object.keys(data).join(","),value);
}else{
this.logger.info("cache_date:%s date:%s 不写缓存",cache_data_last_date,date);
}
} else {
this.logger.warn("warnning=============user_summary_30_day===========is empty!");
}
//缓存日期.方便业务使用
await this.CacheService.set(
DATES,
{
today,
yesterday,
cache_data_last_date,
cache_update_time:moment()
}
);
this.logger.info("===============执行日常任务-缓存每日统计数据(总30天) end==================");
}
}
module.exports = UpdateCache;