andycom12000

data-analysis-backend

0
0
# Install this skill:
npx skills add andycom12000/data-analytic-flow-skill

Or install specific skill: npx add-skill https://github.com/andycom12000/data-analytic-flow-skill

# Description

Use when implementing or modifying data analysis backend features including endpoints, aggregations, dimensions, or formatters. Ensures Clean Architecture principles, three-layer separation (Endpoint-Service-Repository), and standardized 8-step pipeline workflow. Prevents common anti-patterns like SQL in domain layer or DB schema leakage.

# SKILL.md


name: data-analysis-backend
description: "Use when implementing or modifying data analysis backend features including endpoints, aggregations, dimensions, or formatters. Ensures Clean Architecture principles, three-layer separation (Endpoint-Service-Repository), and standardized 8-step pipeline workflow. Prevents common anti-patterns like SQL in domain layer or DB schema leakage."


Data Analysis Backend Development Guide

Overview

這個 skill 指導 Agent 遵循數據分析後端的架構設計原則。核心目標是建立一個高擴充性、低耦合、可預期流程的系統。

核心架構原則

三層架構 (Three-Layer Architecture)

Endpoint Layer (HTTP)
    ↓
Application Service Layer (Orchestration)
    ↓
Domain Layer (Business Logic)
    ↓
Infrastructure Layer (DB/SQL)

依賴反轉原則 (Dependency Inversion)
- Domain Layer 定義 contracts (interfaces),不知道任何 DB schema
- Infrastructure Layer 實作這些 contracts,處理 SQL 和 DB 連接
- Service Layer 協調 Domain 和 Infrastructure,但不直接操作 DB

關鍵設計決策
1. Semantic → Physical 分離:Domain 用語意化的 DimensionSpec,QueryBuilder 轉換為實際 SQL
2. 標準化 Pipeline:所有 endpoint 遵循相同的 8-step 流程
3. 無狀態 Aggregation:Aggregation 是純粹的計算邏輯,不依賴特定 DB 結構
4. View-Only Formatter:Formatter 只做資料重組,不重新計算數值


The Standard 8-Step Pipeline

所有數據分析 endpoint 必須遵循此標準流程:

┌─────────────────────────────────────────────────────────────┐
│ 1. Validate Request                                         │
│    Layer: Endpoint                                          │
│    Input: HTTP Request                                      │
│    Output: Validated DTO                                    │
└─────────────────────────────────────────────────────────────┘
                            ↓
┌─────────────────────────────────────────────────────────────┐
│ 2. Build DimensionSpec                                      │
│    Layer: Endpoint → Service                                │
│    Input: DTO fields (e.g., groupBy, dateRange)             │
│    Output: DimensionSpec (semantic definition)              │
└─────────────────────────────────────────────────────────────┘
                            ↓
┌─────────────────────────────────────────────────────────────┐
│ 3. Resolve AggregationSpec                                  │
│    Layer: Service                                           │
│    Input: Aggregation name or type                          │
│    Output: AggregationSpec (with MetricContracts)           │
└─────────────────────────────────────────────────────────────┘
                            ↓
┌─────────────────────────────────────────────────────────────┐
│ 4. Build QueryPlan                                          │
│    Layer: Service → Infrastructure                          │
│    Input: DimensionSpec + AggregationSpec                   │
│    Output: LogicalQuery or SQL string                       │
└─────────────────────────────────────────────────────────────┘
                            ↓
┌─────────────────────────────────────────────────────────────┐
│ 5. Execute Query                                            │
│    Layer: Infrastructure                                    │
│    Input: SQL query                                         │
│    Output: Raw DB rows                                      │
└─────────────────────────────────────────────────────────────┘
                            ↓
┌─────────────────────────────────────────────────────────────┐
│ 6. Aggregate Result                                         │
│    Layer: Service                                           │
│    Input: Raw rows + AggregationSpec                        │
│    Output: Aggregated data (MetricContract values)          │
└─────────────────────────────────────────────────────────────┘
                            ↓
┌─────────────────────────────────────────────────────────────┐
│ 7. Format Visualization                                     │
│    Layer: Service → Endpoint                                │
│    Input: Aggregated data + Format type                     │
│    Output: Formatted response (chart config, table, etc.)   │
└─────────────────────────────────────────────────────────────┘
                            ↓
┌─────────────────────────────────────────────────────────────┐
│ 8. Return Response                                          │
│    Layer: Endpoint                                          │
│    Input: Formatted data                                    │
│    Output: HTTP Response (200 OK + JSON)                    │
└─────────────────────────────────────────────────────────────┘

Step-by-Step 職責說明

Step 1: Validate Request
- 檢查必要參數存在性
- 驗證日期範圍格式
- 驗證 groupBy 欄位合法性
- 回傳驗證錯誤 (400 Bad Request)

Step 2: Build DimensionSpec
- 將 HTTP 參數轉為語意化的 DimensionSpec
- 不包含任何 DB 欄位名稱
- 範例: { dimensions: ['channel', 'date'], timeGrain: 'day' }

