|
@@ -144,25 +144,69 @@ class MessageRecordService {
|
|
|
* 按模板获取消息统计数据(含用户点击率)
|
|
* 按模板获取消息统计数据(含用户点击率)
|
|
|
*/
|
|
*/
|
|
|
async getStatisticsByTemplate(startDate, endDate, strategyName, page = MessageRecordService.DEFAULT_STATS_PAGE, limit = MessageRecordService.DEFAULT_STATS_LIMIT) {
|
|
async getStatisticsByTemplate(startDate, endDate, strategyName, page = MessageRecordService.DEFAULT_STATS_PAGE, limit = MessageRecordService.DEFAULT_STATS_LIMIT) {
|
|
|
|
|
+ const cacheKey = this.buildStatsCacheKey("by-template", {
|
|
|
|
|
+ startDate: startDate?.toISOString(),
|
|
|
|
|
+ endDate: endDate?.toISOString(),
|
|
|
|
|
+ strategyName: strategyName || null,
|
|
|
|
|
+ page,
|
|
|
|
|
+ limit,
|
|
|
|
|
+ });
|
|
|
|
|
+ const cached = await this.getCache(cacheKey);
|
|
|
|
|
+ if (cached) {
|
|
|
|
|
+ console.log(`[MessageStatsCache] hit key=${cacheKey}`);
|
|
|
|
|
+ return cached;
|
|
|
|
|
+ }
|
|
|
const matchConditions = this.buildMatchConditions(startDate, endDate, strategyName);
|
|
const matchConditions = this.buildMatchConditions(startDate, endDate, strategyName);
|
|
|
- const groupFields = ["templateId", "templateName"];
|
|
|
|
|
- return this.getStatisticsByGroup(matchConditions, groupFields, { clickThroughRate: -1 }, page, limit);
|
|
|
|
|
|
|
+ // templateId 是 ObjectId,分组时增加基数但对展示无意义,移除后减少第一阶段 group key 大小
|
|
|
|
|
+ const groupFields = ["templateName"];
|
|
|
|
|
+ const result = await this.getStatisticsByGroup(matchConditions, groupFields, { clickThroughRate: -1 }, page, limit);
|
|
|
|
|
+ await this.setCache(cacheKey, result);
|
|
|
|
|
+ console.log(`[MessageStatsCache] miss key=${cacheKey}`);
|
|
|
|
|
+ return result;
|
|
|
}
|
|
}
|
|
|
/**
|
|
/**
|
|
|
* 按国家代码获取消息统计数据(含用户点击率)
|
|
* 按国家代码获取消息统计数据(含用户点击率)
|
|
|
*/
|
|
*/
|
|
|
async getStatisticsByCc(startDate, endDate, strategyName, page = MessageRecordService.DEFAULT_STATS_PAGE, limit = MessageRecordService.DEFAULT_STATS_LIMIT) {
|
|
async getStatisticsByCc(startDate, endDate, strategyName, page = MessageRecordService.DEFAULT_STATS_PAGE, limit = MessageRecordService.DEFAULT_STATS_LIMIT) {
|
|
|
|
|
+ const cacheKey = this.buildStatsCacheKey("by-cc", {
|
|
|
|
|
+ startDate: startDate?.toISOString(),
|
|
|
|
|
+ endDate: endDate?.toISOString(),
|
|
|
|
|
+ strategyName: strategyName || null,
|
|
|
|
|
+ page,
|
|
|
|
|
+ limit,
|
|
|
|
|
+ });
|
|
|
|
|
+ const cached = await this.getCache(cacheKey);
|
|
|
|
|
+ if (cached) {
|
|
|
|
|
+ console.log(`[MessageStatsCache] hit key=${cacheKey}`);
|
|
|
|
|
+ return cached;
|
|
|
|
|
+ }
|
|
|
const matchConditions = this.buildMatchConditions(startDate, endDate, strategyName);
|
|
const matchConditions = this.buildMatchConditions(startDate, endDate, strategyName);
|
|
|
- const groupFields = ["cc"];
|
|
|
|
|
- return this.getStatisticsByGroup(matchConditions, groupFields, { totalRecords: -1 }, page, limit);
|
|
|
|
|
|
|
+ const result = await this.getStatisticsByGroup(matchConditions, ["cc"], { totalRecords: -1 }, page, limit);
|
|
|
|
|
+ await this.setCache(cacheKey, result);
|
|
|
|
|
+ console.log(`[MessageStatsCache] miss key=${cacheKey}`);
|
|
|
|
|
+ return result;
|
|
|
}
|
|
}
|
|
|
/**
|
|
/**
|
|
|
* 按图片 URL 获取消息统计数据(含用户点击率)
|
|
* 按图片 URL 获取消息统计数据(含用户点击率)
|
|
|
*/
|
|
*/
|
|
|
async getStatisticsByImage(startDate, endDate, strategyName, page = MessageRecordService.DEFAULT_STATS_PAGE, limit = MessageRecordService.DEFAULT_STATS_LIMIT) {
|
|
async getStatisticsByImage(startDate, endDate, strategyName, page = MessageRecordService.DEFAULT_STATS_PAGE, limit = MessageRecordService.DEFAULT_STATS_LIMIT) {
|
|
|
|
|
+ const cacheKey = this.buildStatsCacheKey("by-image", {
|
|
|
|
|
+ startDate: startDate?.toISOString(),
|
|
|
|
|
+ endDate: endDate?.toISOString(),
|
|
|
|
|
+ strategyName: strategyName || null,
|
|
|
|
|
+ page,
|
|
|
|
|
+ limit,
|
|
|
|
|
+ });
|
|
|
|
|
+ const cached = await this.getCache(cacheKey);
|
|
|
|
|
+ if (cached) {
|
|
|
|
|
+ console.log(`[MessageStatsCache] hit key=${cacheKey}`);
|
|
|
|
|
+ return cached;
|
|
|
|
|
+ }
|
|
|
const matchConditions = this.buildMatchConditions(startDate, endDate, strategyName);
|
|
const matchConditions = this.buildMatchConditions(startDate, endDate, strategyName);
|
|
|
- const groupFields = ["image"];
|
|
|
|
|
- return this.getStatisticsByGroup(matchConditions, groupFields, { clickThroughRate: -1 }, page, limit);
|
|
|
|
|
|
|
+ const result = await this.getStatisticsByGroup(matchConditions, ["image"], { clickThroughRate: -1 }, page, limit);
|
|
|
|
|
+ await this.setCache(cacheKey, result);
|
|
|
|
|
+ console.log(`[MessageStatsCache] miss key=${cacheKey}`);
|
|
|
|
|
+ return result;
|
|
|
}
|
|
}
|
|
|
/**
|
|
/**
|
|
|
* 按时间维度的趋势分析,每日统计(含用户点击率)
|
|
* 按时间维度的趋势分析,每日统计(含用户点击率)
|