Fix for runs.list with from/to Date or timestamp
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
---
|
||||
"@trigger.dev/core": patch
|
||||
---
|
||||
|
||||
OTEL attributes can include Dates that will be formatted as ISO strings
|
||||
@@ -8,6 +8,27 @@ import { ApiRetrieveRunPresenter } from "./ApiRetrieveRunPresenter.server";
|
||||
import { RunListOptions, RunListPresenter } from "./RunListPresenter.server";
|
||||
import { BasePresenter } from "./basePresenter.server";
|
||||
|
||||
const CoercedDate = z.preprocess((arg) => {
|
||||
if (arg === undefined || arg === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (typeof arg === "number") {
|
||||
return new Date(arg);
|
||||
}
|
||||
|
||||
if (typeof arg === "string") {
|
||||
const num = Number(arg);
|
||||
if (!isNaN(num)) {
|
||||
return new Date(num);
|
||||
}
|
||||
|
||||
return new Date(arg);
|
||||
}
|
||||
|
||||
return arg;
|
||||
}, z.date().optional());
|
||||
|
||||
const SearchParamsSchema = z.object({
|
||||
"page[size]": z.coerce.number().int().positive().min(1).max(100).optional(),
|
||||
"page[after]": z.string().optional(),
|
||||
@@ -95,8 +116,8 @@ const SearchParamsSchema = z.object({
|
||||
|
||||
return z.NEVER;
|
||||
}),
|
||||
"filter[createdAt][from]": z.coerce.date().optional(),
|
||||
"filter[createdAt][to]": z.coerce.date().optional(),
|
||||
"filter[createdAt][from]": CoercedDate,
|
||||
"filter[createdAt][to]": CoercedDate,
|
||||
"filter[createdAt][period]": z.string().optional(),
|
||||
});
|
||||
|
||||
|
||||
@@ -33,6 +33,11 @@ export function flattenAttributes(
|
||||
return result;
|
||||
}
|
||||
|
||||
if (obj instanceof Date) {
|
||||
result[prefix || ""] = obj.toISOString();
|
||||
return result;
|
||||
}
|
||||
|
||||
for (const [key, value] of Object.entries(obj)) {
|
||||
const newPrefix = `${prefix ? `${prefix}.` : ""}${Array.isArray(obj) ? `[${key}]` : key}`;
|
||||
if (Array.isArray(value)) {
|
||||
|
||||
@@ -1,9 +1,8 @@
|
||||
import { RunTags } from "@trigger.dev/core/v3";
|
||||
import { logger, runs, tags, task, tasks } from "@trigger.dev/sdk/v3";
|
||||
import { simpleChildTask } from "./subtasks";
|
||||
import { logger, runs, task, tasks } from "@trigger.dev/sdk/v3";
|
||||
import { simpleChildTask } from "./subtasks.js";
|
||||
|
||||
type Payload = {
|
||||
tags: RunTags;
|
||||
tags: string | string[];
|
||||
};
|
||||
|
||||
export const triggerRunsWithTags = task({
|
||||
@@ -16,6 +15,22 @@ export const triggerRunsWithTags = task({
|
||||
{ tags: payload.tags }
|
||||
);
|
||||
|
||||
//runs in the past 5 seconds, as a date
|
||||
const from = new Date();
|
||||
from.setSeconds(from.getSeconds() - 5);
|
||||
const result2 = await runs.list({ tag: payload.tags, from });
|
||||
logger.log("list with Date()", { length: result2.data.length, data: result2.data });
|
||||
|
||||
//runs in the past 5 seconds, as a number timestamp
|
||||
const result3 = await runs.list({ tag: payload.tags, from: from.getTime() - 5000 });
|
||||
logger.log("list with timestamp", { length: result3.data.length, data: result3.data });
|
||||
|
||||
logger.log("run usage", {
|
||||
costInCents: result2.data[0].costInCents,
|
||||
baseCostInCents: result2.data[0].baseCostInCents,
|
||||
durationMs: result2.data[0].durationMs,
|
||||
});
|
||||
|
||||
await simpleChildTask.triggerAndWait(
|
||||
{ message: "triggerAndWait from triggerRunsWithTags" },
|
||||
{ tags: payload.tags }
|
||||
@@ -71,13 +86,5 @@ export const triggerRunsWithTags = task({
|
||||
baseCostInCents: run.baseCostInCents,
|
||||
durationMs: run.durationMs,
|
||||
});
|
||||
|
||||
const result2 = await runs.list({ tag: payload.tags });
|
||||
logger.log("trigger runs ", { length: result2.data.length, data: result2.data });
|
||||
logger.log("run usage", {
|
||||
costInCents: result2.data[0].costInCents,
|
||||
baseCostInCents: result2.data[0].baseCostInCents,
|
||||
durationMs: result2.data[0].durationMs,
|
||||
});
|
||||
},
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user