-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathAuditLog.tsx
More file actions
227 lines (207 loc) · 7.6 KB
/
AuditLog.tsx
File metadata and controls
227 lines (207 loc) · 7.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, you can obtain one at https://mozilla.org/MPL/2.0/.
*
* Copyright Oxide Computer Company
*/
import { getLocalTimeZone, now } from '@internationalized/date'
import { useInfiniteQuery, useIsFetching } from '@tanstack/react-query'
import { useVirtualizer } from '@tanstack/react-virtual'
import cn from 'classnames'
import { differenceInMilliseconds } from 'date-fns'
import { useMemo, useRef } from 'react'
import { api } from '@oxide/api'
import { Logs16Icon, Logs24Icon } from '@oxide/design-system/icons/react'
import { DocsPopover } from '~/components/DocsPopover'
import { useDateTimeRangePicker } from '~/components/form/fields/DateTimeRangePicker'
import { useIntervalPicker } from '~/components/RefetchIntervalPicker'
import { Badge } from '~/ui/lib/Badge'
import { Button } from '~/ui/lib/Button'
import { PageHeader, PageTitle } from '~/ui/lib/PageHeader'
import { Spinner } from '~/ui/lib/Spinner'
import { classed } from '~/util/classed'
import { toSyslogDateString, toSyslogTimeString } from '~/util/date'
import { docLinks } from '~/util/links'
export const handle = { crumb: 'Audit Log' }
// todo
// might want to still render the items in case of error
const ErrorState = () => {
return <div>Error State</div>
}
// todo
const LoadingState = () => {
return <div>Loading State</div>
}
const HeaderCell = classed.div`text-mono-sm text-tertiary`
// for virtualizer
const estimateSize = () => 36
export default function SiloAuditLogsPage() {
// pass refetch interval to this to keep the date up to date
const { preset, startTime, endTime, dateTimeRangePicker, onRangeChange } =
useDateTimeRangePicker({
initialPreset: 'lastHour',
maxValue: now(getLocalTimeZone()),
})
const { intervalPicker } = useIntervalPicker({
enabled: preset !== 'custom',
isLoading: useIsFetching({ queryKey: ['auditLogList'] }) > 0,
// sliding the range forward is sufficient to trigger a refetch
fn: () => onRangeChange(preset),
})
const queryParams = {
startTime,
endTime,
limit: 500,
}
const {
data,
fetchNextPage,
hasNextPage,
isFetchingNextPage,
isLoading,
isPending,
isFetching,
error,
} = useInfiniteQuery({
queryKey: ['auditLogList', { query: queryParams }],
queryFn: ({ pageParam }) =>
api.methods
.auditLogList({ query: { ...queryParams, pageToken: pageParam } })
.then((result) => {
if (result.type === 'success') return result.data
throw result
}),
initialPageParam: undefined as string | undefined,
getNextPageParam: (lastPage) => lastPage.nextPage || undefined,
placeholderData: (x) => x,
})
const auditLogs = useMemo(() => {
return data?.pages.flatMap((page) => page.items) || []
}, [data])
const parentRef = useRef<HTMLDivElement>(null)
const rowVirtualizer = useVirtualizer({
count: auditLogs.length,
getScrollElement: () => document.querySelector('#scroll-container'),
estimateSize,
overscan: 20,
})
const logTable = (
<>
<div
className="relative w-full"
style={{
height: `${rowVirtualizer.getTotalSize()}px`,
}}
>
{rowVirtualizer.getVirtualItems().map((virtualRow) => {
const log = auditLogs[virtualRow.index]
return (
<div
key={virtualRow.index}
className="absolute left-0 right-0 top-0 w-full"
style={{
height: `${virtualRow.size}px`,
transform: `translateY(${virtualRow.start}px)`,
}}
>
<div
className={cn(
'grid h-9 w-full items-center gap-8 px-[var(--content-gutter)] text-left text-sans-md border-secondary',
virtualRow.index !== 0 && 'border-t'
)}
style={{
gridTemplateColumns: '7rem 4.25rem 180px 120px 120px 120px 300px 300px',
}}
>
<div className="overflow-hidden whitespace-nowrap text-mono-sm">
<span className="text-tertiary">
{toSyslogDateString(log.timeCompleted)}
</span>{' '}
{toSyslogTimeString(log.timeCompleted)}
</div>
<div className="flex gap-1 overflow-hidden whitespace-nowrap">
<span className="text-mono-sm text-tertiary">POST</span>
<Badge>200</Badge>
</div>
<div>
<Badge color="neutral" className="text-tertiary">
{log.operationId.split('_').join(' ')}
</Badge>
</div>
<div className="text-secondary">hannah.arendt</div>
<div>
{!!log.accessMethod && (
<Badge color="neutral" className="text-tertiary">
{log.accessMethod.split('_').join(' ')}
</Badge>
)}
</div>
<div className="text-secondary">maze-war</div>
<div className="text-secondary">
{differenceInMilliseconds(new Date(log.timeCompleted), log.timeStarted)}
ms
</div>
</div>
</div>
)
})}
</div>
<div className="flex justify-center border-t px-[var(--content-gutter)] py-4 border-secondary">
{!hasNextPage && !isFetching && !isPending && auditLogs.length > 0 ? (
<div className="text-mono-sm text-quaternary">
No more logs to show within selected timeline
</div>
) : (
<Button
variant="ghost"
onClick={() => fetchNextPage()}
disabled={isFetchingNextPage}
className="text-mono-sm text-quaternary"
type="button"
>
<div className="flex items-center gap-2">
{isFetchingNextPage && <Spinner variant="secondary" />} Load More
</div>
</Button>
)}
</div>
</>
)
return (
<>
<PageHeader>
<PageTitle icon={<Logs24Icon />}>Audit Log</PageTitle>
<DocsPopover
heading="audit log"
icon={<Logs16Icon />}
summary="The audit log provides a record of system activities, including user actions, API calls, and system events."
links={[docLinks.auditLogs]}
/>
</PageHeader>
<div className="!mx-0 mb-3 mt-8 flex !w-full flex-wrap justify-between gap-3 border-b px-[var(--content-gutter)] pb-4 border-secondary">
<div className="flex gap-2">{intervalPicker}</div>
<div className="flex items-center gap-2">{dateTimeRangePicker}</div>
</div>
<div
className="sticky top-0 z-10 !mx-0 grid !w-full items-center gap-8 border-b px-[var(--content-gutter)] pb-2 pt-4 bg-default border-secondary"
style={{
gridTemplateColumns: '7rem 4.25rem 180px 120px 120px 120px 300px 300px',
}}
>
<HeaderCell>Time</HeaderCell>
<HeaderCell>Status</HeaderCell>
<HeaderCell>Operation</HeaderCell>
<HeaderCell>Actor</HeaderCell>
<HeaderCell>Access Method</HeaderCell>
<HeaderCell>Silo</HeaderCell>
<HeaderCell>Duration</HeaderCell>
</div>
<div className="!mx-0 flex h-full !w-full flex-col">
<div className="w-full flex-1" ref={parentRef}>
{error ? <ErrorState /> : !isLoading ? logTable : <LoadingState />}
</div>
</div>
</>
)
}