1
0
Fork 0
mirror of synced 2024-09-20 19:33:10 +12:00

automation steps using names

This commit is contained in:
Peter Clement 2024-09-10 17:09:42 +01:00
parent 4231dafc94
commit abaa40a272
5 changed files with 180 additions and 20 deletions

View file

@ -49,6 +49,7 @@
import { import {
getSchemaForDatasourcePlus, getSchemaForDatasourcePlus,
getEnvironmentBindings, getEnvironmentBindings,
runtimeToReadableBinding,
} from "dataBinding" } from "dataBinding"
import { TriggerStepID, ActionStepID } from "constants/backend/automations" import { TriggerStepID, ActionStepID } from "constants/backend/automations"
import { onMount } from "svelte" import { onMount } from "svelte"
@ -595,9 +596,13 @@
let loopBlockCount = 0 let loopBlockCount = 0
const addBinding = (name, value, icon, idx, isLoopBlock, bindingName) => { const addBinding = (name, value, icon, idx, isLoopBlock, bindingName) => {
if (!name) return if (!name) return
const runtimeBinding = determineRuntimeBinding(name, idx, isLoopBlock) const runtimeBinding = determineRuntimeBinding(
name,
idx,
isLoopBlock,
bindingName
)
const categoryName = determineCategoryName(idx, isLoopBlock, bindingName) const categoryName = determineCategoryName(idx, isLoopBlock, bindingName)
bindings.push( bindings.push(
createBindingObject( createBindingObject(
name, name,
@ -613,7 +618,7 @@
) )
} }
const determineRuntimeBinding = (name, idx, isLoopBlock) => { const determineRuntimeBinding = (name, idx, isLoopBlock, bindingName) => {
let runtimeName let runtimeName
/* Begin special cases for generating custom schemas based on triggers */ /* Begin special cases for generating custom schemas based on triggers */
@ -634,12 +639,18 @@
} }
/* End special cases for generating custom schemas based on triggers */ /* End special cases for generating custom schemas based on triggers */
let hasUserDefinedName =
automation.stepNames?.[allSteps[idx - loopBlockCount]]?.id
if (isLoopBlock) { if (isLoopBlock) {
runtimeName = `loop.${name}` runtimeName = `loop.${name}`
} else if (block.name.startsWith("JS")) { } else if (block.name.startsWith("JS")) {
runtimeName = `steps[${idx - loopBlockCount}].${name}` runtimeName = hasUserDefinedName
? `stepsByName.${bindingName}.${name}`
: `steps[${idx - loopBlockCount}].${name}`
} else { } else {
runtimeName = `steps.${idx - loopBlockCount}.${name}` runtimeName = hasUserDefinedName
? `stepsByName.${bindingName}.${name}`
: `steps.${idx - loopBlockCount}.${name}`
} }
return idx === 0 ? `trigger.${name}` : runtimeName return idx === 0 ? `trigger.${name}` : runtimeName
} }
@ -666,11 +677,12 @@
const field = Object.values(FIELDS).find( const field = Object.values(FIELDS).find(
field => field.type === value.type && field.subtype === value.subtype field => field.type === value.type && field.subtype === value.subtype
) )
console.log(bindingName)
return { return {
readableBinding: bindingName readableBinding:
? `${bindingName}.${name}` bindingName && !isLoopBlock
: runtimeBinding, ? `steps.${bindingName}.${name}`
: runtimeBinding,
runtimeBinding, runtimeBinding,
type: value.type, type: value.type,
description: value.description, description: value.description,
@ -690,8 +702,15 @@
allSteps[idx]?.stepId === ActionStepID.LOOP && allSteps[idx]?.stepId === ActionStepID.LOOP &&
allSteps.some(x => x.blockToLoop === block.id) allSteps.some(x => x.blockToLoop === block.id)
let schema = cloneDeep(allSteps[idx]?.schema?.outputs?.properties) ?? {} let schema = cloneDeep(allSteps[idx]?.schema?.outputs?.properties) ?? {}
if (wasLoopBlock) {
break
}
let bindingName = let bindingName =
automation.stepNames?.[allSteps[idx - loopBlockCount].id] automation.stepNames?.[allSteps[idx - loopBlockCount].id] ||
!isLoopBlock
? allSteps[idx]?.name
: allSteps[idx - 1]?.name
console.log(idx == 4 && bindingName)
if (isLoopBlock) { if (isLoopBlock) {
schema = { schema = {
@ -746,7 +765,6 @@
addBinding(name, value, icon, idx, isLoopBlock, bindingName) addBinding(name, value, icon, idx, isLoopBlock, bindingName)
) )
} }
return bindings return bindings
} }

