./ct_report/coverage/mod_last_mnesia.COVER.html

1 %%%----------------------------------------------------------------------
2 %%% File : mod_last.erl
3 %%% Author : MichaƂ Piotrowski <michal.piotrowski@erlang-solutions.com>
4 %%% Purpose : mod_last mnesia backend (XEP-0012)
5 %%%
6 %%%
7 %%% ejabberd, Copyright (C) 2002-2014 ProcessOne
8 %%% MongooseIM, Copyright (C) 2014 Erlang Solutions Ltd.
9 %%%
10 %%%----------------------------------------------------------------------
11
12
13 -module(mod_last_mnesia).
14
15 -behaviour(mod_last_backend).
16
17 -include("mod_last.hrl").
18 -include("mongoose.hrl").
19
20 %% API
21 -export([init/2,
22 get_last/3,
23 count_active_users/3,
24 set_last_info/5,
25 remove_user/3,
26 remove_domain/2]).
27
28 -type host_type() :: mongooseim:host_type().
29
30 -spec init(host_type(), gen_mod:module_opts()) -> ok.
31 init(_HostType, _Opts) ->
32 10 mongoose_mnesia:create_table(last_activity,
33 [{disc_copies, [node()]},
34 {attributes, record_info(fields, last_activity)}]),
35 10 ok.
36
37 -spec get_last(host_type(), jid:luser(), jid:lserver()) ->
38 {ok, mod_last:timestamp(), mod_last:status()} | {error, term()} | not_found.
39 get_last(_HostType, LUser, LServer) ->
40 33 case catch mnesia:dirty_read(last_activity, {LUser, LServer}) of
41
:-(
{'EXIT', Reason} -> {error, Reason};
42 8 [] -> not_found;
43 [#last_activity{timestamp = TimeStamp,
44 status = Status}] ->
45 25 {ok, TimeStamp, Status}
46 end.
47
48 -spec count_active_users(host_type(), jid:lserver(), mod_last:timestamp()) -> non_neg_integer().
49 count_active_users(_HostType, LServer, TimeStamp) ->
50 6 MS = [{{last_activity, {'_', LServer}, '$1', '_'},
51 [{'>', '$1', TimeStamp}],
52 [true]}],
53 6 ets:select_count(last_activity, MS).
54
55 -spec set_last_info(host_type(), jid:luser(), jid:lserver(),
56 mod_last:timestamp(), mod_last:status()) -> ok | {error, term()}.
57 set_last_info(_HostType, LUser, LServer, TimeStamp, Status) ->
58 65 US = {LUser, LServer},
59 65 F = fun() ->
60 65 mnesia:write(#last_activity{us = US,
61 timestamp = TimeStamp,
62 status = Status})
63 end,
64 65 wrap_transaction_result(mnesia:transaction(F)).
65
66 -spec remove_user(host_type(), jid:luser(), jid:lserver()) -> ok.
67 remove_user(_HostType, LUser, LServer) ->
68 53 US = {LUser, LServer},
69 53 F = fun() -> mnesia:delete({last_activity, US}) end,
70 53 wrap_transaction_result(mnesia:transaction(F)).
71
72 % Implementation only for RDBMS backends
73 -spec remove_domain(host_type(), jid:lserver()) -> ok.
74 remove_domain(_HostType, _Domain) ->
75
:-(
ok.
76
77 -spec wrap_transaction_result({atomic, ok | term()} | term()) -> ok | {error, term()}.
78 118 wrap_transaction_result({atomic, ok}) -> ok;
79
:-(
wrap_transaction_result({atomic, Error}) -> {error, Error};
80
:-(
wrap_transaction_result(Error) -> {error, Error}.
81
82
Line Hits Source