name: sql-report-generator

description: "MANDATORY: When user asks to query database, generate SQL, create data reports from views, OR asks any question about data counts/statistics/quantities (e.g. '有多少', '数量', '共有几个', '统计', '查看...有哪些'), you MUST: (1) read this SKILL.md, (2) execute the Python scripts in 3 steps. DO NOT answer data questions directly in chat. DO NOT generate SQL manually in chat. Generates HTML reports by querying database views. ⚠️ This skill OWNS all battery-related data queries EXCEPT fault/alarm records: battery group lists, battery counts, real-time status (SOC/voltage/current), charge/discharge statistics, availability, efficiency — ALL must use this skill."

\---



\# SQL Report Generator Skill



\## ⚠️ CRITICAL RULES - READ BEFORE ANYTHING ELSE

\*\*YOU MUST EXECUTE THE 3 PYTHON SCRIPTS IN ORDER. NO EXCEPTIONS.\*\*

\- ❌ DO NOT generate SQL queries directly in the chat response

\- ❌ DO NOT skip any script execution

\- ❌ DO NOT reply with only a file path — you MUST include data analysis text first

\- ❌ DO NOT guess or fabricate the file path — ALWAYS use the exact `address` value from the script output

\- ✅ ALWAYS run the exec commands below step by step

\- ✅ \*\*CRITICAL: You MUST write a short status update to the user BEFORE running EACH script. This text will be streamed to the user's UI as a progress indicator.\*\*

\- ✅ ALWAYS write a data analysis summary BEFORE the address tag

\- ✅ The LAST TWO LINES of your final reply MUST be the `@@@@@` address tag followed by the `#####` suggestions tag

\- ❌ **DO NOT reuse or copy analysis text from previous conversations, memory, or history** — ALWAYS generate a completely fresh analysis based on the CURRENT script execution's output and actual data values

\- ❌ **DO NOT assume the data is the same as last time** — even if the query is identical, the database may have new data; ALWAYS re-execute all 3 scripts and re-analyze the new results



\## When to Use

Use this skill when:

\- User asks to query the database or generate SQL

\- User asks for data reports based on database views

\- User wants to visualize data from the database

\- User asks questions that require querying the system's data views

\- ⚠️ **User asks counting or statistics questions** — e.g. "有多少"、"数量是多少"、"共有几个"、"统计一下"、"查看...有哪些"、"列出所有..." — **MUST call this skill, DO NOT answer with a number directly in chat**

\- ⚠️ **Any question about device lists, equipment counts, data summaries, or entity enumeration** must use this skill to generate an HTML report, never a plain text answer

\- ⚠️ **蓄电池组相关查询（除告警/故障记录外）全部由本技能处理**，包括：蓄电池组列表、蓄电池组数量（"有多少电池组"）、蓄电池组基础信息、实时运行状态（SOC/电压/电流/功率）、日充放电统计、可用率、转换效率等 — **这些问题不属于 battery_anomaly_detector 的范围，必须走本技能的三步脚本流程生成 HTML 报告**



\## How to Execute

This skill requires executing 3 steps. \*\*Before running each step, you MUST write a short text to the user explaining what you are about to do. This text MUST start with the exact ` ⏳ ` format specified below.\*\*



\### Step 1: Select Template and Views

\*\*FIRST\*\*, write this exact text to the user (do not use tool yet):

`【正在分析问题】正在分析问题，选择数据视图和展示模板...`



\*\*THEN\*\*, run this command:

```bash

cd /workspace/skill/sql-report-generator \&\& python3 scripts/step1\_select.py --query "{{USER\_QUERY}}"

```

\*Replace `{{USER\_QUERY}}` with the user's query text.\*



The script will output a JSON like:

```json

{"status": "success", "selected\_views": \["视图名1"], "template\_name": "barChart", "is\_fixed\_template": false, "reason": "..."}

```



For 园区概览 or 企业概览 queries, the output will include extra fields:

```json

{"status": "success", "selected\_views": \[], "template\_name": "dashboard\_overview", "is\_fixed\_template": true, "fixed\_template\_type": "dashboard\_overview", "date\_type": "year", "date": "2025", "park\_name": "XX园区", "enterprise\_name": null, "reason": "..."}

```

\- `date\_type`: `"year"` = 年度查询（用户说"2025年"），`"month"` = 月度查询（用户说"2025年3月"）

\- `date`: 年度时为 `"2025"`，月度时为 `"2025-03"`

