From 26f98741990a12a17142c164b699d2d2825bb9eb Mon Sep 17 00:00:00 2001 From: Aleix Date: Thu, 6 Feb 2025 07:18:34 -0500 Subject: [PATCH] feat: Schedule services (#2357) * feat: Schedule services * fixup! feat: Schedule services * fixup! Merge branch 'main' into acasanov-schedule-services --- .../ui/src/app/Catalog/CatalogItemForm.tsx | 19 +++++++++++++++ .../src/app/Catalog/CatalogItemFormReducer.ts | 16 ++++++++----- catalog/ui/src/app/api.ts | 23 ++++--------------- 3 files changed, 33 insertions(+), 25 deletions(-) diff --git a/catalog/ui/src/app/Catalog/CatalogItemForm.tsx b/catalog/ui/src/app/Catalog/CatalogItemForm.tsx index 71fe8368..31ee8c97 100644 --- a/catalog/ui/src/app/Catalog/CatalogItemForm.tsx +++ b/catalog/ui/src/app/Catalog/CatalogItemForm.tsx @@ -215,6 +215,7 @@ const CatalogItemFormData: React.FC<{ catalogItemName: string; catalogNamespaceN serviceNamespace: formState.serviceNamespace, usePoolIfAvailable: formState.usePoolIfAvailable, useAutoDetach: formState.useAutoDetach, + startDate: formState.startDate, stopDate: formState.stopDate, endDate: formState.endDate, email, @@ -663,6 +664,24 @@ const CatalogItemFormData: React.FC<{ catalogItemName: string; catalogNamespaceN ) : null} + {!formState.workshop && !catalogItem.spec.externalUrl ? ( + +
+ + dispatchFormState({ + type: 'initDates', + catalogItem, + startDate: d, + }) + } + minDate={Date.now()} + /> +
+
+ ) : null} + {!isAutoStopDisabled(catalogItem) && !formState.workshop && !catalogItem.spec.externalUrl ? (
diff --git a/catalog/ui/src/app/Catalog/CatalogItemFormReducer.ts b/catalog/ui/src/app/Catalog/CatalogItemFormReducer.ts index 2a4167c8..34ba2f62 100644 --- a/catalog/ui/src/app/Catalog/CatalogItemFormReducer.ts +++ b/catalog/ui/src/app/Catalog/CatalogItemFormReducer.ts @@ -272,13 +272,17 @@ export async function checkConditionsInFormState( } } -function initDates(catalogItem: CatalogItem) { +function initDates(catalogItem: CatalogItem, currTime?: number) { + let _currTime = Date.now(); + if (currTime) { + _currTime = currTime; + } return { - startDate: null, + startDate: currTime ? new Date(currTime) : null, stopDate: isAutoStopDisabled(catalogItem) ? null - : new Date(Date.now() + parseDuration(catalogItem.spec.runtime?.default || '4h')), - endDate: new Date(Date.now() + parseDuration(catalogItem.spec.lifespan?.default || '2d')), + : new Date(_currTime + parseDuration(catalogItem.spec.runtime?.default || '4h')), + endDate: new Date(_currTime + parseDuration(catalogItem.spec.lifespan?.default || '2d')), }; } @@ -362,7 +366,7 @@ function reduceFormStateInit( message: '', }, sfdc_enabled, - ...initDates(catalogItem), + ...initDates(catalogItem, null), }; } @@ -546,7 +550,7 @@ export function reduceFormState(state: FormState, action: FormStateAction): Form action.sfdc_enabled ); case 'initDates': - return { ...state, ...initDates(action.catalogItem) }; + return { ...state, ...initDates(action.catalogItem, action.startDate ? action.startDate.getTime() : null) }; case 'parameterUpdate': return reduceFormStateParameterUpdate(state, { name: action.parameter.name, diff --git a/catalog/ui/src/app/api.ts b/catalog/ui/src/app/api.ts index 09e657e0..fbec42ca 100644 --- a/catalog/ui/src/app/api.ts +++ b/catalog/ui/src/app/api.ts @@ -39,16 +39,6 @@ declare const window: Window & sessionPromiseInstance?: Promise; }; -type CreateServiceRequestOptScheduleStart = { - date: Date; -}; -interface CreateServiceRequestOptScheduleStartLifespan extends CreateServiceRequestOptScheduleStart { - type: 'lifespan'; -} -interface CreateServiceRequestOptScheduleStartResource extends CreateServiceRequestOptScheduleStart { - type: 'resource'; - autoStop: Date; -} type CreateServiceRequestOpt = { catalogItem: CatalogItem; catalogNamespaceName: string; @@ -59,7 +49,7 @@ type CreateServiceRequestOpt = { usePoolIfAvailable: boolean; stopDate?: Date; endDate: Date; - start?: CreateServiceRequestOptScheduleStartLifespan | CreateServiceRequestOptScheduleStartResource; + startDate?: Date; useAutoDetach: boolean; email: string; skippedSfdc: boolean; @@ -400,7 +390,7 @@ export async function createServiceRequest({ isAdmin, parameterValues, serviceNamespace, - start, + startDate, stopDate, endDate, usePoolIfAvailable, @@ -452,16 +442,11 @@ export async function createServiceRequest({ name: catalogItem.metadata.name, parameterValues: { purpose: parameterValues.purpose as string, - ...(start ? { start_timestamp: dateToApiString(start.date) } : {}), - ...(start && start.type === 'resource' && start.autoStop - ? { stop_timestamp: dateToApiString(start.autoStop) } - : stopDate - ? { stop_timestamp: dateToApiString(stopDate) } - : {}), + ...(stopDate ? { stop_timestamp: dateToApiString(stopDate) } : {}), }, }, lifespan: { - ...(start && start.type === 'lifespan' ? { start: dateToApiString(start.date) } : {}), + ...(startDate ? { start: dateToApiString(startDate) } : {}), end: dateToApiString(endDate), }, ...(useAutoDetach