File size: 3,748 Bytes
e8c001c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
---
id: offline-compute_MySQL_mysql_014
name: Notebook执行实例明细按天拆分与引擎关联
category: offline-compute/MySQL
timeout_seconds: 1800
modality: pure-text
engine: mysql
---
## Prompt
我需要你生成一段 MySQL 代码,从 Notebook runner trace 数据中筛选出存在 execute.code 等代码执行 span 的 trace,按天拆分跨天运行记录,计算实例/代码/资源等待时长等指标,关联引擎信息,产出每日执行实例明细表。

**业务背景与目标**:Notebook runner 在运行过程中会产生多个 trace span(如 runner.execute、execute.code、set.permanent.compute 等)。本任务需要筛选出包含代码执行 span 的 trace,对跨天运行的记录按天拆分,计算各 span 类别的运行时长,并左关联引擎信息(serving_id、is_permanent、GPU 申请数量),最终写入每日执行实例明细表。

**输入表(全名 + 简要描述)**- `internal_platform_db.notebook_span_info_mysql_014`(Notebook trace span 数据)
- `internal_platform_db.notebook_engine_info_mysql_014`(引擎信息)

(各表结构与字段含义请自行连接数据库查询确认)

**处理规则**1. 过滤 notebook_span_info:databus_imp_date 在 ['2026050400', '2026050700'] 范围内,且 trace_id 满足子查询条件(compute_type='ray'、service_name='notebook-runner'、span_name='runner.execute'),保留 span_name 为 'runner.execute' 或在 ['execute.code', 'execute.code.cell', 'client.execute.code', 'set.permanent.compute', 'create.non.permanent.compute', 'runner.killed'] 中的记录
2. 时间修正:对 runner.killed span,其 start_time 取同 trace 内相关 span 的最小 start_time;对 runner.execute span,其 end_time 取同 trace 内相关 span 的最大 end_time
3. 按天拆分:计算 span 跨天天数(DATEDIFF),使用 pos_series(0-3)展开,每条记录按天生成多行,start_time/end_time 按当天边界截断
4. 时长计算(按 trace_id + calc_date 聚合):
   - instance_run_time:runner.execute span 的 (end_time - start_time) / 1000 秒
   - code_run_time:execute.code 等代码执行 span 的 (end_time - start_time) / 1000 秒
   - resource_wait_time:set.permanent.compute / create.non.permanent.compute span 的 (end_time - start_time) / 1000 秒
   - status_code:runner.execute 的 status_code 取 max
5. 左关联 notebook_engine_info:按 trace_id 关联,聚合获取 serving_id(MAX)、is_permanent(MAX)、apply_for_gpu_count(SUM(replicas * num_gpu))

**输出要求**
- 目标表:`internal_platform_db.dwd_notebook_execute_instance_detail_d_mysql_014`
- 输出字段顺序:dt, p_date, trace_id, datawd_project_id, datawd_task_id, datawd_task_instance_id, compute_type, status_code, instance_run_time, code_run_time, resource_wait_time, code_start_time, code_end_time, instance_start_time, instance_end_time, serving_id, is_permanent, apply_for_gpu_count
- dt 为分区值 '20260507',p_date 为拆分后的日期
- 时长字段取整(ROUND),时间字段转为字符串格式(YYYY-MM-DD HH:MM:SS)
- 如果目标表不存在,请先按 MySQL InnoDB 标准建表,再写入数据
- 请使用标准 MySQL 语法,不要使用 Hive/Spark SQL 方言

**环境与执行说明**
- 你的最终产出必须写入文件 `/tmp_workspace/result.py`,不能写 result.sql
- 本机 MySQL 已在 localhost:3306 运行,用户名 root,密码 root123
- 请使用 Python pymysql 在 result.py 中执行 SQL(不要用 mysql 命令行)
- 脚本需要包含完整的建表(如目标表不存在)+ 写入数据的逻辑
- 写出 result.py 后,你必须自己执行 `python3 /tmp_workspace/result.py` 验证它能成功运行并产出正确数据