132 lines
5.5 KiB
TypeScript
132 lines
5.5 KiB
TypeScript
import Refresh from '@mui/icons-material/Refresh'
|
|
import VerticalAlignBottom from '@mui/icons-material/VerticalAlignBottom'
|
|
import { Alert, Box, Button, MenuItem, Tab, Tabs, TextField, ToggleButton } from '@mui/material'
|
|
import { useCallback, useEffect, useState } from 'react'
|
|
import LogView from './LogView.tsx'
|
|
import Toolbar from './Toolbar.tsx'
|
|
import { LOGS_SHAPE, LOG_LINES_SHAPE, request, type Logs } from './api.ts'
|
|
|
|
/** The numbers of lines offered, up to the server's most. */
|
|
const LINE_CHOICES = [100, 200, 500, 1000]
|
|
|
|
/** Seconds between the reads of a followed log. */
|
|
const FOLLOW_SECONDS = 2
|
|
|
|
/**
|
|
* The server's logs, from its /logs: the Journal sub-tab shows the last lines of the journal of a unit the server names, or of
|
|
* the whole journal, and the Files sub-tab the last lines of a log file the server's web.log_files lists. The number of lines
|
|
* is chosen up to the server's most; a log is read when its unit, file or number of lines is chosen and with Refresh, and
|
|
* while Follow is on every FOLLOW_SECONDS, scrolled to its last line, as tail -f shows it.
|
|
*/
|
|
export default function LogsPanel() {
|
|
const [sources, setSources] = useState<Logs | null>(null)
|
|
const [kind, setKind] = useState<'journal' | 'file'>('journal')
|
|
const [unit, setUnit] = useState('')
|
|
const [file, setFile] = useState('')
|
|
const [count, setCount] = useState(200)
|
|
const [lines, setLines] = useState<string[] | null>(null)
|
|
const [error, setError] = useState<string | null>(null)
|
|
const [busy, setBusy] = useState(false)
|
|
const [follow, setFollow] = useState(false)
|
|
|
|
useEffect(() => {
|
|
request<Logs>('GET', '/logs', LOGS_SHAPE)
|
|
.then((found) => {
|
|
setSources(found)
|
|
setFile((current) => current || found.files[0] || '')
|
|
})
|
|
.catch((caught) => setError((caught as Error).message))
|
|
}, [])
|
|
|
|
// The query of the log chosen, null while the Files tab has no file.
|
|
const query = kind === 'file' && !file
|
|
? null
|
|
: kind === 'journal'
|
|
? `/logs/journal?lines=${count}${unit ? `&unit=${encodeURIComponent(unit)}` : ''}`
|
|
: `/logs/file?lines=${count}&path=${encodeURIComponent(file)}`
|
|
// The query whose answer is shown.
|
|
const [shown, setShown] = useState<string | null>(null)
|
|
const reading = busy || (sources !== null && query !== null && shown !== query)
|
|
|
|
/** Reads the log chosen; the state changes only once the server answers. */
|
|
const read = useCallback(async () => {
|
|
if (query === null) {
|
|
return
|
|
}
|
|
await request<{ lines: string[] }>('GET', query, LOG_LINES_SHAPE)
|
|
.then((found) => {
|
|
setLines(found.lines)
|
|
setError(null)
|
|
}, (caught) => {
|
|
setLines(null)
|
|
setError((caught as Error).message)
|
|
})
|
|
.finally(() => {
|
|
setShown(query)
|
|
setBusy(false)
|
|
})
|
|
}, [query])
|
|
|
|
/** Reads the log again, busy until the server answers. */
|
|
const refresh = useCallback(() => {
|
|
setBusy(true)
|
|
return read()
|
|
}, [read])
|
|
|
|
useEffect(() => {
|
|
if (sources) {
|
|
void read()
|
|
}
|
|
}, [sources, read])
|
|
|
|
useEffect(() => {
|
|
if (!follow || !sources) {
|
|
return
|
|
}
|
|
const timer = window.setInterval(() => void read(), FOLLOW_SECONDS * 1000)
|
|
return () => window.clearInterval(timer)
|
|
}, [follow, sources, read])
|
|
|
|
return (
|
|
<Box component="section">
|
|
<Tabs value={kind} onChange={(_event, next: 'journal' | 'file') => setKind(next)} sx={{ mb: 2 }}>
|
|
<Tab value="journal" label="Journal" />
|
|
<Tab value="file" label="Files" />
|
|
</Tabs>
|
|
<Toolbar description={kind === 'journal' ? 'The last lines of the systemd journal.' : 'The last lines of a log file.'}>
|
|
{kind === 'journal' ? (
|
|
<TextField select size="small" label="Unit" value={unit} onChange={(event) => setUnit(event.target.value)} sx={{ minWidth: 240 }}>
|
|
<MenuItem value="">All units</MenuItem>
|
|
{(sources?.units ?? []).map((name) => <MenuItem key={name} value={name}>{name}</MenuItem>)}
|
|
</TextField>
|
|
) : (
|
|
<TextField
|
|
select
|
|
size="small"
|
|
label="File"
|
|
value={file}
|
|
disabled={!sources?.files.length}
|
|
onChange={(event) => setFile(event.target.value)}
|
|
sx={{ minWidth: 240 }}
|
|
>
|
|
{(sources?.files ?? []).map((name) => <MenuItem key={name} value={name}>{name}</MenuItem>)}
|
|
</TextField>
|
|
)}
|
|
<TextField select size="small" label="Lines" value={count} onChange={(event) => setCount(Number(event.target.value))} sx={{ minWidth: 100 }}>
|
|
{LINE_CHOICES.filter((choice) => choice <= (sources?.max_lines ?? 0)).map((choice) => <MenuItem key={choice} value={choice}>{choice}</MenuItem>)}
|
|
</TextField>
|
|
<ToggleButton value="follow" size="small" color="primary" selected={follow} disabled={!sources} onChange={() => setFollow(!follow)}>
|
|
<VerticalAlignBottom fontSize="small" sx={{ mr: 0.5 }} />
|
|
Follow
|
|
</ToggleButton>
|
|
<Button variant="outlined" startIcon={<Refresh />} disabled={reading || follow || !sources} onClick={() => void refresh()}>
|
|
Refresh
|
|
</Button>
|
|
</Toolbar>
|
|
{error && <Alert severity="error" sx={{ mb: 2, whiteSpace: 'pre-line' }}>{error}</Alert>}
|
|
{kind === 'file' && sources && sources.files.length === 0 && <Alert severity="info" sx={{ mb: 2 }}>No log files are configured.</Alert>}
|
|
<LogView lines={query === null ? null : lines} empty={reading ? '(reading)' : '(no lines)'} maxHeight={600} follow={follow} />
|
|
</Box>
|
|
)
|
|
}
|