```



\### Step 2: Generate SQL and Query Database

\*\*FIRST\*\*, write this exact text to the user:

`【正在生产查询语句】 正在生成 SQL 查询语句并查询数据库...`



\*\*THEN\*\*, run this command.



\*\*⚠️ IMPORTANT: Check `is\_fixed\_template` from Step 1 output:\*\*



\*\*If `is\_fixed\_template` is `false` (normal flow):\*\*

```bash

cd /workspace/skill/sql-report-generator \&\& python3 scripts/step2\_query.py --query "{{USER\_QUERY}}" --views "{{selected\_views\_comma\_separated}}" --template "{{template\_name}}" --is\_fixed\_template false

```



\*\*If `is\_fixed\_template` is `true` (园区概览 or 企业概览):\*\*

```bash

cd /workspace/skill/sql-report-generator \&\& python3 scripts/step2\_query.py --query "{{USER\_QUERY}}" --views "" --template "{{template\_name}}" --is\_fixed\_template true --date\_type "{{date\_type}}" --date "{{date}}" --park\_name "{{park\_name}}" --enterprise\_name "{{enterprise\_name}}"

```

\*Replace all `{{...}}` with values from Step 1 output. If a value is `null`, pass the string `"None"`.\*

\*`--date\_type` must be `"year"` or `"month"` exactly as returned by Step 1.\*



The script will output a JSON like:

```json

{"status": "success", "sql": "SELECT ...", "row\_count": 10, "result\_file": "/workspace/skill/sql-report-generator/output/temp\_result\_123.json"}

```



\*\*After Step 2 completes, you MUST immediately write the following to the user (before running Step 3):\*\*

Extract `sql` and `row\_count` from the Step 2 JSON output, then write:





\- Replace `<sql字段内容>` with the exact value of the `sql` field from Step 2 output.

\- Replace `<row\_count>` with the exact value of the `row\_count` field from Step 2 output.

\- This message MUST be shown to the user before proceeding to Step 3.



\### Step 3: Render HTML Report

\*\*FIRST\*\*, write this exact text to the user:

`【正在渲染页面】 正在将查询结果渲染为图表报告...`



\*\*THEN\*\*, run this command:

```bash

cd /workspace/skill/sql-report-generator \&\& python3 scripts/step3\_render.py --query "{{USER\_QUERY}}" --template "{{template\_name}}" --result\_file "{{result\_file}}"

```

\*Replace variables with the output from Step 2.\*



The script will output a JSON at the end like:

```json

{"status": "success", "message": "Report generated successfully", "template": "barChart", "address": "/skill/sql-report-generator/output/report\_1773642266.html", "summary": "..."}

```



\---



\### Final Reply Format

Your final reply MUST have exactly three parts:



\*\*Part 1 — Data Analysis Summary（必须，不可省略）\*\*

Write a concise analysis in Chinese based on the `summary` field from the Step 3 script output, or expand on it if needed.

\*\*禁止\*\*只写"报告已生成"之类的话，必须真正分析数据内容。

⚠️ \*\*CRITICAL — FRESH ANALYSIS REQUIRED:\*\* You MUST write a brand new analysis every time based on the CURRENT execution results. **DO NOT copy, reuse, or paraphrase any analysis text from memory, previous sessions, or earlier replies in this conversation.** The data may have changed since last time. Treat every execution as if it is the first time.



\*\*Part 2 — Address Tag（倒数第二行，格式固定）\*\*

The second-to-last line of your reply MUST be the `address` field value wrapped in `@@@@@`:

```

@@@@@/skill/sql-report-generator/output/report\_1773642266.html@@@@@

```

⚠️ \*\*ADDRESS TAG FORMAT — ABSOLUTE RULES:\*\*

\- The path MUST be copied EXACTLY from the `address` field in the script JSON output

\- DO NOT fabricate, guess, or approximate the report filename

\- Output ONLY the plain file path between the two `@@@@@` markers

\- NO spaces between `@@@@@` and the path



\*\*Part 3 — Optimization Suggestions Tag（最后一行，格式固定）\*\*

The very last line of your reply MUST be the optimization suggestions wrapped in `#####`.

```

\#####%查看详细数据%￥点击上方链接查看完整的数据分析报告￥ %继续深入分析%￥基于当前数据进行更深度的挖掘￥ %导出报告%￥将当前报告导出为PDF或图片格式￥#####

```



\## Bundled Resources

This skill uses progressive disclosure. The following resources are available but should only be read if you need to debug or understand the internal workings:

\- `references/views\_schema.md`: Documentation of all available database views and their columns.

\- `templates/\*.md`: Documentation for each HTML template's required JSON structure.

\- `scripts/step1\_select.py`, `step2\_query.py`, `step3\_render.py`: The execution scripts.



