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 |
10 |
get_last(Args); |
19 |
|
execute(_Ctx, last, <<"countActiveUsers">>, Args) -> |
20 |
11 |
count_active_users(Args); |
21 |
|
execute(_Ctx, last, <<"listOldUsers">>, Args) -> |
22 |
13 |
list_old_users(Args). |
23 |
|
|
24 |
|
-spec get_last(args()) -> {ok, last_info()} | {error, resolver_error()}. |
25 |
|
get_last(#{<<"user">> := JID}) -> |
26 |
10 |
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 |
11 |
DefTimestamp = null_to_default(Timestamp, os:system_time(microsecond)), |
31 |
11 |
TimestampSec = mongoose_graphql_last_helper:microseconds_to_seconds(DefTimestamp), |
32 |
11 |
Res = mod_last_api:count_active_users(Domain, TimestampSec), |
33 |
11 |
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 |
8 |
Timestamp2 = mongoose_graphql_last_helper:microseconds_to_seconds(Timestamp), |
38 |
8 |
{ok, OldUsers} = mod_last_api:list_old_users(Timestamp2), |
39 |
8 |
{ok, format_old_users(OldUsers)}; |
40 |
|
list_old_users(#{<<"domain">> := Domain, <<"timestamp">> := Timestamp}) -> |
41 |
5 |
Timestamp2 = mongoose_graphql_last_helper:microseconds_to_seconds(Timestamp), |
42 |
5 |
case mod_last_api:list_old_users(Domain, Timestamp2) of |
43 |
|
{ok, OldUsers} -> |
44 |
3 |
{ok, format_old_users(OldUsers)}; |
45 |
|
Error -> |
46 |
2 |
make_error(Error, #{domain => Domain}) |
47 |
|
end. |