Skip to content

Commit 699fcad

Browse files
authored
Fix CLI to handle Timestamp (#323)
* Fix CLI to handle Timestamp * More cli fixes
1 parent 7e4554d commit 699fcad

File tree

3 files changed

+27
-10
lines changed

3 files changed

+27
-10
lines changed

src/cli/hpr_cli_config.erl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ config_list(_, _, _) ->
208208
config_oui_list(["config", "oui", OUIString], [], Flags) ->
209209
Options = maps:from_list(Flags),
210210
OUI = erlang:list_to_integer(OUIString),
211-
RoutesETS = hpr_route_storage:oui_routes(OUI),
211+
RoutesETS = hpr_route_storage:oui_routes_ets(OUI),
212212

213213
%% OUI 4
214214
%% ========================================================
@@ -882,7 +882,7 @@ sync_routes([], [Route | LeftoverRoutes], #{removed := RemovedRoutes} = Updates)
882882
sync_routes([Route | Routes], ExistingRoutes, #{added := AddedRoutes} = Updates) ->
883883
RouteID = hpr_route:id(Route),
884884
case hpr_route_storage:lookup(RouteID) of
885-
{ok, _Route} ->
885+
{ok, _RouteETS} ->
886886
lager:info([{route_id, RouteID}], "doing nothing, route already exists"),
887887
sync_routes(
888888
Routes,

src/cli/hpr_cli_info.erl

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,21 +80,30 @@ info_ips(_, _, _) ->
8080
usage.
8181

8282
info_netids(["info", "netids"], [], []) ->
83+
NowMs = erlang:system_time(millisecond),
8384
List = lists:map(
84-
fun({NetID, Count}) ->
85+
fun({NetID, Count, Timestamp}) ->
86+
TsISO = erlang:list_to_binary(
87+
calendar:system_time_to_rfc3339(
88+
Timestamp, [{unit, millisecond}, {offset, "Z"}]
89+
)
90+
),
91+
ElapsedMs = max(1, NowMs - Timestamp),
92+
Hours = ElapsedMs / 3600000.0,
93+
CountPerHour = math:ceil(Count / Hours * 10) / 10.0,
8594
#{
8695
net_id_str => erlang:list_to_binary(hpr_utils:net_id_display(NetID)),
8796
net_id_int => NetID,
88-
count => Count
97+
count => Count,
98+
timestamp => TsISO,
99+
count_per_hour => CountPerHour
89100
}
90101
end,
91102
hpr_netid_stats:export()
92103
),
93104
Json = jsx:encode(List, [{indent, 2}]),
94-
case file:open("/tmp/net_ids.json", [write]) of
95-
{ok, File} ->
96-
file:write(File, Json),
97-
file:close(File),
105+
case file:write_file("/tmp/net_ids.json", Json) of
106+
ok ->
98107
c_text("Exported to /tmp/net_ids.json");
99108
{error, Reason} ->
100109
c_text("Failed to export ~p", [Reason])

src/grpc/iot_config/hpr_route_storage.erl

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
all_routes/0,
1414
all_route_ets/0,
1515
oui_routes/1,
16+
oui_routes_ets/1,
1617

1718
delete_all/0
1819
]).
@@ -148,14 +149,21 @@ test_size() ->
148149

149150
-spec all_routes() -> list(hpr_route:route()).
150151
all_routes() ->
151-
[hpr_route_ets:route(R) || R <- ets:tab2list(?ETS)].
152+
[hpr_route_ets:route(RouteETS) || RouteETS <- ets:tab2list(?ETS)].
152153

153154
-spec all_route_ets() -> list(hpr_route_ets:route()).
154155
all_route_ets() ->
155156
ets:tab2list(?ETS).
156157

157-
-spec oui_routes(OUI :: non_neg_integer()) -> list(hpr_route_ets:route()).
158+
-spec oui_routes(OUI :: non_neg_integer()) -> list(hpr_route:route()).
158159
oui_routes(OUI) ->
160+
[
161+
hpr_route_ets:route(RouteETS)
162+
|| RouteETS <- ets:tab2list(?ETS), OUI == hpr_route:oui(hpr_route_ets:route(RouteETS))
163+
].
164+
165+
-spec oui_routes_ets(OUI :: non_neg_integer()) -> list(hpr_route_ets:route()).
166+
oui_routes_ets(OUI) ->
159167
[
160168
RouteETS
161169
|| RouteETS <- ets:tab2list(?ETS), OUI == hpr_route:oui(hpr_route_ets:route(RouteETS))

0 commit comments

Comments
 (0)