Step 3: Resolve AggregationSpec
- 從 AggregationRegistry 取得對應的 Aggregation
- 包含需要計算的 MetricContracts
- 定義 compute 邏輯(純函數)

Step 4: Build QueryPlan
- QueryBuilder 將 DimensionSpec 轉為 LogicalQuery
- LogicalQuery 透過 DB Dialect 轉為實際 SQL
- 這是唯一知道 DB schema 的地方

Step 5: Execute Query
- Infrastructure Layer 執行 SQL
- 回傳原始 DB rows
- 處理連接池、錯誤重試

Step 6: Aggregate Result
- 使用 AggregationSpec 的 compute 函數
- 計算 MetricContract 定義的所有指標
- 輸出標準化的 aggregated data

Step 7: Format Visualization
- 根據 output format (chart/table/pivot) 重組資料
- 不做任何數值計算
- 範例: 將 flat array 轉為 nested hierarchy

Step 8: Return Response
- 包裝為 HTTP response
- 加上 metadata (total, page, etc.)
- 回傳 200 OK


Layer Responsibilities

✅ Endpoint Layer

該做的事
- ✅ Validate HTTP request
- ✅ Parse query parameters
- ✅ Build DimensionSpec from request
- ✅ Call Service methods
- ✅ Apply formatter
- ✅ Return HTTP response with status codes
- ✅ Handle endpoint-level errors (400, 404, 500)

不該做的事
- ❌ Business logic computation
- ❌ SQL query generation
- ❌ Direct DB access
- ❌ Data aggregation logic
- ❌ Metric calculation
- ❌ Join or filter logic

Checkpoint Questions
- 這個 endpoint handler 有超過 50 行嗎?(可能包含太多邏輯)
- 有看到 SQL 字串嗎?(應該在 Infrastructure)
- 有 for-loop 計算數值嗎?(應該在 Service/Domain)


✅ Application Service Layer

該做的事
- ✅ Orchestrate DimensionSpec + AggregationSpec
- ✅ Call QueryBuilder to build LogicalQuery
- ✅ Call Repository to execute query
- ✅ Aggregate raw results using AggregationSpec
- ✅ Coordinate multiple domain operations
- ✅ Transaction management (if needed)

不該做的事
- ❌ HTTP concerns (status codes, headers)
- ❌ SQL generation (delegate to QueryBuilder)
- ❌ Direct DB connection management
- ❌ Request validation (done in Endpoint)
- ❌ Visualization formatting (done in Formatter)

Checkpoint Questions
- Service 方法有處理 HTTP status code 嗎?(應該在 Endpoint)
- Service 直接使用 DB client 嗎?(應該透過 Repository)
- Service 包含 SQL template strings 嗎?(應該在 QueryBuilder)


✅ Domain Layer

該做的事
- ✅ Define DimensionSpec (semantic dimensions)
- ✅ Define AggregationSpec (metric contracts + compute logic)
- ✅ Define MetricContract (metric definitions)
- ✅ Define business rules (pure functions)
- ✅ Define interfaces for repositories
- ✅ Domain validation logic

不該做的事
- ❌ Know about DB schema (table names, column names)
- ❌ SQL query strings
- ❌ Join conditions
- ❌ DB-specific data types
- ❌ HTTP request/response structures
- ❌ Visualization formats

Checkpoint Questions
- DimensionSpec 包含 DB table 或 column 名稱嗎?(應該是語意化名稱)
- AggregationSpec 的 compute 函數有 SQL 嗎?(應該是純計算)
- Domain model 有 import DB client 嗎?(絕對禁止)

Critical Rule: Semantic Naming

// ❌ BAD: DB schema leakage
interface DimensionSpec {
  groupBy: ['user_channels.channel_name', 'DATE(created_at)']
}

// ✅ GOOD: Semantic naming
interface DimensionSpec {
  dimensions: ['channel', 'date'],
  timeGrain: 'day'
}

✅ Infrastructure Layer

該做的事
- ✅ Implement QueryBuilder (Semantic → SQL)
- ✅ Implement DB Dialect (LogicalQuery → SQL)
- ✅ DB connection management
- ✅ Execute SQL queries
- ✅ Map DB rows to domain objects
- ✅ Handle DB-specific errors

不該做的事
- ❌ Business logic (revenue calculation, conversion rate)
- ❌ Aggregation logic (should use AggregationSpec)
- ❌ Visualization transformation
- ❌ Request validation
- ❌ Define business rules

Checkpoint Questions
- QueryBuilder 包含 revenue 計算邏輯嗎?(應該在 AggregationSpec)
- Repository 做資料聚合運算嗎?(應該回傳 raw rows)
- Infrastructure 定義 business metrics 嗎?(應該在 Domain)


Development Workflows

Workflow 1: Adding a New Endpoint