View file

@ -0,0 +1,85 @@
import { AutomationActionStepId } from "@budibase/types"
export const updateBindingsInInputs = (inputs, oldName, newName) => {
if (typeof inputs === "string") {
return inputs.replace(
new RegExp(`stepsByName\\.${oldName}\\.`, "g"),
`stepsByName.${newName}.`
)
}
if (Array.isArray(inputs)) {
return inputs.map(item => updateBindingsInInputs(item, oldName, newName))
}
if (typeof inputs === "object" && inputs !== null) {
const updatedInputs = {}
for (const [key, value] of Object.entries(inputs)) {
updatedInputs[key] = updateBindingsInInputs(value, oldName, newName)
}
return updatedInputs
}
return inputs
}
export const updateBindingsInSteps = (steps, oldName, newName) => {
return steps.map(step => {
const updatedStep = {
...step,
inputs: updateBindingsInInputs(step.inputs, oldName, newName),
}
// Handle branch steps
if ("branches" in updatedStep.inputs) {
updatedStep.inputs.branches = updatedStep.inputs.branches.map(branch => ({
...branch,
condition: updateBindingsInInputs(branch.condition, oldName, newName),
}))
if (updatedStep.inputs.children) {
for (const [key, childSteps] of Object.entries(
updatedStep.inputs.children
)) {
updatedStep.inputs.children[key] = updateBindingsInSteps(
childSteps,
oldName,
newName
)
}
}
}
return updatedStep
})
}
export const getNewStepName = (automation, step) => {
const baseName = step.name
const countExistingSteps = steps => {
return steps.reduce((count, currentStep) => {
if (currentStep.name && currentStep.name.startsWith(baseName)) {
count++
}
if (
currentStep.stepId === AutomationActionStepId.BRANCH &&
currentStep.inputs &&
currentStep.inputs.children
) {
Object.values(currentStep.inputs.children).forEach(branchSteps => {
count += countExistingSteps(branchSteps)
})
}
return count
}, 0)
}
const existingCount = countExistingSteps(automation.definition.steps)
if (existingCount === 0) {
return baseName
}
return `${baseName} ${existingCount + 1}`
}

View file

