Skip to content

Commit

Permalink
Merge branch 'main' of github.com:DrDroidLab/PlayBooks into sandbox
Browse files Browse the repository at this point in the history
  • Loading branch information
dimittal committed Jul 23, 2024
2 parents e7b1ebe + 6e6c23a commit 1893fcb
Show file tree
Hide file tree
Showing 14 changed files with 100 additions and 34 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Generated by Django 4.1.13 on 2024-07-23 15:59

from django.db import migrations
from hashlib import md5


def transform_logs_task_execution_task(apps, schema_editor):
PlayBookTaskExecutionLog = apps.get_model("executor", "PlayBookTaskExecutionLog")

for db_task in PlayBookTaskExecutionLog.objects.filter(playbook_task_result__type="LOGS"):
try:
task_result = db_task.playbook_task_result
task_table_result = task_result.get('table', {})
if task_table_result:
task_result['logs'] = task_table_result
task_result.pop('table')
db_task.save()
except Exception as e:
print('skip task transformation for task id: ', db_task.id)
print(f"Error transforming task {db_task.id}: {e}")
continue


class Migration(migrations.Migration):
dependencies = [
('executor', '0042_playbooktaskexecutionlog_execution_global_variable_set'),
]

operations = [
migrations.RunPython(transform_logs_task_execution_task, migrations.RunPython.noop)
]
2 changes: 1 addition & 1 deletion executor/source_task_executors/azure_task_executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def filter_log_events(self, time_range: TimeRange, global_variable_set: Dict,
total_count=UInt64Value(value=len(table_rows)),
)

task_result = PlaybookTaskResult(type=PlaybookTaskResultType.LOGS, table=result, source=self.source)
task_result = PlaybookTaskResult(type=PlaybookTaskResultType.LOGS, logs=result, source=self.source)
return task_result
except Exception as e:
raise Exception(f"Error while executing Azure task: {e}")
2 changes: 1 addition & 1 deletion executor/source_task_executors/cloudwatch_task_executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ def execute_filter_log_events(self, time_range: TimeRange, global_variable_set:
total_count=UInt64Value(value=len(table_rows)),
)

