-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathroute.tsx
More file actions
487 lines (446 loc) · 16.6 KB
/
route.tsx
File metadata and controls
487 lines (446 loc) · 16.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
import { type LoaderFunctionArgs, redirect } from "@remix-run/server-runtime";
import { type MetaFunction, useFetcher, useNavigation, useLocation, Form } from "@remix-run/react";
import { XMarkIcon } from "@heroicons/react/20/solid";
import { ServiceValidationError } from "~/v3/services/baseService.server";
import {
TypedAwait,
typeddefer,
type UseDataFunctionReturn,
useTypedLoaderData,
} from "remix-typedjson";
import { requireUser } from "~/services/session.server";
import { getCurrentPlan } from "~/services/platform.v3.server";
import { EnvironmentParamSchema } from "~/utils/pathBuilder";
import { findProjectBySlug } from "~/models/project.server";
import { findEnvironmentBySlug } from "~/models/runtimeEnvironment.server";
import { LogsListPresenter, LogEntry } from "~/presenters/v3/LogsListPresenter.server";
import type { LogLevel } from "~/utils/logUtils";
import { $replica, prisma } from "~/db.server";
import { clickhouseClient } from "~/services/clickhouseInstance.server";
import { NavBar, PageTitle } from "~/components/primitives/PageHeader";
import { PageBody, PageContainer } from "~/components/layout/AppLayout";
import { Suspense, useCallback, useEffect, useMemo, useRef, useState, useTransition } from "react";
import { useOptimisticLocation } from "~/hooks/useOptimisticLocation";
import { Spinner } from "~/components/primitives/Spinner";
import { Paragraph } from "~/components/primitives/Paragraph";
import { Callout } from "~/components/primitives/Callout";
import { LogsTable } from "~/components/logs/LogsTable";
import { LogDetailView } from "~/components/logs/LogDetailView";
import { LogsSearchInput } from "~/components/logs/LogsSearchInput";
import { LogsLevelFilter } from "~/components/logs/LogsLevelFilter";
import { LogsTaskFilter } from "~/components/logs/LogsTaskFilter";
import { LogsRunIdFilter } from "~/components/logs/LogsRunIdFilter";
import { TimeFilter } from "~/components/runs/v3/SharedFilters";
import {
ResizableHandle,
ResizablePanel,
ResizablePanelGroup,
} from "~/components/primitives/Resizable";
import { Button } from "~/components/primitives/Buttons";
import { FEATURE_FLAG, validateFeatureFlagValue } from "~/v3/featureFlags.server";
// Valid log levels for filtering
const validLevels: LogLevel[] = ["DEBUG", "INFO", "WARN", "ERROR"];
function parseLevelsFromUrl(url: URL): LogLevel[] | undefined {
const levelParams = url.searchParams.getAll("levels").filter((v) => v.length > 0);
if (levelParams.length === 0) return undefined;
return levelParams.filter((l): l is LogLevel => validLevels.includes(l as LogLevel));
}
export const meta: MetaFunction = () => {
return [
{
title: `Logs | Trigger.dev`,
},
];
};
// TODO: Move this to a more appropriate shared location
async function hasLogsPageAccess(
userId: string,
isAdmin: boolean,
isImpersonating: boolean,
organizationSlug: string
): Promise<boolean> {
if (isAdmin || isImpersonating) {
return true;
}
// Check organization feature flags
const organization = await prisma.organization.findFirst({
where: {
slug: organizationSlug,
members: { some: { userId } },
},
select: {
featureFlags: true,
},
});
if (!organization?.featureFlags) {
return false;
}
const flags = organization.featureFlags as Record<string, unknown>;
const hasLogsPageAccessResult = validateFeatureFlagValue(
FEATURE_FLAG.hasLogsPageAccess,
flags.hasLogsPageAccess
);
return hasLogsPageAccessResult.success && hasLogsPageAccessResult.data === true;
}
export const loader = async ({ request, params }: LoaderFunctionArgs) => {
const user = await requireUser(request);
const userId = user.id;
const { projectParam, organizationSlug, envParam } = EnvironmentParamSchema.parse(params);
const canAccess = await hasLogsPageAccess(
userId,
user.admin,
user.isImpersonating,
organizationSlug
);
if (!canAccess) {
throw redirect("/");
}
const project = await findProjectBySlug(organizationSlug, projectParam, userId);
if (!project) {
throw new Response("Project not found", { status: 404 });
}
const environment = await findEnvironmentBySlug(project.id, envParam, userId);
if (!environment) {
throw new Response("Environment not found", { status: 404 });
}
// Get filters from query params
const url = new URL(request.url);
const tasks = url.searchParams.getAll("tasks").filter((t) => t.length > 0);
const runId = url.searchParams.get("runId") ?? undefined;
const search = url.searchParams.get("search") ?? undefined;
const levels = parseLevelsFromUrl(url);
const period = url.searchParams.get("period") ?? undefined;
const fromStr = url.searchParams.get("from");
const toStr = url.searchParams.get("to");
const from = fromStr ? parseInt(fromStr, 10) : undefined;
const to = toStr ? parseInt(toStr, 10) : undefined;
// Get the user's plan to determine log retention limit
const plan = await getCurrentPlan(project.organizationId);
const retentionLimitDays = plan?.v3Subscription?.plan?.limits.logRetentionDays.number ?? 30;
const presenter = new LogsListPresenter($replica, clickhouseClient);
const listPromise = presenter
.call(project.organizationId, environment.id, {
userId,
projectId: project.id,
tasks: tasks.length > 0 ? tasks : undefined,
runId,
search,
levels,
period,
from,
to,
defaultPeriod: "1h",
retentionLimitDays
})
.catch((error) => {
if (error instanceof ServiceValidationError) {
return { error: error.message };
}
throw error;
});
return typeddefer({
data: listPromise,
defaultPeriod: "1h",
retentionLimitDays,
});
};
export default function Page() {
const { data, defaultPeriod, retentionLimitDays } =
useTypedLoaderData<typeof loader>();
return (
<PageContainer>
<NavBar>
<PageTitle title="Logs" />
</NavBar>
<PageBody scrollable={false}>
<Suspense
fallback={
<div className="grid h-full max-h-full grid-rows-[2.5rem_auto] overflow-hidden">
<div className="border-b border-grid-bright" />
<div className="my-2 flex items-center justify-center">
<div className="mx-auto flex items-center gap-2">
<Spinner />
<Paragraph variant="small">Loading logs…</Paragraph>
</div>
</div>
</div>
}
>
<TypedAwait
resolve={data}
errorElement={
<div className="grid h-full max-h-full grid-rows-[2.5rem_auto_1fr] overflow-hidden">
<FiltersBar
defaultPeriod={defaultPeriod}
retentionLimitDays={retentionLimitDays}
/>
<div className="flex items-center justify-center px-3 py-12">
<Callout variant="error" className="max-w-fit">
Unable to load your logs. Please refresh the page or try again in a moment.
</Callout>
</div>
</div>
}
>
{(result) => {
// Check if result contains an error
if ("error" in result) {
return (
<div className="grid h-full max-h-full grid-rows-[2.5rem_auto_1fr] overflow-hidden">
<FiltersBar
defaultPeriod={defaultPeriod}
retentionLimitDays={retentionLimitDays}
/>
<div className="flex items-center justify-center px-3 py-12">
<Callout variant="error" className="max-w-fit">
{result.error}
</Callout>
</div>
</div>
);
}
return (
<div className="grid h-full max-h-full grid-rows-[2.5rem_1fr] overflow-hidden">
<FiltersBar
list={result}
defaultPeriod={defaultPeriod}
retentionLimitDays={retentionLimitDays}
/>
<LogsList
list={result}
defaultPeriod={defaultPeriod}
/>
</div>
);
}}
</TypedAwait>
</Suspense>
</PageBody>
</PageContainer>
);
}
function FiltersBar({
list,
defaultPeriod,
retentionLimitDays,
}: {
list?: Exclude<Awaited<UseDataFunctionReturn<typeof loader>["data"]>, { error: string }>;
defaultPeriod?: string;
retentionLimitDays: number;
}) {
const location = useOptimisticLocation();
const searchParams = new URLSearchParams(location.search);
const hasFilters =
searchParams.has("tasks") ||
searchParams.has("runId") ||
searchParams.has("search") ||
searchParams.has("levels") ||
searchParams.has("period") ||
searchParams.has("from") ||
searchParams.has("to");
return (
<div className="flex items-start justify-between gap-x-2 border-b border-grid-bright p-2">
<div className="flex flex-row flex-wrap items-center gap-1">
{list ? (
<>
<LogsTaskFilter possibleTasks={list.possibleTasks} />
<LogsRunIdFilter />
<TimeFilter defaultPeriod={defaultPeriod} maxPeriodDays={retentionLimitDays} />
<LogsLevelFilter />
<LogsSearchInput />
{hasFilters && (
<Form className="h-6">
<Button
variant="secondary/small"
LeadingIcon={XMarkIcon}
tooltip="Clear all filters"
/>
</Form>
)}
</>
) : (
<>
<LogsTaskFilter possibleTasks={[]} />
<LogsRunIdFilter />
<TimeFilter defaultPeriod={defaultPeriod} maxPeriodDays={retentionLimitDays} />
<LogsLevelFilter />
<LogsSearchInput />
{hasFilters && (
<Form className="h-6">
<Button
variant="secondary/small"
LeadingIcon={XMarkIcon}
tooltip="Clear all filters"
/>
</Form>
)}
</>
)}
</div>
</div>
);
}
function LogsList({
list,
}: {
list: Exclude<Awaited<UseDataFunctionReturn<typeof loader>["data"]>, { error: string }>; //exclude error, it is handled
defaultPeriod?: string;
}) {
const navigation = useNavigation();
const location = useLocation();
const fetcher = useFetcher<{ logs: LogEntry[]; pagination: { next?: string } }>();
const [, startTransition] = useTransition();
const isLoading = navigation.state !== "idle";
// Accumulated logs state
const [accumulatedLogs, setAccumulatedLogs] = useState<LogEntry[]>(list.logs);
const [nextCursor, setNextCursor] = useState<string | undefined>(list.pagination.next);
// Selected log state - managed locally to avoid triggering navigation
const [selectedLogId, setSelectedLogId] = useState<string | undefined>();
// Track which filter state (search params) the current fetcher request corresponds to
const fetcherFilterStateRef = useRef<string>(location.search);
// Track whether the current fetch is a "check for new" request vs "load more"
const isCheckingForNewRef = useRef<boolean>(false);
// Clear accumulated logs immediately when filters change (for instant visual feedback)
useEffect(() => {
setAccumulatedLogs([]);
setNextCursor(undefined);
// Close side panel when filters change to avoid showing a log that's no longer visible
setSelectedLogId(undefined);
}, [location.search]);
// Populate accumulated logs when new data arrives
useEffect(() => {
setAccumulatedLogs(list.logs);
setNextCursor(list.pagination.next);
}, [list.logs, list.pagination.next]);
// Clear log parameter from URL when selectedLogId is cleared
useEffect(() => {
if (!selectedLogId) {
const url = new URL(window.location.href);
if (url.searchParams.has("log")) {
url.searchParams.delete("log");
window.history.replaceState(null, "", url.toString());
}
}
}, [selectedLogId]);
// Append/prepend new logs when fetcher completes (with deduplication)
useEffect(() => {
if (fetcher.data && fetcher.state === "idle") {
// Ignore fetcher data if it was loaded for a different filter state
if (fetcherFilterStateRef.current !== location.search) {
return;
}
if (isCheckingForNewRef.current) {
// "Check for new" - prepend new logs, don't update cursor
setAccumulatedLogs((prev) => {
const existingIds = new Set(prev.map((log) => log.id));
const newLogs = fetcher.data!.logs.filter((log) => !existingIds.has(log.id));
return newLogs.length > 0 ? [...newLogs, ...prev] : prev;
});
isCheckingForNewRef.current = false;
} else {
// "Load more" - append logs and update cursor
setAccumulatedLogs((prev) => {
const existingIds = new Set(prev.map((log) => log.id));
const newLogs = fetcher.data!.logs.filter((log) => !existingIds.has(log.id));
return newLogs.length > 0 ? [...prev, ...newLogs] : prev;
});
setNextCursor(fetcher.data.pagination.next);
}
}
}, [fetcher.data, fetcher.state, location.search]);
// Build resource URL for loading more
const loadMoreUrl = useMemo(() => {
if (!nextCursor) return null;
const resourcePath = `/resources${location.pathname}`;
const params = new URLSearchParams(location.search);
params.set("cursor", nextCursor);
params.delete("log");
return `${resourcePath}?${params.toString()}`;
}, [location.pathname, location.search, nextCursor]);
const handleLoadMore = useCallback(() => {
if (loadMoreUrl && fetcher.state === "idle") {
// Store the current filter state before loading
fetcherFilterStateRef.current = location.search;
fetcher.load(loadMoreUrl);
}
}, [loadMoreUrl, fetcher, location.search]);
const selectedLog = useMemo(() => {
if (!selectedLogId) return undefined;
return accumulatedLogs.find((log) => log.id === selectedLogId);
}, [selectedLogId, accumulatedLogs]);
const updateUrlWithLog = useCallback((logId: string | undefined) => {
const url = new URL(window.location.href);
if (logId) {
url.searchParams.set("log", logId);
} else {
url.searchParams.delete("log");
}
window.history.replaceState(null, "", url.toString());
}, []);
const handleLogSelect = useCallback(
(logId: string) => {
startTransition(() => {
setSelectedLogId(logId);
});
updateUrlWithLog(logId);
},
[updateUrlWithLog, startTransition]
);
const handleClosePanel = useCallback(() => {
startTransition(() => {
setSelectedLogId(undefined);
});
updateUrlWithLog(undefined);
}, [updateUrlWithLog, startTransition]);
const handleCheckForMore = useCallback(() => {
if (fetcher.state !== "idle") return;
// Fetch without cursor to check for new logs
const resourcePath = `/resources${location.pathname}`;
const params = new URLSearchParams(location.search);
params.delete("cursor");
params.delete("log");
fetcherFilterStateRef.current = location.search;
isCheckingForNewRef.current = true;
fetcher.load(`${resourcePath}?${params.toString()}`);
}, [fetcher, location.pathname, location.search]);
return (
<ResizablePanelGroup orientation="horizontal" className="max-h-full">
<ResizablePanel id="logs-main" min="200px">
<LogsTable
key={location.search}
logs={accumulatedLogs}
searchTerm={list.searchTerm}
isLoading={isLoading}
isLoadingMore={fetcher.state === "loading"}
hasMore={!!nextCursor}
onLoadMore={handleLoadMore}
onCheckForMore={handleCheckForMore}
selectedLogId={selectedLogId}
onLogSelect={handleLogSelect}
/>
</ResizablePanel>
{/* Side panel for log details */}
{selectedLogId && (
<>
<ResizableHandle id="logs-handle" />
<ResizablePanel id="log-detail" min="300px" default="430px" max="600px" isStaticAtRest>
<Suspense
fallback={
<div className="flex h-full items-center justify-center">
<Spinner />
</div>
}
>
<LogDetailView
logId={selectedLogId}
initialLog={selectedLog}
onClose={handleClosePanel}
searchTerm={list.searchTerm}
/>
</Suspense>
</ResizablePanel>
</>
)}
</ResizablePanelGroup>
);
}