Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions assets/js/charts/last-runs.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const LastRunsHooks = {
this.lastRuns = []
// LAST RUNS GRAPH
const COLORS = {
pending: "#FBBF24CC",
success: "#3333FFCC",
error: "#FF3333CC",
cancelled: "#999999CC",
Expand Down
30 changes: 27 additions & 3 deletions lib/dashy/charts/workflow_runs.ex
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ defmodule Dashy.Charts.WorkflowRuns do
seconds: seconds,
minutes: seconds / 60,
link: link_from(data),
status: "success"
status: data.status
}
end)
end
Expand All @@ -36,7 +36,12 @@ defmodule Dashy.Charts.WorkflowRuns do
from(
j in WorkflowRunJob,
where: j.head_sha in ^head_shas(runs),
select: %{head_sha: j.head_sha, start: min(j.started_at), end: max(j.completed_at)},
select: %{
head_sha: j.head_sha,
start: min(j.started_at),
end: max(j.completed_at),
conclusion: fragment("array_agg(?)", j.conclusion)
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found this little gem of a PostgreSQL function and it does exactly what I need: aggregate the results in an array.

},
group_by: j.head_sha,
order_by: min(j.started_at)
)
Expand All @@ -47,7 +52,7 @@ defmodule Dashy.Charts.WorkflowRuns do
job = jobs |> Enum.find(fn job -> job.head_sha == run.head_sha end)

run
|> Map.merge(%{start: job.start, end: job.end})
|> Map.merge(%{start: job.start, end: job.end, status: status_from(job.conclusion)})
end)
end

Expand All @@ -56,4 +61,23 @@ defmodule Dashy.Charts.WorkflowRuns do
|> Enum.map(fn element -> Map.get(element, :head_sha) end)
|> Enum.uniq()
end

defp status_from(list) do
cond do
Enum.any?(list, fn e -> e == "pending" end) ->
"pending"

Enum.any?(list, fn e -> e == "failure" end) ->
"error"

Enum.any?(list, fn e -> e == "cancelled" end) ->
"cancelled"

list |> Enum.uniq() == ["success"] ->
"success"

true ->
"pending"
end
end
end
5 changes: 3 additions & 2 deletions lib/dashy/charts/workflow_runs_fake.ex
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ defmodule Dashy.Charts.WorkflowRunsFake do

defp set_status() do
case :rand.uniform(100) do
x when x < 10.0 -> "cancelled"
x when x < 20.0 -> "error"
x when x < 20.0 -> "cancelled"
x when x < 40.0 -> "error"
x when x < 60.0 -> "pending"
_ -> "success"
end
end
Expand Down
66 changes: 66 additions & 0 deletions test/dashy/charts/workflow_runs_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
defmodule Dashy.Charts.WorkflowRunsTest do
use Dashy.DataCase

describe "runs/1" do
test "lists all runs" do
sha = "1"
run = insert(:workflow_run, head_sha: sha, created_at: ~U[2021-05-16 09:00:00Z])
run2 = insert(:workflow_run, head_sha: sha, created_at: ~U[2021-05-16 09:00:00Z])

insert(:workflow_run_job,
workflow_run: run,
head_sha: sha,
started_at: ~U[2021-05-16 09:00:00Z],
completed_at: ~U[2021-05-16 10:00:00Z],
status: "completed",
conclusion: "success"
)

insert(:workflow_run_job,
workflow_run: run2,
head_sha: sha,
started_at: ~U[2021-05-16 09:00:00Z],
completed_at: ~U[2021-05-16 11:00:00Z],
status: "completed",
conclusion: "success"
)

assert [%Dashy.Charts.Run{} = fetched_run] = Dashy.Charts.WorkflowRuns.runs()

# 2 hours
assert fetched_run.minutes == 120
assert fetched_run.seconds == 7200
assert fetched_run.time == ~U[2021-05-16 09:00:00Z]
assert fetched_run.status == "success"
assert fetched_run.link == "https://github.com/decidim/decidim/commit/#{sha}"
end

test "handles the state correctly" do
sha = "1"
run = insert(:workflow_run, head_sha: sha, created_at: ~U[2021-05-16 09:00:00Z])
run2 = insert(:workflow_run, head_sha: sha, created_at: ~U[2021-05-16 09:00:00Z])

insert(:workflow_run_job,
workflow_run: run,
head_sha: sha,
started_at: ~U[2021-05-16 09:00:00Z],
completed_at: ~U[2021-05-16 10:00:00Z],
status: "completed",
conclusion: "success"
)

