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 {
getSchemaForDatasourcePlus,
getEnvironmentBindings,
runtimeToReadableBinding,
} from "dataBinding"
import { TriggerStepID, ActionStepID } from "constants/backend/automations"
import { onMount } from "svelte"
@ -595,9 +596,13 @@
let loopBlockCount = 0
const addBinding = (name, value, icon, idx, isLoopBlock, bindingName) => {
if (!name) return
const runtimeBinding = determineRuntimeBinding(name, idx, isLoopBlock)
const runtimeBinding = determineRuntimeBinding(
name,
idx,
isLoopBlock,
bindingName
)
const categoryName = determineCategoryName(idx, isLoopBlock, bindingName)
bindings.push(
createBindingObject(
name,
@ -613,7 +618,7 @@
)
}
const determineRuntimeBinding = (name, idx, isLoopBlock) => {
const determineRuntimeBinding = (name, idx, isLoopBlock, bindingName) => {
let runtimeName
/* Begin special cases for generating custom schemas based on triggers */
@ -634,12 +639,18 @@
}
/* End special cases for generating custom schemas based on triggers */
let hasUserDefinedName =
automation.stepNames?.[allSteps[idx - loopBlockCount]]?.id
if (isLoopBlock) {
runtimeName = `loop.${name}`
} else if (block.name.startsWith("JS")) {
runtimeName = `steps[${idx - loopBlockCount}].${name}`
runtimeName = hasUserDefinedName
? `stepsByName.${bindingName}.${name}`
: `steps[${idx - loopBlockCount}].${name}`
} else {
runtimeName = `steps.${idx - loopBlockCount}.${name}`
runtimeName = hasUserDefinedName
? `stepsByName.${bindingName}.${name}`
: `steps.${idx - loopBlockCount}.${name}`
}
return idx === 0 ? `trigger.${name}` : runtimeName
}
@ -666,10 +677,11 @@
const field = Object.values(FIELDS).find(
field => field.type === value.type && field.subtype === value.subtype
)
console.log(bindingName)
return {
readableBinding: bindingName
? `${bindingName}.${name}`
readableBinding:
bindingName && !isLoopBlock
? `steps.${bindingName}.${name}`
: runtimeBinding,
runtimeBinding,
type: value.type,
@ -690,8 +702,15 @@
allSteps[idx]?.stepId === ActionStepID.LOOP &&
allSteps.some(x => x.blockToLoop === block.id)
let schema = cloneDeep(allSteps[idx]?.schema?.outputs?.properties) ?? {}
if (wasLoopBlock) {
break
}
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) {
schema = {
@ -746,7 +765,6 @@
addBinding(name, value, icon, idx, isLoopBlock, bindingName)
)
}
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 { updateReferencesInObject } from "dataBinding"
import { AutomationTriggerStepId } from "@budibase/types"
import {
updateBindingsInSteps,
getNewStepName,
} from "helpers/automations/nameHelpers"
const initialAutomationState = {
automations: [],
@ -275,13 +279,17 @@ const automationActions = store => ({
await store.actions.save(newAutomation)
},
constructBlock(type, stepId, blockDefinition) {
return {
let newName
const newStep = {
...blockDefinition,
inputs: blockDefinition.inputs || {},
stepId,
type,
id: generate(),
}
newName = getNewStepName(get(selectedAutomation), newStep)
newStep.name = newName
return newStep
},
addBlockToAutomation: async (block, blockIdx) => {
const automation = get(selectedAutomation)
@ -301,15 +309,36 @@ const automationActions = store => ({
saveAutomationName: async (blockId, name) => {
const automation = get(selectedAutomation)
let newAutomation = cloneDeep(automation)
if (!automation) {
if (!newAutomation) {
return
}
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]: name.trim(),
[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 => {
const automation = get(selectedAutomation)

View file

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

View file

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