Skip to content

Commit c738ef3

Browse files
committed
Fix for runs.list with from/to Date or timestamp
1 parent ca2cb72 commit c738ef3

File tree

4 files changed

+52
-14
lines changed

4 files changed

+52
-14
lines changed

.changeset/new-items-glow.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@trigger.dev/core": patch
3+
---
4+
5+
OTEL attributes can include Dates that will be formatted as ISO strings

apps/webapp/app/presenters/v3/ApiRunListPresenter.server.ts

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,27 @@ import { ApiRetrieveRunPresenter } from "./ApiRetrieveRunPresenter.server";
88
import { RunListOptions, RunListPresenter } from "./RunListPresenter.server";
99
import { BasePresenter } from "./basePresenter.server";
1010

11+
const CoercedDate = z.preprocess((arg) => {
12+
if (arg === undefined || arg === null) {
13+
return;
14+
}
15+
16+
if (typeof arg === "number") {
17+
return new Date(arg);
18+
}
19+
20+
if (typeof arg === "string") {
21+
const num = Number(arg);
22+
if (!isNaN(num)) {
23+
return new Date(num);
24+
}
25+
26+
return new Date(arg);
27+
}
28+
29+
return arg;
30+
}, z.date().optional());
31+
1132
const SearchParamsSchema = z.object({
1233
"page[size]": z.coerce.number().int().positive().min(1).max(100).optional(),
1334
"page[after]": z.string().optional(),
@@ -95,8 +116,8 @@ const SearchParamsSchema = z.object({
95116

96117
return z.NEVER;
97118
}),
98-
"filter[createdAt][from]": z.coerce.date().optional(),
99-
"filter[createdAt][to]": z.coerce.date().optional(),
119+
"filter[createdAt][from]": CoercedDate,
120+
"filter[createdAt][to]": CoercedDate,
100121
"filter[createdAt][period]": z.string().optional(),
101122
});
102123

packages/core/src/v3/utils/flattenAttributes.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ export function flattenAttributes(
3333
return result;
3434
}
3535

36+
if (obj instanceof Date) {
37+
result[prefix || ""] = obj.toISOString();
38+
return result;
39+
}
40+
3641
for (const [key, value] of Object.entries(obj)) {
3742
const newPrefix = `${prefix ? `${prefix}.` : ""}${Array.isArray(obj) ? `[${key}]` : key}`;
3843
if (Array.isArray(value)) {

references/v3-catalog/src/trigger/tags.ts

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
import { RunTags } from "@trigger.dev/core/v3";
2-
import { logger, runs, tags, task, tasks } from "@trigger.dev/sdk/v3";
3-
import { simpleChildTask } from "./subtasks";
1+
import { logger, runs, task, tasks } from "@trigger.dev/sdk/v3";
2+
import { simpleChildTask } from "./subtasks.js";
43

54
type Payload = {
6-
tags: RunTags;
5+
tags: string | string[];
76
};
87

98
export const triggerRunsWithTags = task({
@@ -16,6 +15,22 @@ export const triggerRunsWithTags = task({
1615
{ tags: payload.tags }
1716
);
1817

18+
//runs in the past 5 seconds, as a date
19+
const from = new Date();
20+
from.setSeconds(from.getSeconds() - 5);
21+
const result2 = await runs.list({ tag: payload.tags, from });
22+
logger.log("list with Date()", { length: result2.data.length, data: result2.data });
23+
24+
//runs in the past 5 seconds, as a number timestamp
25+
const result3 = await runs.list({ tag: payload.tags, from: from.getTime() - 5000 });
26+
logger.log("list with timestamp", { length: result3.data.length, data: result3.data });
27+
28+
logger.log("run usage", {
29+
costInCents: result2.data[0].costInCents,
30+
baseCostInCents: result2.data[0].baseCostInCents,
31+
durationMs: result2.data[0].durationMs,
32+
});
33+
1934
await simpleChildTask.triggerAndWait(
2035
{ message: "triggerAndWait from triggerRunsWithTags" },
2136
{ tags: payload.tags }
@@ -71,13 +86,5 @@ export const triggerRunsWithTags = task({
7186
baseCostInCents: run.baseCostInCents,
7287
durationMs: run.durationMs,
7388
});
74-
75-
const result2 = await runs.list({ tag: payload.tags });
76-
logger.log("trigger runs ", { length: result2.data.length, data: result2.data });
77-
logger.log("run usage", {
78-
costInCents: result2.data[0].costInCents,
79-
baseCostInCents: result2.data[0].baseCostInCents,
80-
durationMs: result2.data[0].durationMs,
81-
});
8289
},
8390
});

0 commit comments

Comments
 (0)