@ -6,6 +6,10 @@ import { createHistoryStore } from "stores/builder/history"
import { notifications } from "@budibase/bbui" import { notifications } from "@budibase/bbui"
import { updateReferencesInObject } from "dataBinding" import { updateReferencesInObject } from "dataBinding"
import { AutomationTriggerStepId } from "@budibase/types" import { AutomationTriggerStepId } from "@budibase/types"
import {
updateBindingsInSteps,
getNewStepName,
} from "helpers/automations/nameHelpers"
const initialAutomationState = { const initialAutomationState = {
automations: [], automations: [],
@ -275,13 +279,17 @@ const automationActions = store => ({
await store.actions.save(newAutomation) await store.actions.save(newAutomation)
}, },
constructBlock(type, stepId, blockDefinition) { constructBlock(type, stepId, blockDefinition) {
return { let newName
const newStep = {
...blockDefinition, ...blockDefinition,
inputs: blockDefinition.inputs || {}, inputs: blockDefinition.inputs || {},
stepId, stepId,
type, type,
id: generate(), id: generate(),
} }
newName = getNewStepName(get(selectedAutomation), newStep)
newStep.name = newName
return newStep
}, },
addBlockToAutomation: async (block, blockIdx) => { addBlockToAutomation: async (block, blockIdx) => {
const automation = get(selectedAutomation) const automation = get(selectedAutomation)
@ -301,15 +309,36 @@ const automationActions = store => ({
saveAutomationName: async (blockId, name) => { saveAutomationName: async (blockId, name) => {
const automation = get(selectedAutomation) const automation = get(selectedAutomation)
let newAutomation = cloneDeep(automation) let newAutomation = cloneDeep(automation)
if (!automation) { if (!newAutomation) {
return return
} }
newAutomation.definition.stepNames = {
...newAutomation.definition.stepNames,
[blockId]: name.trim(),
}
await store.actions.save(newAutomation) const stepIndex = newAutomation.definition.steps.findIndex(
step => step.id === blockId
)
if (stepIndex !== -1) {
const oldName = newAutomation.definition.steps[stepIndex].name
const newName = name.trim()
// Update stepNames
newAutomation.definition.stepNames = {
...newAutomation.definition.stepNames,
[blockId]: newName,
}
// Update the name in the step itself
newAutomation.definition.steps[stepIndex].name = newName
// Update bindings in all steps
newAutomation.definition.steps = updateBindingsInSteps(
newAutomation.definition.steps,
oldName,
newName
)
await store.actions.save(newAutomation)
}
}, },
deleteAutomationName: async blockId => { deleteAutomationName: async blockId => {
const automation = get(selectedAutomation) const automation = get(selectedAutomation)

View file

@ -15,6 +15,7 @@ export interface TriggerOutput {
export interface AutomationContext extends AutomationResults { export interface AutomationContext extends AutomationResults {
steps: any[] steps: any[]
stepsByName?: Record<string, any>
env?: Record<string, string> env?: Record<string, string>
trigger: any trigger: any
} }

View file

@ -89,7 +89,12 @@ class Orchestrator {
delete triggerOutput.appId delete triggerOutput.appId
delete triggerOutput.metadata delete triggerOutput.metadata
// step zero is never used as the template string is zero indexed for customer facing // step zero is never used as the template string is zero indexed for customer facing
this.context = { steps: [{}], trigger: triggerOutput } this.context = {
steps: [{}],
stepsByName: {},
trigger: triggerOutput,
}
this.automation = automation this.automation = automation
// create an emitter which has the chain count for this automation run in it, so it can block // create an emitter which has the chain count for this automation run in it, so it can block
// excessive chaining if required // excessive chaining if required
@ -449,6 +454,11 @@ class Orchestrator {
outputs: tempOutput, outputs: tempOutput,
inputs: steps[stepToLoopIndex].inputs, inputs: steps[stepToLoopIndex].inputs,
}) })
console.log(this.context)
const stepName = steps[stepToLoopIndex].name || steps[stepToLoopIndex].id
this.context.stepsByName![stepName] = tempOutput
console.log(this.context)
this.context.steps[this.context.steps.length] = tempOutput this.context.steps[this.context.steps.length] = tempOutput
this.context.steps = this.context.steps.filter( this.context.steps = this.context.steps.filter(
item => !item.hasOwnProperty.call(item, "currentItem") item => !item.hasOwnProperty.call(item, "currentItem")
@ -542,8 +552,11 @@ class Orchestrator {
loopIteration loopIteration
) )
} }
console.log(this.context)
const stepFn = await this.getStepFunctionality(step.stepId) const stepFn = await this.getStepFunctionality(step.stepId)
let inputs = await processObject(originalStepInput, this.context) let inputs = await this.testProcesss(originalStepInput, this.context)
inputs = automationUtils.cleanInputValues(inputs, step.schema.inputs) inputs = automationUtils.cleanInputValues(inputs, step.schema.inputs)
const outputs = await stepFn({ const outputs = await stepFn({
@ -570,6 +583,18 @@ class Orchestrator {
return null return null
} }
private async testProcesss(inputs: any, context: any) {
const processContext = {
...context,
steps: {
...context.steps,
...context.stepsByName,
},
}
return processObject(inputs, processContext)
}
private handleStepOutput( private handleStepOutput(
step: AutomationStep, step: AutomationStep,
outputs: any, outputs: any,
@ -587,6 +612,8 @@ class Orchestrator {
} else { } else {
this.updateExecutionOutput(step.id, step.stepId, step.inputs, outputs) this.updateExecutionOutput(step.id, step.stepId, step.inputs, outputs)
this.context.steps[this.context.steps.length] = outputs this.context.steps[this.context.steps.length] = outputs
const stepName = step.name || step.id
this.context.stepsByName![stepName] = outputs
} }
} }
} }