73 lines
2.2 KiB
PHP
73 lines
2.2 KiB
PHP
<?php
|
|
require_once 'db/config.php';
|
|
|
|
header('Content-Type: application/json');
|
|
|
|
$profile_id = $_GET['id'] ?? null;
|
|
|
|
if (!$profile_id) {
|
|
echo json_encode(['error' => 'Profile ID is missing.']);
|
|
exit;
|
|
}
|
|
|
|
// Basic AI/Rule-based KPI generation
|
|
function generate_kpi_data($profile) {
|
|
// In a real scenario, you'd have a more complex logic or an API call to an AI model
|
|
$kpis = [
|
|
['name' => 'Monthly Recurring Revenue (MRR)', 'current' => '$' . number_format(rand(5000, 15000)), 'target' => '$25,000'],
|
|
['name' => 'Customer Acquisition Cost (CAC)', 'current' => '$' . number_format(rand(300, 800)), 'target' => '$400'],
|
|
['name' => 'Net Revenue Retention (NRR)', 'current' => rand(95, 110) . '%', 'target' => '115%']
|
|
];
|
|
|
|
// Generate sample data for the chart
|
|
$labels = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun'];
|
|
$datasets = [
|
|
[
|
|
'label' => 'MRR (Actual)',
|
|
'data' => array_map(fn() => rand(5000, 18000), range(1, 6)),
|
|
'borderColor' => '#0d6efd',
|
|
'tension' => 0.1
|
|
],
|
|
[
|
|
'label' => 'MRR (Target)',
|
|
'data' => array_map(fn($i) => 15000 + ($i * 1500), range(1, 6)),
|
|
'borderColor' => '#dc3545',
|
|
'borderDash' => [5, 5],
|
|
'tension' => 0.1
|
|
]
|
|
];
|
|
|
|
return [
|
|
'kpis' => $kpis,
|
|
'chartData' => [
|
|
'labels' => $labels,
|
|
'datasets' => $datasets
|
|
]
|
|
];
|
|
}
|
|
|
|
try {
|
|
$pdo = db();
|
|
$stmt = $pdo->prepare("SELECT * FROM gtm_profiles WHERE id = ?");
|
|
$stmt->execute([$profile_id]);
|
|
$profile = $stmt->fetch(PDO::FETCH_ASSOC);
|
|
|
|
if (!$profile) {
|
|
echo json_encode(['error' => 'Profile not found.']);
|
|
exit;
|
|
}
|
|
|
|
$kpi_data = generate_kpi_data($profile);
|
|
$kpi_data_json = json_encode($kpi_data);
|
|
|
|
// Save the generated data to the database
|
|
$update_stmt = $pdo->prepare("UPDATE gtm_profiles SET kpi_data = ? WHERE id = ?");
|
|
$update_stmt->execute([$kpi_data_json, $profile_id]);
|
|
|
|
echo json_encode(['success' => true, 'kpi_data' => $kpi_data]);
|
|
|
|
} catch (PDOException $e) {
|
|
error_log('KPI Generation Error: ' . $e->getMessage());
|
|
echo json_encode(['error' => 'Database error during KPI generation.']);
|
|
}
|