Skip to content

Commit

Permalink
Rename controllers context (#54)
Browse files Browse the repository at this point in the history
* Rename save_result; cleanup results context

* Rename controller actions

* Update tests

* Add aliases to server
  • Loading branch information
fabriziosestito authored Oct 13, 2022
1 parent 63c2f79 commit c4f0a80
Show file tree
Hide file tree
Showing 12 changed files with 44 additions and 29 deletions.
18 changes: 14 additions & 4 deletions lib/wanda/execution/server.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,18 @@ defmodule Wanda.Execution.Server do

use GenServer, restart: :transient

alias Wanda.Execution.{Evaluation, Gathering, State, Target}
alias Wanda.Messaging
alias Wanda.Execution.{
Evaluation,
Gathering,
Result,
State,
Target
}

alias Wanda.{
Messaging,
Results
}

require Logger

Expand Down Expand Up @@ -137,8 +147,8 @@ defmodule Wanda.Execution.Server do
end
end

defp store_and_publish_execution_result(%Wanda.Execution.Result{} = result) do
Wanda.Results.save_result(result)
defp store_and_publish_execution_result(%Result{} = result) do
Results.create_execution_result(result)

execution_completed = Messaging.Mapper.to_execution_completed(result)
:ok = Messaging.publish("results", execution_completed)
Expand Down
20 changes: 12 additions & 8 deletions lib/wanda/results.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,37 @@ defmodule Wanda.Results do
This module exposes functionalities to interact with the historycal log of ExecutionResults.
"""

alias Wanda.Repo

alias Wanda.Execution.Result
alias Wanda.Results.ExecutionResult

import Ecto.Query

@doc """
Saves a result to the database.
Create a new result.
"""
@spec save_result(Result.t()) :: ExecutionResult.t()
def save_result(
@spec create_execution_result(Result.t()) :: ExecutionResult.t()
def create_execution_result(
%Result{
execution_id: execution_id,
group_id: group_id
} = result
) do
Wanda.Repo.insert!(%ExecutionResult{
Repo.insert!(%ExecutionResult{
execution_id: execution_id,
group_id: group_id,
payload: result
})
end

@doc """
Gets execution results from the database.
Get a paginated list of results.
Can be filtered by group_id.
"""
@spec list_execution_results(map()) :: [ExecutionResult.t()]
def list_execution_results(params) do
def list_execution_results(params \\ %{}) do
page = Map.get(params, :page, 1)
items_per_page = Map.get(params, :items_per_page, 10)
group_id = Map.get(params, :group_id)
Expand All @@ -40,7 +44,7 @@ defmodule Wanda.Results do
|> maybe_filter_by_group_id(group_id)
|> limit([_], ^items_per_page)
|> offset([_], ^offset)
|> Wanda.Repo.all()
|> Repo.all()
end

@doc """
Expand All @@ -53,7 +57,7 @@ defmodule Wanda.Results do
from(e in ExecutionResult)
|> maybe_filter_by_group_id(group_id)
|> select([e], count())
|> Wanda.Repo.one()
|> Repo.one()
end

@spec maybe_filter_by_group_id(Ecto.Query.t(), String.t()) :: Ecto.Query.t()
Expand Down
4 changes: 2 additions & 2 deletions lib/wanda_web/controllers/catalog_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ defmodule WandaWeb.CatalogController do
alias Wanda.Catalog
alias WandaWeb.Schemas.CatalogResponse

operation :list_catalog,
operation :catalog,
summary: "List checks catalog",
responses: [
ok: {"Check catalog response", "application/json", CatalogResponse}
]

def list_catalog(conn, _) do
def catalog(conn, _) do
catalog = Catalog.get_catalog()
render(conn, catalog: catalog)
end
Expand Down
4 changes: 2 additions & 2 deletions lib/wanda_web/controllers/result_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ defmodule WandaWeb.ResultController do

plug OpenApiSpex.Plug.CastAndValidate, json_render_error_v2: true

operation :list_results,
operation :index,
summary: "List results",
parameters: [
group_id: [
Expand All @@ -29,7 +29,7 @@ defmodule WandaWeb.ResultController do
422 => OpenApiSpex.JsonErrorResponse.response()
}

def list_results(conn, params) do
def index(conn, params) do
results = Results.list_execution_results(params)
total_count = Results.count_execution_results(params)

Expand Down
7 changes: 4 additions & 3 deletions lib/wanda_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ defmodule WandaWeb.Router do
plug OpenApiSpex.Plug.PutApiSpec, module: WandaWeb.ApiSpec
end

scope "/api", WandaWeb do
scope "/api/checks", WandaWeb do
pipe_through :api
get "/checks/results", ResultController, :list_results
get "/checks/catalog", CatalogController, :list_catalog

resources "/results", ResultController, only: [:index]
get "/catalog", CatalogController, :catalog
end

scope "/api" do
Expand Down
2 changes: 1 addition & 1 deletion lib/wanda_web/views/catalog_view.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ defmodule WandaWeb.CatalogView do

alias Wanda.Catalog.Check

def render("list_catalog.json", %{catalog: catalog}) do
def render("catalog.json", %{catalog: catalog}) do
%{items: render_many(catalog, WandaWeb.CatalogView, "check.json", as: :check)}
end

Expand Down
2 changes: 1 addition & 1 deletion lib/wanda_web/views/result_view.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ defmodule WandaWeb.ResultView do

alias Wanda.Results.ExecutionResult

def render("list_results.json", %{results: results, total_count: total_count}) do
def render("index.json", %{results: results, total_count: total_count}) do
%{
items: render_many(results, WandaWeb.ResultView, "result.json", as: :result),
total_count: total_count
Expand Down
2 changes: 1 addition & 1 deletion priv/repo/seeds.exs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
execution_id = "00000000-0000-0000-0000-000000000001"
group_id = "00000000-0000-0000-0000-000000000002"

Wanda.Results.save_result(%Wanda.Execution.Result{
Wanda.Results.create_execution_result(%Wanda.Execution.Result{
execution_id: execution_id,
group_id: group_id,
check_results: [
Expand Down
4 changes: 2 additions & 2 deletions test/results_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ defmodule Wanda.ResultsTest do
execution_id = UUID.uuid4()
group_id = UUID.uuid4()

Results.save_result(
Results.create_execution_result(
build(
:result,
execution_id: execution_id,
Expand Down Expand Up @@ -42,7 +42,7 @@ defmodule Wanda.ResultsTest do
execution_id = UUID.uuid4()
group_id = UUID.uuid4()

Results.save_result(
Results.create_execution_result(
build(
:result,
execution_id: execution_id,
Expand Down
2 changes: 1 addition & 1 deletion test/wanda_web/controllers/catalog_controller_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ defmodule WandaWeb.CatalogControllerTest do
test "listing the checks catalog produces a CatalogResponse", %{conn: conn} do
json =
conn
|> get(Routes.catalog_path(conn, :list_catalog))
|> get(Routes.catalog_path(conn, :catalog))
|> json_response(200)

api_spec = ApiSpec.spec()
Expand Down
4 changes: 2 additions & 2 deletions test/wanda_web/views/catalog_view_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ defmodule WandaWeb.CatalogViewTest do
import Wanda.Factory

describe "CatalogView" do
test "renders list_catalog.json" do
test "renders catalog.json" do
checks = build_list(3, :check)

assert %{
items: ^checks
} = render(WandaWeb.CatalogView, "list_catalog.json", catalog: checks)
} = render(WandaWeb.CatalogView, "catalog.json", catalog: checks)
end
end
end
4 changes: 2 additions & 2 deletions test/wanda_web/views/result_view_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ defmodule WandaWeb.ListResultsViewTest do
alias Wanda.Results.ExecutionResult

describe "ResultView" do
test "renders list_results.json" do
test "renders index.json" do
inserted_at = DateTime.utc_now()

[
Expand Down Expand Up @@ -36,7 +36,7 @@ defmodule WandaWeb.ListResultsViewTest do
],
total_count: 10
} =
render(WandaWeb.ResultView, "list_results.json",
render(WandaWeb.ResultView, "index.json",
results: execution_results,
total_count: 10
)
Expand Down

0 comments on commit c4f0a80

Please sign in to comment.