Current section

Files

Jump to
diffo lib diffo provider resources process_status.ex
Raw

lib/diffo/provider/resources/process_status.ex

defmodule Diffo.Provider.ProcessStatus do
@moduledoc """
Diffo - TMF Service and Resource Management with a difference
Copyright Matt Beanland beanland@live.com.au
ProcessStatus - Ash Resource for a TMF ProcessStatus
"""
use Ash.Resource,
otp_app: :diffo,
domain: Diffo.Provider,
data_layer: AshNeo4j.DataLayer,
extensions: [AshOutstanding.Resource, AshJason.Resource]
neo4j do
label(:ProcessStatus)
relate([
{:instance, :STATUSES, :outgoing}
])
translate(id: :uuid)
end
jason do
pick([:code, :severity, :message, :parameterized_message, :timestamp])
customize(fn result, record ->
result
|> Diffo.Util.suppress(:message)
|> Diffo.Util.suppress(:parameterized_message)
|> Diffo.Util.set(:timestamp, Diffo.Util.to_iso8601(record.timestamp))
end)
rename(parameterized_message: :parameterizedMessage, timestamp: :timeStamp)
end
outstanding do
expect([:code, :severity, :message, :parameterized_message, :timestamp])
end
attributes do
uuid_primary_key :id do
description "a uuid4, unique to this process status, generated by default"
public? false
end
attribute :code, :string do
description "the code of this process status, this is a mandatory value"
allow_nil? false
public? true
end
attribute :severity, :atom do
description "the severity of this process status, this is a mandatory value"
allow_nil? false
public? true
constraints one_of: [:INFO, :WARN, :ERROR, :CRITICAL]
end
attribute :message, :string do
description "the message of this process status, this is a mandatory value"
allow_nil? false
public? true
end
attribute :parameterized_message, :term do
description "the parameterized message of this process status, this is an optional value"
allow_nil? true
public? true
end
attribute :timestamp, :utc_datetime_usec do
description "the timestamp of this process status, timestamp is create or last update"
allow_nil? false
end
end
relationships do
belongs_to :instance, Diffo.Provider.Instance do
description "the instance statused by this process status"
allow_nil? false
public? true
end
end
actions do
defaults [:read, :destroy]
create :create do
description "creates a process status related to an instance"
primary? true
accept [:code, :severity, :message, :parameterized_message]
argument :instance_id, :uuid
change manage_relationship(:instance_id, :instance, type: :append_and_remove)
change set_attribute(:timestamp, &DateTime.utc_now/0)
end
read :list do
description "lists all process statuses"
end
read :list_process_statuses_by_instance_id do
description "lists process statuses by instance id"
argument :instance_id, :uuid
filter expr(instance_id == ^arg(:instance_id))
end
update :update do
description "updates a process status, touching the timestamp"
accept [:code, :severity, :message, :parameterized_message]
change set_attribute(:timestamp, &DateTime.utc_now/0)
end
end
code_interface do
define :create
define :update
define :destroy
end
preparations do
prepare build(sort: [timestamp: :desc])
end
@doc """
Compares two process status, by timestamp
## Examples
iex> Diffo.Provider.ProcessStatus.compare(%{timestamp: "a"}, %{timestamp: "a"})
:eq
iex> Diffo.Provider.ProcessStatus.compare(%{timestamp: "b"}, %{timestamp: "a"})
:gt
iex> Diffo.Provider.ProcessStatus.compare(%{timestamp: "a"}, %{timestamp: "b"})
:lt
"""
def compare(%{timestamp: timestamp0}, %{timestamp: timestamp1}),
do: Diffo.Util.compare(timestamp0, timestamp1)
end