# 消费组治理项明细提取 (MySQL)

## Prompt
任务目标：从消费特征增量表提取近90天有消费量且非 reader 类型的消费组治理项明细。

**输入表**：`internal_platform_db.dws_mq_consumption_feature_d_increase_mysql_008`（消费特征增量表）

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

**处理规则**：

1. 过滤条件：`dt = '20260507'` AND `(total_consume_last_90d <> 0 AND total_consume_last_90d IS NOT NULL)` AND `is_reader = 0`
2. 派生字段：
   - `mq_full_topic`：若 `tenant` 和 `namespaces` 均非 NULL，拼接 `'persistent://' + tenant + '/' + namespaces + '/' + topic`；否则取 `topic`
   - `app_group`：取自 `dw_appgroup`
   - `consumergroup_incharge`：`COALESCE(consumergroup_incharge, bid_incharge)`，即消费组负责人为空时取 bid 负责人
   - `consumergroup_description`：取自 `usage_desc`
   - `hitted_gov_items`：固定 NULL
   - `governance_benefit_estimate`：固定 NULL
3. 无 Join，单表处理

**输出要求**：

- 目标表：`internal_platform_db.ads_mq_consumergroup_governance_item_d_cand_mysql_008`
- 输出字段顺序为：`dt`、`business_id`、`business_name`、`cluster_set`、`mq_full_topic`、`topic`、`consumergroup`、`system_belong`、`bg`、`category_name`、`app_group`、`consumergroup_incharge`、`last_operator`、`consumergroup_description`、`create_time`、`modify_time`、`bid_incharge`、`cluster_id`、`has_metadata`、`total_produce_pkg_d`、`total_consume_pkg_d`、`consume_ratio`、`total_produce_pkg_last_7d`、`total_produce_pkg_last_30d`、`total_produce_pkg_last_90d`、`total_consume_last_7d`、`consume_ratio_last_7d`、`backlog_ratio_last_7d`、`total_consume_last_30d`、`consume_ratio_last_30d`、`backlog_ratio_last_30d`、`total_consume_last_90d`、`consume_ratio_last_90d`、`backlog_ratio_last_90d`、`backlog_days_last_30d`、`hitted_gov_items`、`governance_benefit_estimate`
- 字段类型：
  - `dt` VARCHAR(8)
  - `mq_full_topic` VARCHAR(512)
  - `app_group` VARCHAR(256)
  - `consumergroup_incharge` VARCHAR(256)
  - `consumergroup_description` VARCHAR(256)
  - `has_metadata` TINYINT
  - `total_produce_pkg_d` BIGINT
  - `total_consume_pkg_d` BIGINT
  - `consume_ratio` VARCHAR(256)
  - `total_produce_pkg_last_7d` BIGINT
  - `total_produce_pkg_last_30d` BIGINT
  - `total_produce_pkg_last_90d` BIGINT
  - `total_consume_last_7d` BIGINT
  - `consume_ratio_last_7d` VARCHAR(256)
  - `backlog_ratio_last_7d` VARCHAR(256)
  - `total_consume_last_30d` BIGINT
  - `consume_ratio_last_30d` VARCHAR(256)
  - `backlog_ratio_last_30d` VARCHAR(256)
  - `total_consume_last_90d` BIGINT
  - `consume_ratio_last_90d` VARCHAR(256)
  - `backlog_ratio_last_90d` VARCHAR(256)
  - `backlog_days_last_30d` INT
  - `hitted_gov_items` VARCHAR(256)
  - `governance_benefit_estimate` DOUBLE
  - 其余字符串字段均为 VARCHAR(256)
