-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f446928
commit 6d5120f
Showing
22 changed files
with
700 additions
and
264 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
defmodule Wanda.Catalog.Expectation do | ||
@moduledoc """ | ||
Represents an expectation. | ||
""" | ||
|
||
defstruct [:name, :type, :expression] | ||
|
||
@type t :: %__MODULE__{ | ||
name: String.t(), | ||
type: :expect | :expect_same, | ||
expression: String.t() | ||
} | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
defmodule Wanda.Execution.AgentCheckResult do | ||
@moduledoc """ | ||
Represents the result of a check on a specific agent. | ||
""" | ||
|
||
alias Wanda.Execution.{ExpectationEvaluation, ExpectationEvaluationError} | ||
|
||
defstruct [ | ||
:agent_id, | ||
:facts, | ||
:expectation_evaluations | ||
] | ||
|
||
@type t :: %__MODULE__{ | ||
agent_id: String.t(), | ||
facts: %{(name :: String.t()) => result :: any()}, | ||
expectation_evaluations: [ExpectationEvaluation.t() | ExpectationEvaluationError.t()] | ||
} | ||
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 |
---|---|---|
@@ -1,21 +1,21 @@ | ||
defmodule Wanda.Execution.CheckResult do | ||
@moduledoc """ | ||
Represents the result of check. | ||
Represents the result of a check. | ||
""" | ||
|
||
alias Wanda.Execution.ExpectationResult | ||
alias Wanda.Execution.{AgentCheckResult, ExpectationResult} | ||
|
||
defstruct [ | ||
:check_id, | ||
:expectations_results, | ||
:facts, | ||
:expectation_results, | ||
:agents_check_results, | ||
:result | ||
] | ||
|
||
@type t :: %__MODULE__{ | ||
check_id: String.t(), | ||
expectations_results: [ExpectationResult.t()], | ||
facts: %{(name :: String.t()) => result :: any()}, | ||
expectation_results: [ExpectationResult.t()], | ||
agents_check_results: [AgentCheckResult.t()], | ||
result: :passing | :warning | :critical | ||
} | ||
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,171 @@ | ||
defmodule Wanda.Execution.Evaluation do | ||
@moduledoc """ | ||
Evaluation functional core. | ||
""" | ||
|
||
alias Wanda.Catalog | ||
alias Wanda.Catalog.Expectation | ||
|
||
alias Wanda.Execution.{ | ||
AgentCheckResult, | ||
CheckResult, | ||
ExpectationEvaluation, | ||
ExpectationEvaluationError, | ||
ExpectationResult, | ||
Result | ||
} | ||
|
||
# Abacus spec is wrong | ||
@dialyzer {:nowarn_function, eval_expectation: 2} | ||
|
||
@spec execute(String.t(), String.t(), map()) :: Result.t() | ||
def execute(execution_id, group_id, gathered_facts) do | ||
%Result{ | ||
execution_id: execution_id, | ||
group_id: group_id | ||
} | ||
|> add_checks_result(gathered_facts) | ||
|> aggregate_execution_result() | ||
end | ||
|
||
defp add_checks_result(%Result{} = result, gathered_facts) do | ||
check_results = | ||
Enum.map(gathered_facts, fn {check_id, agents_facts} -> | ||
build_check_result(check_id, agents_facts) | ||
end) | ||
|
||
%Result{result | check_results: check_results} | ||
end | ||
|
||
defp build_check_result(check_id, agents_facts) do | ||
%CheckResult{ | ||
check_id: check_id | ||
} | ||
|> add_agents_results(agents_facts) | ||
|> add_expectation_evaluations() | ||
|> aggregate_check_result() | ||
end | ||
|
||
defp add_agents_results(%CheckResult{check_id: check_id} = check_result, agents_facts) do | ||
agents_results = | ||
Enum.map(agents_facts, fn {agent_id, facts} -> | ||
%AgentCheckResult{agent_id: agent_id, facts: facts} | ||
|> add_agent_expectation_result(check_id) | ||
end) | ||
|
||
%CheckResult{check_result | agents_check_results: agents_results} | ||
end | ||
|
||
defp add_agent_expectation_result( | ||
%AgentCheckResult{facts: facts} = agent_check_result, | ||
check_id | ||
) do | ||
expectation_evaluations = | ||
check_id | ||
|> Catalog.get_expectations() | ||
|> Enum.map(&eval_expectation(&1, facts)) | ||
|
||
%AgentCheckResult{ | ||
agent_check_result | ||
| expectation_evaluations: expectation_evaluations | ||
} | ||
end | ||
|
||
defp eval_expectation(%Expectation{name: name, type: type, expression: expression}, facts) do | ||
case Abacus.eval(expression, facts) do | ||
{:ok, return_value} -> | ||
%ExpectationEvaluation{name: name, type: type, return_value: return_value} | ||
|
||
{:error, :einkey} -> | ||
%ExpectationEvaluationError{ | ||
name: name, | ||
message: "Fact is not present.", | ||
type: :fact_missing_error | ||
} | ||
|
||
_ -> | ||
%ExpectationEvaluationError{ | ||
name: name, | ||
message: "Illegal expression provided, check expression syntax.", | ||
type: :illegal_expression_error | ||
} | ||
end | ||
end | ||
|
||
defp add_expectation_evaluations( | ||
%CheckResult{check_id: check_id, agents_check_results: agents_check_results} = result | ||
) do | ||
expectation_results = | ||
agents_check_results | ||
|> Enum.flat_map(fn %AgentCheckResult{expectation_evaluations: expectation_evaluations} -> | ||
expectation_evaluations | ||
end) | ||
|> Enum.group_by(& &1.name) | ||
|> Enum.map(fn {name, expectation_evaluations} -> | ||
%{type: type} = Catalog.get_expectation(check_id, name) | ||
|
||
%ExpectationResult{ | ||
name: name, | ||
type: type, | ||
result: eval_expectation_result_or_error(type, expectation_evaluations) | ||
} | ||
end) | ||
|
||
%CheckResult{result | expectation_results: expectation_results} | ||
end | ||
|
||
defp eval_expectation_result_or_error(type, expectation_evaluations) do | ||
if has_error?(expectation_evaluations) do | ||
false | ||
else | ||
eval_expectation_result(type, expectation_evaluations) | ||
end | ||
end | ||
|
||
defp has_error?(expectation_evaluations) do | ||
Enum.any?(expectation_evaluations, fn | ||
%ExpectationEvaluationError{} -> true | ||
_ -> false | ||
end) | ||
end | ||
|
||
defp eval_expectation_result(:expect_same, expectation_evaluations) do | ||
expectation_evaluations | ||
|> Enum.uniq_by(& &1.return_value) | ||
|> Kernel.length() == 1 | ||
end | ||
|
||
defp eval_expectation_result(:expect, expectations_evaluations) do | ||
Enum.all?(expectations_evaluations, &(&1.return_value == true)) | ||
end | ||
|
||
defp aggregate_check_result( | ||
%CheckResult{expectation_results: expectation_results} = check_result | ||
) do | ||
result = | ||
if Enum.all?(expectation_results, &(&1.result == true)) do | ||
:passing | ||
else | ||
:critical | ||
end | ||
|
||
%CheckResult{check_result | result: result} | ||
end | ||
|
||
defp aggregate_execution_result(%Result{check_results: check_results} = execution_result) do | ||
result = | ||
check_results | ||
|> Enum.map(& &1.result) | ||
|> Enum.map(&{&1, result_weight(&1)}) | ||
|> Enum.max_by(fn {_, weight} -> weight end) | ||
|> elem(0) | ||
|
||
%Result{execution_result | result: result} | ||
end | ||
|
||
# TODO: is unknown needed? | ||
# defp result_weight(:unknown), do: 3 | ||
defp result_weight(:critical), do: 2 | ||
defp result_weight(:warning), do: 1 | ||
defp result_weight(:passing), do: 0 | ||
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,17 @@ | ||
defmodule Wanda.Execution.ExpectationEvaluation do | ||
@moduledoc """ | ||
Represents the evaluation of an expectation. | ||
""" | ||
|
||
defstruct [ | ||
:name, | ||
:return_value, | ||
:type | ||
] | ||
|
||
@type t :: %__MODULE__{ | ||
name: String.t(), | ||
return_value: number() | boolean() | String.t(), | ||
type: :expect | :expect_same | ||
} | ||
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,17 @@ | ||
defmodule Wanda.Execution.ExpectationEvaluationError do | ||
@moduledoc """ | ||
Represents an error occurred during the evaluation of an expectation. | ||
""" | ||
|
||
defstruct [ | ||
:name, | ||
:message, | ||
:type | ||
] | ||
|
||
@type t :: %__MODULE__{ | ||
name: String.t(), | ||
message: String.t(), | ||
type: :fact_missing_error | :illegal_expression_error | ||
} | ||
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
Oops, something went wrong.