-
Notifications
You must be signed in to change notification settings - Fork 13
/
executions.ex
107 lines (91 loc) · 2.65 KB
/
executions.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
defmodule Wanda.Executions do
@moduledoc """
This module exposes functionalities to interact with the historycal log of executions.
"""
import Ecto.Query
alias Wanda.Repo
alias Wanda.Executions.{
Execution,
Result,
Target
}
@doc """
Create a new execution.
If the execution already exists, it will be returned.
"""
@spec create_execution!(String.t(), String.t(), [Target.t()]) :: Execution.t()
def create_execution!(execution_id, group_id, targets) do
%Execution{}
|> Execution.changeset(%{
execution_id: execution_id,
group_id: group_id,
status: :running,
targets: Enum.map(targets, &Map.from_struct/1)
})
|> Repo.insert!(on_conflict: :nothing)
end
@doc """
Get an execution by execution_id.
"""
@spec get_execution!(String.t()) :: Execution.t()
def get_execution!(execution_id) do
Repo.get!(Execution, execution_id)
end
@doc """
Get the last execution of a group by group_id.
"""
@spec get_last_execution_by_group_id!(String.t()) :: Execution.t()
def get_last_execution_by_group_id!(group_id) do
Execution
|> last(:started_at)
|> where(group_id: ^group_id)
|> Repo.one!()
end
@doc """
Get a paginated list of executions.
Can be filtered by group_id.
"""
@spec list_executions(map()) :: [Execution.t()]
def list_executions(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)
offset = (page - 1) * items_per_page
from(e in Execution)
|> maybe_filter_by_group_id(group_id)
|> limit([_], ^items_per_page)
|> offset([_], ^offset)
|> order_by(desc: :started_at)
|> Repo.all()
end
@doc """
Counts executions in the database.
"""
@spec count_executions(map()) :: non_neg_integer()
def count_executions(params) do
group_id = Map.get(params, :group_id)
from(e in Execution)
|> maybe_filter_by_group_id(group_id)
|> select([e], count())
|> Repo.one()
end
@doc """
Marks a previously started execution as completed
"""
@spec complete_execution!(String.t(), Result.t()) ::
Execution.t()
def complete_execution!(execution_id, %Result{} = result) do
Execution
|> Repo.get!(execution_id)
|> Execution.changeset(%{
result: result,
status: :completed,
completed_at: DateTime.utc_now()
})
|> Repo.update!()
end
@spec maybe_filter_by_group_id(Ecto.Query.t(), String.t()) :: Ecto.Query.t()
defp maybe_filter_by_group_id(query, nil), do: query
defp maybe_filter_by_group_id(query, group_id),
do: from(e in query, where: [group_id: ^group_id])
end