# APK Threat Scan Instance GPU Card-Hour 5-Minute Window Statistics (PrestoSQL)

## Prompt
**Task Objective**: Compute the GPU card-hour consumption of APK scan instances, aggregated by 5-minute time windows, joining with Pod mapping and task instance GPU configuration information, and write the results to the output table.

**Input Tables**:
- `internal_platform_db.t_gpu_monitor_parsed_prestosql_007` (GPU monitoring data table)
  - `container` STRING — container name
  - `pod_name` STRING — Pod name
  - `pkg_time` STRING — reporting time (epoch second string)
  - `gpu_name` STRING — GPU model
  - `metric` STRING — metric name
  - `value` STRING — metric value
  - `dt` STRING — partition field (format `'2026060800'`)

- `internal_platform_db.dwd_scan_instance_podname_prestosql_007` (Pod-to-scan-instance mapping table)
  - `dt` STRING — partition field
  - `instance_uuid` STRING — instance unique identifier
  - `pod_name` STRING — Pod name
  - `pod_phase` STRING — Pod phase
  - `namespace` STRING — namespace

- `internal_platform_db.dwd_scan_task_instance_prestosql_007` (task instance GPU configuration table 1)
  - `databus_imp_date` STRING — partition field
  - `instance_uuid` STRING — instance unique identifier
  - `host_gpu_num` DOUBLE — host GPU card count
  - `host_num` DOUBLE — host count
  - `last_modify` DOUBLE — last modification timestamp
  - `gpu_name` STRING — GPU model

- `internal_platform_db.scan_task_instance_prestosql_007` (scan task instance GPU configuration table 2)
  - `databus_imp_date` STRING — partition field
  - `instance_uuid` STRING — instance unique identifier
  - `host_gpu_num` DOUBLE — host GPU card count
  - `host_num` DOUBLE — host count
  - `last_modify` DOUBLE — last modification timestamp
  - `gpu_name` STRING — GPU model
  - `scan_type` STRING — scan type

**Computation Logic**:
1. Filter specified metrics from the GPU monitoring data, and compute runtime per Pod in 5-minute time windows
2. Join with the Pod-to-instance mapping to aggregate the GPU runtime and Pod count per instance per 5-minute window
3. Retrieve the latest GPU configuration information for each task instance
4. Compute the GPU card-hours (GPU-hour) per instance per 5-minute window
5. The specific time window bucketing method, join conditions, aggregation logic, and GPU-hour computation formula must be determined based on the table structures and business semantics

**Output Requirements**:
- Target table: `internal_platform_db.t_scan_instance_gpu_time_stats_cand_prestosql_007`
- Output fields and order: `instance_uuid` STRING, `time_5min` BIGINT, `host_gpu_num` DOUBLE, `sum_run_time_m` DOUBLE, `gpu_hour` DOUBLE, `gpu_name` STRING, `pod_count` BIGINT, `host_num` DOUBLE
- If the target table does not exist, first create the table, then write the data
- Use Presto/Trino SQL syntax; do not use Hive/Spark SQL dialects

**Environment and Execution Notes**:
- Presto is running, connected via the Hive catalog
- Execute SQL: `presto-cli --catalog hive --schema internal_platform_db -f /tmp_workspace/result.sql`
- After writing `result.sql`, you must execute it yourself to verify that it runs successfully and produces correct data

## Reference Solution
INSERT INTO internal_platform_db.t_scan_instance_gpu_time_stats_prestosql_007
WITH
-- Step 1: From GPU monitoring data, extract pod run records with minute-level dedup
pod_run_minutes AS (
    SELECT
        pod_name,
        gpu_name,
        FLOOR(CAST(pkg_time AS BIGINT) / 60) * 60 AS minute_timestamp,
        FLOOR(CAST(pkg_time AS BIGINT) / 300) * 300 AS time_5min
    FROM internal_platform_db.t_gpu_monitor_parsed_prestosql_007
    WHERE dt = '2026060800'
        AND metric IN ('k8s_container_vgpu_gpu_mem_usage', 'k8s_dcgm_fi_dev_fb_util')
    GROUP BY pod_name, gpu_name, FLOOR(CAST(pkg_time AS BIGINT) / 60) * 60, FLOOR(CAST(pkg_time AS BIGINT) / 300) * 300
),

-- Step 2: Aggregate run minutes per pod per 5-minute window
pod_run_time AS (
    SELECT
        pod_name,
        MAX(gpu_name) AS gpu_name,
        time_5min,
        CAST(COUNT(DISTINCT minute_timestamp) AS DOUBLE) AS run_time_m
    FROM pod_run_minutes
    GROUP BY pod_name, time_5min
),

-- Step 3: Join pod-to-instance mapping, aggregate per instance per window
instance_run_time AS (
    SELECT
        p.time_5min,
        m.instance_uuid,
        MAX(p.gpu_name) AS gpu_name,
        CAST(SUM(p.run_time_m) AS DOUBLE) AS sum_run_time_m,
        COUNT(DISTINCT m.pod_name) AS pod_count
    FROM pod_run_time p
    INNER JOIN internal_platform_db.dwd_scan_instance_podname_prestosql_007 m
        ON p.pod_name = m.pod_name
        AND m.dt = '2026060800'
    GROUP BY p.time_5min, m.instance_uuid
),

-- Step 4: Get latest GPU config per instance from dual-table UNION ALL + ROW_NUMBER dedup
latest_task_config AS (
    SELECT
        instance_uuid,
        CAST(host_gpu_num AS DOUBLE) AS host_gpu_num,
        CAST(host_num AS DOUBLE) AS host_num,
        gpu_name
    FROM (
        SELECT
            instance_uuid,
            host_gpu_num,
            host_num,
            gpu_name,
            last_modify,
            ROW_NUMBER() OVER (PARTITION BY instance_uuid ORDER BY last_modify DESC) AS rn
        FROM (
            SELECT
                instance_uuid,
                host_gpu_num,
                host_num,
                gpu_name,
                last_modify
            FROM internal_platform_db.dwd_scan_task_instance_prestosql_007
            WHERE databus_imp_date = (
                SELECT MAX(databus_imp_date)
                FROM internal_platform_db.dwd_scan_task_instance_prestosql_007
            )

            UNION ALL

            SELECT
                instance_uuid,
                host_gpu_num,
                host_num,
                gpu_name,
                last_modify
            FROM internal_platform_db.scan_task_instance_prestosql_007
            WHERE databus_imp_date = (
                SELECT MAX(databus_imp_date)
                FROM internal_platform_db.scan_task_instance_prestosql_007
            )
        ) combined
    ) ranked
    WHERE rn = 1
)

-- Step 5: Join instance run time with GPU config, compute GPU-hour
SELECT
    i.instance_uuid,
    i.time_5min,
    t.host_gpu_num,
    i.sum_run_time_m,
    CASE
        WHEN t.host_gpu_num IS NOT NULL AND i.sum_run_time_m IS NOT NULL
        THEN t.host_gpu_num * i.sum_run_time_m / 60.0
        ELSE NULL
    END AS gpu_hour,
    i.gpu_name,
    i.pod_count,
    t.host_num
FROM instance_run_time i
LEFT JOIN latest_task_config t
    ON i.instance_uuid = t.instance_uuid
;