All files / src/utils/execution executeStep.ts

0% Statements 0/76
0% Branches 0/32
0% Functions 0/11
0% Lines 0/74

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169                                                                                                                                                                                                                                                                                                                                                 
import { store } from "../../store/index.ts";
import {
  executePlaybookStep,
  executionStepExecute,
} from "../../store/features/playbook/api/index.ts";
import {
  playbookSelector,
  popFromExecutionStack,
} from "../../store/features/playbook/playbookSlice.ts";
import getCurrentStep from "../playbook/step/getCurrentStep.ts";
import updateStepById from "../playbook/step/updateStepById.ts";
import checkId from "../common/checkId.ts";
import { Task } from "../../types/index.ts";
import { updateCardById } from "./updateCardById.ts";
import { extractTimeFromHours } from "../../components/Playbooks/task/taskConfiguration/comparison/utils/extractTimeFromHours.ts";
import { setCurrentVisibleStepFunction } from "../playbook/step/setCurrentVisibleStepFunction.ts";
 
const handleOutput = (output: any, list: any[], id: string | undefined) => {
  const outputError = output?.result?.error;
  list.push({
    data: { ...output?.result, timestamp: output?.timestamp },
    interpretation: output?.interpretation,
    execution_global_variable_set: output?.execution_global_variable_set,
    error: outputError ? outputError : undefined,
  });
  Iif (outputError) {
    updateCardById("ui_requirement.showError", true, id);
    updateCardById("ui_requirement.outputError", true, id);
  }
};
 
export async function executeStep(id?: string) {
  const { executionId, currentPlaybook } = playbookSelector(store.getState());
  const tasks = currentPlaybook?.ui_requirement?.tasks ?? [];
  const dispatch = store.dispatch;
  const [step, currentStepId] = getCurrentStep(id);
  const stepTasks = step?.tasks.map((taskId: Task | string) =>
    tasks.find(
      (task) => task.id === (typeof taskId === "string" ? taskId : taskId.id),
    ),
  );
 
  const globalVariableSet = {};
  Iif (currentPlaybook?.global_variable_set) {
    for (let key in currentPlaybook?.global_variable_set) {
      globalVariableSet[key] = String(
        currentPlaybook?.global_variable_set[key],
      );
    }
  }
 
  Iif (!currentStepId) return;
  const stepData = {
    ...step,
    id: checkId(currentStepId),
    ui_requirement: undefined,
    tasks: (stepTasks ?? [])?.map((e: Task | undefined) => ({
      ...e,
      id: checkId(e?.id ?? ""),
      ui_requirement: undefined,
      global_variable_set: globalVariableSet,
      execution_configuration: e?.execution_configuration
        ? {
            ...e.execution_configuration,
            timeseries_offsets: e?.execution_configuration
              ?.timeseries_offsets?.[0]
              ? [
                  extractTimeFromHours(
                    e?.execution_configuration?.timeseries_offsets?.[0],
                  ),
                ]
              : undefined,
          }
        : {},
    })),
  };
 
  // Check Task errors
  stepTasks?.forEach((task) => {
    Iif (Object.keys(task?.ui_requirement.errors ?? {}).length > 0) {
      updateCardById("ui_requirement.showError", true, currentStepId);
      return;
    }
  });
 
  // Set tasks to loading
  stepTasks?.forEach((task) => {
    updateCardById("ui_requirement.outputLoading", true, task?.id);
    updateCardById("ui_requirement.showOutput", false, task?.id);
    updateCardById("ui_requirement.outputError", undefined, task?.id);
  });
 
  // Set step to loading
  updateStepById("ui_requirement.outputLoading", true, currentStepId);
  updateStepById("ui_requirement.showOutput", false, currentStepId);
  updateStepById("ui_requirement.outputError", undefined, currentStepId);
 
  dispatch(popFromExecutionStack());
 
  try {
    const res = executionId
      ? await store.dispatch(executionStepExecute.initiate(stepData)).unwrap()
      : await store.dispatch(executePlaybookStep.initiate(stepData)).unwrap();
    const outputList: any = [];
    const output = res?.step_execution_log;
    for (let outputData of output?.task_execution_logs ?? []) {
      outputList.push(outputData);
    }
    const outputErrors = outputList?.filter(
      (output: any) => output?.result?.error,
    );
    const error = outputErrors.length > 0 ? outputErrors[0] : undefined;
 
    // Set task data (error and output)
    outputList.forEach((outputs, index: number) => {
      const list: any = [];
      const taskId: Task | string | undefined = step?.tasks[index];
      const id = typeof taskId === "string" ? taskId : taskId?.id;
      if (Array.isArray(outputs)) {
        outputs.forEach((output) => {
          handleOutput(output, list, id);
        });
      } else {
        handleOutput(outputs, list, id);
      }
 
      updateCardById("ui_requirement.outputs", list, id);
    });
 
    // Set step output
    updateStepById(
      "ui_requirement.output",
      {
        data: output?.result,
        interpretation: output?.interpretation,
      },
      currentStepId,
    );
 
    // Push step to execution stack
    dispatch(popFromExecutionStack());
 
    // Set step error
    Iif (error) {
      updateStepById("ui_requirement.showError", true, currentStepId);
      updateStepById(
        "ui_requirement.outputError",
        error.result?.error,
        currentStepId,
      );
    }
  } catch (e: any) {
    updateStepById("ui_requirement.showError", true, currentStepId);
    updateStepById("ui_requirement.outputError", e.message, currentStepId);
    console.error(e);
  } finally {
    updateStepById("ui_requirement.showOutput", true, currentStepId);
    updateStepById("ui_requirement.outputLoading", false, currentStepId);
 
    // Stop tasks loading
    stepTasks?.forEach((task) => {
      updateCardById("ui_requirement.outputLoading", false, task?.id);
      updateCardById("ui_requirement.showOutput", true, task?.id);
    });
 
    Iif (!executionId) setCurrentVisibleStepFunction(currentStepId);
  }
}