From 55a01e7c33e3bc6098739508890c8728e1d99748 Mon Sep 17 00:00:00 2001 From: Taylor Downs Date: Fri, 25 Aug 2023 16:26:31 +0100 Subject: [PATCH] hack, temporarily fixes #1054 until 2nd gen yaml exporter done --- lib/lightning/export_utils.ex | 21 +++++++++++--- lib/lightning/jobs/job.ex | 2 ++ lib/lightning/setup_utils.ex | 12 ++++++++ .../controllers/api/provisioning_json.ex | 28 ++++++++++++++++--- test/lightning/projects_test.exs | 10 +++++++ 5 files changed, 65 insertions(+), 8 deletions(-) diff --git a/lib/lightning/export_utils.ex b/lib/lightning/export_utils.ex index 537542e90b..e992693fa3 100644 --- a/lib/lightning/export_utils.ex +++ b/lib/lightning/export_utils.ex @@ -175,7 +175,9 @@ defmodule Lightning.ExportUtils do def build_yaml_tree(workflows, project) do workflows_map = - Enum.reduce(workflows, %{}, fn workflow, acc -> + workflows + |> Enum.sort_by(& &1.inserted_at, NaiveDateTime) + |> Enum.reduce(%{}, fn workflow, acc -> ytree = build_workflow_yaml_tree(workflow) Map.put(acc, hyphenate(workflow.name), ytree) end) @@ -191,9 +193,20 @@ defmodule Lightning.ExportUtils do end defp build_workflow_yaml_tree(workflow) do - jobs = Enum.map(workflow.jobs, fn j -> job_to_treenode(j) end) - triggers = Enum.map(workflow.triggers, fn t -> trigger_to_treenode(t) end) - edges = Enum.map(workflow.edges, fn e -> edge_to_treenode(e, triggers) end) + jobs = + workflow.jobs + |> Enum.sort_by(& &1.inserted_at, NaiveDateTime) + |> Enum.map(fn j -> job_to_treenode(j) end) + + triggers = + workflow.triggers + |> Enum.sort_by(& &1.inserted_at, NaiveDateTime) + |> Enum.map(fn t -> trigger_to_treenode(t) end) + + edges = + workflow.edges + |> Enum.sort_by(& &1.inserted_at, NaiveDateTime) + |> Enum.map(fn e -> edge_to_treenode(e, triggers) end) flow_map = %{jobs: jobs, edges: edges, triggers: triggers} diff --git a/lib/lightning/jobs/job.ex b/lib/lightning/jobs/job.ex index 754b9f95c8..d0b4529d53 100644 --- a/lib/lightning/jobs/job.ex +++ b/lib/lightning/jobs/job.ex @@ -62,6 +62,8 @@ defmodule Lightning.Jobs.Job do job |> cast(attrs, [ :id, + # Note: we can drop inserted_at once there's a reliable way to sort yaml for export + :inserted_at, :name, :body, :enabled, diff --git a/lib/lightning/setup_utils.ex b/lib/lightning/setup_utils.ex index 71cf741505..2ac9926431 100644 --- a/lib/lightning/setup_utils.ex +++ b/lib/lightning/setup_utils.ex @@ -448,6 +448,8 @@ defmodule Lightning.SetupUtils do {:ok, fhir_standard_data} = Jobs.create_job(%{ name: "Transform data to FHIR standard", + # Note: we can drop inserted_at once there's a reliable way to sort yaml for export + inserted_at: NaiveDateTime.utc_now() |> Timex.shift(seconds: 0), body: """ fn(state => state); """, @@ -467,6 +469,8 @@ defmodule Lightning.SetupUtils do {:ok, send_to_openhim} = Jobs.create_job(%{ name: "Send to OpenHIM to route to SHR", + # Note: we can drop inserted_at once there's a reliable way to sort yaml for export + inserted_at: NaiveDateTime.utc_now() |> Timex.shift(seconds: 1), body: """ fn(state => state); """, @@ -486,6 +490,8 @@ defmodule Lightning.SetupUtils do {:ok, notify_upload_successful} = Jobs.create_job(%{ name: "Notify CHW upload successful", + # Note: we can drop inserted_at once there's a reliable way to sort yaml for export + inserted_at: NaiveDateTime.utc_now() |> Timex.shift(seconds: 2), body: """ fn(state => state); """, @@ -505,6 +511,8 @@ defmodule Lightning.SetupUtils do {:ok, notify_upload_failed} = Jobs.create_job(%{ name: "Notify CHW upload failed", + # Note: we can drop inserted_at once there's a reliable way to sort yaml for export + inserted_at: NaiveDateTime.utc_now() |> Timex.shift(seconds: 3), body: """ fn(state => state); """, @@ -634,6 +642,8 @@ defmodule Lightning.SetupUtils do {:ok, get_dhis2_data} = Jobs.create_job(%{ name: "Get DHIS2 data", + # Note: we can drop inserted_at once there's a reliable way to sort yaml for export + inserted_at: NaiveDateTime.utc_now() |> Timex.shift(seconds: 0), body: """ get('trackedEntityInstances/PQfMcpmXeFE'); """, @@ -662,6 +672,8 @@ defmodule Lightning.SetupUtils do {:ok, upload_to_google_sheet} = Jobs.create_job(%{ name: "Upload to Google Sheet", + # Note: we can drop inserted_at once there's a reliable way to sort yaml for export + inserted_at: NaiveDateTime.utc_now() |> Timex.shift(seconds: 1), body: """ fn(state => state); """, diff --git a/lib/lightning_web/controllers/api/provisioning_json.ex b/lib/lightning_web/controllers/api/provisioning_json.ex index 9622726f9b..4139910a54 100644 --- a/lib/lightning_web/controllers/api/provisioning_json.ex +++ b/lib/lightning_web/controllers/api/provisioning_json.ex @@ -11,14 +11,34 @@ defmodule LightningWeb.API.ProvisioningJSON do def as_json(%Project{} = project) do Ecto.embedded_dump(project, :json) - |> Map.put("workflows", Enum.map(project.workflows, &as_json/1)) + |> Map.put( + "workflows", + project.workflows + |> Enum.sort_by(& &1.inserted_at, NaiveDateTime) + |> Enum.map(&as_json/1) + ) end def as_json(%Workflow{} = workflow) do Ecto.embedded_dump(workflow, :json) - |> Map.put("jobs", Enum.map(workflow.jobs, &as_json/1)) - |> Map.put("triggers", Enum.map(workflow.triggers, &as_json/1)) - |> Map.put("edges", Enum.map(workflow.edges, &as_json/1)) + |> Map.put( + "jobs", + workflow.jobs + |> Enum.sort_by(& &1.inserted_at, NaiveDateTime) + |> Enum.map(&as_json/1) + ) + |> Map.put( + "triggers", + workflow.triggers + |> Enum.sort_by(& &1.inserted_at, NaiveDateTime) + |> Enum.map(&as_json/1) + ) + |> Map.put( + "edges", + workflow.edges + |> Enum.sort_by(& &1.inserted_at, NaiveDateTime) + |> Enum.map(&as_json/1) + ) end def as_json(%Job{} = job) do diff --git a/test/lightning/projects_test.exs b/test/lightning/projects_test.exs index 5705b4babb..8e5a660481 100644 --- a/test/lightning/projects_test.exs +++ b/test/lightning/projects_test.exs @@ -543,6 +543,8 @@ defmodule Lightning.ProjectsTest do workflow_1_job = insert(:job, name: "webhook job", + # Note: we can drop inserted_at once there's a reliable way to sort yaml for export + inserted_at: NaiveDateTime.utc_now() |> Timex.shift(seconds: 0), project: project, workflow: workflow_1, project_credential: %{credential: credential, project: project}, @@ -563,6 +565,8 @@ defmodule Lightning.ProjectsTest do target_job: insert(:job, name: "on fail", + # Note: we can drop inserted_at once there's a reliable way to sort yaml for export + inserted_at: NaiveDateTime.utc_now() |> Timex.shift(seconds: 1), workflow: workflow_1, body: "console.log('on fail')\nfn(state => state)" ) @@ -575,6 +579,8 @@ defmodule Lightning.ProjectsTest do target_job: insert(:job, name: "on success", + # Note: we can drop inserted_at once there's a reliable way to sort yaml for export + inserted_at: NaiveDateTime.utc_now() |> Timex.shift(seconds: 2), workflow: workflow_1 ) ) @@ -582,6 +588,8 @@ defmodule Lightning.ProjectsTest do workflow_2_job = insert(:job, name: "some cronjob", + # Note: we can drop inserted_at once there's a reliable way to sort yaml for export + inserted_at: NaiveDateTime.utc_now() |> Timex.shift(seconds: 3), workflow: workflow_2 ) @@ -604,6 +612,8 @@ defmodule Lightning.ProjectsTest do target_job: insert(:job, name: "on cron failure", + # Note: we can drop inserted_at once there's a reliable way to sort yaml for export + inserted_at: NaiveDateTime.utc_now() |> Timex.shift(seconds: 4), workflow: workflow_2 ) )