Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 28 additions & 3 deletions src/seshat.erl
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,35 @@ delete(Group, Id) ->
-spec build_counters_map(counters:counters_ref(), fields_spec()) ->
#{atom() => integer()}.
build_counters_map(CRef, FieldsSpec) ->
build_counters_map(CRef, FieldsSpec, all).

%% Helper function to build the map of counters for a given CRef and FieldSpec
%% with optional filtering by Names
-spec build_counters_map(counters:counters_ref(), fields_spec(), all | [atom()]) ->
#{atom() => integer()}.
build_counters_map(CRef, FieldsSpec, all) ->
Fields = resolve_fields_spec(FieldsSpec),
lists:foldl(fun ({Name, Index, _Type, _Help}, Acc0) ->
Acc0#{Name => counters:get(CRef, Index)}
end, #{}, Fields);
build_counters_map(CRef, FieldsSpec, [Name]) ->
%% Optimized path for single name lookup
Fields = resolve_fields_spec(FieldsSpec),
case lists:keyfind(Name, 1, Fields) of
{Name, Index, _Type, _Help} ->
#{Name => counters:get(CRef, Index)};
false ->
#{}
end;
build_counters_map(CRef, FieldsSpec, Names) when is_list(Names) ->
Fields = resolve_fields_spec(FieldsSpec),
lists:foldl(fun ({Name, Index, _Type, _Help}, Acc0) ->
Acc0#{Name => counters:get(CRef, Index)}
case lists:member(Name, Names) of
true ->
Acc0#{Name => counters:get(CRef, Index)};
false ->
Acc0
end
end, #{}, Fields).

%% @doc Return a map with all metrics of all objects in the group
Expand Down Expand Up @@ -159,8 +185,7 @@ counters(Group, Id) ->
counters(Group, Id, Names) ->
case ets:lookup(seshat_counters_server:get_table(Group), Id) of
[#entry{cref = CRef, field_spec = FieldsSpec}] ->
AllCountersMap = build_counters_map(CRef, FieldsSpec),
maps:with(Names, AllCountersMap);
build_counters_map(CRef, FieldsSpec, Names);
_ ->
undefined
end.
Expand Down