Skip to content

Commit

Permalink
feat(logs): render logs in table
Browse files Browse the repository at this point in the history
  • Loading branch information
kunish committed Sep 1, 2023
1 parent 4a16271 commit 7eee024
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 22 deletions.
1 change: 0 additions & 1 deletion docs/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ mode: rule
log-level: info
ipv6: true
external-controller: 0.0.0.0:9090
external-ui: ui
global-client-fingerprint: random

profile:
Expand Down
104 changes: 84 additions & 20 deletions src/pages/Logs.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
import { createEventSignal } from '@solid-primitives/event-listener'
import { createReconnectingWS } from '@solid-primitives/websocket'
import {
ColumnDef,
createSolidTable,
flexRender,
getCoreRowModel,
} from '@tanstack/solid-table'
import { For, createEffect, createSignal } from 'solid-js'
import { secret, wsEndpointURL } from '~/signals'
import { Log } from '~/types'

type LogWithSeq = Log & { seq: number }

export default () => {
let seq = 0
const [search, setSearch] = createSignal('')
const [logs, setLogs] = createSignal<string[]>([])
const [logs, setLogs] = createSignal<LogWithSeq[]>([])

const ws = createReconnectingWS(`${wsEndpointURL()}/logs?token=${secret()}`)

Expand All @@ -21,11 +31,37 @@ export default () => {
}

setLogs((logs) =>
[
...logs,
(JSON.parse(data) as { type: string; payload: string }).payload,
].slice(-100),
[{ ...(JSON.parse(data) as Log), seq }, ...logs].slice(0, 100),
)

seq++
})

const columns: ColumnDef<LogWithSeq>[] = [
{
accessorKey: 'Sequence',
accessorFn: (row) => row.seq,
},
{
accessorKey: 'Type',
accessorFn: (row) => row.type,
},
{
accessorKey: 'Payload',
accessorFn: (row) => row.payload,
},
]

const table = createSolidTable({
get data() {
return search()
? logs().filter((log) =>
log.payload.toLowerCase().includes(search().toLowerCase()),
)
: logs()
},
columns,
getCoreRowModel: getCoreRowModel(),
})

return (
Expand All @@ -37,21 +73,49 @@ export default () => {
/>

<div class="overflow-x-auto whitespace-nowrap">
<For
each={
search()
? logs().filter((log) =>
log.toLowerCase().includes(search().toLowerCase()),
)
: logs()
}
>
{(log) => (
<div class="flex gap-4">
<div class="flex-shrink-0">{log}</div>
</div>
)}
</For>
<table class="table table-zebra-zebra table-xs bg-base-200">
<thead>
<For each={table.getHeaderGroups()}>
{(headerGroup) => (
<tr>
<For each={headerGroup.headers}>
{(header) => (
<th class="bg-base-300">
<div>
{header.isPlaceholder
? null
: flexRender(
header.column.columnDef.header,
header.getContext(),
)}
</div>
</th>
)}
</For>
</tr>
)}
</For>
</thead>

<tbody>
<For each={table.getRowModel().rows}>
{(row) => (
<tr>
<For each={row.getVisibleCells()}>
{(cell) => (
<td>
{flexRender(
cell.column.columnDef.cell,
cell.getContext(),
)}
</td>
)}
</For>
</tr>
)}
</For>
</tbody>
</table>
</div>
</div>
)
Expand Down
2 changes: 1 addition & 1 deletion src/pages/Overview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ export default () => {
</TrafficWidget>
</div>

<div class="mx-auto grid w-full max-w-screen-lg grid-cols-1 gap-4">
<div class="mx-auto grid h-full w-full max-w-screen-xl grid-cols-1 gap-4">
<SolidApexCharts
type="area"
options={trafficChartOptions()}
Expand Down
5 changes: 5 additions & 0 deletions src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@ export type Connection = {
}
}

export type Log = {
type: string
payload: string
}

export type Config = {
port: number
'socks-port': number
Expand Down

0 comments on commit 7eee024

Please sign in to comment.