檢查清單
- [ ] Step 1: Define route and handler in Endpoint Layer
- 範例: router.get('/api/analytics/revenue-by-channel', handler)
- [ ] Step 2: Implement request DTO validation
- 檢查 required fields: dateRange, groupBy, etc.
- [ ] Step 3: Build DimensionSpec from request
typescript const dimensionSpec: DimensionSpec = { dimensions: req.query.groupBy.split(','), timeGrain: req.query.timeGrain || 'day', dateRange: parseDateRange(req.query.dateRange) };
- [ ] Step 4: Resolve or create AggregationSpec
- 從 AggregationRegistry.get('revenue-by-channel') 取得
- 如果不存在,先走 Workflow 2 新增 Aggregation
- [ ] Step 5: Call Service with Specs
typescript const result = await analyticsService.aggregate(dimensionSpec, aggregationSpec);
- [ ] Step 6: Apply formatter (if needed)
typescript const formatted = chartFormatter.format(result, 'bar-chart');
- [ ] Step 7: Return HTTP response
typescript res.status(200).json({ data: formatted, metadata: { total: result.length } });
- [ ] Step 8: Add integration test
- 測試完整 request → response flow
- 驗證 response schema

參考範例: assets/templates/standard-endpoint.ts


Workflow 2: Adding a New Aggregation

