Статистика для дашборда #7
This commit is contained in:
@@ -4,6 +4,7 @@
|
||||
-export([create/2, get_by_id/1, get_by_event_and_user/2, list_by_event/1, list_by_user/1]).
|
||||
-export([update_status/2, delete/1]).
|
||||
-export([generate_id/0]).
|
||||
-export([count_bookings/0]).
|
||||
|
||||
%% Создание бронирования
|
||||
create(EventId, UserId) ->
|
||||
@@ -97,6 +98,8 @@ delete(Id) ->
|
||||
{aborted, Reason} -> {error, Reason}
|
||||
end.
|
||||
|
||||
count_bookings() -> mnesia:table_info(booking, size).
|
||||
|
||||
%% Внутренние функции
|
||||
generate_id() ->
|
||||
base64:encode(crypto:strong_rand_bytes(16), #{mode => urlsafe, padding => false}).
|
||||
@@ -3,6 +3,7 @@
|
||||
|
||||
-export([create/4, create/5, get_by_id/1, list_by_owner/1, update/2, delete/1]).
|
||||
-export([generate_id/0]).
|
||||
-export([count_calendars/0]).
|
||||
|
||||
%% Создание календаря
|
||||
create(OwnerId, Title, Description, Confirmation) ->
|
||||
@@ -95,6 +96,8 @@ update(Id, Updates) ->
|
||||
delete(Id) ->
|
||||
update(Id, [{status, deleted}]).
|
||||
|
||||
count_calendars() -> mnesia:table_info(calendar, size).
|
||||
|
||||
%% Внутренние функции
|
||||
generate_id() ->
|
||||
base64:encode(crypto:strong_rand_bytes(16), #{mode => urlsafe, padding => false}).
|
||||
|
||||
@@ -4,6 +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]).
|
||||
|
||||
%% Создание одиночного события
|
||||
create(CalendarId, Title, StartTime, Duration) ->
|
||||
@@ -167,6 +168,9 @@ update(Id, Updates) ->
|
||||
delete(Id) ->
|
||||
update(Id, [{status, deleted}]).
|
||||
|
||||
count_events() ->
|
||||
mnesia:table_info(event, size).
|
||||
|
||||
%% Внутренние функции
|
||||
generate_id() ->
|
||||
base64:encode(crypto:strong_rand_bytes(16), #{mode => urlsafe, padding => false}).
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
-export([create/4, get_by_id/1, list_by_target/2, list_by_reporter/1, list_all/0]).
|
||||
-export([update_status/3, get_count_by_target/2]).
|
||||
-export([generate_id/0]).
|
||||
-export([count_reports_by_status/1, count_reports_by_admin/2]).
|
||||
|
||||
%% Создание жалобы
|
||||
create(ReporterId, TargetType, TargetId, Reason) ->
|
||||
@@ -83,6 +84,14 @@ get_count_by_target(TargetType, TargetId) ->
|
||||
Reports = mnesia:dirty_match_object(Match),
|
||||
length(Reports).
|
||||
|
||||
count_reports_by_status(Status) ->
|
||||
Match = #report{status = Status, _ = '_'},
|
||||
length(mnesia:dirty_match_object(Match)).
|
||||
|
||||
count_reports_by_admin(AdminId, Status) ->
|
||||
Match = #report{resolved_by = AdminId, status = Status, _ = '_'},
|
||||
length(mnesia:dirty_match_object(Match)).
|
||||
|
||||
%% Внутренние функции
|
||||
generate_id() ->
|
||||
base64:encode(crypto:strong_rand_bytes(16), #{mode => urlsafe, padding => false}).
|
||||
@@ -5,6 +5,7 @@
|
||||
update/2, delete/1, hide/1, unhide/1]).
|
||||
-export([get_average_rating/2, has_user_reviewed/3]).
|
||||
-export([generate_id/0]).
|
||||
-export([count_reviews/0]).
|
||||
|
||||
%% Создание отзыва
|
||||
create(UserId, TargetType, TargetId, Rating, Comment) ->
|
||||
@@ -113,6 +114,8 @@ has_user_reviewed(UserId, TargetType, TargetId) ->
|
||||
_ -> true
|
||||
end.
|
||||
|
||||
count_reviews() -> mnesia:table_info(review, size).
|
||||
|
||||
%% Внутренние функции
|
||||
generate_id() ->
|
||||
base64:encode(crypto:strong_rand_bytes(16), #{mode => urlsafe, padding => false}).
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
update_subscription/2,
|
||||
delete_subscription/1
|
||||
]).
|
||||
-export([count_subscription/0]).
|
||||
|
||||
-define(TRIAL_DAYS, 30).
|
||||
|
||||
@@ -214,4 +215,6 @@ apply_updates(Sub, Updates) ->
|
||||
Acc#subscription{expires_at = Value, updated_at = calendar:universal_time()};
|
||||
_ -> Acc
|
||||
end
|
||||
end, Sub, maps:to_list(Updates)).
|
||||
end, Sub, maps:to_list(Updates)).
|
||||
|
||||
count_subscription() -> mnesia:table_info(subscription, size).
|
||||
@@ -7,6 +7,7 @@
|
||||
stats/0,
|
||||
create_ticket/1,
|
||||
list_by_user/1]).
|
||||
-export([count_tickets_by_status/1, count_tickets_by_admin/2]).
|
||||
|
||||
list_all() ->
|
||||
mnesia:dirty_match_object(#ticket{_ = '_'}).
|
||||
@@ -83,4 +84,12 @@ apply_updates(Ticket, Updates) ->
|
||||
end, Ticket, maps:to_list(Updates)).
|
||||
|
||||
count_by_status(Status, Tickets) ->
|
||||
length([T || T <- Tickets, T#ticket.status =:= Status]).
|
||||
length([T || T <- Tickets, T#ticket.status =:= Status]).
|
||||
|
||||
count_tickets_by_status(Status) ->
|
||||
Match = #ticket{status = Status, _ = '_'},
|
||||
length(mnesia:dirty_match_object(Match)).
|
||||
|
||||
count_tickets_by_admin(AdminId, Status) ->
|
||||
Match = #ticket{assigned_to = AdminId, status = Status, _ = '_'},
|
||||
length(mnesia:dirty_match_object(Match)).
|
||||
@@ -6,6 +6,7 @@
|
||||
-export([generate_id/0]).
|
||||
-export([list_users/0]).
|
||||
-export([block/1, unblock/1]).
|
||||
-export([count_users/0]).
|
||||
|
||||
%% Создание пользователя
|
||||
create(Email, Password) ->
|
||||
@@ -122,6 +123,9 @@ unblock(Id) ->
|
||||
Error -> Error
|
||||
end.
|
||||
|
||||
count_users() ->
|
||||
mnesia:table_info(user, size).
|
||||
|
||||
%% Внутренние функции
|
||||
generate_id() ->
|
||||
base64:encode(crypto:strong_rand_bytes(16), #{mode => urlsafe, padding => false}).
|
||||
|
||||
@@ -13,16 +13,8 @@ get_stats(Req) ->
|
||||
{ok, AdminId, Req1} ->
|
||||
case admin_utils:is_admin(AdminId) of
|
||||
true ->
|
||||
Stats = #{
|
||||
users => count_users(),
|
||||
calendars => count_calendars(),
|
||||
events => count_events(),
|
||||
bookings => count_bookings(),
|
||||
reviews => count_reviews(),
|
||||
reports => count_reports(),
|
||||
tickets => count_tickets(),
|
||||
subscriptions => count_subscriptions()
|
||||
},
|
||||
{ok, Admin} = core_admin:get_by_id(AdminId),
|
||||
Stats = logic_stats:get_stats(Admin#admin.role, AdminId),
|
||||
send_json(Req1, 200, Stats);
|
||||
false ->
|
||||
send_error(Req1, 403, <<"Admin access required">>)
|
||||
@@ -31,15 +23,6 @@ get_stats(Req) ->
|
||||
send_error(Req1, Code, Message)
|
||||
end.
|
||||
|
||||
count_users() -> length(mnesia:dirty_match_object(#user{_ = '_'})).
|
||||
count_calendars() -> length(mnesia:dirty_match_object(#calendar{_ = '_'})).
|
||||
count_events() -> length(mnesia:dirty_match_object(#event{is_instance = false, _ = '_'})).
|
||||
count_bookings() -> length(mnesia:dirty_match_object(#booking{_ = '_'})).
|
||||
count_reviews() -> length(mnesia:dirty_match_object(#review{_ = '_'})).
|
||||
count_reports() -> length(mnesia:dirty_match_object(#report{_ = '_'})).
|
||||
count_tickets() -> length(mnesia:dirty_match_object(#ticket{_ = '_'})).
|
||||
count_subscriptions() -> length(mnesia:dirty_match_object(#subscription{_ = '_'})).
|
||||
|
||||
send_json(Req, Status, Data) ->
|
||||
Body = jsx:encode(Data),
|
||||
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req),
|
||||
|
||||
@@ -13,8 +13,8 @@ list_users(Req) ->
|
||||
{ok, AdminId, Req1} ->
|
||||
case admin_utils:is_admin(AdminId) of
|
||||
true ->
|
||||
Users = core_user:list_users(),
|
||||
send_json(Req1, 200, [user_to_json(U) || U <- Users]);
|
||||
{ok, Users} = core_user:list_users(),
|
||||
send_json(Req1, 200, [user_to_map(U) || U <- Users]);
|
||||
false ->
|
||||
send_error(Req1, 403, <<"Admin access required">>)
|
||||
end;
|
||||
@@ -22,14 +22,23 @@ list_users(Req) ->
|
||||
send_error(Req1, Code, Message)
|
||||
end.
|
||||
|
||||
user_to_json(U) ->
|
||||
user_to_map(User) when is_map(User) ->
|
||||
#{
|
||||
id => U#user.id,
|
||||
email => U#user.email,
|
||||
role => U#user.role,
|
||||
status => U#user.status,
|
||||
created_at => datetime_to_iso8601(U#user.created_at),
|
||||
updated_at => datetime_to_iso8601(U#user.updated_at)
|
||||
id => maps:get(id, User),
|
||||
email => maps:get(email, User),
|
||||
role => maps:get(role, User, <<"user">>),
|
||||
status => maps:get(status, User, <<"active">>),
|
||||
created_at => datetime_to_iso8601(maps:get(created_at, User)),
|
||||
updated_at => datetime_to_iso8601(maps:get(updated_at, User))
|
||||
};
|
||||
user_to_map(User) ->
|
||||
#{
|
||||
id => User#user.id,
|
||||
email => User#user.email,
|
||||
role => atom_to_binary(User#user.role, utf8),
|
||||
status => atom_to_binary(User#user.status, utf8),
|
||||
created_at => datetime_to_iso8601(User#user.created_at),
|
||||
updated_at => datetime_to_iso8601(User#user.updated_at)
|
||||
}.
|
||||
|
||||
datetime_to_iso8601({{Y,M,D},{H,Min,S}}) ->
|
||||
|
||||
@@ -22,7 +22,7 @@ init(Req, _Opts) ->
|
||||
case logic_auth:verify_jwt(Token) of
|
||||
{ok, UserId, Role} ->
|
||||
io:format("[ADMIN_WS] UserId: ~s, Role: ~s~n", [UserId, Role]),
|
||||
case admin_utils:is_admin(Role) of
|
||||
case lists:member(Role, [<<"admin">>, <<"superadmin">>, <<"moderator">>, <<"support">>]) of
|
||||
true ->
|
||||
io:format("[ADMIN_WS] Admin access granted~n"),
|
||||
{cowboy_websocket, Req, #state{admin_id = UserId}};
|
||||
|
||||
@@ -133,7 +133,7 @@ authenticate_admin_request(_Req, Email, Password) ->
|
||||
case logic_auth:authenticate_admin(Email, Password) of
|
||||
{ok, AdminMap} ->
|
||||
Role = maps:get(role, AdminMap, <<"admin">>),
|
||||
case admin_utils:is_admin(Role) of
|
||||
case is_admin_role(Role) of
|
||||
true ->
|
||||
AdminId = maps:get(id, AdminMap),
|
||||
Token = generate_admin_token(AdminId, Role),
|
||||
@@ -143,6 +143,9 @@ authenticate_admin_request(_Req, Email, Password) ->
|
||||
Error -> Error
|
||||
end.
|
||||
|
||||
is_admin_role(Role) ->
|
||||
lists:member(Role, [<<"admin">>, <<"superadmin">>, <<"moderator">>, <<"support">>]).
|
||||
|
||||
%% ========== REFRESH TOKEN ==========
|
||||
|
||||
-spec generate_refresh_token(UserId :: binary()) -> {binary(), calendar:datetime()}.
|
||||
|
||||
@@ -17,7 +17,15 @@ generate_jwt(UserId, Role) ->
|
||||
eventhub_auth:generate_user_token(UserId, Role).
|
||||
|
||||
verify_jwt(Token) ->
|
||||
eventhub_auth:verify_user_token(Token).
|
||||
case eventhub_auth:verify_user_token(Token) of
|
||||
{ok, UserId, Role} -> {ok, UserId, Role};
|
||||
{error, _} ->
|
||||
% Если не подошёл пользовательский, пробуем админский
|
||||
case eventhub_auth:verify_admin_token(Token) of
|
||||
{ok, AdminId, Role} -> {ok, AdminId, Role};
|
||||
{error, Reason} -> {error, Reason}
|
||||
end
|
||||
end.
|
||||
|
||||
generate_refresh_token(UserId) ->
|
||||
eventhub_auth:generate_refresh_token(UserId).
|
||||
|
||||
29
src/logic/logic_stats.erl
Normal file
29
src/logic/logic_stats.erl
Normal file
@@ -0,0 +1,29 @@
|
||||
-module(logic_stats).
|
||||
-export([get_stats/2]).
|
||||
|
||||
-include("records.hrl").
|
||||
|
||||
-spec get_stats(Role :: atom(), AdminId :: binary()) -> map().
|
||||
get_stats(superadmin, _AdminId) ->
|
||||
#{
|
||||
users => core_user:count_users(),
|
||||
calendars => core_calendar:count_calendars(),
|
||||
events => core_event:count_events(),
|
||||
bookings => core_booking:count_bookings(),
|
||||
reviews => core_review:count_reviews(),
|
||||
reports_total => core_report:count_reports_by_status(pending),
|
||||
tickets_open => core_ticket:count_tickets_by_status(open),
|
||||
subscriptions => core_subscription:count_subscription()
|
||||
};
|
||||
get_stats(moderator, AdminId) ->
|
||||
#{
|
||||
reports_reviewed => core_report:count_reports_by_admin(AdminId, reviewed),
|
||||
events_moderated => 0 % пока заглушка, можно добавить позже
|
||||
};
|
||||
get_stats(support, AdminId) ->
|
||||
#{
|
||||
tickets_assigned => core_ticket:count_tickets_by_admin(AdminId, open),
|
||||
reports_pending => core_report:count_reports_by_status(pending)
|
||||
};
|
||||
get_stats(_, _) ->
|
||||
#{}.
|
||||
Reference in New Issue
Block a user