rm mock data
This commit is contained in:
@@ -838,7 +838,7 @@ export class AnalyticsService {
|
||||
|
||||
// 合并数据,生成最终结果
|
||||
const transformedPosts: PostPerformanceData[] = postsData.map(post => {
|
||||
// 获取帖文的指标数据,如果没有则使用空值或模拟数据
|
||||
// 获取帖文的指标数据
|
||||
const metrics = metricsMap[post.post_id] || {};
|
||||
const postMetrics = {
|
||||
views: Number(metrics.views || 0),
|
||||
@@ -847,16 +847,10 @@ export class AnalyticsService {
|
||||
shares: Number(metrics.shares || 0)
|
||||
};
|
||||
|
||||
// 有真实数据则使用真实数据,否则生成模拟数据
|
||||
const hasRealMetrics = postMetrics.views > 0 || postMetrics.likes > 0 ||
|
||||
postMetrics.comments > 0 || postMetrics.shares > 0;
|
||||
|
||||
const finalMetrics = hasRealMetrics ? postMetrics : this.generateMockMetrics();
|
||||
|
||||
// 同样,有真实情感分数则使用真实数据,否则生成模拟数据
|
||||
// 获取情感分数
|
||||
const sentimentScore = metrics.sentiment_score !== undefined
|
||||
? Number(metrics.sentiment_score)
|
||||
: this.generateMockSentimentScore();
|
||||
: 0; // 默认为0(中性)
|
||||
|
||||
return {
|
||||
post_id: post.post_id,
|
||||
@@ -865,7 +859,7 @@ export class AnalyticsService {
|
||||
kol_name: post.kol_name || '未知KOL',
|
||||
platform: post.platform || 'unknown',
|
||||
publish_date: post.publish_date,
|
||||
metrics: finalMetrics,
|
||||
metrics: postMetrics,
|
||||
sentiment_score: sentimentScore,
|
||||
post_url: post.post_url || `https://${post.platform || 'example'}.com/post/${post.post_id}`
|
||||
};
|
||||
@@ -893,19 +887,11 @@ export class AnalyticsService {
|
||||
});
|
||||
}
|
||||
|
||||
// 统计真实数据vs模拟数据的比例
|
||||
const realDataCount = transformedPosts.filter(post =>
|
||||
post.metrics.views > 0 || post.metrics.likes > 0 ||
|
||||
post.metrics.comments > 0 || post.metrics.shares > 0
|
||||
).length;
|
||||
|
||||
const duration = Date.now() - startTime;
|
||||
logger.info('KOL post performance data fetched successfully', {
|
||||
duration,
|
||||
resultCount: transformedPosts.length,
|
||||
totalPosts: total,
|
||||
realDataCount,
|
||||
mockDataCount: transformedPosts.length - realDataCount
|
||||
totalPosts: total
|
||||
});
|
||||
|
||||
return {
|
||||
@@ -916,73 +902,13 @@ export class AnalyticsService {
|
||||
const duration = Date.now() - startTime;
|
||||
logger.error(`Error in getPostPerformance (${duration}ms)`, error);
|
||||
|
||||
// 发生错误时,尝试返回模拟数据
|
||||
try {
|
||||
const mockPosts = this.generateMockPostPerformanceData(limit);
|
||||
logger.info('Returning mock data due to error', {
|
||||
mockDataCount: mockPosts.length,
|
||||
error: error instanceof Error ? error.message : 'Unknown error'
|
||||
});
|
||||
|
||||
return {
|
||||
posts: mockPosts,
|
||||
total: 100 // 模拟总数
|
||||
};
|
||||
} catch (mockError) {
|
||||
// 如果连模拟数据都无法生成,则抛出原始错误
|
||||
throw error;
|
||||
}
|
||||
// 发生错误时返回空数据
|
||||
return {
|
||||
posts: [],
|
||||
total: 0
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成模拟贴文互动指标
|
||||
*/
|
||||
private generateMockMetrics(): {views: number, likes: number, comments: number, shares: number} {
|
||||
// 生成在合理范围内的随机数
|
||||
const views = Math.floor(Math.random() * 10000) + 1000;
|
||||
const likes = Math.floor(views * (Math.random() * 0.2 + 0.05)); // 5-25% 的观看转化为点赞
|
||||
const comments = Math.floor(likes * (Math.random() * 0.2 + 0.02)); // 2-22% 的点赞转化为评论
|
||||
const shares = Math.floor(likes * (Math.random() * 0.1 + 0.01)); // 1-11% 的点赞转化为分享
|
||||
|
||||
return { views, likes, comments, shares };
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成模拟情感分数 (-1 到 1 之间)
|
||||
*/
|
||||
private generateMockSentimentScore(): number {
|
||||
// 生成-1到1之间的随机数,倾向于正面情绪
|
||||
return parseFloat((Math.random() * 1.6 - 0.6).toFixed(2));
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成完整的模拟贴文表现数据
|
||||
*/
|
||||
private generateMockPostPerformanceData(count: number): PostPerformanceData[] {
|
||||
const platforms = ['instagram', 'youtube', 'tiktok', 'facebook', 'twitter'];
|
||||
const mockPosts: PostPerformanceData[] = [];
|
||||
|
||||
for (let i = 0; i < count; i++) {
|
||||
const platform = platforms[Math.floor(Math.random() * platforms.length)];
|
||||
const publishDate = new Date();
|
||||
publishDate.setDate(publishDate.getDate() - Math.floor(Math.random() * 90));
|
||||
|
||||
mockPosts.push({
|
||||
post_id: `mock-post-${i+1}`,
|
||||
title: `模拟贴文 ${i+1}`,
|
||||
kol_id: `mock-kol-${Math.floor(Math.random() * 10) + 1}`,
|
||||
kol_name: `模拟KOL ${Math.floor(Math.random() * 10) + 1}`,
|
||||
platform,
|
||||
publish_date: publishDate.toISOString(),
|
||||
metrics: this.generateMockMetrics(),
|
||||
sentiment_score: this.generateMockSentimentScore(),
|
||||
post_url: `https://${platform}.com/post/mock-${i+1}`
|
||||
});
|
||||
}
|
||||
|
||||
return mockPosts;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取概览卡片数据
|
||||
|
||||
Reference in New Issue
Block a user