File size: 7,528 Bytes
603975a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#!/usr/bin/env python3
"""Integrity check for the GPU rental price dataset (Python standard library only).

Verifies the price CSV (row count, columns, values), that the JSON file holds the same
rows, and that the summary CSV matches statistics recomputed from the price rows.
Exits with status 1 if any check fails.
"""

from __future__ import annotations

import csv
import json
import math
import sys
from datetime import date
from pathlib import Path

ROOT = Path(__file__).resolve().parent.parent
PRICES_CSV = ROOT / "data" / "gpu_rental_prices_2026_11.csv"
PRICES_JSON = ROOT / "data" / "gpu_rental_prices_2026_11.json"
SUMMARY_CSV = ROOT / "data" / "summary_2026_11.csv"

EXPECTED_ROWS = 75
RETRIEVED = "2026-10-26"
PRICE_COLUMNS = [
    "provider",
    "gpu",
    "offer",
    "price_usd_per_gpu_hour",
    "listing",
    "source_url",
    "retrieved",
]
SUMMARY_COLUMNS = [
    "gpu",
    "offer",
    "providers",
    "min_usd_per_gpu_hour",
    "median_usd_per_gpu_hour",
    "max_usd_per_gpu_hour",
    "spread_max_over_min",
]
GPUS = ["H100 SXM", "H100 PCIe", "H200", "B200", "A100 80GB", "L40S", "RTX 4090", "RTX 5090"]
OFFERS = ["on-demand", "spot-community", "reserved-capacity-block"]
TOLERANCE = 1e-6
SPREAD_TOLERANCE = 0.0005  # spread is rounded to three decimals

errors: list[str] = []


def fail(message: str) -> None:
    errors.append(message)


def read_csv(path: Path) -> tuple[list[str], list[dict[str, str]]]:
    with path.open(newline="", encoding="utf-8") as handle:
        reader = csv.DictReader(handle)
        return list(reader.fieldnames or []), list(reader)


def median(values: list[float]) -> float:
    ordered = sorted(values)
    middle = len(ordered) // 2
    if len(ordered) % 2:
        return ordered[middle]
    return (ordered[middle - 1] + ordered[middle]) / 2


def check_prices() -> list[dict[str, object]]:
    columns, rows = read_csv(PRICES_CSV)
    if columns != PRICE_COLUMNS:
        fail(f"price CSV columns {columns} != {PRICE_COLUMNS}")
    if len(rows) != EXPECTED_ROWS:
        fail(f"price CSV has {len(rows)} rows, expected {EXPECTED_ROWS}")

    records: list[dict[str, object]] = []
    seen: set[tuple[str, str, str]] = set()
    sources: dict[str, str] = {}
    for line, row in enumerate(rows, start=2):
        key = (row["provider"], row["gpu"], row["offer"])
        if key in seen:
            fail(f"line {line}: duplicate provider/GPU/offer {key}")
        seen.add(key)
        if not row["provider"]:
            fail(f"line {line}: empty provider")
        if row["gpu"] not in GPUS:
            fail(f"line {line}: unknown GPU {row['gpu']!r}")
        if row["offer"] not in OFFERS:
            fail(f"line {line}: unknown offer {row['offer']!r}")
        try:
            price = float(row["price_usd_per_gpu_hour"])
        except ValueError:
            fail(f"line {line}: price {row['price_usd_per_gpu_hour']!r} is not a number")
            continue
        if not math.isfinite(price) or price <= 0:
            fail(f"line {line}: price {price} must be a positive number")
        if not row["listing"]:
            fail(f"line {line}: empty listing")
        if not row["source_url"].startswith("https://"):
            fail(f"line {line}: source URL {row['source_url']!r} is not HTTPS")
        if sources.setdefault(row["provider"], row["source_url"]) != row["source_url"]:
            fail(f"line {line}: provider {row['provider']} has more than one source URL")
        if row["retrieved"] != RETRIEVED:
            fail(f"line {line}: retrieved {row['retrieved']!r} != {RETRIEVED}")
        try:
            date.fromisoformat(row["retrieved"])
        except ValueError:
            fail(f"line {line}: retrieved {row['retrieved']!r} is not an ISO date")
        records.append({**row, "price_usd_per_gpu_hour": price})
    return records


