./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 11 mnesia:create_table(last_activity,
33 [{disc_copies, [node()]},
34 {attributes,
35 record_info(fields, last_activity)}]),
36 11 ok.
37
38 -spec get_last(host_type(), jid:luser(), jid:lserver()) ->
39 {ok, mod_last:timestamp(), mod_last:status()} | {error, term()} | not_found.
40 get_last(_HostType, LUser, LServer) ->
41 26 case catch mnesia:dirty_read(last_activity, {LUser, LServer}) of
42
:-(
{'EXIT', Reason} -> {error, Reason};
43 2 [] -> not_found;
44 [#last_activity{timestamp = TimeStamp,
45 status = Status}] ->
46 24 {ok, TimeStamp, Status}
47 end.
48
49 -spec count_active_users(host_type(), jid:lserver(), mod_last:timestamp()) -> non_neg_integer().
50 count_active_users(_HostType, LServer, TimeStamp) ->
51 3 MS = [{{last_activity, {'_', LServer}, '$1', '_'},
52 [{'>', '$1', TimeStamp}],
53 [true]}],
54 3 ets:select_count(last_activity, MS).
55
56 -spec set_last_info(host_type(), jid:luser(), jid:lserver(),
57 mod_last:timestamp(), mod_last:status()) -> ok | {error, term()}.
58 set_last_info(_HostType, LUser, LServer, TimeStamp, Status) ->
59 83 US = {LUser, LServer},
60 83 F = fun() ->
61 83 mnesia:write(#last_activity{us = US,
62 timestamp = TimeStamp,
63 status = Status})
64 end,
65 83 wrap_transaction_result(mnesia:transaction(F)).
66
67 -spec remove_user(host_type(), jid:luser(), jid:lserver()) -> ok.
68 remove_user(_HostType, LUser, LServer) ->
69 46 US = {LUser, LServer},
70 46 F = fun() -> mnesia:delete({last_activity, US}) end,
71 46 wrap_transaction_result(mnesia:transaction(F)).
72
73 % Implementation only for RDBMS backends
74 -spec remove_domain(host_type(), jid:lserver()) -> ok.
75 remove_domain(_HostType, _Domain) ->
76
:-(
ok.
77
78 -spec wrap_transaction_result({atomic, ok | term()} | term()) -> ok | {error, term()}.
79 129 wrap_transaction_result({atomic, ok}) -> ok;
80
:-(
wrap_transaction_result({atomic, Error}) -> {error, Error};
81
:-(
wrap_transaction_result(Error) -> {error, Error}.
82
83
Line Hits Source