diff --git a/scripts/backfill_prices.py b/scripts/backfill_prices.py index 771568f..0f63cdb 100755 --- a/scripts/backfill_prices.py +++ b/scripts/backfill_prices.py @@ -44,12 +44,21 @@ def _price_row(row: dict, rate: float | None) -> dict | None: cost_rub, basis = 0.0, "usd" elif cost_usd is not None and rate: cost_rub, basis = round(cost_usd * rate, 4), "usd" - elif api_usd == 0: - cost_rub, basis = 0.0, "api" - elif api_usd is not None and rate: - # Тот же запасной путь, что и в живом расчёте: пока цена подписки не задана, - # себестоимость зайки берём по её тарифам и говорим об этом основанием. - cost_rub, basis = round(api_usd * rate, 4), "api" + elif pricing.compute_credits(usage, row["model"] or "") is not None: + # Тот же запасной путь, что и в живом расчёте, и ТОЛЬКО для моделей подписки: + # чужой прайс годится как мерка для зайки, но не для подписки Claude — там цену + # по-прежнему взять неоткуда. Пока цена подписки не задана, работу зайки оцениваем + # по прайсу дипсика того дня — сопоставимая работа за те же токены + # (распоряжение Хозяина 10.09.2026). Её собственные тарифы остаются справкой. + reference = pricing.deepseek_reference(row["ts"]) + as_deepseek = pricing.compute_cost(usage, reference or "", + pricing.tariff_for(row["ts"]), at=row["ts"]) + if as_deepseek == 0: + cost_rub, basis = 0.0, "deepseek" + elif as_deepseek is not None and rate: + cost_rub, basis = round(as_deepseek * rate, 4), "deepseek" + else: + return None else: return None return {"cost_rub": cost_rub, "price_rub": round(cost_rub * markup, 4), @@ -66,7 +75,7 @@ def main() -> int: rate = billing.usd_rub_rate() with db._conn() as conn: rows = [dict(r) for r in conn.execute( - "SELECT id, username, model, input_tokens, cache_tokens, output_tokens, " + "SELECT id, ts, username, model, input_tokens, cache_tokens, output_tokens, " "cost_usd, cost_rub, price_rub FROM llm_usage " "WHERE price_rub IS NULL AND output_tokens IS NOT NULL ORDER BY id")] diff --git a/tests/test_backfill_prices.py b/tests/test_backfill_prices.py index 39f714b..4750a90 100644 --- a/tests/test_backfill_prices.py +++ b/tests/test_backfill_prices.py @@ -84,18 +84,18 @@ def test_deepseek_rows_get_rubles_from_dollars(env): assert row["cost_basis"] == "usd" -def test_zaika_rows_are_priced_by_api_tariffs(env): - """У зайки долларов нет вовсе — считаем по её потокенным тарифам из токенов строки. +def test_zaika_rows_are_priced_by_the_deepseek_list(env): + """У зайки долларов нет вовсе — считаем по прайсу дипсика того дня, как это делает + живой расчёт (распоряжение Хозяина 10.09.2026). Порча: пропускать строки без cost_usd → упадёт.""" db, path = env db.record_llm_usage("joe@x", "t1", "agent", "glm", "glm-5.3", 1_000_000, 0, 0, None, credits=690.0) _run(path, "--apply") row = _rows(db)[0] - assert row["api_cost_rub"] == pytest.approx(1.4 * RATE, abs=0.05) - assert row["cost_rub"] == pytest.approx(1.4 * RATE, abs=0.05) - assert row["price_rub"] == pytest.approx(1.4 * RATE * 1.2, abs=0.05) - assert row["cost_basis"] == "api" + assert row["cost_basis"] == "deepseek" + assert row["cost_rub"] == pytest.approx(0.15 * RATE, abs=0.05) + assert row["api_cost_rub"] == pytest.approx(1.4 * RATE, abs=0.05), "справка сохранена" def test_already_priced_rows_are_left_alone(env): @@ -185,3 +185,19 @@ def test_zero_cost_needs_no_rate_here_too(env): row = _rows(db)[0] assert row["cost_rub"] == 0.0 and row["price_rub"] == 0.0 assert row["cost_basis"] == "usd" + + +def test_zaika_rows_are_backfilled_by_the_deepseek_list(env): + """Дозаполнение считает так же, как живой расчёт: пока нет цены подписки — по прайсу + дипсика того дня, а не по тарифам самой зайки (распоряжение Хозяина 10.09.2026). + Порча: вернуть тарифы зайки → цена окажется в разы выше и проверка упадёт.""" + db, path = env + import calendar + day = calendar.timegm((2026, 9, 8, 12, 0, 0, 0, 0, 0)) # до смены прайса дипсика + db.record_llm_usage("joe@x", "t1", "agent", "glm", "glm-5.3", + 1_000_000, 0, 0, None, credits=690.0, ts=day) + _run(path, "--apply") + row = _rows(db)[0] + assert row["cost_basis"] == "deepseek" + assert row["cost_rub"] == pytest.approx(0.22 * RATE, abs=0.05) + assert row["api_cost_rub"] == pytest.approx(1.4 * RATE, abs=0.05), "справка сохранена"