- 写入方式：使用 `INSERT INTO ... SELECT ...` 写入目标表
- 如果目标表不存在，请先按 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` 验证它能成功运行并产出正确数据

## Reference Solution
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
mysql_008 ground truth: 消费组治理项明细提取

Task:
  Filter input table WHERE dt = '20260507'
    AND (total_consume_last_90d <> 0 AND total_consume_last_90d IS NOT NULL)
    AND is_reader = 0
  Derive mq_full_topic, app_group, consumergroup_incharge,
    consumergroup_description, hitted_gov_items, governance_benefit_estimate
  Write to output table.
"""
import pymysql
import sys

DB_NAME = "internal_platform_db"
INPUT_TABLE = "dws_mq_consumption_feature_d_increase_mysql_008"
OUTPUT_TABLE = "ads_mq_consumergroup_governance_item_d_cand_mysql_008"

MYSQL_CONFIG = {
    "host": "localhost",
    "port": 3306,
    "user": "root",
    "password": "root123",
    "charset": "utf8mb4",
}

gt_sql = f"""
INSERT INTO {DB_NAME}.{OUTPUT_TABLE}
    (dt, business_id, business_name, cluster_set, mq_full_topic, topic,
     consumergroup, system_belong, bg, category_name, app_group,
     consumergroup_incharge, last_operator, consumergroup_description,
     create_time, modify_time, bid_incharge, cluster_id, has_metadata,
     total_produce_pkg_d, total_consume_pkg_d, consume_ratio,
     total_produce_pkg_last_7d, total_produce_pkg_last_30d, total_produce_pkg_last_90d,
     total_consume_last_7d, consume_ratio_last_7d, backlog_ratio_last_7d,
     total_consume_last_30d, consume_ratio_last_30d, backlog_ratio_last_30d,
     total_consume_last_90d, consume_ratio_last_90d, backlog_ratio_last_90d,
     backlog_days_last_30d, hitted_gov_items, governance_benefit_estimate)
SELECT
    '20260507' AS dt,
    business_id,
    business_name,
    cluster_set,
    IF(tenant IS NOT NULL AND namespaces IS NOT NULL,
       CONCAT('persistent://', tenant, '/', namespaces, '/', topic),
       topic) AS mq_full_topic,
    topic,
    consumergroup,
    system_belong,
    bg,
    category_name,
    dw_appgroup AS app_group,
    COALESCE(consumergroup_incharge, bid_incharge) AS consumergroup_incharge,
    last_operator,
    usage_desc AS consumergroup_description,
    create_time,
    modify_time,
    bid_incharge,
    cluster_id,
    has_metadata,
    total_produce_pkg_d,
    total_consume_pkg_d,
    consume_ratio,
    total_produce_pkg_last_7d,
    total_produce_pkg_last_30d,
    total_produce_pkg_last_90d,
    total_consume_last_7d,
    consume_ratio_last_7d,
    backlog_ratio_last_7d,
    total_consume_last_30d,
    consume_ratio_last_30d,
    backlog_ratio_last_30d,
    total_consume_last_90d,
    consume_ratio_last_90d,
    backlog_ratio_last_90d,
    backlog_days_last_30d,
    NULL AS hitted_gov_items,
    NULL AS governance_benefit_estimate
FROM {DB_NAME}.{INPUT_TABLE}
WHERE dt = '20260507'
  AND (total_consume_last_90d <> 0 AND total_consume_last_90d IS NOT NULL)
  AND is_reader = 0
"""


def main():
    conn = pymysql.connect(**MYSQL_CONFIG)

    try:
        with conn.cursor() as cur:
            # Ensure output table exists
            cur.execute(f"""
                CREATE TABLE IF NOT EXISTS {DB_NAME}.{OUTPUT_TABLE} (
                    dt                              VARCHAR(8)    COMMENT '天分区',
                    business_id                     VARCHAR(256)  COMMENT '消费组所属的business_id',
                    business_name                   VARCHAR(256)  COMMENT 'business_name',
                    cluster_set                     VARCHAR(256)  COMMENT 'cluster_set',
                    mq_full_topic               VARCHAR(512)  COMMENT 'mq的完整topic名',
                    topic                           VARCHAR(256)  COMMENT '消费组消费的topic',
                    consumergroup                   VARCHAR(256)  COMMENT '消费组',
                    system_belong                   VARCHAR(256)  COMMENT 'bid所属的系统（数据总线/流处理平台）',
                    bg                              VARCHAR(256)  COMMENT '消费组所属的bg',
                    category_name                   VARCHAR(256)  COMMENT '消费组所归属的产品',
                    app_group                       VARCHAR(256)  COMMENT '消费组所属的应用组',
                    consumergroup_incharge          VARCHAR(256)  COMMENT '消费组负责人',
                    last_operator                   VARCHAR(256)  COMMENT '消费组最后一次的操作人',
                    consumergroup_description       VARCHAR(256)  COMMENT '消费组使用描述',
                    create_time                     VARCHAR(256)  COMMENT '消费组创建时间',
                    modify_time                     VARCHAR(256)  COMMENT '消费组修改时间',
                    bid_incharge                    VARCHAR(256)  COMMENT 'bid负责人',
                    cluster_id                      VARCHAR(256)  COMMENT 'bid所属集群id',
                    has_metadata                    TINYINT       COMMENT '消费组是否在数据总线/流处理平台中有元数据',
                    total_produce_pkg_d             BIGINT        COMMENT '当天上游的消息数的总量',
                    total_consume_pkg_d             BIGINT        COMMENT '当天消费者消费的消息数的总量',
                    consume_ratio                   VARCHAR(256)  COMMENT '当天消费组的消费生产比例(%)',
                    total_produce_pkg_last_7d       BIGINT        COMMENT '近7天topic生产的消息数的总量',
                    total_produce_pkg_last_30d      BIGINT        COMMENT '近30天topic生产的消息数的总量',
                    total_produce_pkg_last_90d      BIGINT        COMMENT '近90天topic生产的消息数的总量',
                    total_consume_last_7d           BIGINT        COMMENT '消费组近7天总消费量',
                    consume_ratio_last_7d           VARCHAR(256)  COMMENT '消费组近7天的生产消费比例(%)',
                    backlog_ratio_last_7d           VARCHAR(256)  COMMENT '消费组近7天的生产积压比例(%)',
                    total_consume_last_30d          BIGINT        COMMENT '消费组近30天总消费量',
                    consume_ratio_last_30d          VARCHAR(256)  COMMENT '消费组近30天的生产消费比例(%)',
                    backlog_ratio_last_30d          VARCHAR(256)  COMMENT '消费组近30天的生产积压比例(%)',
                    total_consume_last_90d          BIGINT        COMMENT '消费组近90天总消费量',
                    consume_ratio_last_90d          VARCHAR(256)  COMMENT '消费组近90天的生产消费比例(%)',
                    backlog_ratio_last_90d          VARCHAR(256)  COMMENT '消费组近90天的生产积压比例(%)',
                    backlog_days_last_30d           INT           COMMENT '消费组近30天有新增积压的天数',
                    hitted_gov_items                VARCHAR(256)  COMMENT '命中治理项',
                    governance_benefit_estimate     DOUBLE        COMMENT '预估治理收益（集群单元/月）'
                ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
            """)

            # Truncate + insert
            cur.execute(f"TRUNCATE TABLE {DB_NAME}.{OUTPUT_TABLE}")
            cur.execute(gt_sql)

        conn.commit()
        print("mysql_008 ground_truth done: rows written to output table")
    except Exception as e:
        print(f"ground_truth error: {e}", file=sys.stderr)
        conn.rollback()
        sys.exit(1)
    finally:
        conn.close()


if __name__ == "__main__":
    main()