Files
Siro/backend/Admin/marketing/get_telemetry.php
2026-06-21 18:58:13 +03:00

56 lines
1.8 KiB
PHP

<?php
require_once __DIR__ . '/../../connect.php';
if ($role !== 'admin' && $role !== 'super_admin') {
http_response_code(403);
echo json_encode(['status' => 'failure', 'message' => 'Unauthorized access. Admin role required.']);
exit;
}
try {
$countryCode = filterRequest('country_code');
$countSql = "SELECT COUNT(*) FROM marketing_campaigns_log";
$params = [];
if ($countryCode) {
$countSql .= " WHERE country_code = :country";
$params[':country'] = strtoupper($countryCode);
}
$stmt = $con->prepare($countSql);
foreach ($params as $key => $val) {
$stmt->bindValue($key, $val);
}
$stmt->execute();
$campaignCount = (int)$stmt->fetchColumn();
$estTokensPerCampaign = 3250;
$estCostPerCampaign = 0.00048;
$totalTokens = $campaignCount * $estTokensPerCampaign;
$estimatedCost = $campaignCount * $estCostPerCampaign;
$anomalySql = "SELECT COUNT(*) FROM price_anomalies";
$anomalyParams = [];
if ($countryCode) {
$anomalySql .= " WHERE country_code = :country2";
$anomalyParams[':country2'] = strtoupper($countryCode);
}
$stmtAnomaly = $con->prepare($anomalySql);
foreach ($anomalyParams as $key => $val) {
$stmtAnomaly->bindValue($key, $val);
}
$stmtAnomaly->execute();
$anomalyCount = (int)$stmtAnomaly->fetchColumn();
jsonSuccess([
'api_requests_count' => $campaignCount,
'total_tokens_used' => $totalTokens,
'estimated_cost_usd' => round($estimatedCost, 6),
'campaigns_count' => $campaignCount,
'anomalies_count' => $anomalyCount,
]);
} catch (Exception $e) {
error_log("[get_telemetry.php] Error: " . $e->getMessage());
jsonError("Failed to fetch telemetry: " . $e->getMessage());
}