MCPcopy
hub / github.com/msgbyte/tianji / handleCreateDailyReport

Function handleCreateDailyReport

apps/daily-ai-trigger/src/handler.ts:53–117  ·  view source on GitHub ↗
(env: Env)

Source from the content-addressed store, hash-verified

51}
52
53export async function handleCreateDailyReport(env: Env) {
54 console.log('handleCreateDailyReport');
55
56 const workspaceId = env.WORKSPACE_ID;
57 const surveyId = env.SURVEY_ID;
58
59 const startDate = dayjs().subtract(1, 'day').startOf('day');
60 const endDate = dayjs().subtract(1, 'day').endOf('day');
61
62 const { items } = await openApiClient.SurveyService.surveyResultList({
63 workspaceId,
64 surveyId,
65 startAt: startDate.valueOf(),
66 endAt: endDate.valueOf(),
67 limit: 1000,
68 });
69
70 const resultList = items.filter((item) => Boolean(item.aiCategory));
71
72 const title = `[${startDate.format('YYYY-MM-DD HH:mm:ss')} ~ ${endDate.format('HH:mm:ss')}] Survey Daily Report`;
73
74 let content = '';
75
76 const categoryCount: { [category: string]: number } = {};
77 resultList.forEach((record) => {
78 const category = record.aiCategory || 'Uncategorized';
79 categoryCount[category] = (categoryCount[category] || 0) + 1;
80 });
81
82 const sortedCategories = Object.entries(categoryCount).sort((a, b) => b[1] - a[1]);
83
84 content += `All Categories(${resultList.length}):\n`;
85 sortedCategories.forEach(([category, count]) => {
86 content += `- ${category}(${count})\n`;
87 });
88
89 // 3. Get top 3 categories with the most records
90 const top3Categories = sortedCategories.slice(0, 3);
91
92 content += `\n\nTop Issues:\n`;
93 top3Categories.forEach(([category, count]) => {
94 // Filter all records of the corresponding category
95 const records = resultList.filter((record) => record.aiCategory === category);
96 // Sample 3 records (or return all if less than 3)
97 const sampledRecords = sampleItems(records, 3);
98
99 content += `- ${category} (${count})\n`;
100 sampledRecords.forEach((record) => {
101 content += ` - ${record.aiTranslation}\n`;
102 });
103 });
104
105 await Promise.all([
106 notifyToFeishu(env, title, content),
107 addFeishuBitableRecord(
108 env,
109 items.map((item) => ({
110 id: item.id,

Callers 2

fetchFunction · 0.90
scheduledFunction · 0.90

Calls 5

notifyToFeishuFunction · 0.90
addFeishuBitableRecordFunction · 0.90
sampleItemsFunction · 0.85
logMethod · 0.80
surveyResultListMethod · 0.80

Tested by

no test coverage detected