Skip to content

Commit

Permalink
Fix [Batch run] rename method to handler (#2082)
Browse files Browse the repository at this point in the history
  • Loading branch information
mavdryk authored Nov 20, 2023
1 parent 3e05eef commit 6f53b21
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 70 deletions.
76 changes: 38 additions & 38 deletions src/components/JobWizard/JobWizard.util.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ export const generateJobWizardData = (
const functions = selectedFunctionData.functions
const functionInfo = getFunctionInfo(selectedFunctionData)
const defaultResources = frontendSpec?.default_function_pod_resources ?? {}
const functionParameters = getFunctionParameters(functions, functionInfo.method)
const functionParameters = getFunctionParameters(functions, functionInfo.handler)
const [functionPriorityClassName] = getFunctionPriorityClass(functions)
const [limits] = getLimits(functions)
const [requests] = getRequests(functions)
Expand All @@ -125,7 +125,7 @@ export const generateJobWizardData = (
? getDefaultSchedule(defaultData.schedule)
: scheduleDataInitialState
const jobAdditionalData = {
methodOptions: functionInfo.methodOptions,
handlerOptions: functionInfo.handlerOptions,
versionOptions: functionInfo.versionOptions
}
const currentLimits = parseLimits(limits, defaultResources.limits, gpuType)
Expand All @@ -135,8 +135,8 @@ export const generateJobWizardData = (
[RUN_DETAILS_STEP]: {
runName: functionInfo.name,
version: functionInfo.version,
method: functionInfo.method,
methodData: functionInfo.methodData,
handler: functionInfo.handler,
handlerData: functionInfo.handlerData,
labels: [],
image: parseImageData(functionInfo.function, frontendSpec, currentProjectName)
},
Expand Down Expand Up @@ -211,7 +211,7 @@ export const generateJobWizardDefaultData = (
if (isEmpty(defaultData)) return [{}, {}]

const runInfo = getRunDefaultInfo(defaultData, selectedFunctionData)
const functionParameters = getFunctionDefaultParameters(selectedFunctionData, runInfo.method)
const functionParameters = getFunctionDefaultParameters(selectedFunctionData, runInfo.handler)
const [predefinedParameters, customParameters] = parseDefaultParameters(
functionParameters,
defaultData.task.spec.parameters,
Expand All @@ -225,7 +225,7 @@ export const generateJobWizardDefaultData = (
const requests = defaultData.function.spec?.resources?.requests
const gpuType = getLimitsGpuType(limits)
const jobAdditionalData = {
methodOptions: runInfo.methodOptions,
handlerOptions: runInfo.handlerOptions,
versionOptions: runInfo.versionOptions
}
const currentLimits = parseLimits(limits, defaultResources.limits, gpuType)
Expand All @@ -238,8 +238,8 @@ export const generateJobWizardDefaultData = (
[RUN_DETAILS_STEP]: {
runName: runInfo.name,
version: runInfo.version,
method: runInfo.method,
methodData: runInfo.methodData,
handler: runInfo.handler,
handlerData: runInfo.handlerData,
labels: runInfo.labels,
image: parseImageData(selectedFunctionData, frontendSpec, currentProjectName)
},
Expand Down Expand Up @@ -299,16 +299,16 @@ export const generateJobWizardDefaultData = (
return [jobFormData, jobAdditionalData]
}

export const getMethodData = (selectedFunctionData, method) => {
export const getHandlerData = (selectedFunctionData, handler) => {
const currentFunction = selectedFunctionData?.functions
? chain(selectedFunctionData.functions).orderBy('metadata.updated', 'desc').get(0).value()
: selectedFunctionData
const methodData = get(currentFunction, ['spec', 'entry_points', method], {})
const outputs = (methodData?.outputs ?? []).filter(output => !isEveryObjectValueEmpty(output))
const handlerData = get(currentFunction, ['spec', 'entry_points', handler], {})
const outputs = (handlerData?.outputs ?? []).filter(output => !isEveryObjectValueEmpty(output))

return {
doc: methodData?.doc,
has_kwargs: methodData?.has_kwargs || false,
doc: handlerData?.doc,
has_kwargs: handlerData?.has_kwargs || false,
outputs
}
}
Expand All @@ -318,10 +318,10 @@ const getFunctionInfo = selectedFunctionData => {

if (!isEmpty(functions)) {
const versionOptions = getVersionOptions(functions)
const methodOptions = getMethodOptions(functions)
const { defaultVersion, defaultMethod } = getDefaultMethodAndVersion(
const handlerOptions = getHandlerOptions(functions)
const { defaultVersion, defaultHandler } = getDefaultHandlerAndVersion(
versionOptions,
methodOptions,
handlerOptions,
functions
)
const currentFunctionVersion = selectedFunctionData.tag || defaultVersion
Expand All @@ -330,10 +330,10 @@ const getFunctionInfo = selectedFunctionData => {

return {
name: selectedFunctionData.name,
method: defaultMethod,
handler: defaultHandler,
version: currentFunctionVersion,
methodData: getMethodData(currentFunction, defaultMethod),
methodOptions,
handlerData: getHandlerData(currentFunction, defaultHandler),
handlerOptions,
versionOptions,
function: currentFunction || {}
}
Expand All @@ -344,15 +344,15 @@ const getRunDefaultInfo = (defaultData, selectedFunctionData) => {
return {
labels: parseChipsData(defaultData.task?.metadata?.labels),
name: defaultData.task?.metadata?.name || '',
method: defaultData.task?.spec?.handler,
methodData: getMethodData(selectedFunctionData, defaultData.task?.spec?.handler),
methodOptions: [],
handler: defaultData.task?.spec?.handler,
handlerData: getHandlerData(selectedFunctionData, defaultData.task?.spec?.handler),
handlerOptions: [],
version: '',
versionOptions: []
}
}

const getMethodOptions = selectedFunctions => {
const getHandlerOptions = selectedFunctions => {
return chain(selectedFunctions)
.map(func => Object.values(func.spec?.entry_points ?? {}))
.flatten()
Expand All @@ -379,47 +379,47 @@ const getVersionOptions = selectedFunctions => {
return versionOptions.length ? versionOptions : [{ label: 'N/A', id: TAG_NA }]
}

const getDefaultMethod = (methodOptions, selectedFunctions) => {
let method = ''
const getDefaultHandler = (handlerOptions, selectedFunctions) => {
let handler = ''

const latestFunction = selectedFunctions.find(item => item.metadata.tag === TAG_LATEST)

if (methodOptions.length) {
method = methodOptions[0]?.id
if (handlerOptions.length) {
handler = handlerOptions[0]?.id
} else if (latestFunction) {
method = latestFunction.spec.default_handler || FUNCTION_DEFAULT_HANDLER
handler = latestFunction.spec.default_handler || FUNCTION_DEFAULT_HANDLER
} else {
method = selectedFunctions[0]?.spec.default_handler || FUNCTION_DEFAULT_HANDLER
handler = selectedFunctions[0]?.spec.default_handler || FUNCTION_DEFAULT_HANDLER
}

return method
return handler
}

const getDefaultMethodAndVersion = (versionOptions, methodOptions, selectedFunctions) => {
const getDefaultHandlerAndVersion = (versionOptions, handlerOptions, selectedFunctions) => {
const defaultVersion =
versionOptions.find(version => version.id === TAG_LATEST)?.id || versionOptions[0].id || ''

const defaultMethod = getDefaultMethod(methodOptions, selectedFunctions)
const defaultHandler = getDefaultHandler(handlerOptions, selectedFunctions)

return {
defaultVersion,
defaultMethod
defaultHandler
}
}

export const getFunctionParameters = (selectedFunction, method) => {
export const getFunctionParameters = (selectedFunction, handler) => {
return chain(selectedFunction)
.orderBy('metadata.updated', 'desc')
.map(func => {
return func.spec.entry_points ? func.spec.entry_points[method]?.parameters ?? [] : []
return func.spec.entry_points ? func.spec.entry_points[handler]?.parameters ?? [] : []
})
.flatten()
.unionBy('name')
.value()
}

export const getFunctionDefaultParameters = (selectedFunction, method) => {
const functionParameters = get(selectedFunction, `spec.entry_points[${method}].parameters`, [])
export const getFunctionDefaultParameters = (selectedFunction, handler) => {
const functionParameters = get(selectedFunction, `spec.entry_points[${handler}].parameters`, [])

return keyBy(functionParameters, 'name')
}
Expand Down Expand Up @@ -1081,7 +1081,7 @@ export const generateJobRequestData = (
// secret_sources: formData[ADVANCED_STEP].secretSourcesTable.map(secretSource => {
// return { kind: secretSource.data.key, source: secretSource.data.value }
// }),
handler: formData[RUN_DETAILS_STEP].method ?? '',
handler: formData[RUN_DETAILS_STEP].handler ?? '',
input_path: formData[ADVANCED_STEP].inputPath ?? '',
output_path: formData[ADVANCED_STEP].outputPath,
function:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const JobWizardDataInputs = ({ formState, stepIsActive }) => {
exitEditModeTriggerItem={stepIsActive}
fieldsPath={`${DATA_INPUTS_STEP}.dataInputsTable`}
formState={formState}
rowCanBeAdded={formState.values.runDetails.methodData?.has_kwargs}
rowCanBeAdded={formState.values.runDetails.handlerData?.has_kwargs}
/>
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ import { generateProjectsList } from '../../../../utils/projects'
import { functionRunKinds } from '../../../Jobs/jobs.util'
import { openConfirmPopUp } from 'igz-controls/utils/common.util'
import {
filterTrainFunctionMethods,
filterTrainFunctionHandlers,
FUNCTIONS_SELECTION_FUNCTIONS_TAB,
FUNCTIONS_SELECTION_HUB_TAB,
functionsSelectionTabs,
Expand Down Expand Up @@ -335,7 +335,7 @@ const JobWizardFunctionSelection = ({
const functionTemplatePath = `${functionData.spec.item_uri}${functionData.spec.assets.function}`

dispatch(functionsActions.fetchFunctionTemplate(functionTemplatePath)).then(result => {
const resultData = isTrain ? filterTrainFunctionMethods(result) : result
const resultData = isTrain ? filterTrainFunctionHandlers(result) : result

setSelectedFunctionData(resultData)
generateData(resultData)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,13 @@ export const generateFunctionTemplateCardData = templateData => {
return functionTemplateCardData
}

export const filterTrainFunctionMethods = result => {
const allowedMethods = trainModelAllowedHubFunctions[result.name]
export const filterTrainFunctionHandlers = result => {
const allowedHandlers = trainModelAllowedHubFunctions[result.name]
const { entry_points } = result.functions[0].spec

if (entry_points) {
result.functions[0].spec.entry_points = Object.fromEntries(
Object.entries(entry_points).filter(([key]) => allowedMethods.includes(key))
Object.entries(entry_points).filter(([key]) => allowedHandlers.includes(key))
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ const JobWizardParameters = ({ formState, stepIsActive }) => {
fieldsPath={`${PARAMETERS_STEP}.parametersTable`}
formState={formState}
parametersFromPath={parametersFromPath}
rowCanBeAdded={formState.values.runDetails.methodData?.has_kwargs}
rowCanBeAdded={formState.values.runDetails.handlerData?.has_kwargs}
withHyperparameters={
hyperParametersAreEnabled && selectedFromValue === PARAMETERS_FROM_UI_VALUE
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ import { getValidationRules } from 'igz-controls/utils/validation.util'
import { openPopUp } from 'igz-controls/utils/common.util'
import {
getFunctionParameters,
getMethodData,
getHandlerData,
parseDataInputs,
parsePredefinedParameters
} from '../../JobWizard.util'
Expand All @@ -64,10 +64,10 @@ const JobWizardRunDetails = ({
selectedFunctionData,
stepIsActive
}) => {
const methodPath = `${RUN_DETAILS_STEP}.method`
const handlerPath = `${RUN_DETAILS_STEP}.handler`
const imageSourcePath = `${RUN_DETAILS_STEP}.image.imageSource`
const outputsPath = `${RUN_DETAILS_STEP}.methodData.outputs`
const [spyOnMethodChange, setSpyOnMethodChange] = useState(true)
const outputsPath = `${RUN_DETAILS_STEP}.handlerData.outputs`
const [spyOnHandlerChange, setspyOnHandlerChange] = useState(true)
const commonImageWarningMsg =
'The image must include all the software packages that are required to run the function. ' +
'For example, for an XGBoost model, ensure that the image includes the correct XGboost package and version'
Expand All @@ -80,32 +80,32 @@ const JobWizardRunDetails = ({
[formState.values, imageSourcePath]
)

const handleMethodChange = method => {
setSpyOnMethodChange(true)
const handleHandlerChange = handler => {
setspyOnHandlerChange(true)

const functionParameters = getFunctionParameters(selectedFunctionData.functions, method)
const functionParameters = getFunctionParameters(selectedFunctionData.functions, handler)
const dataInputs = parseDataInputs(functionParameters, prePopulatedData?.dataInputs)
const predefinedParameters = parsePredefinedParameters(functionParameters)
const methodData = getMethodData(selectedFunctionData, method)
const handlerData = getHandlerData(selectedFunctionData, handler)

set(formState.initialValues, `${DATA_INPUTS_STEP}.dataInputsTable`, dataInputs)
set(
formState.initialValues,
`${PARAMETERS_STEP}.parametersTable.predefined`,
predefinedParameters
)
set(formState.initialValues, `${RUN_DETAILS_STEP}.methodData`, methodData)
set(formState.initialValues, `${RUN_DETAILS_STEP}.handlerData`, handlerData)
formState.form.change(`${DATA_INPUTS_STEP}.dataInputsTable`, dataInputs)
formState.form.change(`${PARAMETERS_STEP}.parametersTable.predefined`, predefinedParameters)
formState.form.change(`${RUN_DETAILS_STEP}.methodData`, methodData)
formState.form.change(`${RUN_DETAILS_STEP}.handlerData`, handlerData)
formState.form.change(
`${PARAMETERS_STEP}.parametersTable.custom`,
get(formState.initialValues, `${PARAMETERS_STEP}.parametersTable.custom`, [])
)
}

const onMethodChange = (value, prevValue) => {
setSpyOnMethodChange(false)
const onHandlerChange = (value, prevValue) => {
setspyOnHandlerChange(false)

const dataInputsAreChanged = areFormValuesChanged(
formState.initialValues[DATA_INPUTS_STEP].dataInputsTable,
Expand All @@ -126,22 +126,22 @@ const JobWizardRunDetails = ({
label: 'Cancel',
variant: TERTIARY_BUTTON,
handler: () => {
formState.form.change(methodPath, prevValue)
setSpyOnMethodChange(true)
formState.form.change(handlerPath, prevValue)
setspyOnHandlerChange(true)
}
},
confirmButton: {
label: 'OK',
variant: SECONDARY_BUTTON,
handler: () => {
handleMethodChange(value)
handleHandlerChange(value)
}
},
header: 'Are you sure?',
message: 'Changes made to the Data Inputs and Parameters sections will be lost'
})
} else {
handleMethodChange(value)
handleHandlerChange(value)
}
}

Expand Down Expand Up @@ -180,18 +180,18 @@ const JobWizardRunDetails = ({
</div>
)}
{!isBatchInference ? (
jobAdditionalData.methodOptions?.length !== 0 ? (
jobAdditionalData.handlerOptions?.length !== 0 ? (
<div className="form-col-1">
<FormSelect
label="Method"
name={methodPath}
options={jobAdditionalData.methodOptions || []}
label="Handler"
name={handlerPath}
options={jobAdditionalData.handlerOptions || []}
scrollToView={false}
/>
</div>
) : (
<div className="form-col-1">
<FormInput label="Method" name={methodPath} disabled={isEditMode} />
<FormInput label="Handler" name={handlerPath} disabled={isEditMode} />
</div>
)
) : null}
Expand Down Expand Up @@ -268,10 +268,10 @@ const JobWizardRunDetails = ({
</div>
</>
)}
{get(formState.values, `${RUN_DETAILS_STEP}.methodData.doc`, '') && (
{get(formState.values, `${RUN_DETAILS_STEP}.handlerData.doc`, '') && (
<>
<div className="form-row form-table-title">Description</div>
<div className="form-row">{formState.values[RUN_DETAILS_STEP].methodData.doc}</div>
<div className="form-row">{formState.values[RUN_DETAILS_STEP].handlerData.doc}</div>
</>
)}
{get(formState.values, outputsPath, []).length > 0 && (
Expand Down Expand Up @@ -310,8 +310,8 @@ const JobWizardRunDetails = ({
</>
)}

{stepIsActive && spyOnMethodChange && (
<OnChange name={methodPath}>{onMethodChange}</OnChange>
{stepIsActive && spyOnHandlerChange && (
<OnChange name={handlerPath}>{onHandlerChange}</OnChange>
)}
</div>
)
Expand Down

0 comments on commit 6f53b21

Please sign in to comment.