Thesaurus/ajax/getDashboardStats.php
2026-02-23 16:11:35 +01:00

126 lines
3.5 KiB
PHP

<?php
/**
* getDashboardStats.php - Liefert Statistiken für das Dashboard
*/
header('Content-Type: application/json; charset=utf-8');
include "db_connection.php";
$conn = mysqli_connect(HOST, USER, PASSWORD, DATABASE);
if (mysqli_connect_errno()) {
echo json_encode(['error' => 'Datenbankverbindung fehlgeschlagen']);
exit();
}
mysqli_set_charset($conn, "utf8");
$stats = [];
// 1. Anzahl Datensätze pro Typ
$types = ['Subject', 'Person', 'Corporate', 'Publisher', 'Classification'];
$counts = [];
foreach ($types as $type) {
$sql = "SELECT COUNT(*) as cnt FROM Anchor WHERE Type = '$type'";
$res = mysqli_query($conn, $sql);
if ($res) {
$row = mysqli_fetch_assoc($res);
$counts[$type] = intval($row['cnt']);
} else {
$counts[$type] = 0;
}
}
$stats['counts'] = $counts;
$stats['total'] = array_sum($counts);
// 2. Anzahl Relationen
$sql = "SELECT COUNT(*) as cnt FROM Linking";
$res = mysqli_query($conn, $sql);
if ($res) {
$row = mysqli_fetch_assoc($res);
$stats['relationsTotal'] = intval($row['cnt']);
} else {
$stats['relationsTotal'] = 0;
}
// 3. Zuletzt geänderte Einträge (Top 10)
$sql = "SELECT a.ID, a.Text, a.Type, e.DateModified
FROM Anchor a
JOIN Entry e ON a.Text = e.Text
WHERE e.DateModified > '2000-01-01'
ORDER BY e.DateModified DESC
LIMIT 10";
$res = mysqli_query($conn, $sql);
$recentlyModified = [];
if ($res) {
while ($row = mysqli_fetch_assoc($res)) {
$recentlyModified[] = [
'id' => $row['ID'],
'text' => $row['Text'],
'type' => $row['Type'],
'date' => $row['DateModified']
];
}
}
$stats['recentlyModified'] = $recentlyModified;
// 4. Zuletzt hinzugefügte Einträge (Top 10)
$sql = "SELECT a.ID, a.Text, a.Type, e.DateCreated
FROM Anchor a
JOIN Entry e ON a.Text = e.Text
WHERE e.DateCreated > '2000-01-01'
ORDER BY e.DateCreated DESC
LIMIT 10";
$res = mysqli_query($conn, $sql);
$recentlyCreated = [];
if ($res) {
while ($row = mysqli_fetch_assoc($res)) {
$recentlyCreated[] = [
'id' => $row['ID'],
'text' => $row['Text'],
'type' => $row['Type'],
'date' => $row['DateCreated']
];
}
}
$stats['recentlyCreated'] = $recentlyCreated;
// 5. Qualitäts-Check: Einträge ohne Relationen (Waisen)
$orphanCounts = [];
foreach ($types as $type) {
$sql = "SELECT COUNT(*) as cnt FROM Anchor a
WHERE a.Type = '$type'
AND a.ID NOT IN (SELECT DISTINCT IDAnchor FROM Linking)";
$res = mysqli_query($conn, $sql);
if ($res) {
$row = mysqli_fetch_assoc($res);
$orphanCounts[$type] = intval($row['cnt']);
} else {
$orphanCounts[$type] = 0;
}
}
$stats['orphans'] = $orphanCounts;
$stats['orphansTotal'] = array_sum($orphanCounts);
// 6. Deskriptoren vs. Non-Deskriptoren (für Schlagworte)
$sql = "SELECT
SUM(CASE WHEN DetailType = 'Deskriptor' OR DetailType = 'deskriptor' THEN 1 ELSE 0 END) as descriptors,
SUM(CASE WHEN DetailType != 'Deskriptor' AND DetailType != 'deskriptor' THEN 1 ELSE 0 END) as nonDescriptors
FROM Anchor WHERE Type = 'Subject'";
$res = mysqli_query($conn, $sql);
if ($res) {
$row = mysqli_fetch_assoc($res);
$stats['descriptors'] = intval($row['descriptors']);
$stats['nonDescriptors'] = intval($row['nonDescriptors']);
} else {
$stats['descriptors'] = 0;
$stats['nonDescriptors'] = 0;
}
mysqli_close($conn);
echo json_encode($stats, JSON_UNESCAPED_UNICODE);
?>