#!/bin/bash
###############################################################################
# Approach B (aPaaS Charts) Deployment Script
#
# This script validates JSON configurations and generates deployment artifacts
# for Lark aPaaS visual chart deployment
#
# Author: HypeLab
# Date: 2025-12-09
# Version: 1.0.0
###############################################################################
set -e # Exit on error
# Color codes for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Configuration
CONFIG_DIR="config/apaas-charts"
OUTPUT_DIR="dist/apaas-deployment"
MANIFEST_FILE="${OUTPUT_DIR}/chart-manifest.json"
PACKAGE_FILE="${OUTPUT_DIR}/apaas-import-package.zip"
# Bitable Configuration
APP_TOKEN="C8kmbTsqoa6rBesTKRpl8nV8gHd"
TABLE_ID="tblG4uuUvbwfvI9Z"
APAAS_APP_ID="Dffwb10dwaz6UZs6c4HlWyV3g7c"
echo -e "${BLUE}========================================${NC}"
echo -e "${BLUE} Approach B (aPaaS Charts) Deployment${NC}"
echo -e "${BLUE}========================================${NC}"
echo ""
# Function to print status
print_status() {
echo -e "${GREEN}✓${NC} $1"
}
print_error() {
echo -e "${RED}✗${NC} $1"
}
print_warning() {
echo -e "${YELLOW}⚠${NC} $1"
}
print_info() {
echo -e "${BLUE}ℹ${NC} $1"
}
# Step 1: Validate JSON files
echo -e "${BLUE}Step 1: Validating JSON configuration files...${NC}"
echo ""
CONFIG_FILES=(
"01-metric-card-total-views.json"
"02-metric-card-total-likes.json"
"03-metric-card-avg-engagement.json"
"04-metric-card-total-followers.json"
"05-line-chart-views-engagement.json"
"06-bar-chart-engagement-breakdown.json"
"07-pie-chart-engagement-distribution.json"
"08-table-video-performance.json"
)
VALIDATION_PASSED=true
for config_file in "${CONFIG_FILES[@]}"; do
file_path="${CONFIG_DIR}/${config_file}"
if [ ! -f "$file_path" ]; then
print_error "File not found: $config_file"
VALIDATION_PASSED=false
continue
fi
# Validate JSON syntax using Node.js or Python
if command -v node &> /dev/null; then
if node -e "JSON.parse(require('fs').readFileSync('$file_path', 'utf8'))" 2>/dev/null; then
print_status "$config_file - Valid JSON"
else
print_error "$config_file - Invalid JSON syntax"
VALIDATION_PASSED=false
fi
elif command -v python3 &> /dev/null; then
if python3 -c "import json; json.load(open('$file_path'))" 2>/dev/null; then
print_status "$config_file - Valid JSON"
else
print_error "$config_file - Invalid JSON syntax"
VALIDATION_PASSED=false
fi
else
print_warning "$config_file - Cannot validate (node/python3 not found)"
fi
done
echo ""
if [ "$VALIDATION_PASSED" = false ]; then
print_error "JSON validation failed. Please fix the errors above."
exit 1
fi
print_status "All JSON files validated successfully!"
echo ""
# Step 2: Create output directory
echo -e "${BLUE}Step 2: Creating deployment directory...${NC}"
mkdir -p "$OUTPUT_DIR"
print_status "Created: $OUTPUT_DIR"
echo ""
# Step 3: Generate combined manifest
echo -e "${BLUE}Step 3: Generating combined manifest...${NC}"
cat > "$MANIFEST_FILE" <<EOF
{
"version": "1.0.0",
"generatedAt": "$(date -u +"%Y-%m-%dT%H:%M:%SZ")",
"deployment": {
"approach": "B - aPaaS Visual Charts",
"appId": "${APAAS_APP_ID}",
"dataSource": {
"type": "lark-bitable",
"appToken": "${APP_TOKEN}",
"tableId": "${TABLE_ID}",
"dataSourceName": "getTikTokData"
}
},
"components": [
EOF
# Add each config file to manifest
first=true
for config_file in "${CONFIG_FILES[@]}"; do
file_path="${CONFIG_DIR}/${config_file}"
if [ "$first" = false ]; then
echo " ," >> "$MANIFEST_FILE"
fi
first=false
echo " {" >> "$MANIFEST_FILE"
echo " \"file\": \"${config_file}\"," >> "$MANIFEST_FILE"
# Extract component info using node or python
if command -v node &> /dev/null; then
component_id=$(node -e "console.log(JSON.parse(require('fs').readFileSync('$file_path')).id)")
component_name=$(node -e "console.log(JSON.parse(require('fs').readFileSync('$file_path')).name)")
component_type=$(node -e "console.log(JSON.parse(require('fs').readFileSync('$file_path')).component)")
echo " \"id\": \"${component_id}\"," >> "$MANIFEST_FILE"
echo " \"name\": \"${component_name}\"," >> "$MANIFEST_FILE"
echo " \"type\": \"${component_type}\"" >> "$MANIFEST_FILE"
fi
echo -n " }" >> "$MANIFEST_FILE"
done
echo "" >> "$MANIFEST_FILE"
echo " ]," >> "$MANIFEST_FILE"
# Add deployment instructions
cat >> "$MANIFEST_FILE" <<EOF
"deploymentInstructions": {
"steps": [
"1. Open Lark aPaaS App: ${APAAS_APP_ID}",
"2. Navigate to the visual page editor",
"3. Add a new data source: Bitable connector",
"4. Configure data source with App Token: ${APP_TOKEN}, Table ID: ${TABLE_ID}",
"5. Name the data source: 'getTikTokData'",
"6. For each component in the manifest, drag the corresponding chart type",
"7. Configure data bindings as specified in the JSON files",
"8. Set layout positions according to the layout configuration",
"9. Test data flow and chart rendering",
"10. Publish the dashboard page"
],
"dataFields": {
"required": [
"Date",
"Views",
"Likes",
"Comments",
"Shares",
"Followers",
"Engagement_Rate"
],
"calculated": [
"TotalEngagement (Likes + Comments + Shares)"
]
},
"documentationUrl": "config/apaas-charts/DEPLOYMENT_GUIDE.md"
}
}
EOF
print_status "Generated manifest: $MANIFEST_FILE"
echo ""
# Step 4: Copy configuration files to output
echo -e "${BLUE}Step 4: Copying configuration files...${NC}"
cp -r "$CONFIG_DIR"/*.json "$OUTPUT_DIR/"
print_status "Copied $(ls -1 ${CONFIG_DIR}/*.json | wc -l | xargs) configuration files"
echo ""
# Step 5: Create import package (if zip is available)
echo -e "${BLUE}Step 5: Creating import package...${NC}"
if command -v zip &> /dev/null; then
cd "$OUTPUT_DIR"
zip -q -r "$(basename $PACKAGE_FILE)" *.json
cd - > /dev/null
print_status "Created package: $PACKAGE_FILE"
else
print_warning "zip command not found - skipping package creation"
fi
echo ""
# Step 6: Generate deployment summary
echo -e "${BLUE}Step 6: Generating deployment summary...${NC}"
SUMMARY_FILE="${OUTPUT_DIR}/DEPLOYMENT_SUMMARY.txt"
cat > "$SUMMARY_FILE" <<EOF
================================================================================
APPROACH B DEPLOYMENT SUMMARY
================================================================================
Generated: $(date)
CONFIGURATION
-------------
Approach: B - aPaaS Visual Charts
aPaaS App ID: ${APAAS_APP_ID}
Bitable App Token: ${APP_TOKEN}
Table ID: ${TABLE_ID}
COMPONENTS
----------
EOF
for config_file in "${CONFIG_FILES[@]}"; do
file_path="${CONFIG_DIR}/${config_file}"
if command -v node &> /dev/null; then
component_name=$(node -e "console.log(JSON.parse(require('fs').readFileSync('$file_path')).name)" 2>/dev/null || echo "Unknown")
component_type=$(node -e "console.log(JSON.parse(require('fs').readFileSync('$file_path')).component)" 2>/dev/null || echo "Unknown")
echo " - ${component_name} (${component_type})" >> "$SUMMARY_FILE"
else
echo " - ${config_file}" >> "$SUMMARY_FILE"
fi
done
cat >> "$SUMMARY_FILE" <<EOF
DEPLOYMENT FILES
----------------
✓ Chart manifest: ${MANIFEST_FILE}
✓ Configuration files: ${OUTPUT_DIR}/*.json
✓ Deployment guide: ${CONFIG_DIR}/DEPLOYMENT_GUIDE.md
✓ Import package: ${PACKAGE_FILE}
NEXT STEPS
----------
1. Review the DEPLOYMENT_GUIDE.md for detailed instructions
2. Open Lark aPaaS application: ${APAAS_APP_ID}
3. Configure Bitable data source connector
4. Import and configure each chart component
5. Test the dashboard functionality
6. Publish the dashboard page
FIELD MAPPING
-------------
Required fields in Bitable table ${TABLE_ID}:
- Date (Date field)
- Views (Number field)
- Likes (Number field)
- Comments (Number field)
- Shares (Number field)
- Followers (Number field)
- Engagement_Rate (Number field, calculated as %)
RESOURCES
---------
- Main documentation: config/apaas-charts/DEPLOYMENT_GUIDE.md
- Field mapping guide: config/FIELD_MAPPING.md
- Integration guide: config/INTEGRATION_GUIDE.md
================================================================================
DEPLOYMENT READY
================================================================================
The aPaaS configuration is ready for manual deployment via the Lark aPaaS
visual editor. Follow the step-by-step guide in DEPLOYMENT_GUIDE.md.
For questions or issues, refer to the troubleshooting section in the guide.
EOF
print_status "Generated summary: $SUMMARY_FILE"
echo ""
# Step 7: Display deployment summary
echo -e "${GREEN}========================================${NC}"
echo -e "${GREEN} Deployment Preparation Complete!${NC}"
echo -e "${GREEN}========================================${NC}"
echo ""
cat "$SUMMARY_FILE"
# Step 8: Provide quick access commands
echo ""
echo -e "${BLUE}Quick Access:${NC}"
echo -e " View manifest: ${YELLOW}cat ${MANIFEST_FILE}${NC}"
echo -e " View summary: ${YELLOW}cat ${SUMMARY_FILE}${NC}"
echo -e " View guide: ${YELLOW}cat ${CONFIG_DIR}/DEPLOYMENT_GUIDE.md${NC}"
echo -e " Output location: ${YELLOW}${OUTPUT_DIR}${NC}"
echo ""
exit 0