feat: upsert stats counters, daily buckets and ETS tops for admin metrics. Refs EventHub/EventHubBack#42 #43 #44 #45 #46
CI / test (push) Failing after 25m40s
CI / deploy-ift (push) Has been skipped
CI / e2e-ift (push) Has been skipped
CI / deploy-stage (push) Has been skipped
CI / e2e-stage (push) Has been skipped

This commit is contained in:
2026-07-17 23:10:00 +03:00
parent f2445db66c
commit dd754e28cc
20 changed files with 1885 additions and 408 deletions
@@ -0,0 +1,42 @@
%% @doc Replace append-only `stats` with upsert `stats_counter` + `stats_daily`.
-module('20260717180000_stats_counters').
-export([up/0, down/0]).
-include("records.hrl").
up() ->
ensure_table(stats_counter, record_info(fields, stats_counter)),
ensure_table(stats_daily, record_info(fields, stats_daily)),
drop_table(stats),
ok.
down() ->
%% Old append-only shape (record removed from records.hrl).
ensure_table(stats, [id, metric, entity_id, value, timestamp]),
drop_table(stats_counter),
drop_table(stats_daily),
ok.
ensure_table(Table, Attrs) ->
case lists:member(Table, mnesia:system_info(tables)) of
true ->
ok;
false ->
case mnesia:create_table(Table, [{disc_copies, [node()]}, {attributes, Attrs}]) of
{atomic, ok} -> ok;
{aborted, {already_exists, Table}} -> ok;
{aborted, Reason} -> error({create_table_failed, Table, Reason})
end
end.
drop_table(Table) ->
case lists:member(Table, mnesia:system_info(tables)) of
true ->
case mnesia:delete_table(Table) of
{atomic, ok} -> ok;
{aborted, Reason} -> error({delete_table_failed, Table, Reason})
end;
false ->
ok
end.
@@ -0,0 +1,53 @@
%% @doc Indexes for per-admin stats: report.resolved_by, ticket.assigned_to.
-module('20260717190000_admin_stats_indexes').
-export([up/0, down/0]).
-include("records.hrl").
up() ->
ensure_index(report, resolved_by, #report.resolved_by),
ensure_index(ticket, assigned_to, #ticket.assigned_to),
ok.
down() ->
del_index(report, resolved_by),
del_index(ticket, assigned_to),
ok.
ensure_index(Table, Attr, Pos) ->
case lists:member(Table, mnesia:system_info(tables)) of
false ->
ok;
true ->
Indexes = mnesia:table_info(Table, index),
HasIndex = lists:any(fun
(A) when A =:= Attr -> true;
(N) when is_integer(N) -> N =:= Pos;
(_) -> false
end, Indexes),
case HasIndex of
true ->
ok;
false ->
case mnesia:add_table_index(Table, Attr) of
{atomic, ok} -> ok;
{aborted, {already_exists, _}} -> ok;
{aborted, {already_exists, _, _}} -> ok;
{aborted, Reason} -> error({add_index_failed, Table, Attr, Reason})
end
end
end.
del_index(Table, Attr) ->
case lists:member(Table, mnesia:system_info(tables)) of
false ->
ok;
true ->
case catch mnesia:del_table_index(Table, Attr) of
{atomic, ok} -> ok;
{aborted, {no_exists, _}} -> ok;
{aborted, {no_exists, _, _}} -> ok;
_ -> ok
end
end.