Skip to content

Commit

Permalink
Merge pull request #1056 from OpenFn/1054_ordering
Browse files Browse the repository at this point in the history
hack, temporarily fixes #1054 until next gen yaml exporter is created
  • Loading branch information
taylordowns2000 authored Aug 30, 2023
2 parents 1691359 + 55a01e7 commit 86798a9
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 8 deletions.
21 changes: 17 additions & 4 deletions lib/lightning/export_utils.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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}

Expand Down
2 changes: 2 additions & 0 deletions lib/lightning/jobs/job.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
12 changes: 12 additions & 0 deletions lib/lightning/setup_utils.ex
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,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);
""",
Expand All @@ -478,6 +480,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);
""",
Expand All @@ -497,6 +501,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);
""",
Expand All @@ -516,6 +522,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);
""",
Expand Down Expand Up @@ -645,6 +653,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');
""",
Expand Down Expand Up @@ -673,6 +683,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);
""",
Expand Down
28 changes: 24 additions & 4 deletions lib/lightning_web/controllers/api/provisioning_json.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions test/lightning/projects_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand All @@ -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)"
)
Expand All @@ -575,13 +579,17 @@ 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
)
)

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
)

Expand All @@ -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
)
)
Expand Down

0 comments on commit 86798a9

Please sign in to comment.