-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathtimelineSpanEvents.ts
More file actions
224 lines (194 loc) · 5.42 KB
/
timelineSpanEvents.ts
File metadata and controls
224 lines (194 loc) · 5.42 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
import { SpanEvent } from "@trigger.dev/core/v3";
import { millisecondsToNanoseconds } from "@trigger.dev/core/v3/utils/durations";
export type TimelineEventState = "complete" | "error" | "inprogress" | "delayed";
export type TimelineLineVariant = "light" | "normal";
export type TimelineEventVariant =
| "start-cap"
| "dot-hollow"
| "dot-solid"
| "start-cap-thick"
| "end-cap-thick"
| "end-cap";
export type TimelineSpanEvent = {
name: string;
offset: number;
timestamp: Date;
duration?: number;
helpText?: string;
markerVariant: TimelineEventVariant;
lineVariant: TimelineLineVariant;
};
export function createTimelineSpanEventsFromSpanEvents(
spanEvents: SpanEvent[],
isAdmin: boolean,
relativeStartTime?: number
): Array<TimelineSpanEvent> {
if (!spanEvents) {
return [];
}
const matchingSpanEvents = spanEvents.filter((spanEvent) =>
spanEvent.name.startsWith("trigger.dev/")
);
if (matchingSpanEvents.length === 0) {
return [];
}
// Check if there's a fork event
const hasForkEvent = matchingSpanEvents.some(
(spanEvent) =>
"event" in spanEvent.properties &&
typeof spanEvent.properties.event === "string" &&
spanEvent.properties.event === "fork"
);
const sortedSpanEvents = [...matchingSpanEvents].sort((a, b) => {
if (a.time === b.time) {
return a.name.localeCompare(b.name);
}
const aTime = typeof a.time === "string" ? new Date(a.time) : a.time;
const bTime = typeof b.time === "string" ? new Date(b.time) : b.time;
return aTime.getTime() - bTime.getTime();
});
const visibleSpanEvents = sortedSpanEvents.filter((spanEvent) => {
const eventName =
"event" in spanEvent.properties && typeof spanEvent.properties.event === "string"
? spanEvent.properties.event
: spanEvent.name;
// If we're admin, everything is visible
if (isAdmin) {
return true;
}
// If there's no fork event, import events are also visible to non-admins
if (!hasForkEvent && eventName === "import") {
return true;
}
// Otherwise use normal admin-only logic
return !getAdminOnlyForEvent(eventName);
});
if (visibleSpanEvents.length === 0) {
return [];
}
const firstEventTime =
typeof visibleSpanEvents[0].time === "string"
? new Date(visibleSpanEvents[0].time)
: visibleSpanEvents[0].time;
const $relativeStartTime = relativeStartTime ?? firstEventTime.getTime();
const events = visibleSpanEvents.map((spanEvent, index) => {
const timestamp =
typeof spanEvent.time === "string" ? new Date(spanEvent.time) : spanEvent.time;
const offset = millisecondsToNanoseconds(timestamp.getTime() - $relativeStartTime);
const duration =
"duration" in spanEvent.properties && typeof spanEvent.properties.duration === "number"
? spanEvent.properties.duration
: undefined;
const name =
"event" in spanEvent.properties && typeof spanEvent.properties.event === "string"
? spanEvent.properties.event
: spanEvent.name;
let markerVariant: TimelineEventVariant = "dot-hollow";
if (index === 0) {
markerVariant = "start-cap";
}
return {
name: getFriendlyNameForEvent(name, spanEvent.properties),
offset,
timestamp,
duration,
properties: spanEvent.properties,
helpText: getHelpTextForEvent(name),
markerVariant,
lineVariant: "light" as const,
};
});
// Now sort by offset, ascending
events.sort((a, b) => a.offset - b.offset);
return events;
}
function getFriendlyNameForEvent(event: string, properties?: Record<string, any>): string {
switch (event) {
case "dequeue": {
return "Dequeued";
}
case "fork": {
return "Launched";
}
case "create_attempt": {
return "Attempt created";
}
case "import": {
return "Importing task file";
}
case "lazy_payload": {
return "Lazy attempt initialized";
}
case "pod_scheduled": {
return "Pod scheduled";
}
default: {
return event;
}
}
}
function getAdminOnlyForEvent(event: string): boolean {
switch (event) {
case "dequeue": {
return false;
}
case "fork": {
return false;
}
case "create_attempt": {
return true;
}
case "import": {
return false;
}
case "lazy_payload": {
return true;
}
case "pod_scheduled": {
return true;
}
default: {
return true;
}
}
}
export function getHelpTextForEvent(event: string): string | undefined {
switch (event) {
case "dequeue": {
return "The run was dequeued from the queue";
}
case "fork": {
return "The process was created to run the task";
}
case "create_attempt": {
return "An attempt was created for the run";
}
case "import": {
return "A task file was imported";
}
case "lazy_payload": {
return "The payload was initialized lazily";
}
case "pod_scheduled": {
return "The Kubernetes pod was scheduled to run";
}
case "Triggered": {
return "The run was triggered";
}
case "Dequeued": {
return "The run was dequeued from the queue";
}
case "Started": {
return "The run began executing";
}
case "Finished": {
return "The run completed execution";
}
case "Expired": {
return "The run expired before it could be started";
}
default: {
return undefined;
}
}
}