View Source Toolbox.Report behaviour (toolbox v5.4.2)

Behaviour module for notification reports.

This behaviour encapsulates notifications which behave like reports = notifications are sent periodically.

Example

defmodule Scenarios.Template.Reporter do
  @callback Runbox.Scenario.Template.StageBased

  alias Crontab.CronExpression
  alias Runbox.Message
  alias Runbox.Runtime.Stage.Unit
  alias Scenarios.Template.Reporter.DailyReport
  alias Toolbox.Report

  import Crontab.CronExpression

  @impl true
  def init(init_timestamp, %Unit{} = unit) do
    # report schedule defined as crontab expression
    report_schedule = ~e[* * * * *]

    # timezone used to display localized timestamp in notification
    timezone = "Europe/Prague"

    # args to init report state
    init_arg = :ok

    {:ok, outputs, report_state} =
      Report.init(DailyReport, init_timestamp, report_schedule, timezone, init_arg)

    {:ok, outputs, %Unit{unit | state: %{report_state: report_state}}}
  end

  @impl true
  def handle_message(%Message{} = msg, %Unit{} = unit) do
    {:ok, outputs, report_state} = Report.process_message(unit.state.report_state, msg)
    {:reply, outputs, %Unit{unit | state: %{unit.state | report_state: report_state}}}
  end
end

defmodule Scenarios.Template.Reporter.DailyReport do
  @callback Toolbox.Report

  alias Runbox.Message
  alias Runbox.Scenario.OutputAction

  @impl true
  def init(:ok) do
    {:ok, [], %{incoming_messages: []}}
  end

  @impl true
  def handle_process_message(%{} = state, %Message{} = msg) do
    {:ok, [], %{incoming_messages: [msg | state.incoming_messages]}}
  end

  @impl true
  def handle_publish_report(%{} = state, publish_timestamp, last_publish_timestamp) do
    report_notification =
      %OutputAction.Notification{
        type: :daily_report,
        primary_asset: nil,
        data: %{
          "incoming_messages" => state.incoming_messages,
          "publish_time" => publish_timestamp,
          "last_publish_time" => last_timestamp
        }
      }

    {:ok, [report_notification], %{incoming_messages: []}}
  end
end

Summary

Types

State that the Toolbox.Report operates on; should be stored in a unit's state.

User state where a module implementing Toolbox.Report can store anything it wants.

Functions

Processes report message

Resolves date time into non-ambiguous DateTime.

Types

@opaque external_state()

State that the Toolbox.Report operates on; should be stored in a unit's state.

@type report_state() :: term()

User state where a module implementing Toolbox.Report can store anything it wants.

@type timestamp() :: integer()

Callbacks

Link to this callback

handle_process_message(report_state, t)

View Source
@callback handle_process_message(report_state(), Runbox.Message.t()) ::
  {:ok, [term()], report_state()}
Link to this callback

handle_publish_report(report_state, current_report, last_report)

View Source
@callback handle_publish_report(
  report_state(),
  current_report :: timestamp(),
  last_report :: timestamp()
) :: {:ok, [term()], report_state()}
@callback init(term()) :: {:ok, [term()], report_state()}

Functions

Link to this function

init(report_mod, init_timestamp, cron_expr, timezone, init_arg)

View Source
@spec init(module(), timestamp(), Crontab.CronExpression.t(), String.t(), term()) ::
  {:ok, [term()], external_state()}

Initializes report

Link to this function

process_message(state, msg)

View Source
@spec process_message(external_state(), Runbox.Message.t()) ::
  {:ok, [term()], external_state()}

Processes report message

Link to this function

resolve_ambiguous_datetime(datetime)

View Source
@spec resolve_ambiguous_datetime(Timex.AmbiguousDateTime.t() | DateTime.t()) ::
  DateTime.t()

Resolves date time into non-ambiguous DateTime.

If AmbiguousDateTime is given, it picks one version. If any other non-ambiguous date time is given it is returned as is.