update bindings to support looping

This commit is contained in:
Peter Clement 2022-03-28 10:01:56 +01:00
parent 25258ba055
commit 73151722ec
6 changed files with 78 additions and 24 deletions

View File

@ -98,6 +98,7 @@ const automationActions = store => ({
automationId: automation?._id,
testData,
})
console.log(result)
store.update(state => {
state.selectedAutomation.testResults = result
return state

View File

@ -72,7 +72,9 @@
animate:flip={{ duration: 500 }}
in:fly|local={{ x: 500, duration: 1500 }}
>
<FlowItem {testDataModal} {block} />
{#if block.stepId !== "LOOP"}
<FlowItem {testDataModal} {block} />
{/if}
</div>
{/each}
</div>

View File

@ -34,6 +34,7 @@
$: testResult = $automationStore.selectedAutomation.testResults?.steps.filter(
step => (block.id ? step.id === block.id : step.stepId === block.stepId)
)
$: console.log(testResult)
$: isTrigger = block.type === "TRIGGER"
$: selected = $automationStore.selectedBlock?.id === block.id
@ -51,7 +52,10 @@
block.schema?.inputs?.properties || {}
).every(x => block?.inputs[x])
$: loopingSelected = !!block.llop
$: loopingSelected =
$automationStore.selectedAutomation?.automation.definition.steps.find(
x => x.blockToLoop === block.id
)
$: showLooping = false
async function deleteStep() {
try {
@ -77,21 +81,18 @@
)
}
/*
async function removeLooping() {
loopingSelected = false
const idx =
const loopToDelete =
$automationStore.selectedAutomation.automation.definition.steps.findIndex(
x => x.id === block.id
x => x.blockToLoop === block.id
)
delete $automationStore.selectedAutomation.automation.definition.steps[idx]
.loop
automationStore.actions.deleteAutomationBlock(loopToDelete)
await automationStore.actions.save(
$automationStore.selectedAutomation?.automation
)
}*/
}
async function addLooping() {
loopingSelected = true
const loopDefinition = $automationStore.blockDefinitions.ACTION.LOOP
@ -152,18 +153,25 @@
</div>
</div>
</div>
<Divider noMargin />
{#if !showLooping}
<div class="blockSection">
<div class="block-options">
<div class="delete-padding" on:click={() => deleteStep()}>
<Icon name="DeleteOutline" />
</div>
</div>
<Layout noPadding gap="S">
<AutomationBlockSetup
schemaProperties={Object.entries(
$automationStore.blockDefinitions.ACTION.LOOP.schema.inputs
.properties
)}
{block}
block={$automationStore.selectedAutomation?.automation.definition.steps.find(
x => x.blockToLoop === block.id
)}
{webhookModal}
isLoop={true}
/>
</Layout>
</div>

View File

@ -35,7 +35,6 @@
export let testData
export let schemaProperties
export let isTestModal = false
export let isLoop = false
let webhookModal
let drawer
let tempFilters = lookForFilters(schemaProperties) || []
@ -74,11 +73,6 @@
await automationStore.actions.save(
$automationStore.selectedAutomation?.automation
)
} else if (isLoop) {
block.loop[key] = e.detail
await automationStore.actions.save(
$automationStore.selectedAutomation?.automation
)
} else {
block.inputs[key] = e.detail
await automationStore.actions.save(

View File

@ -11,7 +11,7 @@ exports.definition = {
properties: {
option: {
customType: "loopOption",
title: "Whether it's an array or a string",
title: "Input type",
},
binding: {
type: "string",
@ -30,10 +30,18 @@ exports.definition = {
},
outputs: {
properties: {
currentItem: {
items: {
customType: "item",
description: "the item currently being executed",
},
success: {
type: "boolean",
description: "Whether the message sent successfully",
},
iterations: {
type: "number",
descriptions: "The amount of times the block ran",
},
},
required: ["success"],
},

View File

@ -17,6 +17,7 @@ const LOOP_STEP_ID = actions.ACTION_DEFINITIONS.LOOP.stepId
const CRON_STEP_ID = triggerDefs.CRON.stepId
const STOPPED_STATUS = { success: false, status: "STOPPED" }
const { cloneDeep } = require("lodash/fp")
const { loop } = require("svelte/internal")
/**
* The automation orchestrator is a class responsible for executing automations.
@ -87,6 +88,7 @@ class Orchestrator {
let stepCount = 0
let loopStepNumber
let loopSteps = []
for (let step of automation.definition.steps) {
stepCount++
if (step.stepId === LOOP_STEP_ID) {
@ -94,10 +96,18 @@ class Orchestrator {
loopStepNumber = stepCount
continue
}
let iterations = loopStep ? loopStep.inputs.binding.split(",").length : 1
let iterations = loopStep ? loopStep.inputs.iterations : 1
for (let index = 0; index < iterations; index++) {
let originalStepInput = cloneDeep(step.inputs)
/*
if (step.stepId === LOOP_STEP_ID && index >= loopStep.inputs.iterations) {
this.executionOutput.steps[loopStepNumber].outputs.status = "Loop Broken"
break
}
*/
// execution stopped, record state for that
if (stopped) {
this.updateExecutionOutput(step.id, step.stepId, {}, STOPPED_STATUS)
@ -135,25 +145,56 @@ class Orchestrator {
})
continue
}
// THE OUTPUTS GET SET IN THE CONSTRUCTOR SO WE NEED TO RESET THEM
this.updateExecutionOutput(step.id, step.stepId, step.inputs, outputs)
if (loopStep) {
loopSteps.push({
id: step.id,
stepId: step.stepId,
inputs: step.inputs,
outputs,
})
} else {
this.updateExecutionOutput(
step.id,
step.stepId,
step.inputs,
outputs
)
}
} catch (err) {
console.error(`Automation error - ${step.stepId} - ${err}`)
return err
}
if (index === iterations - 1) {
loopStep = null
break
}
}
if (loopSteps) {
this.executionOutput.steps.splice(loopStepNumber, 0, {
id: step.id,
stepId: step.stepId,
outputs: {
success: true,
outputs: loopSteps,
iterations: iterations,
},
})
this._context.steps.splice(loopStepNumber, 0, {
id: step.id,
stepId: step.stepId,
steps: loopSteps,
iterations,
success: true,
})
loopSteps = null
}
}
// Increment quota for automation runs
if (!env.SELF_HOSTED && !isDevAppID(this._appId)) {
await usage.update(usage.Properties.AUTOMATION, 1)
}
// make that we don't loop the next step if we have already been looping (loop block only has one step)
return this.executionOutput
}
}