Статистика для дашборда, расширенная #7
This commit is contained in:
+17
-1
@@ -4,7 +4,7 @@
|
||||
-export([create/4, create_recurring/5, get_by_id/1, list_by_calendar/1,
|
||||
update/2, delete/1, materialize_occurrence/3]).
|
||||
-export([generate_id/0]).
|
||||
-export([count_events/0]).
|
||||
-export([count_events/0, count_events_by_date/2]).
|
||||
|
||||
%% Создание одиночного события
|
||||
create(CalendarId, Title, StartTime, Duration) ->
|
||||
@@ -171,6 +171,22 @@ delete(Id) ->
|
||||
count_events() ->
|
||||
mnesia:table_info(event, size).
|
||||
|
||||
count_events_by_date(From, To) ->
|
||||
All = mnesia:dirty_match_object(#event{_ = '_'}),
|
||||
Filtered = lists:filter(fun(E) ->
|
||||
E#event.created_at >= From andalso E#event.created_at =< To
|
||||
end, All),
|
||||
Counts = lists:foldl(fun(E, Acc) ->
|
||||
Day = date_part(E#event.created_at),
|
||||
case lists:keyfind(Day, 1, Acc) of
|
||||
false -> [{Day, 1} | Acc];
|
||||
{Day, C} -> lists:keyreplace(Day, 1, Acc, {Day, C+1})
|
||||
end
|
||||
end, [], Filtered),
|
||||
lists:sort(Counts).
|
||||
|
||||
date_part({{Y,M,D}, _}) -> {Y,M,D}.
|
||||
|
||||
%% Внутренние функции
|
||||
generate_id() ->
|
||||
base64:encode(crypto:strong_rand_bytes(16), #{mode => urlsafe, padding => false}).
|
||||
|
||||
Reference in New Issue
Block a user