task_result = PlaybookTaskResult(type=PlaybookTaskResultType.LOGS, table=result, source=self.source)
task_result = PlaybookTaskResult(type=PlaybookTaskResultType.LOGS, logs=result, source=self.source)
return task_result
except Exception as e:
raise Exception(f"Error while executing Cloudwatch task: {e}")
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,6 @@ def execute_query_logs(self, time_range: TimeRange, global_variable_set: Dict, e
table = TableResult(raw_query=StringValue(value=f"Execute ```{lucene_query}``` on index {index}"),
total_count=UInt64Value(value=count_result),
rows=table_rows)
return PlaybookTaskResult(type=PlaybookTaskResultType.LOGS, table=table, source=self.source)
return PlaybookTaskResult(type=PlaybookTaskResultType.LOGS, logs=table, source=self.source)
except Exception as e:
raise Exception(f"Error while executing ElasticSearch task: {e}")
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,6 @@ def execute_query_logs(self, time_range: TimeRange, global_variable_set: Dict,
table_rows.append(table_row)
table = TableResult(raw_query=StringValue(value=f"Execute ```{query}```"), total_count=UInt64Value(value=len(result)),
rows=table_rows)
return PlaybookTaskResult(type=PlaybookTaskResultType.LOGS, table=table, source=self.source)
return PlaybookTaskResult(type=PlaybookTaskResultType.LOGS, logs=table, source=self.source)
except Exception as e:
raise Exception(f"Error while executing Grafana task: {e}")
22 changes: 19 additions & 3 deletions web/src/components/Playbooks/task/HandleOutput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,21 @@ import { unsupportedInterpreterTypes } from "../../../utils/unsupportedInterpret
import useCurrentTask from "../../../hooks/useCurrentTask.ts";
import TaskOutput from "./TaskOutput.tsx";
import Interpretation from "../../common/Interpretation/index.tsx";
import { Task } from "../../../types/index.ts";

function HandleOutput({ id, showHeading = true }) {
const [task] = useCurrentTask(id);
type HandleOutputPropTypes = {
id: string | undefined;
showHeading?: boolean;
taskFromExecution?: Task;
};

function HandleOutput({
id,
showHeading = true,
taskFromExecution = undefined,
}: HandleOutputPropTypes) {
const [taskFromPlaybook] = useCurrentTask(id);
const task = taskFromExecution ?? taskFromPlaybook;
const showOutput = task?.ui_requirement?.showOutput;

if (!task) return;
Expand All @@ -30,7 +42,11 @@ function HandleOutput({ id, showHeading = true }) {
!showHeading ? "max-h-full" : "max-h-[500px] overflow-hidden"
} h-full bg-gray-50 flex flex-col items-stretch mr-0 justify-between lg:flex-row w-full gap-2 max-w-full`}>
<div className="w-full">
<TaskOutput showHeading={showHeading} id={task.id} />
<TaskOutput
showHeading={showHeading}
id={task.id}
taskFromExecution={taskFromExecution}
/>
</div>
{Object.keys(output?.interpretation ?? {}).length > 0 &&
!unsupportedInterpreterTypes.includes(
Expand Down
5 changes: 3 additions & 2 deletions web/src/components/Playbooks/task/TaskOutput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ const OutputTypes = {
LOGS: "LOGS",
};

const TaskOutput = ({ id, showHeading }) => {
const [task] = useCurrentTask(id);
const TaskOutput = ({ id, showHeading, taskFromExecution }) => {
const [taskFromPlaybook] = useCurrentTask(id);
const task = taskFromExecution ?? taskFromPlaybook;
const output = task?.ui_requirement.output?.data;
const error = task?.ui_requirement?.outputError;

Expand Down
14 changes: 2 additions & 12 deletions web/src/components/Playbooks/timeline/ExecutingStep.jsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import { CircularProgress } from "@mui/material";
import React from "react";
import { useSelector } from "react-redux";
import { currentPlaybookSelector } from "../../../store/features/playbook/playbookSlice.ts";
import useExecutionStack from "../../../hooks/useExecutionStack.ts";

function ExecutingStep() {
const currentPlaybook = useSelector(currentPlaybookSelector);
const steps = currentPlaybook.steps ?? [];
const executingStep = steps.find((step) => step.ui_requirement.outputLoading);
const { executingStep } = useExecutionStack();

if (Object.keys(executingStep ?? {}).length === 0) return <></>;

Expand All @@ -17,13 +14,6 @@ function ExecutingStep() {
<h1 className="font-semibold text-lg line-clamp-3">
{executingStep.description}
</h1>
{/* <div onClick={() => handleShowConfig(executingStep.id)}>
(
<span className="text-violet-500 cursor-pointer hover:underline">
Show Config
</span>
)
</div> */}
</div>
<div className="flex items-center gap-2 mt-3">
<CircularProgress size={20} />
Expand Down
11 changes: 7 additions & 4 deletions web/src/components/Playbooks/timeline/StepConfig.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ function StepConfig({ step, index }: StepConfigPropTypes) {
const tasks = playbook?.ui_requirement.tasks ?? [];
const stepTasks: Task[] = step.tasks
?.map((taskId: string | Task) =>
tasks.find(
(e) => e.id === (typeof taskId === "string" ? taskId : taskId.id),
),
typeof taskId === "string" ? tasks.find((e) => e.id === taskId) : taskId,
)
.filter((task) => task !== undefined);

Expand Down Expand Up @@ -77,7 +75,12 @@ function StepConfig({ step, index }: StepConfigPropTypes) {
</div>
</div>
{stepTasks?.map((task: Task) => (
<HandleOutput key={task.id} id={task?.id} showHeading={false} />
<HandleOutput
key={task.id}
id={task?.id}
showHeading={false}
taskFromExecution={task}
/>
))}

{step.notes && (
Expand Down
12 changes: 12 additions & 0 deletions web/src/components/Workflows/create/BasicDetails.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,21 @@ function BasicDetails() {
switch (currentWorkflow?.workflowType) {
case "api":
handleGenerateCurl();
dispatch(
setCurrentWorkflowKey({
key: "useTransformer",
value: false,
}),
);
return;
case "pagerduty_incident":
handleGenerateWebhook();
dispatch(
setCurrentWorkflowKey({
key: "useTransformer",
value: false,
}),
);
return;
default:
return;
Expand Down
11 changes: 10 additions & 1 deletion web/src/components/Workflows/triggers/AlertsTable.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,20 @@ import NoExistingTrigger from "./NoExistingTrigger";
import { renderTimestamp } from "../../../utils/DateUtils";
import SeeMoreText from "../../Playbooks/SeeMoreText";
import { useGetSearchTriggersQuery } from "../../../store/features/triggers/api/searchTriggerApi.ts";
import { useEffect } from "react";

const AlertsTable = () => {
const { data: searchTriggerResult, isFetching } = useGetSearchTriggersQuery();
const {
data: searchTriggerResult,
isFetching,
refetch,
} = useGetSearchTriggersQuery();
const data = searchTriggerResult?.alerts;

useEffect(() => {
refetch();
}, [refetch]);

if (isFetching) {
return <CircularProgress size={20} style={{ marginLeft: "10px" }} />;
}
Expand Down
2 changes: 1 addition & 1 deletion web/src/hooks/useExecutionStack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ function useExecutionStack() {
const playbookSteps = currentPlaybook?.steps;
const dispatch = useDispatch();
const { refetch } = useGetPlaybookExecutionQuery();
const executingStep = (steps ?? []).find(
const executingStep = (playbookSteps ?? []).find(
(step: Step) => step.ui_requirement.outputLoading,
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ function extractExecutionTasks(
outputError: log?.result?.error,
outputLoading: false,
};
if (taskInPlaybook.id) executionStep.tasks.push(taskInPlaybook.id);
if (taskInPlaybook.id)
executionStep.tasks.push(structuredClone(taskInPlaybook));
} else {
const newTask = {
...log.task,
Expand All @@ -40,9 +41,9 @@ function extractExecutionTasks(
};
tasks.push(newTask);
if (newTask.id && !executionStep.tasks.includes(newTask.id))
executionStep.tasks.push(newTask.id);
executionStep.tasks.push(newTask);
if (stepIndex === -1) {
step.tasks.push(log.task.id);
step.tasks.push(log.task);
}
}
});
Expand Down
11 changes: 7 additions & 4 deletions web/src/utils/parser/playbook/executionToState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,15 @@ function executionToState(playbook_execution: any): Playbook {
executedSteps.push(executionStep);
});

const variables =
Object.keys(playbook_execution?.execution_global_variable_set ?? {})
.length === 0
? playbook.global_variable_set
: playbook_execution?.execution_global_variable_set;

return {
...(currentPlaybook ?? playbook),
global_variable_set:
playbook_execution?.execution_global_variable_set ??
playbook_execution?.global_variable_set ??
{},
global_variable_set: variables ?? {},
steps: playbookSteps,
step_relations: playbookRelations,
ui_requirement: {
Expand Down

0 comments on commit 1893fcb

Please sign in to comment.