/**
* Example: aPaaS Dashboard Setup
*
* This example demonstrates how to set up a dashboard infrastructure
* for a Lark aPaaS application.
*
* IMPORTANT: Dashboard views cannot be created via API and must be
* created manually in the Lark UI. This script prepares the necessary
* infrastructure and provides step-by-step instructions.
*
* @see docs/APAAS_API_RESEARCH.md
*/
import { LarkBaseClient } from '../src/api';
import { createDashboardManager } from '../src/apaas/DashboardManager';
// Configuration
const APP_TOKEN = 'KdW3bZ3axa636XsocGVln8DAgmg';
const APP_ID = process.env.LARK_APP_ID || '';
const APP_SECRET = process.env.LARK_APP_SECRET || '';
async function main() {
console.log('π Lark aPaaS Dashboard Setup');
console.log('=' .repeat(80));
console.log();
// Initialize client
const client = new LarkBaseClient({
appId: APP_ID,
appSecret: APP_SECRET,
appToken: APP_TOKEN
});
// Create dashboard manager
const dashboardMgr = createDashboardManager(client, APP_TOKEN);
// Step 1: Create tables for dashboard data
console.log('π Creating data tables...');
const campaignsTable = await client.tables.create({
name: 'TikTok Campaigns',
fields: [
{ name: 'Campaign Name', type: 1 }, // Text
{ name: 'Status', type: 3 }, // Single Select
{ name: 'Objective', type: 3 }, // Single Select
{ name: 'Budget', type: 99003 }, // Currency
{ name: 'Spend', type: 99003 }, // Currency
{ name: 'Start Date', type: 5 }, // DateTime
{ name: 'End Date', type: 5 } // DateTime
]
});
const metricsTable = await client.tables.create({
name: 'Campaign Metrics',
fields: [
{ name: 'Campaign', type: 18 }, // Link to Campaigns
{ name: 'Date', type: 5 }, // DateTime
{ name: 'Impressions', type: 2 }, // Number
{ name: 'Clicks', type: 2 }, // Number
{ name: 'Conversions', type: 2 }, // Number
{ name: 'CTR', type: 20 }, // Formula: Clicks/Impressions
{ name: 'CVR', type: 20 }, // Formula: Conversions/Clicks
{ name: 'CPC', type: 99003 }, // Currency
{ name: 'CPA', type: 99003 } // Currency
]
});
console.log(`β
Created table: ${campaignsTable.name} (${campaignsTable.table_id})`);
console.log(`β
Created table: ${metricsTable.name} (${metricsTable.table_id})`);
console.log();
// Step 2: Prepare dashboard infrastructure
console.log('π§ Preparing dashboard infrastructure...');
const instructions = await dashboardMgr.prepareDashboardInfrastructure({
dashboardName: 'TikTok Campaign Performance Dashboard',
description: 'Real-time analytics for TikTok advertising campaigns',
dataSources: [
{
tableId: campaignsTable.table_id,
tableName: 'TikTok Campaigns'
},
{
tableId: metricsTable.table_id,
tableName: 'Campaign Metrics'
}
],
recommendedCharts: [
{
type: 'bar',
title: 'Conversions by Campaign',
dataSource: metricsTable.table_id,
xField: 'Campaign',
yField: 'Conversions',
groupBy: 'Date'
},
{
type: 'line',
title: 'Performance Trend',
dataSource: metricsTable.table_id,
xField: 'Date',
yField: 'Conversions'
},
{
type: 'pie',
title: 'Budget Distribution',
dataSource: campaignsTable.table_id,
xField: 'Campaign Name',
yField: 'Budget'
},
{
type: 'scatter',
title: 'CTR vs CVR',
dataSource: metricsTable.table_id,
xField: 'CTR',
yField: 'CVR'
},
{
type: 'funnel',
title: 'Conversion Funnel',
dataSource: metricsTable.table_id,
xField: 'Stage',
yField: 'Count'
}
],
filters: [
{
field: 'Status',
operator: 'is',
value: 'Active'
},
{
field: 'Date',
operator: 'greaterThan',
value: new Date(Date.now() - 30 * 24 * 60 * 60 * 1000) // Last 30 days
}
]
});
console.log();
// Step 3: Print setup instructions
dashboardMgr.printSetupInstructions(instructions);
// Step 4: Add sample data
console.log('π Adding sample data...');
await dashboardMgr.updateDashboardData(campaignsTable.table_id, [
{
fields: {
'Campaign Name': 'Summer Sale 2025',
'Status': 'Active',
'Objective': 'Conversions',
'Budget': 10000,
'Spend': 7500,
'Start Date': new Date('2025-06-01').getTime(),
'End Date': new Date('2025-08-31').getTime()
}
},
{
fields: {
'Campaign Name': 'Brand Awareness Q1',
'Status': 'Active',
'Objective': 'Reach',
'Budget': 15000,
'Spend': 12000,
'Start Date': new Date('2025-01-01').getTime(),
'End Date': new Date('2025-03-31').getTime()
}
}
]);
await dashboardMgr.updateDashboardData(metricsTable.table_id, [
{
fields: {
'Date': new Date('2025-12-08').getTime(),
'Impressions': 1000000,
'Clicks': 5000,
'Conversions': 250,
'CPC': 1.50,
'CPA': 30.00
}
},
{
fields: {
'Date': new Date('2025-12-07').getTime(),
'Impressions': 950000,
'Clicks': 4800,
'Conversions': 240,
'CPC': 1.45,
'CPA': 29.00
}
}
]);
console.log('β
Sample data added');
console.log();
// Step 5: Final instructions
console.log('π― NEXT STEPS:');
console.log('-'.repeat(80));
console.log();
console.log('1. Open your Base app in Lark:');
console.log(` https://hypelive.sg.larksuite.com/app/${APP_TOKEN}`);
console.log();
console.log('2. Create the dashboard manually (see instructions above)');
console.log();
console.log('3. Add the recommended charts to your dashboard');
console.log();
console.log('4. Configure filters as specified');
console.log();
console.log('5. Once complete, the dashboard will automatically update');
console.log(' when you add new data via the API');
console.log();
console.log('6. To add new data, use:');
console.log(' ```typescript');
console.log(' await dashboardMgr.updateDashboardData(tableId, records);');
console.log(' ```');
console.log();
console.log('=' .repeat(80));
console.log();
console.log('β
Dashboard infrastructure ready!');
console.log();
}
// Run
main().catch(error => {
console.error('β Error:', error);
process.exit(1);
});