File size: 3,375 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
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
;