smeserver-mailstats/root/opt/mailstats/html/showSummaryLogs.php

161 lines
4.6 KiB
PHP
Raw Normal View History

<?php
// Database configuration
$servername = "localhost";
$username = "mailstats";
$password = "mailstats";
$dbname = "mailstats";
// Default date to yesterday
$date = isset($_GET['date']) ? $_GET['date'] : date('Y-m-d', strtotime('-1 day'));
// Default hour to 99 (means all the hours)
$hour = isset($_GET['hour']) ? $_GET['hour'] : 99;
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Prepare and execute the query
if ($hour == 99){
$sql = "SELECT * FROM SummaryLogs WHERE Date = ?";
$stmt = $conn->prepare($sql);
$stmt->bind_param("s", $date);
} else {
$sql = "SELECT * FROM SummaryLogs WHERE Date = ? AND Hour = ?";
$stmt = $conn->prepare($sql);
$stmt->bind_param("si", $date, $hour);
}
$stmt->execute();
$result = $stmt->get_result();
$result_count = $result->num_rows;
function generateLogDataTable($logData) {
$data = json_decode($logData, true);
if (is_null($data)) {
return "Invalid JSON data";
}
2024-06-30 13:05:54 +02:00
//// Remove entries with the key "logterse"
//if (isset($data['logterse'])) {
//unset($data['logterse']);
//}
// Remove entries with the key "logterse" and remove entries with empty values
foreach ($data as $key => $value) {
if ($key === 'logterse' || empty($value)) {
unset($data[$key]);
}
}
// Handle adjacent duplicates by merging keys
$mergedData = [];
$previousValue = null;
foreach ($data as $key => $value) {
if ($value === $previousValue) {
// Merge the current key with the previous key
end($mergedData);
$lastKey = key($mergedData);
$newKey = "$lastKey/$key";
$mergedData[$newKey] = $value;
// Remove the old entry
unset($mergedData[$lastKey]);
} else {
// Otherwise, add a new entry
$mergedData[$key] = $value;
}
$previousValue = $value;
}
$keys = array_keys($mergedData);
$values = array_values($mergedData);
$output = '<table class="stripes" style="border-collapse: collapse; width:95%;overflow-x:auto; margin:2%"><tbody>';
// Divide keys and values into sets of 6
2024-06-30 13:05:54 +02:00
$chunks = array_chunk($keys, 6);
foreach ($chunks as $chunkIndex => $chunk) {
if ($chunkIndex > 0) {
// Add spacing between different sets
#$output .= '<tr><td colspan="6" style="height: 1em;"></td></tr>';
}
$output .= '<tr>';
foreach ($chunk as $key) {
$output .= '<th>' . htmlspecialchars($key) . '</th>';
}
$output .= '</tr><tr>';
foreach ($chunk as $i => $key) {
2024-06-30 13:05:54 +02:00
$output .= '<td>' . htmlspecialchars($values[$chunkIndex * 6+ $i]) . '</td>';
}
$output .= '</tr>';
}
$output .= '</tbody></table>';
return $output;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
2024-06-30 13:05:54 +02:00
<link rel='stylesheet' type='text/css' href='css/mailstats.css' />
<title>Summary Logs</title>
2024-06-30 13:05:54 +02:00
<!-- <style>
table {
2024-06-30 13:05:54 +02:00
xxwidth: 100%;
xxborder-collapse: collapse;
}
table, th, td {
2024-06-30 13:05:54 +02:00
xxborder: 1px solid black;
}
th, td {
2024-06-30 13:05:54 +02:00
xxpadding: 8px;
xxtext-align: left;
}
</style>
2024-06-30 13:05:54 +02:00
-->
</head>
<body>
2024-06-30 13:10:57 +02:00
<div style="width:100%;overflow-x:auto;font-size:0.726cqw">"
<h1>Summary Logs for Date: <?= htmlspecialchars($date) ?> <?= $hour == 99 ? 'for All Hours' : 'and Hour: ' . htmlspecialchars($hour) ?></h1>
<h3>Found <?= $result_count ?> records.</h3>
2024-06-30 13:05:54 +02:00
<table style="border-collapse:collapse;width:98%">
<thead>
<tr>
<th>Id</th>
<!--<th>Date</th>-->
<!--<th>Hour</th>-->
<th>Log Data</th>
</tr>
</thead>
<tbody>
<?php if ($result->num_rows > 0): ?>
<?php while($row = $result->fetch_assoc()): ?>
<tr>
<td><?= htmlspecialchars($row['id']) ?></td>
<td><?= generateLogDataTable($row['logData']) ?></td>
</tr>
<?php endwhile; ?>
<?php else: ?>
<tr>
<td colspan="4">No records found for the specified date and hour.</td>
</tr>
<?php endif; ?>
</tbody>
</table>
2024-06-30 13:05:54 +02:00
</div>
<?php
// Close the connection
$stmt->close();
$conn->close();
?>
</body>
</html>