Skip to content

Commit

Permalink
Add the time in a state to VintageNet.info
Browse files Browse the repository at this point in the history
  • Loading branch information
fhunleth committed Mar 22, 2020
1 parent eba9624 commit b5ffc6a
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions lib/vintage_net/info.ex
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,25 @@ defmodule VintageNet.Info do
defp sanitize_configuration(data), do: data

defp print_if_attribute(ifname, name, print_name) do
value = VintageNet.get(["interface", ifname, name])
IO.puts(" #{print_name}: #{inspect(value)}")
now = :erlang.monotonic_time()

case VintageNet.PropertyTable.fetch_with_timestamp(VintageNet, ["interface", ifname, name]) do
{:ok, value, timestamp} ->
IO.puts(" #{print_name}: #{inspect(value)} (#{friendly_time(now - timestamp)})")

:error ->
IO.puts(" #{print_name}: Error retrieving value")
end
end

defp friendly_time(delta_ns) do
cond do
delta_ns < 1000 -> "#{delta_ns} ns"
delta_ns < 1_000_000 -> "#{delta_ns / 1000} μs"
delta_ns < 1_000_000_000 -> "#{delta_ns / 1_000_000} ms"
delta_ns < 60_000_000_000 -> "#{delta_ns / 1_000_000_000} s"
delta_ns < 3600_000_000_000 -> "#{delta_ns / 60_000_000_000} m"
true -> "#{delta_ns / 3600_000_000_000} h"
end
end
end

0 comments on commit b5ffc6a

Please sign in to comment.