Skip to content

Commit

Permalink
Add duration field in property metadata
Browse files Browse the repository at this point in the history
This adds the field `:duration` to the metadata dispatched when a
property changes. This will be useful for consuming applications to
records metrics on how long a property was one value before it changed
to the new value. Otherwise, the consuming application will need to keep
its own time tracking implementation. This is because the consuming
application will be notified of some change and thus the time stamp of
the old value has been replaced with the time stamp of the new value and
the old time stamp will be lost.
  • Loading branch information
mattludwigs committed Jun 30, 2021
1 parent db39b27 commit 050849e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
6 changes: 4 additions & 2 deletions lib/vintage_net/property_table/table.ex
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,11 @@ defmodule VintageNet.PropertyTable.Table do
# No change, so no notifications
:ok

[{^name, old_value, _last_change}] ->
[{^name, old_value, last_change}] ->
duration = :erlang.monotonic_time() - last_change
metadata_with_duration = Map.merge(%{duration: duration}, metadata)
:ets.insert(state.table, {name, value, timestamp})
dispatch(state, name, old_value, value, metadata)
dispatch(state, name, old_value, value, metadata_with_duration)

[] ->
:ets.insert(state.table, {name, value, timestamp})
Expand Down
15 changes: 15 additions & 0 deletions test/vintage_net/property_table_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -208,4 +208,19 @@ defmodule VintageNet.PropertyTableTest do
{["a", "b", "c"], 1}
]
end

test "duration of old value is provided in metadata", %{test: table} do
name = ["a", "b", "c"]

{:ok, _pid} = start_supervised({PropertyTable, name: table, properties: [{name, 1}]})

PropertyTable.subscribe(table, ["a", "b", "c"])

Process.sleep(2_000)
PropertyTable.put(table, ["a", "b", "c"], 88)

assert_receive {^table, ["a", "b", "c"], 1, 88, metadata}

assert System.convert_time_unit(metadata.duration, :native, :second) == 2
end
end

0 comments on commit 050849e

Please sign in to comment.