檢查清單
- [ ] Step 1: Define AggregationSpec
typescript const revenueByChannelAgg: AggregationSpec = { name: 'revenue-by-channel', metrics: [/* MetricContracts */], compute: (rows) => { /* pure function */ } };
- [ ] Step 2: Define required MetricContracts
typescript const totalRevenueMetric: MetricContract = { name: 'total_revenue', type: 'currency', aggregation: 'sum', semanticField: 'transaction_amount' // NOT DB column name };
- [ ] Step 3: Implement compute formula (pure function)
- 輸入: raw DB rows
- 輸出: aggregated metrics
- 禁止: SQL, DB access, side effects
- [ ] Step 4: Register in AggregationRegistry
typescript AggregationRegistry.register(revenueByChannelAgg);
- [ ] Step 5: Test with multiple DimensionSpecs
- 測試不同 groupBy 組合
- 測試不同 time grains
- 確保 compute 邏輯通用
- [ ] Step 6: Add unit tests
- 測試 compute 函數的邊界條件
- Mock raw rows, 驗證輸出

參考範例: assets/examples/aggregation-spec.ts

Anti-Pattern 警告: 參考 assets/anti-patterns/sql-in-aggregation.ts


Workflow 3: Adding/Modifying Dimensions

檢查清單
- [ ] Step 1: Update DimensionSpec definition
typescript // 新增 'product_category' dimension type SupportedDimension = 'channel' | 'date' | 'product_category';
- [ ] Step 2: Update QueryBuilder dimension mapping
typescript // In QueryBuilder const dimensionMapping = { 'channel': 'user_channels.channel_name', 'date': 'DATE(orders.created_at)', 'product_category': 'products.category' // New mapping };
- [ ] Step 3: Update DB Dialect if needed
- 如果涉及新的 JOIN 或複雜轉換
- 範例: dimension 需要 window function
- [ ] Step 4: Test all existing Aggregations
- 確保 new dimension 不會破壞現有 aggregations
- 執行 regression tests
- [ ] Step 5: Update integration tests
- 測試新 dimension 的 groupBy 組合
- 測試 dimension + 各種 timeGrain

參考範例: assets/examples/dimension-spec.ts

Anti-Pattern 警告: 參考 assets/anti-patterns/db-fields-in-dimension.ts


Workflow 4: Adding/Modifying Output Formats

檢查清單
- [ ] Step 1: Define or update IVisualizationFormatter
typescript interface IChartFormatter { format(data: AggregatedData, chartType: string): ChartConfig; }
- [ ] Step 2: Implement format transformation
- 輸入: aggregated data (已計算完成的 metrics)
- 輸出: visualization-specific structure
- 禁止: 重新計算數值、filtering、aggregation
- [ ] Step 3: Ensure no re-computation
```typescript
// ✅ GOOD: Reorganize only
format(data) {
return data.map(row => ({ x: row.date, y: row.revenue }));
}

// ❌ BAD: Re-calculating
format(data) {
const total = data.reduce((sum, row) => sum + row.amount, 0); // NO!
return { total, items: data };
}
```
- [ ] Step 4: Test with multiple Aggregations
- 確保 formatter 通用,不綁定特定 aggregation
- 測試 edge cases (empty data, single row, etc.)
- [ ] Step 5: Update response schema
- 更新 API documentation
- 更新 TypeScript types

參考範例: assets/examples/logical-query.ts

Anti-Pattern 警告: 參考 assets/anti-patterns/formatter-computation.ts


Architecture Anti-Patterns

🚨 Anti-Pattern 1: SQL in Aggregation

問題描述
AggregationSpec 的 compute 函數包含 SQL query 或 DB-specific 邏輯。

為何有害
- 違反依賴反轉原則
- Aggregation 無法獨立測試
- 無法替換 DB 實作
- 破壞 Domain Layer 的純粹性

正確做法
- Aggregation compute 接收 raw rows,做純計算
- SQL 由 QueryBuilder 根據 MetricContract 生成
- Aggregation 不知道資料從哪來

範例: 參考 assets/anti-patterns/sql-in-aggregation.ts


🚨 Anti-Pattern 2: DB Schema in DimensionSpec

問題描述
DimensionSpec 包含 DB table names, column names, 或 JOIN conditions。

為何有害
- Domain Layer 耦合 Infrastructure
- 修改 DB schema 需要改 Domain code
- 無法抽換 data source
- 語意不清晰

正確做法
- DimensionSpec 使用語意化名稱 ('channel', 'date')
- QueryBuilder 負責 semantic → physical mapping
- DB schema 變更只影響 Infrastructure Layer

範例: 參考 assets/anti-patterns/db-fields-in-dimension.ts


🚨 Anti-Pattern 3: Computation in Formatter

問題描述
Formatter 重新計算 metrics、做 filtering、或執行 aggregation。

為何有害
- Duplication of logic
- Formatter 應該是 view layer,不該有 business logic
- 難以測試和維護
- 可能與 Aggregation 計算不一致

正確做法
- Formatter 只做資料重組和格式轉換
- 所有計算在 Aggregation Layer 完成
- Formatter 是 stateless, pure function

範例: 參考 assets/anti-patterns/formatter-computation.ts


🚨 Anti-Pattern 4: Divergent Endpoint Flow

問題描述
Endpoint 不遵循標準 8-step pipeline,直接呼叫 Repository 或包含 SQL。

為何有害
- 架構不一致,難以維護
- 繞過 Domain Layer 的 business rules
- 無法 reuse Aggregation logic
- Code duplication

正確做法
- 所有 endpoint 遵循相同 pipeline
- 透過 DimensionSpec + AggregationSpec 表達需求
- 讓 Service Layer 協調流程

範例: 參考 assets/anti-patterns/divergent-endpoint.ts


Quick Reference

核心抽象定義

DimensionSpec: 語意化的維度定義,不知道 DB schema

interface DimensionSpec {
  dimensions: string[];      // e.g., ['channel', 'date']
  timeGrain?: 'hour' | 'day' | 'week' | 'month';
  dateRange?: { start: Date; end: Date };
}

AggregationSpec: Metric contracts + 計算邏輯

interface AggregationSpec {
  name: string;
  metrics: MetricContract[];
  compute: (rows: any[]) => AggregatedData;
}

MetricContract: 單一指標的定義

interface MetricContract {
  name: string;
  type: 'number' | 'currency' | 'percentage';
  aggregation: 'sum' | 'avg' | 'count' | 'min' | 'max';
  semanticField: string;  // Semantic name, NOT DB column
}

LogicalQuery: 中間層 query representation

interface LogicalQuery {
  select: MetricContract[];
  groupBy: string[];  // Semantic dimension names
  where?: FilterCondition[];
  orderBy?: OrderByClause[];
}

依賴方向圖

┌─────────────────────────────────────────────────────────┐
│                    Endpoint Layer                       │
│                (HTTP Request/Response)                  │
└────────────────────┬────────────────────────────────────┘
                     ↓
┌─────────────────────────────────────────────────────────┐
│              Application Service Layer                  │
│         (Orchestration, DimensionSpec,                  │
│               AggregationSpec)                          │
└────────────────────┬────────────────────────────────────┘
                     ↓
┌─────────────────────────────────────────────────────────┐
│                  Domain Layer                           │
│        (Business Rules, Contracts, Specs)               │
│             [No DB knowledge]                           │
└────────────────────┬────────────────────────────────────┘
                     ↓
┌─────────────────────────────────────────────────────────┐
│             Infrastructure Layer                        │
│     (QueryBuilder, DB Dialect, Repository)              │
│      [Only layer that knows DB schema]                  │
└─────────────────────────────────────────────────────────┘

依賴規則
- Endpoint → Service → Domain → Infrastructure (單向)
- Domain 定義 interface,Infrastructure 實作
- Infrastructure 不能被 Domain import

常用檢查項目

Before committing code, check:
- [ ] 所有 endpoint 遵循 8-step pipeline
- [ ] DimensionSpec 無 DB column names
- [ ] AggregationSpec compute 無 SQL
- [ ] Formatter 無重新計算 metrics
- [ ] Domain Layer 無 import DB client
- [ ] 所有 semantic names 有對應的 QueryBuilder mapping

When reviewing PRs, look for:
- [ ] SQL strings in Domain or Service Layer
- [ ] DB schema knowledge in DimensionSpec
- [ ] Business logic in Endpoint handlers
- [ ] Aggregation logic in Formatters
- [ ] Direct DB access in Service Layer


Detailed References

需要更詳細的資訊時,參考以下文件:

  • Domain Models 詳解: references/domain-models.md
  • DimensionSpec 完整定義和使用範例
  • AggregationSpec 設計原則
  • MetricContract 類型系統
  • LogicalQuery 結構說明

  • Query Builder 指南: references/query-builder-guide.md

  • Semantic → Physical 轉換機制
  • DB Dialect 實作範例
  • SQL 構建最佳實踐
  • JOIN 和 subquery 處理

  • Formatter 指南: references/formatter-guide.md

  • Formatter 職責邊界
  • Chart/Table/Pivot 格式轉換
  • 通用格式定義
  • Edge case 處理

Code Templates

完整程式碼範例:

  • Standard Endpoint: assets/templates/standard-endpoint.ts
  • DimensionSpec Example: assets/examples/dimension-spec.ts
  • AggregationSpec Example: assets/examples/aggregation-spec.ts
  • MetricContract Example: assets/examples/metric-contract.ts
  • LogicalQuery Example: assets/examples/logical-query.ts

反面教材 (請勿模仿):
- assets/anti-patterns/sql-in-aggregation.ts
- assets/anti-patterns/db-fields-in-dimension.ts
- assets/anti-patterns/formatter-computation.ts
- assets/anti-patterns/divergent-endpoint.ts


Summary

遵循這個指南,確保:
1. ✅ 所有 endpoint 遵循標準 8-step pipeline
2. ✅ Domain Layer 保持純粹,不知道 DB schema
3. ✅ Infrastructure Layer 集中處理 SQL 生成
4. ✅ Aggregation 是可 reuse 的 pure functions
5. ✅ Formatter 只做 view transformation

當有疑問時,問自己:
- 這段 code 放在哪一層?
- 它有違反該層的職責嗎?
- 它有依賴不該依賴的東西嗎?

遵循這些原則,系統將保持高擴充性和可維護性。

# README.md

Data Analysis Backend Development Skill

這是一個 Claude Code skill,用於指導 Agent 遵循數據分析後端的架構設計原則和開發流程。

快速開始

5 分鐘安裝

# 1. 複製到 Claude Code skills 目錄
# Windows (PowerShell)
xcopy /E /I . "$env:USERPROFILE\.claude\skills\data-analysis-backend"

# macOS/Linux
cp -r . ~/.claude/skills/data-analysis-backend

# 2. 重啟 Claude Code

# 3. 測試 skill
# 在 Claude Code 中問: "幫我新增一個數據分析 endpoint"

第一次使用

安裝後,嘗試以下任務來體驗 skill:

  1. 新增 Endpoint
    幫我實作一個按渠道分析收入的 endpoint
    Agent 會遵循 8-step pipeline 建立完整的 endpoint

  2. 新增 Aggregation
    幫我新增一個轉換率分析的 aggregation
    Agent 會建立 AggregationSpec 和 MetricContracts

  3. Code Review
    請檢查這段 code 是否符合數據分析後端的架構原則 [貼上你的 code]
    Agent 會檢查是否遵循層級職責和避免 anti-patterns

目標

  • 指導開發流程: 明確告訴 Agent 在新增 endpoint、aggregation、dimension、formatter 時需要做哪些改動
  • 維護架構一致性: 確保所有實作遵循三層架構和依賴反轉原則
  • 防止常見錯誤: 明確標示每一層該出現和不該出現的邏輯
  • 提供完整參考: 包含資料流 pipeline、層級依賴關係和程式碼範例

Skill 結構

data_analysis_skill/
├── SKILL.md                          # 主指引文件 (核心內容)
├── README.md                         # 本文件
│
├── references/                       # 詳細參考文件
│   ├── domain-models.md              # Domain Model 詳解
│   ├── query-builder-guide.md        # Query Builder 架構指南
│   └── formatter-guide.md            # Formatter 指南
│
└── assets/                           # 程式碼範例和模板
    ├── templates/
    │   └── standard-endpoint.ts      # 標準 endpoint 完整範例
    │
    ├── examples/
    │   ├── dimension-spec.ts         # DimensionSpec 範例
    │   ├── aggregation-spec.ts       # AggregationSpec 範例
    │   ├── metric-contract.ts        # MetricContract 範例
    │   └── logical-query.ts          # LogicalQuery 範例
    │
    └── anti-patterns/                # 反面教材 (常見錯誤)
        ├── sql-in-aggregation.ts     # ❌ Aggregation 包含 SQL
        ├── db-fields-in-dimension.ts # ❌ Dimension 混入 DB 欄位
        ├── formatter-computation.ts  # ❌ Formatter 重新計算數值
        └── divergent-endpoint.ts     # ❌ Endpoint 流程偏離標準

核心概念

標準 8-Step Pipeline

所有數據分析 endpoint 必須遵循此標準流程:

  1. Validate Request (Endpoint Layer)
  2. Build DimensionSpec (Endpoint → Service)
  3. Resolve AggregationSpec (Service)
  4. Build QueryPlan (Service → Infrastructure)
  5. Execute Query (Infrastructure)
  6. Aggregate Result (Service)
  7. Format Visualization (Service → Endpoint)
  8. Return Response (Endpoint)

三層架構

Endpoint Layer (HTTP)
    ↓
Application Service Layer (Orchestration)
    ↓
Domain Layer (Business Logic)
    ↓
Infrastructure Layer (DB/SQL)

核心抽象

  • DimensionSpec: 語意化的維度定義 (不知道 DB schema)
  • AggregationSpec: Metric contracts + 計算邏輯 (pure function)
  • MetricContract: 單一指標的定義 (name, type, aggregation)
  • LogicalQuery: 中間層 query representation (連接 semantic 和 physical)

安裝指南

方法 1: 直接複製到 Skills 目錄 (推薦)

這是最簡單直接的方法:

  1. 找到 Claude Code 的 skills 目錄
    ```bash
    # Windows
    %USERPROFILE%.claude\skills\

# macOS/Linux
~/.claude/skills/
```

  1. 複製整個 skill 目錄
    ```bash
    # Windows (在 PowerShell 或 CMD)
    xcopy /E /I C:\Users\andyc\Projects\data_analysis_skill %USERPROFILE%.claude\skills\data-analysis-backend

# macOS/Linux
cp -r /path/to/data_analysis_skill ~/.claude/skills/data-analysis-backend
```

  1. 驗證安裝
  2. 重啟 Claude Code (如果正在運行)
  3. 檢查 skills 目錄中是否有 data-analysis-backend 資料夾
  4. 確認 SKILL.md 檔案存在且有正確的 YAML frontmatter

  5. 測試 skill

  6. 開啟 Claude Code
  7. 詢問: "幫我新增一個數據分析 endpoint"
  8. Agent 應該會自動使用此 skill 並遵循 8-step pipeline

如果你想要持續更新 skill 並立即看到效果:

# Windows (需要管理員權限)
mklink /D "%USERPROFILE%\.claude\skills\data-analysis-backend" "C:\Users\andyc\Projects\data_analysis_skill"

# macOS/Linux
ln -s /path/to/data_analysis_skill ~/.claude/skills/data-analysis-backend

這樣修改原始檔案就會立即反映到 Claude Code 中。

方法 3: 打包為 .skill 檔案 (進階)

如果你有 Claude Code 的 packaging tools:

  1. 打包 skill
    bash # 假設有 package_skill.py 工具 python package_skill.py data_analysis_skill

  2. 安裝打包後的 .skill 檔案
    bash # 將 .skill 檔案複製到 skills 目錄 cp data-analysis-backend.skill ~/.claude/skills/

  3. 驗證
    bash # 假設有 quick_validate.py 工具 python quick_validate.py ~/.claude/skills/data-analysis-backend.skill

安裝後驗證

  1. 檢查檔案結構
    bash # 確認以下檔案存在 ~/.claude/skills/data-analysis-backend/ ├── SKILL.md # 必須存在 ├── README.md ├── references/ │ ├── domain-models.md │ ├── query-builder-guide.md │ └── formatter-guide.md └── assets/ ├── templates/ ├── examples/ └── anti-patterns/

  2. 檢查 YAML Frontmatter

  3. 打開 SKILL.md
  4. 確認開頭有正確的 YAML frontmatter:
    ```yaml

name: data-analysis-backend
description: "Use when implementing or modifying data analysis backend features..."


```

  1. 重啟 Claude Code
  2. 完全關閉 Claude Code
  3. 重新啟動
  4. Skills 會在啟動時自動載入

卸載 Skill

如果需要移除此 skill:

# Windows
rmdir /S /Q "%USERPROFILE%\.claude\skills\data-analysis-backend"

# macOS/Linux
rm -rf ~/.claude/skills/data-analysis-backend

使用方式

觸發 Skill

當 Agent 需要實作或修改數據分析後端功能時,會自動使用此 skill,包括:

  • 新增 endpoint
  • 新增 aggregation
  • 新增/修改 dimension
  • 新增/修改 formatter

手動觸發 (可選)

你也可以明確要求使用此 skill:

使用 data-analysis-backend skill 幫我新增一個收入分析的 endpoint

驗證 Skill 正在使用

當 Agent 使用此 skill 時,你會看到:
- Agent 會提到 "8-step pipeline"
- Agent 會建立 DimensionSpec 和 AggregationSpec
- Agent 會明確分離 Endpoint/Service/Domain/Infrastructure layers
- Agent 會避免在 Domain Layer 使用 SQL

故障排除

Skill 沒有被自動使用

問題: Agent 沒有使用此 skill

解決方法:
1. 檢查 SKILL.md 的 description 是否包含觸發關鍵字:
- "data analysis backend"
- "endpoint", "aggregation", "dimension", "formatter"

  1. 嘗試明確提到這些關鍵字:
    幫我實作一個數據分析的 endpoint (data analysis endpoint)

  2. 手動指定 skill:
    使用 data-analysis-backend skill

找不到 Skills 目錄

問題: 不確定 skills 目錄在哪裡

解決方法:

# 在 Claude Code 中執行
echo $HOME/.claude/skills/     # macOS/Linux
echo %USERPROFILE%\.claude\skills\   # Windows

# 或者使用 Claude Code 命令 (如果支援)
/skills list

YAML Frontmatter 格式錯誤

問題: Skill 無法載入,可能是 YAML 格式問題

解決方法:
1. 確認 SKILL.md 開頭的 YAML 格式:
```yaml


name: data-analysis-backend
description: "Use when implementing..."


```

  1. 注意事項:
  2. --- 必須在檔案第一行
  3. name 和 description 是必須的
  4. description 需要用雙引號包起來
  5. YAML 結束也需要 ---

Skills 目錄權限問題

問題: 無法複製檔案到 skills 目錄

解決方法:

# 檢查權限
ls -la ~/.claude/skills/    # macOS/Linux
dir %USERPROFILE%\.claude\skills\   # Windows

# 如果目錄不存在,手動建立
mkdir -p ~/.claude/skills/  # macOS/Linux
mkdir %USERPROFILE%\.claude\skills\  # Windows

問題: Windows 建立 symbolic link 失敗

解決方法:
1. 以管理員身份執行 PowerShell 或 CMD
2. 或者使用方法 1 (直接複製) 代替

檢查 Skill 是否正確載入

驗證步驟:
1. 重啟 Claude Code
2. 執行以下測試:
請告訴我你有哪些可用的 skills?
3. 如果 Agent 提到 "data-analysis-backend",表示載入成功

更新 Skill 內容

如果修改了 skill 內容:
1. 直接複製: 重新複製整個目錄
2. Symbolic Link: 修改會自動生效
3. 重啟 Claude Code: 確保變更被載入

主要參考文件

  1. 快速開始: 閱讀 SKILL.md 的 Overview 和 8-Step Pipeline
  2. 開發任務: 查看 SKILL.md 的 Development Workflows 對應的檢查清單
  3. 詳細實作: 參考 references/ 下的詳細指南
  4. 程式碼範例: 查看 assets/templates/assets/examples/
  5. 避免錯誤: 檢查 assets/anti-patterns/ 的反面教材

開發工作流程範例

新增一個 Endpoint

  1. 閱讀 SKILL.md → "Workflow 1: Adding a New Endpoint"
  2. 參考 assets/templates/standard-endpoint.ts 的完整範例
  3. 遵循 8-step pipeline 實作
  4. 檢查 assets/anti-patterns/divergent-endpoint.ts 避免常見錯誤

新增一個 Aggregation

  1. 閱讀 SKILL.md → "Workflow 2: Adding a New Aggregation"
  2. 參考 assets/examples/aggregation-spec.ts 的範例
  3. 定義 MetricContracts (參考 assets/examples/metric-contract.ts)
  4. 實作 compute 函數 (pure function, 無 SQL)
  5. 註冊到 AggregationRegistry
  6. 檢查 assets/anti-patterns/sql-in-aggregation.ts 避免 SQL 耦合

新增一個 Dimension

  1. 閱讀 SKILL.md → "Workflow 3: Adding/Modifying Dimensions"
  2. 參考 assets/examples/dimension-spec.ts
  3. 更新 QueryBuilder 的 dimensionMapping (Infrastructure Layer)
  4. 測試所有現有 Aggregations
  5. 檢查 assets/anti-patterns/db-fields-in-dimension.ts 避免 schema leakage

新增一個 Formatter

  1. 閱讀 SKILL.md → "Workflow 4: Adding/Modifying Output Formats"
  2. 參考 references/formatter-guide.md
  3. 實作 IVisualizationFormatter interface
  4. 確保只做資料重組,不重新計算
  5. 檢查 assets/anti-patterns/formatter-computation.ts 避免重複計算

架構原則

✅ 該做的事

Endpoint Layer
- ✅ Request validation
- ✅ Response formatting
- ✅ HTTP status codes

Service Layer
- ✅ Orchestration logic
- ✅ Spec composition
- ✅ Result aggregation

Domain Layer
- ✅ Business rules
- ✅ Semantic definitions
- ✅ Contracts (interfaces)

Infrastructure Layer
- ✅ SQL generation
- ✅ DB connections
- ✅ Query execution

❌ 不該做的事

Endpoint Layer
- ❌ Business logic
- ❌ SQL queries
- ❌ Data transformation

Service Layer
- ❌ HTTP concerns
- ❌ SQL generation
- ❌ DB connection

Domain Layer
- ❌ DB schema knowledge
- ❌ SQL strings
- ❌ Join logic

Infrastructure Layer
- ❌ Business rules
- ❌ Aggregation logic
- ❌ Visualization logic

關鍵檢查項目

在 commit 前檢查:

  • [ ] 所有 endpoint 遵循 8-step pipeline
  • [ ] DimensionSpec 無 DB column names
  • [ ] AggregationSpec compute 無 SQL
  • [ ] Formatter 無重新計算 metrics
  • [ ] Domain Layer 無 import DB client
  • [ ] 所有 semantic names 有對應的 QueryBuilder mapping

成功標準

這個 skill 成功的標準:

  1. 清晰度: Agent 能立即知道在特定任務時需要做什麼
  2. 完整性: 涵蓋所有開發場景
  3. 防護性: 明確標示各層職責邊界,防止架構腐化
  4. 實用性: 提供完整可執行的程式碼範例
  5. 可維護性: 結構清晰,易於後續擴充和更新

常見問題 (FAQ)

Q1: 這個 skill 適合什麼類型的專案?

A: 適合任何需要數據分析後端的專案,特別是:
- BI/Analytics 平台
- Dashboard 後端 API
- 報表系統
- 數據查詢服務
- 多維度分析系統

Q2: 這個 skill 會自動寫 code 嗎?

A: 不完全是。這個 skill 是指導手冊,它會:
- ✅ 告訴 Agent 該遵循什麼架構原則
- ✅ 提供完整的 code 範例供參考
- ✅ 防止 Agent 犯常見的架構錯誤
- ✅ 確保 Agent 遵循標準 pipeline

Agent 仍然會根據你的具體需求寫 code,但會遵循這個 skill 的指引。

Q3: 我可以修改這個 skill 嗎?

A: 當然可以!這個 skill 是開源的,你可以:
- 修改 SKILL.md 的內容
- 新增你自己的範例到 assets/examples/
- 加入更多 anti-patterns 到 assets/anti-patterns/
- 調整 references/ 的詳細文件

修改後,如果使用 Symbolic Link 安裝,會立即生效;如果是直接複製,需要重新複製。

Q4: 這個 skill 與其他 skills 衝突嗎?

A: 不會。這個 skill 專注於數據分析後端架構,與其他 skills (如 UI/UX, testing, deployment) 是互補的。

Q5: 我的專案用不同的架構可以用這個 skill 嗎?

A: 可以,但可能需要調整。這個 skill 基於:
- Clean Architecture
- Three-Layer Architecture (Endpoint-Service-Domain-Infrastructure)
- Dependency Inversion Principle

如果你的專案使用不同架構,可以修改 SKILL.md 的內容來匹配你的架構。

Q6: Skill 檔案很多,Agent 會一次讀完嗎?

A: 不會。Skill 設計採用漸進式揭露:
- Agent 會先讀 SKILL.md (主指引)
- 需要詳細資訊時才讀 references/
- 需要範例時才讀 assets/

這樣可以避免 context 過載。

Q7: 如何知道 Agent 有在使用這個 skill?

A: 觀察 Agent 的回應:
- 會提到 "8-step pipeline"
- 會明確建立 DimensionSpec, AggregationSpec
- 會避免在 Aggregation 寫 SQL
- 會分離 Endpoint/Service/Domain layers

如果沒有,可以明確說: "請使用 data-analysis-backend skill"

Q8: 這個 skill 支援哪些程式語言?

A: 範例使用 TypeScript,但概念適用於任何語言:
- Python: FastAPI + SQLAlchemy
- Java: Spring Boot + JPA
- C#: ASP.NET Core + Entity Framework
- Go: Gin + GORM

架構原則是語言無關的。

Q9: 我可以分享這個 skill 給團隊嗎?

A: 當然可以!建議方式:
1. 將此 skill 加入你的專案 repo
2. 在 README 說明如何安裝
3. 團隊成員各自安裝到自己的 ~/.claude/skills/

這樣整個團隊都會遵循相同的架構原則。

Q10: Skill 多久更新一次?

A: 目前版本是 1.0.0。建議:
- 根據團隊實際使用經驗更新
- 發現新的 anti-patterns 時加入
- 有更好的範例時替換
- 架構演進時同步更新

貢獻

如果你有改進建議:
1. 修改對應的檔案
2. 測試修改後的效果
3. 分享你的經驗

建議的改進方向:
- 新增更多程式語言的範例
- 加入更多 anti-patterns
- 補充更多 edge cases 的處理方式
- 新增效能優化的指引

相關文件

  • analytics_backend_plan.md: 原始架構設計計劃
  • SKILL.md: 主要指引文件 (必讀)
  • summary.md: 實作總結

授權

本 skill 為開源專案,可自由使用、修改和分發。

版本

  • Version: 1.0.0
  • Last Updated: 2026-01-31
  • Author: Andy Chen (with Claude)

支援

如果遇到問題:
1. 查看本 README 的「故障排除」章節
2. 檢查 SKILL.md 的內容是否正確
3. 確認 Claude Code 已正確安裝並運行


開始使用: 返回到快速開始章節開始安裝!

# Supported AI Coding Agents

This skill is compatible with the SKILL.md standard and works with all major AI coding agents:

Learn more about the SKILL.md standard and how to use these skills with your preferred AI coding agent.