insert(:workflow_run_job,
workflow_run: run2,
head_sha: sha,
started_at: ~U[2021-05-16 09:00:00Z],
completed_at: ~U[2021-05-16 11:00:00Z],
status: "completed",
conclusion: "failure"
)

assert [%Dashy.Charts.Run{} = fetched_run] = Dashy.Charts.WorkflowRuns.runs()

assert fetched_run.status == "error"
end
end
end
6 changes: 4 additions & 2 deletions test/dashy/workflow_run_jobs_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ defmodule Dashy.WorkflowRunJobsTest do
test "finds the workflow_run_job" do
workflow_run_job = insert(:workflow_run_job)

assert workflow_run_job == WorkflowRunJobs.get_by_external_id(workflow_run_job.external_id)
assert workflow_run_job.id ==
WorkflowRunJobs.get_by_external_id(workflow_run_job.external_id).id
end
end

describe "create_or_update/1" do
test "creates a workflow_run_job" do
attrs = params_for(:workflow_run_job)
run = insert(:workflow_run)
attrs = params_for(:workflow_run_job) |> Map.merge(%{workflow_run_id: run.external_id})

assert {:ok, _workflow_run_job} = WorkflowRunJobs.create_or_update(attrs)
end
Expand Down
5 changes: 3 additions & 2 deletions test/dashy/workflow_runs_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ defmodule Dashy.WorkflowRunsTest do
test "finds the workflow_run" do
workflow_run = insert(:workflow_run)

assert workflow_run == WorkflowRuns.get_by_external_id(workflow_run.external_id)
assert workflow_run.id == WorkflowRuns.get_by_external_id(workflow_run.external_id).id
end
end

describe "create_or_update/1" do
test "creates a workflow_run" do
attrs = params_for(:workflow_run)
workflow = insert(:workflow)
attrs = params_for(:workflow_run) |> Map.merge(%{workflow_id: workflow.external_id})

assert {:ok, _workflow_run} = WorkflowRuns.create_or_update(attrs)
end
Expand Down
13 changes: 13 additions & 0 deletions test/support/factories/workflow_run_factory.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,19 @@ defmodule Dashy.WorkflowRunFactory do
defmacro __using__(_opts) do
quote do
def workflow_run_factory do
%Dashy.WorkflowRuns.WorkflowRun{
workflow: build(:workflow),
name: "My workflow run",
conclusion: "completed",
status: "completed",
node_id: sequence(:node_id, &"node-id-#{&1}"),
external_id: sequence(:workflow_run_external_id, fn id -> id end),
head_sha: "2345678sdf5678dfs67543dsfgdrs",
metadata: %{"foo" => 1}
}
end

def workflow_run_with_workflow_id_factory do
%Dashy.WorkflowRuns.WorkflowRun{
workflow_id: insert(:workflow).external_id,
name: "My workflow run",
Expand Down
2 changes: 1 addition & 1 deletion test/support/factories/workflow_run_job_factory.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ defmodule Dashy.WorkflowRunJobFactory do
quote do
def workflow_run_job_factory do
%Dashy.WorkflowRunJobs.WorkflowRunJob{
workflow_run_id: insert(:workflow_run).external_id,
workflow_run: build(:workflow_run),
name: "My workflow run job",
conclusion: "completed",
status: "completed",
Expand Down
6 changes: 3 additions & 3 deletions test/support/test_fetchers/workflow_run_jobs_fetcher.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ defmodule Dashy.TestFetchers.WorkflowRunJobsFetcher do
import Dashy.Factory

@impl GitHubWorkflowRunJobsFetcher
def get(_repo, _workflow_run) do
def get(_repo, id) do
workflow_run_jobs = [
params_for(:workflow_run_job),
params_for(:workflow_run_job)
params_for(:workflow_run_job, workflow_run_id: id),
params_for(:workflow_run_job, workflow_run_id: id)
]

%{body: workflow_run_jobs}
Expand Down
4 changes: 2 additions & 2 deletions test/support/test_fetchers/workflow_runs_fetcher.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ defmodule Dashy.TestFetchers.WorkflowRunsFetcher do
@impl GitHubWorkflowRunsFetcher
def get(_repo, _branch, 1) do
workflow_runs = [
params_for(:workflow_run),
params_for(:workflow_run)
params_for(:workflow_run_with_workflow_id),
params_for(:workflow_run_with_workflow_id)
]

%{body: workflow_runs}
Expand Down