From 8bc90158d957bcf5924e9c24696fab4ae9154cc4 Mon Sep 17 00:00:00 2001 From: Matthias Czech Date: Mon, 17 Mar 2025 22:34:46 +0100 Subject: [PATCH] haupt und konfig seite --- dashboard.php | 476 ++++++++++++++++++++++++++++---------------------- 1 file changed, 267 insertions(+), 209 deletions(-) diff --git a/dashboard.php b/dashboard.php index 1c5be92..2447915 100644 --- a/dashboard.php +++ b/dashboard.php @@ -14,129 +14,177 @@ $options = [ ]; try { - $pdo = new PDO($dsn, $user, $pass, $options); + $pdo = new PDO($dsn, $user, $pass, $options); } catch (PDOException $e) { - header('Content-Type: application/json'); - echo json_encode(['error' => 'Database connection failed: ' . $e->getMessage()]); - exit; + header('Content-Type: application/json'); + echo json_encode(['error' => 'Database connection failed: ' . $e->getMessage()]); + exit; } -// Bearer Token für Mastodon-API (falls benötigt – hier nicht direkt in diesem File) -$bearerToken = 'your_bearer_token_here'; +// ----------------------- +// AJAX-Anfragen (POST) +// ----------------------- +if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action'])) { + $action = $_POST['action']; + header('Content-Type: application/json'); -// Falls eine AJAX-Anfrage vorliegt, wird über den Parameter "action" unterschieden. -if (isset($_POST['action'])) { - $action = $_POST['action']; - header('Content-Type: application/json'); - - if ($action === 'save_hashtags') { - // Erwartet ein Array "hashtags" (JSON-dekodiert via AJAX) - $hashtags = isset($_POST['hashtags']) ? $_POST['hashtags'] : []; - $response = ['saved' => 0, 'updated' => 0]; - - foreach ($hashtags as $row) { - // Falls eine ID vorhanden ist, update ansonsten insert - if (isset($row['hashtag_id']) && !empty($row['hashtag_id'])) { - // Update vorhandener Eintrag - $stmt = $pdo->prepare("UPDATE hashtags SET hashtag = :hashtag, active = :active WHERE hashtag_id = :hashtag_id"); - $stmt->execute([ - 'hashtag' => $row['hashtag'], - 'active' => !empty($row['active']) ? 1 : 0, - 'hashtag_id' => $row['hashtag_id'] - ]); - $response['updated']++; - } else { - // Insert neuer Eintrag - $stmt = $pdo->prepare("INSERT INTO hashtags (hashtag, active) VALUES (:hashtag, :active)"); - $stmt->execute([ - 'hashtag' => $row['hashtag'], - 'active' => !empty($row['active']) ? 1 : 0 - ]); - $response['saved']++; - } - } - echo json_encode($response); - exit; - } elseif ($action === 'save_accounts') { - // Erwartet ein Array "accounts" (JSON) - $accounts = isset($_POST['accounts']) ? $_POST['accounts'] : []; - $response = ['updated' => 0]; - - foreach ($accounts as $row) { - // Aktualisiere den active-Status in der Tabelle "users" - // Annahme: Tabelle "users" hat die Spalten: acc_id, username, active - $stmt = $pdo->prepare("UPDATE users SET active = :active WHERE acc_id = :acc_id"); - $stmt->execute([ - 'active' => !empty($row['active']) ? 1 : 0, - 'acc_id' => $row['acc_id'] - ]); - $response['updated']++; - } - echo json_encode($response); - exit; - } elseif ($action === 'sync_accounts') { - // Synchronisation: Aus der findings-Tabelle werden alle getrackten Accounts (acc_id, username) ermittelt - // Für jeden Account, der noch nicht in der "users"-Tabelle existiert, erfolgt ein Insert mit active=0 - $stmt = $pdo->query("SELECT DISTINCT acc_id, username FROM findings"); - $tracked = $stmt->fetchAll(); - - foreach ($tracked as $acc) { - // Prüfen, ob der Account bereits in der users-Tabelle existiert - $checkStmt = $pdo->prepare("SELECT COUNT(*) as cnt FROM users WHERE acc_id = :acc_id"); - $checkStmt->execute(['acc_id' => $acc['acc_id']]); - $exists = $checkStmt->fetch(); - if ($exists['cnt'] == 0) { - // Neuer Account – unaktiv in die Tabelle schreiben - $insertStmt = $pdo->prepare("INSERT INTO users (acc_id, username, active) VALUES (:acc_id, :username, 0)"); - $insertStmt->execute([ - 'acc_id' => $acc['acc_id'], - 'username' => $acc['username'] - ]); - } - } - // Nun alle Accounts aus der users-Tabelle abrufen - $stmt = $pdo->query("SELECT acc_id, username, active FROM users ORDER BY username ASC"); - $accountsList = $stmt->fetchAll(); - echo json_encode($accountsList); - exit; + if ($action === 'save_config') { + // Erwartet: from_date, to_date, refresh_interval + $from_date = $_POST['from_date']; // z. B. "2025-03-17T14:00" + $to_date = $_POST['to_date']; + $refresh_interval = $_POST['refresh_interval']; + // Aktualisieren – wir gehen davon aus, dass die Config-Tabelle eine einzelne Zeile mit id=1 hat. + $stmt = $pdo->prepare("UPDATE config SET from_date = :from_date, to_date = :to_date, refresh_interval = :refresh_interval WHERE id = 1"); + $stmt->execute([ + 'from_date' => date('Y-m-d H:i:s', strtotime($from_date)), + 'to_date' => date('Y-m-d H:i:s', strtotime($to_date)), + 'refresh_interval' => $refresh_interval + ]); + echo json_encode(['status' => 'ok']); + exit; + } elseif ($action === 'save_hashtags') { + // Erwartet ein Array "hashtags" mit den Feldern: hashtag_id, hashtag, active + $hashtags = isset($_POST['hashtags']) ? $_POST['hashtags'] : []; + $response = ['saved' => 0, 'updated' => 0]; + foreach ($hashtags as $row) { + if (isset($row['hashtag_id']) && !empty($row['hashtag_id'])) { + // Update + $stmt = $pdo->prepare("UPDATE hashtags SET hashtag = :hashtag, active = :active WHERE hashtag_id = :hashtag_id"); + $stmt->execute([ + 'hashtag' => $row['hashtag'], + 'active' => !empty($row['active']) ? 1 : 0, + 'hashtag_id' => $row['hashtag_id'] + ]); + $response['updated']++; + } else { + // Insert neuer Eintrag + $stmt = $pdo->prepare("INSERT INTO hashtags (hashtag, active) VALUES (:hashtag, :active)"); + $stmt->execute([ + 'hashtag' => $row['hashtag'], + 'active' => !empty($row['active']) ? 1 : 0 + ]); + $response['saved']++; + } } - // Weitere Aktionen können hier ergänzt werden + echo json_encode($response); exit; + } elseif ($action === 'save_accounts') { + // Erwartet ein Array "accounts" (Felder: acc_id, active) + $accounts = isset($_POST['accounts']) ? $_POST['accounts'] : []; + $response = ['updated' => 0]; + foreach ($accounts as $row) { + $stmt = $pdo->prepare("UPDATE users SET active = :active WHERE acc_id = :acc_id"); + $stmt->execute([ + 'active' => !empty($row['active']) ? 1 : 0, + 'acc_id' => $row['acc_id'] + ]); + $response['updated']++; + } + echo json_encode($response); + exit; + } elseif ($action === 'get_dashboard_data') { + // Lese den Zeitraum und Refresh-Intervall aus der config-Tabelle (id=1) + $configStmt = $pdo->query("SELECT from_date, to_date, refresh_interval FROM config WHERE id = 1"); + $config = $configStmt->fetch(); + if (!$config) { + $config = ['from_date' => date('Y-m-d H:i:s', strtotime('-1 day')), 'to_date' => date('Y-m-d H:i:s'), 'refresh_interval' => 20]; + } + // Aggregation: Nur Beiträge innerhalb des Zeitraums, zu aktiven Hashtags und von aktiven Usern + $query = "SELECT f.username, h.hashtag, + SUM(f.replies_count) AS total_replies, + SUM(f.reblogs_count) AS total_reblogs, + SUM(f.favorites_count) AS total_favorites + FROM findings f + JOIN hashtags h ON f.hashtag_id = h.hashtag_id + JOIN users u ON f.acc_id = u.acc_id + WHERE f.created_at BETWEEN :from_date AND :to_date + AND h.active = 1 + AND u.active = 1 + GROUP BY h.hashtag, f.username + ORDER BY h.hashtag, f.username"; + $stmt = $pdo->prepare($query); + $stmt->execute([ + 'from_date' => $config['from_date'], + 'to_date' => $config['to_date'] + ]); + $data = $stmt->fetchAll(); + // Bestimme globale Maximalwerte für jeden Metrik, um die Balkenhöhen zu skalieren + $maxReplies = $maxReblogs = $maxFavorites = 0; + foreach ($data as $row) { + if ($row['total_replies'] > $maxReplies) { $maxReplies = $row['total_replies']; } + if ($row['total_reblogs'] > $maxReblogs) { $maxReblogs = $row['total_reblogs']; } + if ($row['total_favorites'] > $maxFavorites) { $maxFavorites = $row['total_favorites']; } + } + echo json_encode([ + 'data' => $data, + 'max' => ['replies' => $maxReplies, 'reblogs' => $maxReblogs, 'favorites' => $maxFavorites] + ]); + exit; + } + // Weitere Aktionen können ergänzt werden. + exit; } -// Wenn kein "action" gesetzt ist, wird der HTML-Teil (Dashboard) ausgegeben. -// Der Konfigbereich ist über den Parameter "setup" erreichbar. -?> +// ----------------------- +// GET-Anfragen: Anzeige der Seite +// ----------------------- + +// Wenn action=setup in der URL übergeben wurde, wird der Konfigurationsbereich angezeigt. +if (isset($_GET['action']) && $_GET['action'] == 'setup') { + // Lese Konfiguration aus der config-Tabelle (angenommen, es gibt eine einzelne Zeile mit id=1) + $configStmt = $pdo->query("SELECT from_date, to_date, refresh_interval FROM config WHERE id = 1"); + $config = $configStmt->fetch(); + if (!$config) { + $config = ['from_date' => date('Y-m-d\TH:i'), 'to_date' => date('Y-m-d\TH:i'), 'refresh_interval' => 20]; + } else { + // Für datetime-local Eingabefelder im Format "YYYY-MM-DDTHH:MM" + $config['from_date'] = date('Y-m-d\TH:i', strtotime($config['from_date'])); + $config['to_date'] = date('Y-m-d\TH:i', strtotime($config['to_date'])); + } + // Lade Hashtag-Konfiguration + $hashtagStmt = $pdo->query("SELECT hashtag_id, hashtag, active FROM hashtags ORDER BY hashtag ASC"); + $hashtags = $hashtagStmt->fetchAll(); + // Lade getrackte User: Alle unterschiedlichen User aus findings, links zu users (falls nicht vorhanden, erscheinen sie als inaktiv) + $userStmt = $pdo->query("SELECT DISTINCT f.acc_id, f.username, IFNULL(u.active, 0) as active + FROM findings f + LEFT JOIN users u ON f.acc_id = u.acc_id + ORDER BY f.username ASC"); + $users = $userStmt->fetchAll(); + ?> - Dashboard & Konfiguration - + Dashboard Konfiguration -

Dashboard - Konfigurationsbereich

- -
-

Zeitraum definieren (von - bis)

- - - - +

Dashboard Konfiguration

+ + +
+

Zeitraum

+
+ + + + + + + +
- -
+ +

Hashtags

- +
@@ -144,33 +192,22 @@ if (isset($_POST['action'])) { - query("SELECT hashtag_id, hashtag, active FROM hashtags ORDER BY hashtag ASC"); - while ($row = $stmt->fetch()) { - echo ""; - echo ""; - $checked = ($row['active'] == 1) ? "checked" : ""; - echo ""; - echo ""; - } - ?> + + + + + +
Hashtag
>
- +
- -
-

Getrackte Accounts

-
- - - - -
- + +
+

Getrackte User

+
@@ -179,39 +216,42 @@ if (isset($_POST['action'])) { - + + + + + + +
Acc_ID
>
- +
+ + + + + + Dashboard Übersicht + + + + +

Dashboard Übersicht

+
+ +
+ + + + +