-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathRunTimeline.tsx
More file actions
677 lines (636 loc) · 18.6 KB
/
RunTimeline.tsx
File metadata and controls
677 lines (636 loc) · 18.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
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
import { ClockIcon } from "@heroicons/react/20/solid";
import type { SpanEvent } from "@trigger.dev/core/v3";
import {
formatDuration,
millisecondsToNanoseconds,
nanosecondsToMilliseconds,
} from "@trigger.dev/core/v3/utils/durations";
import { Fragment, ReactNode, useState } from "react";
import { cn } from "~/utils/cn";
import { DateTime, DateTimeAccurate } from "../primitives/DateTime";
import { LiveTimer } from "../runs/v3/LiveTimer";
import tileBgPath from "~/assets/images/error-banner-tile@2x.png";
import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from "../primitives/Tooltip";
import { getHelpTextForEvent, TimelineSpanEvent } from "~/utils/timelineSpanEvents";
// Types for the RunTimeline component
export type TimelineEventState = "complete" | "error" | "inprogress" | "delayed";
type TimelineLineVariant = "light" | "normal";
type TimelineStyle = "normal" | "diminished";
type TimelineEventVariant =
| "start-cap"
| "dot-hollow"
| "dot-solid"
| "start-cap-thick"
| "end-cap-thick"
| "end-cap";
// Timeline item type definitions
export type TimelineEventDefinition = {
type: "event";
id: string;
title: string;
date?: Date;
previousDate: Date | undefined;
state?: TimelineEventState;
shouldRender: boolean;
variant: TimelineEventVariant;
helpText?: string;
};
export type TimelineLineDefinition = {
type: "line";
id: string;
title: React.ReactNode;
state?: TimelineEventState;
shouldRender: boolean;
variant: TimelineLineVariant;
};
export type TimelineItem = TimelineEventDefinition | TimelineLineDefinition;
/**
* TimelineSpanRun represents the minimal set of run properties needed
* to render the RunTimeline component.
*/
export type TimelineSpanRun = {
// Core timestamps
createdAt: Date; // When the run was created/triggered
startedAt?: Date | null; // When the run was dequeued
executedAt?: Date | null; // When the run actually started executing
updatedAt: Date; // Last update timestamp (used for finish time)
expiredAt?: Date | null; // When the run expired (if applicable)
completedAt?: Date | null; // When the run completed
// Delay information
delayUntil?: Date | null; // If the run is delayed, when it will be processed
ttl?: string | null; // Time-to-live value if applicable
// Status flags
isFinished: boolean; // Whether the run has completed
isError: boolean; // Whether the run ended with an error
};
export function RunTimeline({ run }: { run: TimelineSpanRun }) {
// Build timeline items based on the run state
const timelineItems = buildTimelineItems(run);
// Filter out items that shouldn't be rendered
const visibleItems = timelineItems.filter((item) => item.shouldRender);
return (
<div className="min-w-fit max-w-80">
{visibleItems.map((item) => {
if (item.type === "event") {
return (
<RunTimelineEvent
key={item.id}
title={item.title}
subtitle={
item.date ? (
<DateTimeAccurate date={item.date} previousDate={item.previousDate} />
) : null
}
state={item.state as "complete" | "error"}
variant={item.variant}
helpText={item.helpText}
/>
);
} else {
return (
<RunTimelineLine
key={item.id}
title={item.title}
state={item.state}
variant={item.variant}
/>
);
}
})}
</div>
);
}
// Centralized function to build all timeline items
function buildTimelineItems(run: TimelineSpanRun): TimelineItem[] {
let state: TimelineEventState;
if (run.isError) {
state = "error";
} else if (run.expiredAt) {
state = "error";
} else if (run.isFinished) {
state = "complete";
} else {
state = "inprogress";
}
const items: TimelineItem[] = [];
// 1. Triggered Event
items.push({
type: "event",
id: "triggered",
title: "Triggered",
date: run.createdAt,
previousDate: undefined,
state,
shouldRender: true,
variant: "start-cap",
helpText: getHelpTextForEvent("Triggered"),
});
// 2. Waiting to dequeue line
if (run.delayUntil && !run.startedAt && !run.expiredAt) {
// Delayed, not yet started
items.push({
type: "line",
id: "waiting-to-dequeue",
title: (
<span className="flex items-center gap-1">
<ClockIcon className="size-4" />
<span>
Delayed until <DateTime date={run.delayUntil} /> {run.ttl && <>(TTL {run.ttl})</>}
</span>
</span>
),
state,
shouldRender: true,
variant: "light",
});
} else if (run.startedAt) {
// Already dequeued - show the waiting duration
items.push({
type: "line",
id: "waiting-to-dequeue",
title: formatDuration(run.createdAt, run.startedAt),
state,
shouldRender: true,
variant: "light",
});
} else if (run.expiredAt) {
// Expired before dequeuing
items.push({
type: "line",
id: "waiting-to-dequeue",
title: formatDuration(run.createdAt, run.expiredAt),
state,
shouldRender: true,
variant: "light",
});
} else {
// Still waiting to be dequeued
items.push({
type: "line",
id: "waiting-to-dequeue",
title: (
<>
<LiveTimer
startTime={run.createdAt}
endTime={run.startedAt ?? run.expiredAt ?? undefined}
/>{" "}
{run.ttl && <>(TTL {run.ttl})</>}
</>
),
state,
shouldRender: true,
variant: "light",
});
}
// 3. Dequeued Event (if applicable)
if (run.startedAt) {
items.push({
type: "event",
id: "dequeued",
title: "Dequeued",
date: run.startedAt,
previousDate: run.createdAt,
state,
shouldRender: true,
variant: "dot-hollow",
helpText: getHelpTextForEvent("Dequeued"),
});
}
// 4. Handle the case based on whether executedAt exists
if (run.startedAt && !run.expiredAt) {
if (run.executedAt) {
// New behavior: Run has executedAt timestamp
// 4a. Show waiting to execute line
items.push({
type: "line",
id: "waiting-to-execute",
title: formatDuration(run.startedAt, run.executedAt),
state,
shouldRender: true,
variant: "light",
});
// 4b. Show Started event
items.push({
type: "event",
id: "started",
title: "Started",
date: run.executedAt,
previousDate: run.startedAt,
state,
shouldRender: true,
variant: "start-cap-thick",
helpText: getHelpTextForEvent("Started"),
});
// 4c. Show executing line if applicable
if (run.isFinished) {
items.push({
type: "line",
id: "executing",
title: formatDuration(run.executedAt, run.updatedAt),
state,
shouldRender: true,
variant: "normal",
});
} else {
items.push({
type: "line",
id: "executing",
title: <LiveTimer startTime={run.executedAt} />,
state,
shouldRender: true,
variant: "normal",
});
}
} else {
// Legacy behavior: Run doesn't have executedAt timestamp
// If the run is finished, show a line directly from Dequeued to Finished
if (run.isFinished) {
items.push({
type: "line",
id: "legacy-executing",
title: formatDuration(run.startedAt, run.updatedAt),
state,
shouldRender: true,
variant: "normal",
});
} else {
// Still waiting to start or execute (can't distinguish without executedAt)
items.push({
type: "line",
id: "legacy-waiting-or-executing",
title: <LiveTimer startTime={run.startedAt} />,
state,
shouldRender: true,
variant: "light",
});
}
}
}
// 5. Finished Event (if applicable)
if (run.isFinished && !run.expiredAt) {
items.push({
type: "event",
id: "finished",
title: "Finished",
date: run.updatedAt,
previousDate: run.executedAt ?? run.startedAt ?? undefined,
state,
shouldRender: true,
variant: "end-cap-thick",
helpText: getHelpTextForEvent("Finished"),
});
}
// 6. Expired Event (if applicable)
if (run.expiredAt) {
items.push({
type: "event",
id: "expired",
title: "Expired",
date: run.expiredAt,
previousDate: run.createdAt,
state: "error",
shouldRender: true,
variant: "dot-solid",
helpText: getHelpTextForEvent("Expired"),
});
}
return items;
}
export type RunTimelineEventProps = {
title: ReactNode;
subtitle?: ReactNode;
state?: "complete" | "error" | "inprogress";
variant?: TimelineEventVariant;
helpText?: string;
style?: TimelineStyle;
};
export function RunTimelineEvent({
title,
subtitle,
state,
variant = "dot-hollow",
helpText,
style = "normal",
}: RunTimelineEventProps) {
return (
<div className="grid h-5 grid-cols-[1.125rem_1fr] gap-1 text-sm">
<div className="relative flex flex-col items-center justify-center">
<EventMarker variant={variant} state={state} style={style} />
</div>
<div className="flex items-baseline justify-between gap-3">
<TooltipProvider disableHoverableContent>
<Tooltip>
<TooltipTrigger className="cursor-default">
<span className="font-medium text-text-bright">{title}</span>
</TooltipTrigger>
{helpText && (
<TooltipContent className="flex items-center gap-1 text-xs">
{helpText}
</TooltipContent>
)}
</Tooltip>
</TooltipProvider>
{subtitle ? (
<span className="text-xs tabular-nums text-text-dimmed">{subtitle}</span>
) : null}
</div>
</div>
);
}
function EventMarker({
variant,
state,
style,
}: {
variant: TimelineEventVariant;
state?: TimelineEventState;
style?: TimelineStyle;
}) {
let bgClass = "bg-text-dimmed";
switch (state) {
case "complete":
bgClass = "bg-success";
break;
case "error":
bgClass = "bg-error";
break;
case "delayed":
bgClass = "bg-text-dimmed";
break;
case "inprogress":
bgClass = style === "normal" ? "bg-pending" : "bg-text-dimmed";
break;
}
let borderClass = "border-text-dimmed";
switch (state) {
case "complete":
borderClass = "border-success";
break;
case "error":
borderClass = "border-error";
break;
case "delayed":
borderClass = "border-text-dimmed";
break;
case "inprogress":
borderClass = style === "normal" ? "border-pending" : "border-text-dimmed";
break;
default:
borderClass = "border-text-dimmed";
break;
}
switch (variant) {
case "start-cap":
return (
<>
<div className={cn("h-full w-[0.4375rem] border-b", borderClass)} />
<div className={cn("relative h-full w-px", bgClass)}>
{state === "inprogress" && (
<div
className="absolute inset-0 h-full w-full animate-tile-scroll opacity-30"
style={{
backgroundImage: `url(${tileBgPath})`,
backgroundSize: "8px 8px",
}}
/>
)}
</div>
</>
);
case "dot-hollow":
return (
<>
<div className={cn("relative h-full w-px", bgClass)}>
{state === "inprogress" && (
<div
className="absolute inset-0 h-full w-full animate-tile-scroll-offset opacity-30"
style={{
backgroundImage: `url(${tileBgPath})`,
backgroundSize: "8px 8px",
}}
/>
)}
</div>
<div
className={cn("size-[0.3125rem] min-h-[0.3125rem] rounded-full border", borderClass)}
/>
<div className={cn("relative h-full w-px", bgClass)}>
{state === "inprogress" && (
<div
className="absolute inset-0 h-full w-full animate-tile-scroll-offset opacity-30"
style={{
backgroundImage: `url(${tileBgPath})`,
backgroundSize: "8px 8px",
}}
/>
)}
</div>
</>
);
case "dot-solid":
return <div className={cn("size-[0.3125rem] rounded-full", bgClass)} />;
case "start-cap-thick":
return (
<div className={cn("relative h-full w-[0.4375rem] rounded-t-[0.125rem]", bgClass)}>
{state === "inprogress" && (
<div
className="absolute inset-0 h-full w-full animate-tile-scroll-offset opacity-30"
style={{
backgroundImage: `url(${tileBgPath})`,
backgroundSize: "8px 8px",
}}
/>
)}
</div>
);
case "end-cap-thick":
return <div className={cn("h-full w-[0.4375rem] rounded-b-[0.125rem]", bgClass)} />;
default:
return <div className={cn("size-[0.3125rem] rounded-full bg-yellow-500")} />;
}
}
export type RunTimelineLineProps = {
title: ReactNode;
state?: TimelineEventState;
variant?: TimelineLineVariant;
style?: TimelineStyle;
};
export function RunTimelineLine({
title,
state,
variant = "normal",
style = "normal",
}: RunTimelineLineProps) {
return (
<div className="grid h-6 grid-cols-[1.125rem_1fr] gap-1 text-xs">
<div className="flex items-stretch justify-center">
<LineMarker state={state} variant={variant} style={style} />
</div>
<div className="flex items-center justify-between gap-3">
<span className="text-text-dimmed">{title}</span>
</div>
</div>
);
}
function LineMarker({
state,
variant,
style,
}: {
state?: TimelineEventState;
variant: TimelineLineVariant;
style?: TimelineStyle;
}) {
let containerClass = "bg-text-dimmed";
switch (state) {
case "complete":
containerClass = "bg-success";
break;
case "error":
containerClass = "bg-error";
break;
case "delayed":
containerClass = "bg-text-dimmed";
break;
case "inprogress":
containerClass =
style === "normal"
? "rounded-b-[0.125rem] bg-pending"
: "rounded-b-[0.125rem] bg-text-dimmed";
break;
}
switch (variant) {
case "normal":
return (
<div className={cn("relative w-[0.4375rem]", containerClass)}>
{state === "inprogress" && (
<div
className="absolute inset-0 h-full w-full animate-tile-scroll opacity-30"
style={{
backgroundImage: `url(${tileBgPath})`,
backgroundSize: "8px 8px",
}}
/>
)}
</div>
);
case "light":
return (
<div className={cn("relative w-px", containerClass)}>
{state === "inprogress" && (
<div
className="absolute inset-0 h-full w-full animate-tile-scroll opacity-50"
style={{
backgroundImage: `url(${tileBgPath})`,
backgroundSize: "8px 8px",
}}
/>
)}
</div>
);
default:
return <div className="w-px rounded-[0.125rem] bg-text-dimmed" />;
}
}
export type SpanTimelineProps = {
startTime: Date;
duration: number;
inProgress: boolean;
isError: boolean;
events?: TimelineSpanEvent[];
style?: TimelineStyle;
};
export type SpanTimelineState = "error" | "pending" | "complete";
export function SpanTimeline({
startTime,
duration,
inProgress,
isError,
events,
style = "diminished",
}: SpanTimelineProps) {
const state = isError ? "error" : inProgress ? "inprogress" : undefined;
const visibleEvents = events ?? [];
return (
<>
<div className="min-w-fit max-w-80">
{visibleEvents.map((event, index) => {
// Store previous date to compare
const prevDate = index === 0 ? null : visibleEvents[index - 1].timestamp;
return (
<Fragment key={index}>
<RunTimelineEvent
title={event.name}
subtitle={<DateTimeAccurate date={event.timestamp} previousDate={prevDate} />}
variant={event.markerVariant}
state={state}
helpText={event.helpText}
style={style}
/>
<RunTimelineLine
title={
index === visibleEvents.length - 1
? // Last event - calculate duration until span start time
formatDuration(event.timestamp, startTime)
: // Calculate duration until next event
formatDuration(event.timestamp, visibleEvents[index + 1].timestamp)
}
variant={event.lineVariant}
state={state}
style={style}
/>
</Fragment>
);
})}
<RunTimelineEvent
title="Started"
subtitle={
<DateTimeAccurate
date={startTime}
previousDate={
visibleEvents.length > 0 ? visibleEvents[visibleEvents.length - 1].timestamp : null
}
/>
}
variant={"start-cap-thick"}
state={state}
helpText={getHelpTextForEvent("Started")}
style={style}
/>
{state === "inprogress" ? (
<RunTimelineLine
title={<LiveTimer startTime={startTime} />}
state={state}
variant="normal"
style={style}
/>
) : (
<>
<RunTimelineLine
title={formatDuration(
startTime,
new Date(startTime.getTime() + nanosecondsToMilliseconds(duration))
)}
state={isError ? "error" : undefined}
variant="normal"
style={style}
/>
<RunTimelineEvent
title="Finished"
subtitle={
<DateTimeAccurate
date={new Date(startTime.getTime() + nanosecondsToMilliseconds(duration))}
previousDate={startTime}
/>
}
state={isError ? "error" : undefined}
variant="end-cap-thick"
helpText={getHelpTextForEvent("Finished")}
style={style}
/>
</>
)}
</div>
</>
);
}