/**
* Dashboard Data Manager for Lark aPaaS
*
* Since Lark API doesn't support creating dashboard views programmatically,
* this utility helps manage the DATA that dashboards display.
*
* Workflow:
* 1. Create dashboard view manually in Lark UI (one-time)
* 2. Use this manager to populate/update data
* 3. Dashboard automatically reflects data changes
*/
export interface DashboardMetric {
name: string;
value: number | string;
trend?: 'up' | 'down' | 'stable';
changePercent?: number;
}
export interface ChartDataPoint {
label: string;
value: number;
category?: string;
date?: string;
}
export interface DashboardConfig {
appToken: string;
tableId: string;
metricsFields: {
name: string;
valueField: string;
aggregation: 'sum' | 'count' | 'avg' | 'max' | 'min';
}[];
chartFields: {
xAxisField: string;
yAxisField: string;
groupByField?: string;
};
}
/**
* Generates instructions for manual dashboard creation in Lark UI
*/
export function generateDashboardInstructions(config: {
tableName: string;
dashboardName: string;
metrics: { title: string; field: string; aggregation: string }[];
charts: { type: string; title: string; xField: string; yField: string }[];
}): string {
const lines: string[] = [
`# Dashboard Setup Instructions: ${config.dashboardName}`,
'',
'## Step 1: Create Dashboard View',
`1. Open your Bitable table: ${config.tableName}`,
'2. Click the "+" button next to view tabs',
'3. Select "Dashboard" from the view type options',
`4. Name it: "${config.dashboardName}"`,
'',
'## Step 2: Add Metric Cards',
''
];
config.metrics.forEach((metric, idx) => {
lines.push(`### Metric ${idx + 1}: ${metric.title}`);
lines.push(`- Field: ${metric.field}`);
lines.push(`- Aggregation: ${metric.aggregation.toUpperCase()}`);
lines.push('');
});
lines.push('## Step 3: Add Charts');
lines.push('');
config.charts.forEach((chart, idx) => {
lines.push(`### Chart ${idx + 1}: ${chart.title}`);
lines.push(`- Type: ${chart.type}`);
lines.push(`- X-Axis: ${chart.xField}`);
lines.push(`- Y-Axis: ${chart.yField}`);
lines.push('');
});
lines.push('## Step 4: Configure Layout');
lines.push('- Drag and resize blocks to your preferred layout');
lines.push('- Save the dashboard');
lines.push('');
lines.push('## Done!');
lines.push('The dashboard will now auto-update when data changes via API.');
return lines.join('\n');
}
/**
* Generates sample data for testing dashboards
*/
export function generateSampleData(type: 'sentiment' | 'tiktok' | 'generic'): Record<string, any>[] {
switch (type) {
case 'sentiment':
return [
{ Sentiment: 'Positive', Platform: 'tiktok', Count: 45 },
{ Sentiment: 'Neutral', Platform: 'tiktok', Count: 35 },
{ Sentiment: 'Negative', Platform: 'tiktok', Count: 20 },
{ Sentiment: 'Positive', Platform: 'facebook', Count: 30 },
{ Sentiment: 'Neutral', Platform: 'facebook', Count: 25 },
{ Sentiment: 'Negative', Platform: 'facebook', Count: 15 },
];
case 'tiktok':
return [
{ video_id: 'v001', views: 15000, likes: 1200, shares: 150, comments: 89 },
{ video_id: 'v002', views: 8500, likes: 650, shares: 80, comments: 45 },
{ video_id: 'v003', views: 22000, likes: 1800, shares: 220, comments: 120 },
{ video_id: 'v004', views: 5200, likes: 380, shares: 40, comments: 28 },
{ video_id: 'v005', views: 31000, likes: 2500, shares: 350, comments: 185 },
];
default:
return [
{ category: 'A', value: 100 },
{ category: 'B', value: 200 },
{ category: 'C', value: 150 },
];
}
}
/**
* Prints dashboard setup instructions to console
*/
export function printDashboardSetup(config: {
baseUrl: string;
appToken: string;
tableId: string;
dashboardName: string;
}): void {
console.log('');
console.log('═══════════════════════════════════════════════════════════════');
console.log(' DASHBOARD SETUP INSTRUCTIONS');
console.log('═══════════════════════════════════════════════════════════════');
console.log('');
console.log('Since Lark API does not support creating dashboard views,');
console.log('please create the dashboard manually using these steps:');
console.log('');
console.log(`1. Open: ${config.baseUrl}/base/${config.appToken}?table=${config.tableId}`);
console.log('');
console.log('2. Click "+" next to view tabs');
console.log('');
console.log('3. Select "Dashboard"');
console.log('');
console.log(`4. Name it: "${config.dashboardName}"`);
console.log('');
console.log('5. Add your charts and metrics using the visual editor');
console.log('');
console.log('6. Data will auto-update when you use the API to add/modify records');
console.log('');
console.log('═══════════════════════════════════════════════════════════════');
console.log('');
}
export default {
generateDashboardInstructions,
generateSampleData,
printDashboardSetup,
};