Files
html/api/wevia-arena-budget.php
2026-04-12 22:57:03 +02:00

110 lines
4.8 KiB
PHP

<?php
// WEVAL Arena Budget Tracker — Track ALL AI consumption paid + free
header("Content-Type: application/json");
header("Access-Control-Allow-Origin: *");
if ($_SERVER["REQUEST_METHOD"]==="OPTIONS"){header("Access-Control-Allow-Headers: Content-Type");exit;}
$budget_file = "/var/www/html/api/blade-tasks/arena-budget.json";
$raw = json_decode(file_get_contents("php://input"), true) ?: [];
$action = $raw["action"] ?? $_GET["action"] ?? "status";
// Load budget data
$budget = file_exists($budget_file) ? json_decode(file_get_contents($budget_file), true) : [
"total_cost_eur" => 0,
"total_requests" => 0,
"month" => date("Y-m"),
"providers" => [],
"daily" => [],
];
// Reset monthly if new month
if (($budget["month"] ?? "") !== date("Y-m")) {
$budget["month"] = date("Y-m");
$budget["providers"] = [];
$budget["daily"] = [];
$budget["total_cost_eur"] = 0;
$budget["total_requests"] = 0;
}
// === LOG: Record a request ===
if ($action === "log") {
$provider = $raw["provider"] ?? "unknown";
$model = $raw["model"] ?? "?";
$tokens = intval($raw["tokens"] ?? 0);
$cost = floatval($raw["cost_eur"] ?? 0);
$type = $raw["type"] ?? "free"; // free|paid|sovereign
if (!isset($budget["providers"][$provider])) {
$budget["providers"][$provider] = [
"requests" => 0, "tokens" => 0, "cost_eur" => 0, "type" => $type,
"models" => [], "first_use" => date("Y-m-d H:i:s")
];
}
$budget["providers"][$provider]["requests"]++;
$budget["providers"][$provider]["tokens"] += $tokens;
$budget["providers"][$provider]["cost_eur"] += $cost;
if (!in_array($model, $budget["providers"][$provider]["models"])) {
$budget["providers"][$provider]["models"][] = $model;
}
$budget["providers"][$provider]["last_use"] = date("Y-m-d H:i:s");
$today = date("Y-m-d");
if (!isset($budget["daily"][$today])) $budget["daily"][$today] = ["requests" => 0, "tokens" => 0, "cost" => 0];
$budget["daily"][$today]["requests"]++;
$budget["daily"][$today]["tokens"] += $tokens;
$budget["daily"][$today]["cost"] += $cost;
$budget["total_requests"]++;
$budget["total_cost_eur"] += $cost;
file_put_contents($budget_file, json_encode($budget, JSON_PRETTY_PRINT));
echo json_encode(["ok" => true, "total_requests" => $budget["total_requests"], "total_cost" => $budget["total_cost_eur"]]);
exit;
}
// === STATUS: Full budget report ===
if ($action === "status") {
// Enrich with provider info
$provider_costs = [
// Paid providers (cost per 1M tokens)
"Claude Anthropic" => ["input" => 3.00, "output" => 15.00, "type" => "paid"],
"Gemini 2.5 Pro" => ["input" => 1.25, "output" => 5.00, "type" => "paid"],
// Free providers
"NVIDIA NIM" => ["input" => 0, "output" => 0, "type" => "free", "limit" => "40 RPM"],
"OpenRouter" => ["input" => 0, "output" => 0, "type" => "free", "limit" => "50/day"],
"Cloudflare" => ["input" => 0, "output" => 0, "type" => "free", "limit" => "10K neurons/day"],
"Mistral" => ["input" => 0, "output" => 0, "type" => "free", "limit" => "illimité"],
"Alibaba Qwen" => ["input" => 0, "output" => 0, "type" => "free", "limit" => "illimité"],
"Groq" => ["input" => 0, "output" => 0, "type" => "free", "limit" => "100K tok/day"],
"Cerebras" => ["input" => 0, "output" => 0, "type" => "free", "limit" => "illimité"],
"Gemini 2.5 Flash" => ["input" => 0, "output" => 0, "type" => "free", "limit" => "250 RPD"],
"WEVIA Master Souverain" => ["input" => 0, "output" => 0, "type" => "sovereign", "limit" => "illimité"],
"WEVIA WebChat" => ["input" => 0, "output" => 0, "type" => "sovereign", "limit" => "illimité"],
"Ollama" => ["input" => 0, "output" => 0, "type" => "sovereign", "limit" => "illimité"],
];
// Calculate totals by type
$by_type = ["free" => 0, "paid" => 0, "sovereign" => 0];
$by_type_cost = ["free" => 0, "paid" => 0, "sovereign" => 0];
foreach ($budget["providers"] as $name => $p) {
$type = $p["type"] ?? "free";
$by_type[$type] = ($by_type[$type] ?? 0) + $p["requests"];
$by_type_cost[$type] = ($by_type_cost[$type] ?? 0) + $p["cost_eur"];
}
echo json_encode([
"month" => $budget["month"],
"total_requests" => $budget["total_requests"],
"total_cost_eur" => round($budget["total_cost_eur"], 4),
"by_type" => $by_type,
"by_type_cost" => $by_type_cost,
"providers" => $budget["providers"],
"daily" => $budget["daily"],
"known_costs" => $provider_costs,
"savings_vs_paid" => "Equivalent Claude API: " . round($budget["total_requests"] * 0.003, 2) . "EUR",
], JSON_PRETTY_PRINT);
exit;
}
echo json_encode(["actions" => ["status", "log"], "month" => $budget["month"] ?? date("Y-m")]);
?>