making log viewer better

This commit is contained in:
Janis Steiner
2024-10-14 12:59:16 +01:00
parent 4611b118bf
commit c7a644a8e4

View File

@@ -38,7 +38,7 @@ $(document).ready(function () {
<div class="col-md-auto"> <div class="col-md-auto">
<h1>Log Entries</h1> <h1>Log Entries</h1>
<!-- Filter Form --> <!-- Search Form -->
<div class="overflow-auto"> <div class="overflow-auto">
<form method="GET" action=""> <form method="GET" action="">
<table class="table"> <table class="table">
@@ -51,50 +51,38 @@ $(document).ready(function () {
<th>Info</th> <th>Info</th>
</tr> </tr>
<tr> <tr>
<td>---</td>
<td>---</td> <td>---</td>
<td> <td>
<select class="form-select" name="type_"> <!-- Search by IP Address -->
<option value="All_types">All Types</option> <input type="text" class="form-control" name="search_ip" value="<?= isset($_GET['search_ip']) ? htmlspecialchars($_GET['search_ip']) : '' ?>" placeholder="Search IP...">
<option value="PRINT::UPLOAD::PRINTER">PRINT::UPLOAD::PRINTER</option>
<option value="PRINT:JOB:START:FAILED">PRINT:JOB:START:FAILED</option>
<option value="PRINT::UPLOAD::QUEUE">PRINT::UPLOAD::QUEUE</option>
<option value="PRINT::UPLOAD::FILE::FAILED">PRINT::UPLOAD::FILE::FAILED</option>
<option value="JOB::PRINTERCTRL::FREE">JOB::PRINTERCTRL::FREE</option>
<option value="JOB::QUEUECTRL::REMOVE">JOB::QUEUECTRL::REMOVE</option>
<option value="JOB::PRINTERCTRL::CANCEL::FAILED">JOB::PRINTERCTRL::CANCEL::FAILED</option>
<option value="JOB::PRINTERCTRL::CANCEL">JOB::PRINTERCTRL::CANCEL</option>
</select>
</td> </td>
<td> <td>
<select class="form-select" id="username" name="username"> <!-- Search by Type -->
<option value="All_usernames">All Users</option> <input type="text" class="form-control" name="search_type" value="<?= isset($_GET['search_type']) ? htmlspecialchars($_GET['search_type']) : '' ?>" placeholder="Search Type...">
<?php
// Fetch all usernames in one query
$sql = "SELECT username FROM users ORDER BY username ASC";
$stmt = mysqli_prepare($link, $sql);
mysqli_stmt_execute($stmt);
mysqli_stmt_bind_result($stmt, $uname);
// Populate the dropdown with usernames
while (mysqli_stmt_fetch($stmt)) {
echo '<option value="' . htmlspecialchars($uname) . '">' . htmlspecialchars($uname) . '</option>';
}
mysqli_stmt_close($stmt);
?>
</select>
</td> </td>
<td> <td>
<button type="submit" class="btn btn-primary">Apply Filter</button> <!-- Search by Username -->
<input type="text" class="form-control" name="search_username" value="<?= isset($_GET['search_username']) ? htmlspecialchars($_GET['search_username']) : '' ?>" placeholder="Search Username...">
</td>
<td>
<!-- Search by Info -->
<input type="text" class="form-control" name="search_info" value="<?= isset($_GET['search_info']) ? htmlspecialchars($_GET['search_info']) : '' ?>" placeholder="Search Info...">
</td>
</tr>
<tr>
<td colspan="5">
<button type="submit" class="btn btn-primary">Search</button>
</td> </td>
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
<?php <?php
// Define default filters // Retrieve search terms from GET parameters
$type_filter = isset($_GET["type_"]) ? $_GET["type_"] : "All_types"; $search_ip = isset($_GET['search_ip']) ? $_GET['search_ip'] : '';
$user_filter = isset($_GET["username"]) ? $_GET["username"] : "All_usernames"; $search_type = isset($_GET['search_type']) ? $_GET['search_type'] : '';
$search_username = isset($_GET['search_username']) ? $_GET['search_username'] : '';
$search_info = isset($_GET['search_info']) ? $_GET['search_info'] : '';
// Pagination variables // Pagination variables
$items_per_page = 25; $items_per_page = 25;
@@ -126,11 +114,13 @@ $(document).ready(function () {
// Reverse the log entries to display the latest first // Reverse the log entries to display the latest first
$log_entries = array_reverse($log_entries); $log_entries = array_reverse($log_entries);
// Filter log entries by type and username // Filter log entries by search terms (partial matches)
$filtered_entries = array_filter($log_entries, function ($entry) use ($type_filter, $user_filter) { $filtered_entries = array_filter($log_entries, function ($entry) use ($search_ip, $search_type, $search_username, $search_info) {
$type_match = ($type_filter === "All_types" || $entry['type'] === $type_filter); $ip_match = empty($search_ip) || stripos($entry['ip'], $search_ip) !== false;
$user_match = ($user_filter === "All_usernames" || $entry['username'] === $user_filter); $type_match = empty($search_type) || stripos($entry['type'], $search_type) !== false;
return $type_match && $user_match; $username_match = empty($search_username) || stripos($entry['username'], $search_username) !== false;
$info_match = empty($search_info) || stripos($entry['info'], $search_info) !== false;
return $ip_match && $type_match && $username_match && $info_match;
}); });
// Calculate total pages and slice the log entries for the current page // Calculate total pages and slice the log entries for the current page
@@ -163,19 +153,19 @@ $(document).ready(function () {
<ul class="pagination justify-content-center"> <ul class="pagination justify-content-center">
<?php if ($current_page > 1): ?> <?php if ($current_page > 1): ?>
<li class="page-item"> <li class="page-item">
<a class="page-link" href="?page=<?= $current_page - 1 ?>&type_=<?= urlencode($type_filter) ?>&username=<?= urlencode($user_filter) ?>">Previous</a> <a class="page-link" href="?page=<?= $current_page - 1 ?>&search_ip=<?= urlencode($search_ip) ?>&search_type=<?= urlencode($search_type) ?>&search_username=<?= urlencode($search_username) ?>&search_info=<?= urlencode($search_info) ?>">Previous</a>
</li> </li>
<?php endif; ?> <?php endif; ?>
<?php for ($page = 1; $page <= $total_pages; $page++): ?> <?php for ($page = 1; $page <= $total_pages; $page++): ?>
<li class="page-item <?= ($page === $current_page) ? 'active' : '' ?>"> <li class="page-item <?= ($page === $current_page) ? 'active' : '' ?>">
<a class="page-link" href="?page=<?= $page ?>&type_=<?= urlencode($type_filter) ?>&username=<?= urlencode($user_filter) ?>"><?= $page ?></a> <a class="page-link" href="?page=<?= $page ?>&search_ip=<?= urlencode($search_ip) ?>&search_type=<?= urlencode($search_type) ?>&search_username=<?= urlencode($search_username) ?>&search_info=<?= urlencode($search_info) ?>"><?= $page ?></a>
</li> </li>
<?php endfor; ?> <?php endfor; ?>
<?php if ($current_page < $total_pages): ?> <?php if ($current_page < $total_pages): ?>
<li class="page-item"> <li class="page-item">
<a class="page-link" href="?page=<?= $current_page + 1 ?>&type_=<?= urlencode($type_filter) ?>&username=<?= urlencode($user_filter) ?>">Next</a> <a class="page-link" href="?page=<?= $current_page + 1 ?>&search_ip=<?= urlencode($search_ip) ?>&search_type=<?= urlencode($search_type) ?>&search_username=<?= urlencode($search_username) ?>&search_info=<?= urlencode($search_info) ?>">Next</a>
</li> </li>
<?php endif; ?> <?php endif; ?>
</ul> </ul>