Skip to content

Commit dc62e05

Browse files
committed
Set default time period based on plan
1 parent 40553df commit dc62e05

File tree

1 file changed

+17
-6
lines changed
  • apps/webapp/app/routes/_app.orgs.$organizationSlug.projects.$projectParam.env.$envParam.query

1 file changed

+17
-6
lines changed

apps/webapp/app/routes/_app.orgs.$organizationSlug.projects.$projectParam.env.$envParam.query/route.tsx

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -164,12 +164,20 @@ export const loader = async ({ request, params }: LoaderFunctionArgs) => {
164164

165165
return typedjson({
166166
defaultQuery,
167+
defaultPeriod: await getDefaultPeriod(project.organizationId),
167168
history,
168169
isAdmin,
169170
});
170171
};
171172

172-
const DEFAULT_PERIOD = "7d";
173+
async function getDefaultPeriod(organizationId: string): Promise<string> {
174+
const idealDefaultPeriodDays = 7;
175+
const maxQueryPeriod = await getLimit(organizationId, "queryPeriodDays", 30);
176+
if (maxQueryPeriod < idealDefaultPeriodDays) {
177+
return `${maxQueryPeriod}d`;
178+
}
179+
return `${idealDefaultPeriodDays}d`;
180+
}
173181

174182
const ActionSchema = z.object({
175183
query: z.string().min(1, "Query is required"),
@@ -272,11 +280,12 @@ export const action = async ({ request, params }: ActionFunctionArgs) => {
272280
const explain = explainParam === "true" && isAdmin;
273281

274282
// Build time filter fallback for triggered_at column
283+
const defaultPeriod = await getDefaultPeriod(project.organizationId);
275284
const timeFilter = timeFilters({
276285
period: period ?? undefined,
277286
from: from ?? undefined,
278287
to: to ?? undefined,
279-
defaultPeriod: DEFAULT_PERIOD,
288+
defaultPeriod,
280289
});
281290

282291
// Calculate the effective "from" date the user is requesting (for period clipping check)
@@ -286,7 +295,7 @@ export const action = async ({ request, params }: ActionFunctionArgs) => {
286295
requestedFromDate = new Date(timeFilter.from);
287296
} else if (!timeFilter.to) {
288297
// Period specified (or default) - calculate from now
289-
const periodMs = parse(timeFilter.period ?? DEFAULT_PERIOD) ?? 7 * 24 * 60 * 60 * 1000;
298+
const periodMs = parse(timeFilter.period ?? defaultPeriod) ?? 7 * 24 * 60 * 60 * 1000;
290299
requestedFromDate = new Date(Date.now() - periodMs);
291300
}
292301

@@ -405,6 +414,7 @@ interface QueryEditorFormHandle {
405414
const QueryEditorForm = forwardRef<
406415
QueryEditorFormHandle,
407416
{
417+
defaultPeriod: string;
408418
defaultQuery: string;
409419
defaultScope: QueryScope;
410420
defaultTimeFilter?: { period?: string; from?: string; to?: string };
@@ -414,7 +424,7 @@ const QueryEditorForm = forwardRef<
414424
onQuerySubmit?: () => void;
415425
onHistorySelected?: (item: QueryHistoryItem) => void;
416426
}
417-
>(function QueryEditorForm({ defaultQuery, defaultScope, defaultTimeFilter, history, fetcher, isAdmin, onQuerySubmit, onHistorySelected }, ref) {
427+
>(function QueryEditorForm({ defaultPeriod, defaultQuery, defaultScope, defaultTimeFilter, history, fetcher, isAdmin, onQuerySubmit, onHistorySelected }, ref) {
418428
const isLoading = fetcher.state === "submitting" || fetcher.state === "loading";
419429
const [query, setQuery] = useState(defaultQuery);
420430
const [scope, setScope] = useState<QueryScope>(defaultScope);
@@ -526,7 +536,7 @@ const QueryEditorForm = forwardRef<
526536
/>
527537
) : (
528538
<TimeFilter
529-
defaultPeriod={DEFAULT_PERIOD}
539+
defaultPeriod={defaultPeriod}
530540
labelName="Triggered"
531541
hideLabel
532542
period={period}
@@ -562,7 +572,7 @@ const QueryEditorForm = forwardRef<
562572
});
563573

564574
export default function Page() {
565-
const { defaultQuery, history, isAdmin } = useTypedLoaderData<typeof loader>();
575+
const { defaultPeriod, defaultQuery, history, isAdmin } = useTypedLoaderData<typeof loader>();
566576
const fetcher = useTypedFetcher<typeof action>();
567577
const results = fetcher.data;
568578
const { replace: replaceSearchParams } = useSearchParams();
@@ -696,6 +706,7 @@ export default function Page() {
696706
<ResizablePanel id="query-editor" min="100px" default="300px" className="overflow-hidden">
697707
<QueryEditorForm
698708
ref={editorRef}
709+
defaultPeriod={defaultPeriod}
699710
defaultQuery={initialQuery}
700711
defaultScope={initialScope}
701712
defaultTimeFilter={initialTimeFilter}

0 commit comments

Comments
 (0)