1 |
|
-module(mongoose_graphql_last_admin_query). |
2 |
|
-behaviour(mongoose_graphql). |
3 |
|
|
4 |
|
-export([execute/4]). |
5 |
|
|
6 |
|
-ignore_xref([execute/4]). |
7 |
|
|
8 |
|
-include("../mongoose_graphql_types.hrl"). |
9 |
|
|
10 |
|
-import(mongoose_graphql_helper, [make_error/2, format_result/2, null_to_default/2]). |
11 |
|
-import(mongoose_graphql_last_helper, [format_old_users/1]). |
12 |
|
|
13 |
|
-type old_users() :: mongoose_graphql_last_helper:old_users(). |
14 |
|
-type last_info() :: mongoose_graphql_last_helper:last_info(). |
15 |
|
-type args() :: mongoose_graphql:args(). |
16 |
|
|
17 |
|
execute(_Ctx, last, <<"getLast">>, Args) -> |
18 |
:-( |
get_last(Args); |
19 |
|
execute(_Ctx, last, <<"countActiveUsers">>, Args) -> |
20 |
:-( |
count_active_users(Args); |
21 |
|
execute(_Ctx, last, <<"listOldUsers">>, Args) -> |
22 |
:-( |
list_old_users(Args). |
23 |
|
|
24 |
|
-spec get_last(args()) -> {ok, last_info()} | {error, resolver_error()}. |
25 |
|
get_last(#{<<"user">> := JID}) -> |
26 |
:-( |
mongoose_graphql_last_helper:get_last(JID). |
27 |
|
|
28 |
|
-spec count_active_users(args()) -> {ok, pos_integer()} | {error, resolver_error()}. |
29 |
|
count_active_users(#{<<"domain">> := Domain, <<"timestamp">> := Timestamp}) -> |
30 |
:-( |
DefTimestamp = null_to_default(Timestamp, os:system_time(microsecond)), |
31 |
:-( |
TimestampSec = mongoose_graphql_last_helper:microseconds_to_seconds(DefTimestamp), |
32 |
:-( |
Res = mod_last_api:count_active_users(Domain, TimestampSec), |
33 |
:-( |
format_result(Res, #{domain => Domain}). |
34 |
|
|
35 |
|
-spec list_old_users(args()) -> {ok, old_users()} | {error, resolver_error()}. |
36 |
|
list_old_users(#{<<"domain">> := null, <<"timestamp">> := Timestamp}) -> |
37 |
:-( |
Timestamp2 = mongoose_graphql_last_helper:microseconds_to_seconds(Timestamp), |
38 |
:-( |
{ok, OldUsers} = mod_last_api:list_old_users(Timestamp2), |
39 |
:-( |
{ok, format_old_users(OldUsers)}; |
40 |
|
list_old_users(#{<<"domain">> := Domain, <<"timestamp">> := Timestamp}) -> |
41 |
:-( |
Timestamp2 = mongoose_graphql_last_helper:microseconds_to_seconds(Timestamp), |
42 |
:-( |
case mod_last_api:list_old_users(Domain, Timestamp2) of |
43 |
|
{ok, OldUsers} -> |
44 |
:-( |
{ok, format_old_users(OldUsers)}; |
45 |
|
Error -> |
46 |
:-( |
make_error(Error, #{domain => Domain}) |
47 |
|
end. |