@pn-data/pn-data-analysis 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -2,12 +2,30 @@
2
2
 
3
3
  Cursor Agent Skill — 通过 pn_mid_v2 中台 API 查询项目的**流水、活跃、留存、来源**数据。
4
4
 
5
+ ## 环境变量配置
6
+
7
+ 使用前需配置认证密钥。
8
+
9
+ 密钥在**中台右上角 -> 生成数据分析密钥**中可以获取。
10
+
11
+ ### 必需变量
12
+
13
+ 密钥必须写入 shell 配置文件(zsh 用户写 `~/.zshrc`,bash 用户写 `~/.bashrc`),以确保所有终端会话和 AI 工具都能读取到:
14
+
15
+ ```bash
16
+ export PN_DATA_ANALYSIS_TOKEN="your-token-here" # 数据分析密钥
17
+ ```
18
+
19
+ 将上面内容写入对应的 shell 配置文件后执行 `source` 使其生效。
20
+
21
+ > **注意**:仅在当前终端 `export` 而不写入配置文件的话,新终端窗口或 AI Agent 启动的 shell 会话将无法读取到密钥。Skill 在每次会话首次使用时会自动检查配置文件中是否存在密钥,如果缺失会自动写入。
22
+
5
23
  ## 安装
6
24
 
7
25
  ```bash
8
26
  # 作为项目 Cursor Skill
9
- npm install @pn/pn-data-analysis
10
- # 将 node_modules/@pn/pn-data-analysis/pn-data-analysis/ 复制或链接到 .cursor/skills/
27
+ npm install @pn-data/pn-data-analysis
28
+ # 将 node_modules/@pn-data/pn-data-analysis/pn-data-analysis/ 复制或链接到 .cursor/skills/
11
29
  ```
12
30
 
13
31
  ## 使用方式
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@pn-data/pn-data-analysis",
3
- "version": "0.0.1",
4
- "description": "Cursor Agent Skill for querying pn_mid_v2 project data (pay, activity, retention, source)",
3
+ "version": "0.0.2",
4
+ "description": "Cursor Agent Skill for querying pn_mid_v2 project data",
5
5
  "keywords": [
6
6
  "cursor",
7
7
  "cursor-skill",
@@ -20,6 +20,18 @@ description: Analyzes project data on the middle platform (pn_mid_v2). Use when
20
20
 
21
21
  ---
22
22
 
23
+ ## 执行前:检查 PN_DATA_ANALYSIS_TOKEN
24
+
25
+ **每次执行查询前**必须确保环境变量 `PN_DATA_ANALYSIS_TOKEN` 已存在:
26
+
27
+ 1. **先检查**:`printenv PN_DATA_ANALYSIS_TOKEN` 或 `echo $PN_DATA_ANALYSIS_TOKEN`
28
+ 2. **若为空或未设置**:先执行 `source` 加载用户 shell 配置
29
+ 3. **再执行**下面的查询命令
30
+
31
+ 密钥需用户在中台右上角 -> 生成数据分析密钥 获取并写入 `~/.zshrc` 或 `~/.bashrc`:`export PN_DATA_ANALYSIS_TOKEN="..."`
32
+
33
+ ---
34
+
23
35
  ## 查询命令
24
36
 
25
37
  脚本位置:`.cursor/skills/pn-data-analysis/pn_query.py`
@@ -18,16 +18,25 @@ pn_query.py - 远程查询 pn_mid_v2 中台数据
18
18
 
19
19
  import argparse
20
20
  import json
21
+ import os
21
22
  import sys
22
23
  import urllib.request
23
24
  from calendar import monthrange
24
25
  from datetime import datetime
25
26
 
26
27
  API_BASE = "https://pnv2.17995api.net"
28
+ # API_BASE = "http://localhost:811" # 本地测试
27
29
  API_PATH = "/service/pn_data_analysis"
30
+ ENV_TOKEN = "PN_DATA_ANALYSIS_TOKEN"
28
31
 
29
32
 
30
33
  def query(pid: int, data_type: str, month: str, app_platform: int = 0, group: int = 2) -> dict:
34
+ token = os.environ.get(ENV_TOKEN)
35
+ if not token:
36
+ raise SystemExit(
37
+ f"未配置 {ENV_TOKEN}。请在中台右上角 -> 生成数据分析密钥 获取后写入 shell 配置(如 ~/.zshrc)并 source 生效。"
38
+ )
39
+
31
40
  st_date = datetime.strptime(f"{month}-01", "%Y-%m-%d")
32
41
  y, m = st_date.year, st_date.month
33
42
  _, last = monthrange(y, m)
@@ -43,7 +52,10 @@ def query(pid: int, data_type: str, month: str, app_platform: int = 0, group: in
43
52
  "useCache": True,
44
53
  }
45
54
  url = f"{API_BASE}{API_PATH}"
46
- headers = {"Content-Type": "application/json;charset=UTF-8"}
55
+ headers = {
56
+ "Content-Type": "application/json;charset=UTF-8",
57
+ "X-Pn-Token": token,
58
+ }
47
59
  req = urllib.request.Request(
48
60
  url,
49
61
  data=json.dumps(payload).encode("utf-8"),