Private
Public Access
feat: funcionalidad de logs, paginacion y ui para filtros
This commit is contained in:
1 parent
3af069e9e8
commit
461c381f12
9 files changed
+259
-79
No files matched your search
@@ -2,6 +2,7 @@ package logs
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
|
dto_builders "repuestera_back/dto/Builders"
|
||||||
r "repuestera_back/repositorios"
|
r "repuestera_back/repositorios"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
@@ -31,8 +32,10 @@ func ObtenerLogs(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
logsdto := dto_builders.NewLogTablaBuilder().ConLogs(logs).Build()
|
||||||
|
|
||||||
c.JSON(http.StatusOK, gin.H{
|
c.JSON(http.StatusOK, gin.H{
|
||||||
"logs": logs,
|
"logs": logsdto,
|
||||||
"maxlenght": cantpags,
|
"maxlenght": cantpags,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,36 @@
|
|||||||
|
package dto_builders
|
||||||
|
|
||||||
|
import (
|
||||||
|
dto "repuestera_back/dto/Logs"
|
||||||
|
"repuestera_back/models/logs"
|
||||||
|
)
|
||||||
|
|
||||||
|
type LogTablaBuilder struct {
|
||||||
|
logs []logs.Log
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewLogTablaBuilder() *LogTablaBuilder {
|
||||||
|
return &LogTablaBuilder{}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *LogTablaBuilder) ConLogs(logs []logs.Log) *LogTablaBuilder {
|
||||||
|
b.logs =logs;
|
||||||
|
return b
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
func (b *LogTablaBuilder) Build() []dto.LogTablaDto {
|
||||||
|
var dtos []dto.LogTablaDto
|
||||||
|
|
||||||
|
for i := 0; i < len(b.logs); i++ {
|
||||||
|
var d = &dto.LogTablaDto{
|
||||||
|
ID: b.logs[i].ID,
|
||||||
|
UsuarioID: b.logs[i].UsuarioID,
|
||||||
|
Accion: b.logs[i].Accion,
|
||||||
|
Detalles: b.logs[i].Detalles,
|
||||||
|
CreatedAt: b.logs[i].CreatedAt,
|
||||||
|
}
|
||||||
|
dtos = append(dtos, *d)
|
||||||
|
}
|
||||||
|
return dtos
|
||||||
|
}
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
package dto
|
||||||
|
|
||||||
|
import (
|
||||||
|
"repuestera_back/models/logs"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"gorm.io/datatypes"
|
||||||
|
)
|
||||||
|
type LogTablaDto struct {
|
||||||
|
ID uint64 `gorm:"primaryKey" json:"id"`
|
||||||
|
UsuarioID uint `gorm:"index;not null" json:"usuarioId"`
|
||||||
|
Accion string `gorm:"type:varchar(50);not null" json:"accion"`
|
||||||
|
Detalles datatypes.JSONSlice[logs.LogDetalle] `gorm:"type:json" json:"detalles,omitempty"`
|
||||||
|
CreatedAt time.Time `json:"created_at"`
|
||||||
|
}
|
||||||
@@ -15,6 +15,7 @@ type Log struct {
|
|||||||
Detalles datatypes.JSONSlice[LogDetalle] `gorm:"type:json" json:"detalles,omitempty"`
|
Detalles datatypes.JSONSlice[LogDetalle] `gorm:"type:json" json:"detalles,omitempty"`
|
||||||
CreatedAt time.Time `json:"created_at"`
|
CreatedAt time.Time `json:"created_at"`
|
||||||
Usuario models_seguridad.Usuario `gorm:"foreignKey:UsuarioID" json:"usuario,omitempty"`
|
Usuario models_seguridad.Usuario `gorm:"foreignKey:UsuarioID" json:"usuario,omitempty"`
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (Log) TableName() string {
|
func (Log) TableName() string {
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
@import 'tailwindcss';
|
@import 'tailwindcss';
|
||||||
.page-container {
|
.page-container {
|
||||||
padding: 6rem 0 4rem;
|
padding: 4rem 0 4rem;
|
||||||
max-width: 90%;
|
max-width: 90%;
|
||||||
margin: 0 auto;
|
margin: 0 auto;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,10 +9,13 @@
|
|||||||
Button
|
Button
|
||||||
} from 'carbon-components-svelte';
|
} from 'carbon-components-svelte';
|
||||||
import Renew from 'carbon-icons-svelte/lib/Renew.svelte';
|
import Renew from 'carbon-icons-svelte/lib/Renew.svelte';
|
||||||
import { LogsCache, type RespuestaPagina } from './LogsEstado.svelte';
|
import { LogsCache } from './LogsEstado.svelte';
|
||||||
import type { LogEntry } from '../../types';
|
import type { LogEntry } from '../../types';
|
||||||
import { obtenerLogs } from '$lib/hooks/logs/obtenerLogs';
|
import { obtenerLogs } from '$lib/hooks/logs/obtenerLogs';
|
||||||
import { ArrowRight } from 'carbon-icons-svelte';
|
import { Export } from 'carbon-icons-svelte';
|
||||||
|
import { ExportarLogs } from './ExportarLogs';
|
||||||
|
import TablaLogs from './TablaLogs.svelte';
|
||||||
|
import FiltrosLogs from './FiltrosLogs.svelte';
|
||||||
|
|
||||||
const cache = new LogsCache<LogEntry>();
|
const cache = new LogsCache<LogEntry>();
|
||||||
|
|
||||||
@@ -32,40 +35,37 @@
|
|||||||
irAPagina(0);
|
irAPagina(0);
|
||||||
});
|
});
|
||||||
|
|
||||||
const headers = [
|
|
||||||
{ key: 'id', value: '#' },
|
|
||||||
{ key: 'created_at', value: 'Fecha' },
|
|
||||||
{ 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.by(() => cache.estaCargando(cache.pagina));
|
const cargandoActual = $derived.by(() => cache.estaCargando(cache.pagina));
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<Header />
|
<Header />
|
||||||
<div class="page-container">
|
<div class="page-container">
|
||||||
<div class="flex items-center justify-between">
|
<div class="flex items-center justify-between">
|
||||||
<h2 class="text-lg font-semibold">Logs</h2>
|
<h1>Logs</h1>
|
||||||
<Button
|
<div>
|
||||||
kind="ghost"
|
<Button
|
||||||
size="small"
|
kind="ghost"
|
||||||
icon={Renew}
|
size="small"
|
||||||
iconDescription="Recargar página actual"
|
icon={Renew}
|
||||||
disabled={cargandoActual}
|
iconDescription="Recargar página actual"
|
||||||
onclick={() => irAPagina(cache.pagina, true)}
|
disabled={cargandoActual}
|
||||||
>
|
onclick={() => irAPagina(cache.pagina, true)}
|
||||||
Recargar
|
>
|
||||||
</Button>
|
Recargar
|
||||||
|
</Button>
|
||||||
|
<Button
|
||||||
|
kind="ghost"
|
||||||
|
size="small"
|
||||||
|
icon={Export}
|
||||||
|
iconDescription="Exportar CSV"
|
||||||
|
disabled={!(logs && logs.length != 0)}
|
||||||
|
onclick={() => {
|
||||||
|
new ExportarLogs(logs).descargarCSV();
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
Exportar CSV
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
{#if error}
|
{#if error}
|
||||||
@@ -80,51 +80,11 @@
|
|||||||
<p>No hay logs en esta página.</p>
|
<p>No hay logs en esta página.</p>
|
||||||
</Tile>
|
</Tile>
|
||||||
{:else}
|
{:else}
|
||||||
<DataTable {headers} rows={logs} expandable>
|
<div class="flex flex-col gap-2">
|
||||||
<svelte:fragment slot="cell" let:row let:cell>
|
<FiltrosLogs bind:logs />
|
||||||
{#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="expandedRow" let:row>
|
<TablaLogs {logs} />
|
||||||
{#if row.detalles && row.detalles.length > 0}
|
</div>
|
||||||
{@const detallesConId = row.detalles.map((d, i) => ({ ...d, id: d.id || i + 1 }))}
|
|
||||||
<DataTable
|
|
||||||
headers={[
|
|
||||||
{ key: 'nombreTabla', value: 'Tabla' },
|
|
||||||
{ key: 'columna', value: 'Columna' },
|
|
||||||
{ key: 'valorAnterior', value: 'Valor anterior' },
|
|
||||||
{ key: 'valorNuevo', value: 'Valor nuevo' }
|
|
||||||
]}
|
|
||||||
rows={detallesConId}
|
|
||||||
size="compact"
|
|
||||||
>
|
|
||||||
<svelte:fragment slot="cell" let:cell let:row>
|
|
||||||
{#if cell.key === 'valorAnterior'}
|
|
||||||
<span class="text-red-600">{cell.value ?? '—'}</span>
|
|
||||||
{:else if cell.key === 'valorNuevo'}
|
|
||||||
{#if cell.value}
|
|
||||||
<span class="text-green-600">{cell.value}</span>
|
|
||||||
{:else}
|
|
||||||
<span>—</span>
|
|
||||||
{/if}
|
|
||||||
{:else}
|
|
||||||
{cell.value}
|
|
||||||
{/if}
|
|
||||||
</svelte:fragment>
|
|
||||||
</DataTable>
|
|
||||||
{:else}
|
|
||||||
<p class="py-2 text-sm text-gray-500">Sin detalles registrados.</p>
|
|
||||||
{/if}
|
|
||||||
</svelte:fragment>
|
|
||||||
</DataTable>
|
|
||||||
{/if}
|
{/if}
|
||||||
|
|
||||||
<Pagination
|
<Pagination
|
||||||
@@ -135,7 +95,4 @@
|
|||||||
pageSizeInputDisabled={true}
|
pageSizeInputDisabled={true}
|
||||||
on:change={(e) => irAPagina(e.detail.page - 1)}
|
on:change={(e) => irAPagina(e.detail.page - 1)}
|
||||||
/>
|
/>
|
||||||
<!-- {#if cache.ultimoHit}
|
|
||||||
<span class="text-xs text-gray-500">desde caché</span>
|
|
||||||
{/if} -->
|
|
||||||
</div>
|
</div>
|
||||||
@@ -0,0 +1,64 @@
|
|||||||
|
export class ExportarLogs {
|
||||||
|
#data;
|
||||||
|
|
||||||
|
constructor(logs) {
|
||||||
|
this.#data = this.#extraerInfo(logs);
|
||||||
|
}
|
||||||
|
|
||||||
|
descargarCSV() {
|
||||||
|
const blob = new Blob([this.#data], {
|
||||||
|
type: 'text/csv;charset=utf-8;'
|
||||||
|
});
|
||||||
|
|
||||||
|
const url = URL.createObjectURL(blob);
|
||||||
|
|
||||||
|
const a = document.createElement('a');
|
||||||
|
a.href = url;
|
||||||
|
a.download = 'logs.csv';
|
||||||
|
document.body.appendChild(a);
|
||||||
|
a.click();
|
||||||
|
|
||||||
|
document.body.removeChild(a);
|
||||||
|
URL.revokeObjectURL(url);
|
||||||
|
}
|
||||||
|
|
||||||
|
#extraerInfo(logs) {
|
||||||
|
let csv;
|
||||||
|
csv = Object.entries(logs[0])
|
||||||
|
.map(([k, v]) => {
|
||||||
|
if (Array.isArray(v) && v.length > 0) {
|
||||||
|
return Object.keys(v[0])
|
||||||
|
.map((x) => `${k}_${x};`)
|
||||||
|
.join('');
|
||||||
|
}
|
||||||
|
|
||||||
|
return `${k};`;
|
||||||
|
})
|
||||||
|
.join('');
|
||||||
|
|
||||||
|
csv += '\n';
|
||||||
|
|
||||||
|
// añadimos las filas
|
||||||
|
//
|
||||||
|
csv += logs
|
||||||
|
.map((x) => {
|
||||||
|
let tmp = Object.entries(x)
|
||||||
|
.map(([_, v]) => {
|
||||||
|
if (Array.isArray(v)) {
|
||||||
|
return Object.keys(v[0])
|
||||||
|
.map((campo) => {
|
||||||
|
return `"${v.map((d) => d[campo] ?? '').join('\n')}";`;
|
||||||
|
})
|
||||||
|
.join('');
|
||||||
|
}
|
||||||
|
|
||||||
|
return `${v};`;
|
||||||
|
})
|
||||||
|
.join('');
|
||||||
|
|
||||||
|
return tmp + '\n';
|
||||||
|
})
|
||||||
|
.join(''); // console.log(csv);
|
||||||
|
return csv;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,36 @@
|
|||||||
|
<script>
|
||||||
|
import {
|
||||||
|
DatePicker,
|
||||||
|
DatePickerInput,
|
||||||
|
Disclosure,
|
||||||
|
Form,
|
||||||
|
NumberInput,
|
||||||
|
Select,
|
||||||
|
SelectItem,
|
||||||
|
Tile
|
||||||
|
} from 'carbon-components-svelte';
|
||||||
|
|
||||||
|
let { logs = $bindable() } = $props();
|
||||||
|
|
||||||
|
let filtros = $state({
|
||||||
|
idusu: 0
|
||||||
|
});
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<Tile>
|
||||||
|
<Disclosure summary="Filtros">
|
||||||
|
<Form>
|
||||||
|
<NumberInput fluid labelText="Id de Usuario" min={0} bind:value={filtros.idusu}></NumberInput>
|
||||||
|
<Select fluid labelText="Accion del Log">
|
||||||
|
<SelectItem text="Actualizar" value="ACTUALIZAR" />
|
||||||
|
<SelectItem text="Crear" value="CREAR" />
|
||||||
|
<SelectItem text="Eliminar" value="ELIMINAR" />
|
||||||
|
</Select>
|
||||||
|
|
||||||
|
<DatePicker fluid datePickerType="range" on:change>
|
||||||
|
<DatePickerInput labelText="Fecha inicio" placeholder="mm/dd/yyyy" />
|
||||||
|
<DatePickerInput labelText="Fecha fin" placeholder="mm/dd/yyyy" />
|
||||||
|
</DatePicker>
|
||||||
|
</Form>
|
||||||
|
</Disclosure>
|
||||||
|
</Tile>
|
||||||
@@ -0,0 +1,68 @@
|
|||||||
|
<script>
|
||||||
|
import { DataTable, Tag } from 'carbon-components-svelte';
|
||||||
|
|
||||||
|
const headers = [
|
||||||
|
{ key: 'id', value: '#' },
|
||||||
|
{ key: 'created_at', value: 'Fecha' },
|
||||||
|
{ key: 'usuario', value: 'Usuario' },
|
||||||
|
{ key: 'accion', value: 'Acción' }
|
||||||
|
];
|
||||||
|
|
||||||
|
let { logs } = $props();
|
||||||
|
|
||||||
|
function formatFecha(iso) {
|
||||||
|
return new Date(iso).toLocaleString('es-AR', {
|
||||||
|
day: '2-digit',
|
||||||
|
month: '2-digit',
|
||||||
|
year: 'numeric',
|
||||||
|
hour: '2-digit',
|
||||||
|
minute: '2-digit'
|
||||||
|
});
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<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="expandedRow" let:row>
|
||||||
|
{#if row.detalles && row.detalles.length > 0}
|
||||||
|
{@const detallesConId = row.detalles.map((d, i) => ({ ...d, id: d.id || i + 1 }))}
|
||||||
|
<DataTable
|
||||||
|
headers={[
|
||||||
|
{ key: 'nombreTabla', value: 'Tabla' },
|
||||||
|
{ key: 'columna', value: 'Columna' },
|
||||||
|
{ key: 'valorAnterior', value: 'Valor anterior' },
|
||||||
|
{ key: 'valorNuevo', value: 'Valor nuevo' }
|
||||||
|
]}
|
||||||
|
rows={detallesConId}
|
||||||
|
size="compact"
|
||||||
|
>
|
||||||
|
<svelte:fragment slot="cell" let:cell let:row>
|
||||||
|
{#if cell.key === 'valorAnterior'}
|
||||||
|
<span class="text-red-600">{cell.value ?? '—'}</span>
|
||||||
|
{:else if cell.key === 'valorNuevo'}
|
||||||
|
{#if cell.value}
|
||||||
|
<span class="text-green-600">{cell.value}</span>
|
||||||
|
{:else}
|
||||||
|
<span>—</span>
|
||||||
|
{/if}
|
||||||
|
{:else}
|
||||||
|
{cell.value}
|
||||||
|
{/if}
|
||||||
|
</svelte:fragment>
|
||||||
|
</DataTable>
|
||||||
|
{:else}
|
||||||
|
<p class="py-2 text-sm text-gray-500">Sin detalles registrados.</p>
|
||||||
|
{/if}
|
||||||
|
</svelte:fragment>
|
||||||
|
</DataTable>
|
||||||
Reference in new issue
Block a user