Files
dobetternorge-tools/api/health.php
T

90 lines
3.0 KiB
PHP

<?php
declare(strict_types=1);
require_once __DIR__ . '/../includes/bootstrap.php';
require_once __DIR__ . '/../includes/AzureOpenAiGateway.php';
dbnToolsRequireMethod('GET');
dbnToolsRequireAuth();
$checks = [];
$checks['caveau_auth'] = [
'ok' => true,
'detail' => 'Tools login uses Caveau client_users for tenant ' . dbnToolsClientSlug(),
];
$azure = new DbnAzureOpenAiGateway();
$missingChat = $azure->missingChatConfig();
$missingEmbedding = $azure->missingEmbeddingConfig();
$checks['azure_chat_config'] = [
'ok' => !$missingChat,
'detail' => !$missingChat ? 'Configured' : 'Missing: ' . implode(', ', $missingChat),
];
$checks['azure_embedding_config'] = [
'ok' => !$missingEmbedding,
'detail' => !$missingEmbedding ? 'Configured' : 'Missing: ' . implode(', ', $missingEmbedding),
];
$checks['azure_reachability'] = [
'ok' => false,
'detail' => 'Not checked because chat config is incomplete',
];
if (!$missingChat) {
$reachable = $azure->ping(8);
$checks['azure_reachability'] = [
'ok' => $reachable,
'detail' => $reachable ? 'Azure chat deployment responded' : 'Azure chat deployment did not respond',
];
}
try {
$db = dbnToolsDb();
$db->query('SELECT 1');
$checks['db_connectivity'] = ['ok' => true, 'detail' => 'CaveauAI admin DB reachable'];
$client = dbnToolsFetchClient($db);
$checks['dobetter_client'] = [
'ok' => (bool)$client,
'detail' => $client ? 'Client id ' . $client['id'] . ' found' : 'Client slug ' . dbnToolsClientSlug() . ' not found',
];
$packageSlug = dbnToolsRequiredPackageSlug();
$package = dbnToolsFetchPackage($packageSlug, $db);
$checks['family_legal_package'] = [
'ok' => (bool)$package && !empty($package['is_active']),
'detail' => $package ? 'Package id ' . $package['id'] . ' found' : $packageSlug . ' package not found',
];
$subOk = $client && $package && dbnToolsHasActiveSubscription((int)$client['id'], (int)$package['id'], $db);
$checks['family_legal_subscription'] = [
'ok' => (bool)$subOk,
'detail' => $subOk ? 'Active subscription visible' : 'Active subscription not visible',
];
} catch (Throwable $e) {
$checks['db_connectivity'] = ['ok' => false, 'detail' => $e->getMessage()];
$checks['dobetter_client'] = ['ok' => false, 'detail' => 'Not checked'];
$checks['family_legal_package'] = ['ok' => false, 'detail' => 'Not checked'];
$checks['family_legal_subscription'] = ['ok' => false, 'detail' => 'Not checked'];
}
$logPath = dbnToolsMetadataLogPath();
$dir = dirname($logPath);
$checks['metadata_log'] = [
'ok' => is_dir($dir) && is_writable($dir),
'detail' => is_dir($dir) && is_writable($dir) ? 'Metadata directory is writable' : 'Metadata directory is not writable',
];
$ok = true;
foreach ($checks as $check) {
if (empty($check['ok'])) {
$ok = false;
break;
}
}
dbnToolsRespond([
'ok' => $ok,
'status' => $ok ? 'ok' : 'degraded',
'version' => DBN_TOOLS_VERSION,
'checks' => $checks,
], $ok ? 200 : 503);