Расширена статистика для разделов #22
This commit is contained in:
@@ -5,6 +5,7 @@
|
||||
-export([count_events/0, count_events_by_date/2]).
|
||||
-export([freeze/2, unfreeze/2]).
|
||||
-export([list_all/0]).
|
||||
-export([count_events_by_type/0, count_events_by_status/0, get_top_events_by_rating/1]).
|
||||
|
||||
%% ─────────────────────────────────────────────────────────────────
|
||||
%% Значения по умолчанию для необязательных полей
|
||||
@@ -251,6 +252,46 @@ count_events_by_date(From, To) ->
|
||||
end, [], Filtered),
|
||||
lists:sort(Counts).
|
||||
|
||||
%%%-------------------------------------------------------------------
|
||||
%%% @doc Подсчёт событий по типам (single, recurring).
|
||||
%%% @end
|
||||
%%%-------------------------------------------------------------------
|
||||
-spec count_events_by_type() -> [{atom(), non_neg_integer()}].
|
||||
count_events_by_type() ->
|
||||
Events = mnesia:dirty_match_object(#event{_ = '_'}),
|
||||
lists:foldl(fun(#event{event_type = Type}, Acc) ->
|
||||
case lists:keyfind(Type, 1, Acc) of
|
||||
false -> [{Type, 1} | Acc];
|
||||
{Type, C} -> lists:keyreplace(Type, 1, Acc, {Type, C+1})
|
||||
end
|
||||
end, [], Events).
|
||||
|
||||
%%%-------------------------------------------------------------------
|
||||
%%% @doc Подсчёт событий по статусам (active, cancelled, completed).
|
||||
%%% @end
|
||||
%%%-------------------------------------------------------------------
|
||||
-spec count_events_by_status() -> [{atom(), non_neg_integer()}].
|
||||
count_events_by_status() ->
|
||||
Events = mnesia:dirty_match_object(#event{_ = '_'}),
|
||||
lists:foldl(fun(#event{status = Status}, Acc) ->
|
||||
case lists:keyfind(Status, 1, Acc) of
|
||||
false -> [{Status, 1} | Acc];
|
||||
{Status, C} -> lists:keyreplace(Status, 1, Acc, {Status, C+1})
|
||||
end
|
||||
end, [], Events).
|
||||
|
||||
%%%-------------------------------------------------------------------
|
||||
%%% @doc Возвращает топ-N событий по среднему рейтингу (по убыванию).
|
||||
%%% @end
|
||||
%%%-------------------------------------------------------------------
|
||||
-spec get_top_events_by_rating(N :: non_neg_integer()) -> [#event{}].
|
||||
get_top_events_by_rating(N) ->
|
||||
Events = mnesia:dirty_match_object(#event{_ = '_'}),
|
||||
Sorted = lists:reverse(lists:sort(
|
||||
fun(A, B) -> A#event.rating_avg =< B#event.rating_avg end,
|
||||
Events)),
|
||||
lists:sublist(Sorted, N).
|
||||
|
||||
%%%-------------------------------------------------------------------
|
||||
%%% @doc Выделить из datetime только дату `{Y,M,D}`.
|
||||
%%% @end
|
||||
|
||||
Reference in New Issue
Block a user