-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add steps API based on pre-existing run API
- Loading branch information
Showing
11 changed files
with
462 additions
and
239 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
defmodule LightningWeb.API.StepController do | ||
use LightningWeb, :controller | ||
|
||
alias Lightning.Invocation | ||
alias Lightning.Policies.Permissions | ||
alias Lightning.Policies.ProjectUsers | ||
alias Lightning.Projects | ||
|
||
@valid_params ~w(page page_size project_id) | ||
@max_page_size Application.compile_env( | ||
:lightning, | ||
LightningWeb.API.StepController | ||
)[:max_page_size] || 100 | ||
|
||
action_fallback LightningWeb.FallbackController | ||
|
||
def index(conn, %{"project_id" => project_id} = params) do | ||
with :ok <- validate_params(params), | ||
:ok <- authorize_read(conn, project_id) do | ||
pagination_attrs = | ||
params | ||
|> Map.take(["page", "page_size"]) | ||
|> Map.update( | ||
"page_size", | ||
@max_page_size, | ||
&min(@max_page_size, String.to_integer(&1)) | ||
) | ||
|
||
page = | ||
project_id | ||
|> Projects.get_project!() | ||
|> Invocation.list_steps_for_project(pagination_attrs) | ||
|
||
render(conn, "index.json", %{page: page, conn: conn}) | ||
end | ||
end | ||
|
||
def show(conn, %{"project_id" => project_id, "id" => id}) do | ||
with :ok <- authorize_read(conn, project_id) do | ||
step = Invocation.get_step_with_job!(id) | ||
render(conn, "show.json", %{step: step, conn: conn}) | ||
end | ||
end | ||
|
||
defp validate_params(params) do | ||
with [] <- Map.keys(params) -- @valid_params, | ||
{_n, ""} <- Integer.parse(params["page"] || "1"), | ||
{_n, ""} <- Integer.parse(params["page_size"] || "1") do | ||
:ok | ||
else | ||
_invalid -> {:error, :bad_request} | ||
end | ||
end | ||
|
||
defp authorize_read(conn, project_id) do | ||
Permissions.can( | ||
ProjectUsers, | ||
:access_project, | ||
conn.assigns.current_resource, | ||
%{project_id: project_id} | ||
) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
defmodule LightningWeb.API.StepJSON do | ||
@moduledoc false | ||
|
||
def render("index.json", %{page: page}) do | ||
page.entries | ||
|> Enum.map(&process_instance/1) | ||
end | ||
|
||
def render("show.json", %{step: step}) do | ||
process_instance(step) | ||
end | ||
|
||
defp process_instance(step) do | ||
%{ | ||
id: step.id, | ||
processRef: "#{step.job.name}:1:#{step.job.id}", | ||
initTime: step.started_at, | ||
state: step_state(step), | ||
lastChangeTime: step.updated_at | ||
} | ||
end | ||
|
||
defp step_state(step) do | ||
case {step.started_at, step.finished_at, step.exit_reason} do | ||
{nil, nil, _reason} -> | ||
"Ready" | ||
|
||
{_started_at, _finished_at, failed} when failed in ["cancel", "kill"] -> | ||
"Terminated" | ||
|
||
{_started_at, nil, _reason} -> | ||
"Active" | ||
|
||
{_started_at, _finished_at, "sucess"} -> | ||
"Completed" | ||
|
||
{_started_at, _finished_at, _reason} -> | ||
"Failed" | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.