feat: enhance audit logs with geo flags, relative time, and UX improvements (#2760)

This commit is contained in:
ACX
2026-02-15 08:11:39 -07:00
committed by GitHub
parent c9551ef4ab
commit 0318d1b3bb
11 changed files with 224 additions and 11 deletions
@@ -12,6 +12,7 @@ export interface AuditLog {
entityId: number | null;
description: string;
ipAddress: string | null;
countryCode: string | null;
createdAt: string;
}
@@ -43,6 +43,20 @@
[showButtonBar]="true"
class="date-range-filter">
</p-datepicker>
<button
*ngIf="hasActiveFilters"
(click)="clearAllFilters()"
class="icon-btn clear-btn"
[title]="t('clearFilters')">
<i class="pi pi-filter-slash"></i>
</button>
<button
(click)="toggleAutoRefresh()"
class="icon-btn refresh-btn"
[class.active]="autoRefresh"
[title]="t('autoRefresh')">
<i class="pi pi-sync" [class.pi-spin]="autoRefresh"></i>
</button>
</div>
</div>
<div class="section-body">
@@ -61,25 +75,34 @@
<ng-template pTemplate="header">
<tr>
<th style="width: 175px">{{ t('columns.timestamp') }}</th>
<th style="width: 120px">{{ t('columns.timestamp') }}</th>
<th style="width: 100px">{{ t('columns.user') }}</th>
<th style="width: 140px">{{ t('columns.action') }}</th>
<th>{{ t('columns.description') }}</th>
<th style="width: 130px">{{ t('columns.ip') }}</th>
<th style="width: 160px">{{ t('columns.ip') }}</th>
</tr>
</ng-template>
<ng-template pTemplate="body" let-log>
<tr>
<td class="timestamp-cell">{{ log.createdAt | date:'medium' }}</td>
<td class="timestamp-cell" [title]="log.createdAt | date:'medium'">
{{ getRelativeTime(log.createdAt) }}
</td>
<td>{{ log.username }}</td>
<td>
<span class="action-badge" [ngClass]="getActionClass(log.action)">
{{ formatAction(log.action) }}
</span>
</td>
<td class="description-cell">{{ log.description }}</td>
<td class="ip-cell">{{ log.ipAddress || '—' }}</td>
<td class="description-cell"
[class.expanded]="expandedRows.has(log.id)"
(click)="toggleDescription(log.id)">
{{ log.description }}
</td>
<td class="ip-cell">
<span *ngIf="log.countryCode" class="flag-emoji">{{ countryCodeToFlag(log.countryCode) }}</span>
{{ log.ipAddress || '—' }}
</td>
</tr>
</ng-template>
@@ -57,6 +57,31 @@
min-width: 220px;
}
.icon-btn {
display: inline-flex;
align-items: center;
justify-content: center;
width: 2rem;
height: 2rem;
border: 1px solid var(--p-content-border-color);
border-radius: 6px;
background: transparent;
color: var(--p-text-muted-color);
cursor: pointer;
transition: all 0.2s;
&:hover {
background: var(--p-content-hover-background);
color: var(--p-text-color);
}
&.active {
background: color-mix(in srgb, var(--p-primary-color) 15%, transparent);
color: var(--p-primary-color);
border-color: var(--p-primary-color);
}
}
::ng-deep .p-datatable-table {
table-layout: fixed;
}
@@ -95,12 +120,19 @@
white-space: nowrap;
font-size: 0.875rem;
color: var(--p-text-muted-color);
cursor: default;
}
.description-cell {
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
cursor: pointer;
&.expanded {
white-space: normal;
word-break: break-word;
}
}
.ip-cell {
@@ -109,6 +141,12 @@
color: var(--p-text-muted-color);
}
.flag-emoji {
font-size: 1.15rem;
margin-right: 0.25rem;
vertical-align: middle;
}
::ng-deep .p-datatable.p-datatable-sm .p-datatable-tbody > tr > td.empty-message {
text-align: center;
padding: 3rem 2rem;
@@ -1,10 +1,11 @@
import {Component, inject, OnInit} from '@angular/core';
import {Component, inject, OnInit, OnDestroy} from '@angular/core';
import {CommonModule} from '@angular/common';
import {TableLazyLoadEvent, TableModule} from 'primeng/table';
import {Select} from 'primeng/select';
import {DatePicker} from 'primeng/datepicker';
import {FormsModule} from '@angular/forms';
import {TranslocoDirective} from '@jsverse/transloco';
import {Subscription, interval} from 'rxjs';
import {AuditLog, AuditLogService} from './audit-log.service';
interface ActionOption {
@@ -24,7 +25,7 @@ interface UsernameOption {
templateUrl: './audit-logs.component.html',
styleUrl: './audit-logs.component.scss'
})
export class AuditLogsComponent implements OnInit {
export class AuditLogsComponent implements OnInit, OnDestroy {
private readonly auditLogService = inject(AuditLogService);
logs: AuditLog[] = [];
@@ -34,6 +35,9 @@ export class AuditLogsComponent implements OnInit {
selectedAction: string | null = null;
selectedUsername: string | null = null;
dateRange: Date[] | null = null;
expandedRows = new Set<number>();
autoRefresh = false;
private autoRefreshSub?: Subscription;
usernameOptions: UsernameOption[] = [{label: 'All Users', value: ''}];
@@ -79,6 +83,10 @@ export class AuditLogsComponent implements OnInit {
this.loadLogs();
}
ngOnDestroy(): void {
this.autoRefreshSub?.unsubscribe();
}
loadUsernames(): void {
this.auditLogService.getDistinctUsernames().subscribe({
next: (usernames) => {
@@ -90,8 +98,10 @@ export class AuditLogsComponent implements OnInit {
});
}
loadLogs(): void {
this.loading = true;
loadLogs(showLoading = true): void {
if (showLoading) {
this.loading = true;
}
const action = this.selectedAction || undefined;
const username = this.selectedUsername || undefined;
const from = this.dateRange?.[0] ? this.formatDateTime(this.dateRange[0]) : undefined;
@@ -124,6 +134,59 @@ export class AuditLogsComponent implements OnInit {
}
}
toggleAutoRefresh(): void {
this.autoRefresh = !this.autoRefresh;
if (this.autoRefresh) {
this.autoRefreshSub = interval(10000).subscribe(() => this.loadLogs(false));
} else {
this.autoRefreshSub?.unsubscribe();
}
}
clearAllFilters(): void {
this.selectedAction = null;
this.selectedUsername = null;
this.dateRange = null;
this.currentPage = 0;
this.loadLogs();
}
get hasActiveFilters(): boolean {
return !!this.selectedAction || !!this.selectedUsername ||
(this.dateRange != null && this.dateRange.length > 0 && this.dateRange[0] != null);
}
toggleDescription(logId: number): void {
if (this.expandedRows.has(logId)) {
this.expandedRows.delete(logId);
} else {
this.expandedRows.add(logId);
}
}
countryCodeToFlag(code: string): string {
if (!code || code.length !== 2) return '';
return [...code.toUpperCase()]
.map(c => String.fromCodePoint(0x1F1E6 + c.charCodeAt(0) - 65))
.join('');
}
getRelativeTime(dateStr: string): string {
const now = new Date();
const date = new Date(dateStr);
const diffMs = now.getTime() - date.getTime();
const diffSec = Math.floor(diffMs / 1000);
const diffMin = Math.floor(diffSec / 60);
const diffHour = Math.floor(diffMin / 60);
const diffDay = Math.floor(diffHour / 24);
if (diffSec < 60) return 'Just now';
if (diffMin < 60) return `${diffMin}m ago`;
if (diffHour < 24) return `${diffHour}h ago`;
if (diffDay < 30) return `${diffDay}d ago`;
return new Intl.DateTimeFormat(undefined, {dateStyle: 'medium'}).format(date);
}
formatAction(action: string): string {
return action.replace(/_/g, ' ').replace(/\b\w/g, c => c.toUpperCase()).replace(/\B\w+/g, c => c.toLowerCase());
}