def check_json(records: list[dict[str, object]]) -> None:
    data = json.loads(PRICES_JSON.read_text(encoding="utf-8"))
    if not isinstance(data, list):
        fail("JSON file is not an array")
        return
    if len(data) != len(records):
        fail(f"JSON has {len(data)} rows, CSV has {len(records)}")
    for index, (item, record) in enumerate(zip(data, records)):
        if list(item) != PRICE_COLUMNS:
            fail(f"JSON row {index}: keys {list(item)} != {PRICE_COLUMNS}")
        if not isinstance(item.get("price_usd_per_gpu_hour"), (int, float)):
            fail(f"JSON row {index}: price is not a number")
        if item != record:
            fail(f"JSON row {index} differs from CSV row: {item} != {record}")


def expected_summary(records: list[dict[str, object]]) -> list[dict[str, object]]:
    summary: list[dict[str, object]] = []
    for offer in OFFERS:
        for gpu in GPUS:
            prices = [
                float(r["price_usd_per_gpu_hour"])  # type: ignore[arg-type]
                for r in records
                if r["gpu"] == gpu and r["offer"] == offer
            ]
            if not prices:
                continue
            low, high = min(prices), max(prices)
            summary.append(
                {
                    "gpu": gpu,
                    "offer": offer,
                    "providers": len(prices),
                    "min": low,
                    "median": median(prices),
                    "max": high,
                    "spread": high / low,
                }
            )
    return summary


def check_summary(records: list[dict[str, object]]) -> None:
    columns, rows = read_csv(SUMMARY_CSV)
    if columns != SUMMARY_COLUMNS:
        fail(f"summary columns {columns} != {SUMMARY_COLUMNS}")
    expected = expected_summary(records)
    if len(rows) != len(expected):
        fail(f"summary has {len(rows)} rows, expected {len(expected)}")
    for line, (row, want) in enumerate(zip(rows, expected), start=2):
        label = f"summary line {line} ({want['gpu']}, {want['offer']})"
        if (row["gpu"], row["offer"]) != (want["gpu"], want["offer"]):
            fail(f"{label}: found {row['gpu']}, {row['offer']}")
            continue
        try:
            if int(row["providers"]) != want["providers"]:
                fail(f"{label}: providers {row['providers']} != {want['providers']}")
            for column, key in (
                ("min_usd_per_gpu_hour", "min"),
                ("median_usd_per_gpu_hour", "median"),
                ("max_usd_per_gpu_hour", "max"),
            ):
                if abs(float(row[column]) - float(want[key])) > TOLERANCE:  # type: ignore[arg-type]
                    fail(f"{label}: {column} {row[column]} != {want[key]}")
            if abs(float(row["spread_max_over_min"]) - float(want["spread"])) > SPREAD_TOLERANCE:  # type: ignore[arg-type]
                fail(f"{label}: spread {row['spread_max_over_min']} != {want['spread']:.3f}")
        except ValueError as error:
            fail(f"{label}: {error}")


def main() -> int:
    for path in (PRICES_CSV, PRICES_JSON, SUMMARY_CSV):
        if not path.is_file():
            print(f"FAIL: missing {path.relative_to(ROOT)}")
            return 1
    records = check_prices()
    check_json(records)
    check_summary(records)
    if errors:
        for message in errors:
            print(f"FAIL: {message}")
        print(f"{len(errors)} error(s)")
        return 1
    providers = len({r["provider"] for r in records})
    print(f"OK: {len(records)} price rows, {providers} providers, summary and JSON consistent")
    return 0


if __name__ == "__main__":
    sys.exit(main())