Private
Public Access
funcionalidad de logs andando
This commit is contained in:
1 parent
06ed4a518b
commit
aac88856ac
21 files changed
+627
-31
No files matched your search
@@ -0,0 +1,124 @@
|
||||
<script lang="ts">
|
||||
import Header from '$lib/components/Header.svelte';
|
||||
import {
|
||||
DataTable,
|
||||
PaginationNav,
|
||||
InlineLoading,
|
||||
Tag,
|
||||
Tile,
|
||||
Button
|
||||
} from 'carbon-components-svelte';
|
||||
import Renew from 'carbon-icons-svelte/lib/Renew.svelte';
|
||||
import { LogsCache, type RespuestaPagina } from './LogsEstado.svelte';
|
||||
import type { LogEntry } from '../../types';
|
||||
import { obtenerLogs } from '$lib/hooks/logs/obtenerLogs';
|
||||
|
||||
const cache = new LogsCache<LogEntry>();
|
||||
|
||||
let logs = $state<LogEntry[]>([]);
|
||||
let error = $state<string | null>(null);
|
||||
|
||||
async function irAPagina(pagina: number, forzar = false) {
|
||||
error = null;
|
||||
try {
|
||||
logs = await cache.cargarPagina(pagina, obtenerLogs, forzar);
|
||||
} catch (e) {
|
||||
error = e instanceof Error ? e.message : 'Error desconocido al cargar los logs';
|
||||
}
|
||||
}
|
||||
|
||||
$effect(() => {
|
||||
irAPagina(0);
|
||||
});
|
||||
|
||||
const headers = [
|
||||
{ key: 'created_at', value: 'Fecha' },
|
||||
{ key: 'empresaId', value: 'Empresa' },
|
||||
{ key: 'usuario', value: 'Usuario' },
|
||||
{ key: 'accion', value: 'Acción' }
|
||||
];
|
||||
|
||||
function formatFecha(iso: string) {
|
||||
return new Date(iso).toLocaleString('es-AR', {
|
||||
day: '2-digit',
|
||||
month: '2-digit',
|
||||
year: 'numeric',
|
||||
hour: '2-digit',
|
||||
minute: '2-digit'
|
||||
});
|
||||
}
|
||||
|
||||
const cargandoActual = $derived(() => cache.estaCargando(cache.paginaActual()));
|
||||
</script>
|
||||
|
||||
<Header />
|
||||
<div class="page-container">
|
||||
<div class="flex items-center justify-between">
|
||||
<h2 class="text-lg font-semibold">Logs</h2>
|
||||
<Button
|
||||
kind="ghost"
|
||||
size="small"
|
||||
icon={Renew}
|
||||
iconDescription="Recargar página actual"
|
||||
disabled={cargandoActual()}
|
||||
onclick={() => irAPagina(cache.paginaActual(), true)}
|
||||
>
|
||||
Recargar
|
||||
</Button>
|
||||
</div>
|
||||
|
||||
{#if error}
|
||||
<Tile>
|
||||
<p class="mb-2 text-red-600">{error}</p>
|
||||
<Button size="small" onclick={() => irAPagina(cache.paginaActual(), true)}>Reintentar</Button>
|
||||
</Tile>
|
||||
{:else if cargandoActual() && logs.length === 0}
|
||||
<InlineLoading description="Cargando logs…" />
|
||||
{:else if logs.length === 0}
|
||||
<Tile>
|
||||
<p>No hay logs en esta página.</p>
|
||||
</Tile>
|
||||
{:else}
|
||||
<DataTable {headers} rows={logs} expandable>
|
||||
<svelte:fragment slot="cell" let:row let:cell>
|
||||
{#if cell.key === 'created_at'}
|
||||
{formatFecha(row.created_at)}
|
||||
{:else if cell.key === 'usuario'}
|
||||
{row.usuario?.nombre ?? `#${row.usuarioId}`}
|
||||
{:else if cell.key === 'accion'}
|
||||
<Tag type="blue">{row.accion}</Tag>
|
||||
{:else}
|
||||
{cell.value}
|
||||
{/if}
|
||||
</svelte:fragment>
|
||||
|
||||
<svelte:fragment slot="expanded-row" let:row>
|
||||
{#if row.detalles && row.detalles.length > 0}
|
||||
<div class="flex flex-col gap-2 py-2">
|
||||
{#each row.detalles as d}
|
||||
<div class="grid grid-cols-4 gap-2 text-sm">
|
||||
<span class="font-medium">{d.nombreTabla}</span>
|
||||
<span>{d.columna}</span>
|
||||
<span class="text-red-600 line-through">{d.valorAnterior ?? '—'}</span>
|
||||
<span class="text-green-600">{d.valorNuevo ?? '—'}</span>
|
||||
</div>
|
||||
{/each}
|
||||
</div>
|
||||
{:else}
|
||||
<p class="py-2 text-sm text-gray-500">Sin detalles registrados.</p>
|
||||
{/if}
|
||||
</svelte:fragment>
|
||||
</DataTable>
|
||||
{/if}
|
||||
|
||||
<div class="flex items-center gap-3">
|
||||
<PaginationNav
|
||||
page={cache.paginaActual()}
|
||||
total={cache.paginaMaxima() + 1}
|
||||
on:change={(e) => irAPagina(e.detail)}
|
||||
/>
|
||||
{#if cache.ultimoFueCache()}
|
||||
<span class="text-xs text-gray-500">desde caché</span>
|
||||
{/if}
|
||||
</div>
|
||||
</div>
|
||||
@@ -0,0 +1,86 @@
|
||||
// logs-cache.svelte.ts
|
||||
import { SvelteMap, SvelteSet } from 'svelte/reactivity';
|
||||
|
||||
export interface RespuestaPagina<T> {
|
||||
logs: T[];
|
||||
maxPagina: number;
|
||||
}
|
||||
|
||||
export class LogsCache<T = unknown> {
|
||||
#cache = new SvelteMap<number, T[]>();
|
||||
#cargando = new SvelteSet<number>();
|
||||
#enVuelo = new Map<number, Promise<T[]>>();
|
||||
#pagina = $state(0);
|
||||
#maxPagina = $state(0);
|
||||
|
||||
#ultimoHit = $state(true);
|
||||
ultimoFueCache = $derived(() => this.#ultimoHit);
|
||||
paginaActual = $derived(() => this.#pagina);
|
||||
paginaMaxima = $derived(() => this.#maxPagina);
|
||||
paginaActMax = $derived(() => `${this.#pagina}/${this.#maxPagina}`);
|
||||
|
||||
tieneCache(pagina: number): boolean {
|
||||
return this.#cache.has(pagina);
|
||||
}
|
||||
|
||||
obtenerLogs(pagina: number): T[] | undefined {
|
||||
return this.#cache.get(pagina);
|
||||
}
|
||||
|
||||
estaCargando(pagina: number): boolean {
|
||||
return this.#cargando.has(pagina);
|
||||
}
|
||||
|
||||
async cargarPagina(
|
||||
pagina: number,
|
||||
fetcher: (pagina: number) => Promise<RespuestaPagina<T>>,
|
||||
forzar = false
|
||||
): Promise<T[]> {
|
||||
this.#pagina = pagina;
|
||||
|
||||
if (!forzar && this.#cache.has(pagina)) {
|
||||
this.#ultimoHit = true;
|
||||
return this.#cache.get(pagina)!;
|
||||
}
|
||||
|
||||
if (this.#enVuelo.has(pagina)) {
|
||||
return this.#enVuelo.get(pagina)!;
|
||||
}
|
||||
|
||||
this.#ultimoHit = false;
|
||||
this.#cargando.add(pagina);
|
||||
const promesa = fetcher(pagina)
|
||||
.then(({ logs, maxPagina }) => {
|
||||
this.#cache.set(pagina, logs);
|
||||
this.#maxPagina = maxPagina;
|
||||
return logs;
|
||||
})
|
||||
.finally(() => {
|
||||
this.#cargando.delete(pagina);
|
||||
this.#enVuelo.delete(pagina);
|
||||
});
|
||||
|
||||
this.#enVuelo.set(pagina, promesa);
|
||||
return promesa;
|
||||
}
|
||||
|
||||
invalidar(pagina?: number) {
|
||||
if (pagina === undefined) {
|
||||
this.#cache.clear();
|
||||
} else {
|
||||
this.#cache.delete(pagina);
|
||||
}
|
||||
}
|
||||
|
||||
paginasCacheadas(): number[] {
|
||||
return Array.from(this.#cache.keys());
|
||||
}
|
||||
|
||||
get puedeAvanzar(): boolean {
|
||||
return this.#pagina < this.#maxPagina;
|
||||
}
|
||||
|
||||
get puedeRetroceder(): boolean {
|
||||
return this.#pagina > 0;
|
||||
}
|
||||
}
|
||||
@@ -142,6 +142,6 @@
|
||||
openEditar = false;
|
||||
repuestoEditar = { id: 0 };
|
||||
}}
|
||||
repuesto={repuestoEditar}
|
||||
bind:repuesto={repuestoEditar}
|
||||
></EditarRepuestoForm>
|
||||
<ModalStructured objeto={repuestoInfo} bind:open={openInfo} />
|
||||
@@ -7,7 +7,7 @@
|
||||
let {
|
||||
open = $bindable(),
|
||||
onClose,
|
||||
repuesto
|
||||
repuesto = $bindable()
|
||||
}: { open: boolean; onClose: Function; repuesto: ActualizarRepuestoDto } = $props();
|
||||
|
||||
let error = $state('');
|
||||
|
||||
Reference in new issue
Block a user