PageRenderTime 74ms CodeModel.GetById 63ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/modules/mod_logging/models/m_log_email.erl

https://code.google.com/p/zotonic/
Erlang | 134 lines | 104 code | 13 blank | 17 comment | 5 complexity | 79460ad0b801f753e19b78b914e88ce1 MD5 | raw file
  1%% @author Marc Worrell <marc@worrell.nl>
  2%% @copyright 2011 Marc Worrell
  3%%
  4%% @doc Model for email log messages.
  5
  6%% Copyright 2011 Marc Worrell
  7%%
  8%% Licensed under the Apache License, Version 2.0 (the "License");
  9%% you may not use this file except in compliance with the License.
 10%% You may obtain a copy of the License at
 11%% 
 12%%     http://www.apache.org/licenses/LICENSE-2.0
 13%% 
 14%% Unless required by applicable law or agreed to in writing, software
 15%% distributed under the License is distributed on an "AS IS" BASIS,
 16%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 17%% See the License for the specific language governing permissions and
 18%% limitations under the License.
 19
 20-module(m_log_email).
 21-author("Marc Worrell <marc@worrell.nl>").
 22
 23-export([
 24    search/2,
 25    install/1
 26]).
 27
 28-include_lib("zotonic.hrl").
 29
 30
 31search(Filter, Context) ->
 32    {Where, Args} = lists:foldl(fun(F, Acc) -> map_search(F, Acc, Context) end, {[], []}, Filter),
 33    #search_sql{
 34        select="*",
 35        from="log_email",
 36        where=lists:flatten(string:join(Where, " and ")),
 37        order="id desc",
 38        args=lists:reverse(Args),
 39        assoc=true
 40    }.
 41
 42    map_search({severity, [C]}, {Ws,As}, _Context) when C >= $0, C =< $9 ->
 43        {["severity <= "++[C]|Ws], As};
 44    map_search({severity, _}, {Ws,As}, _Context) ->
 45        {["severity <= 1"|Ws], As};
 46    map_search({_, ""}, Acc, _Context) -> Acc;
 47    map_search({_, undefined}, Acc, _Context) -> Acc;
 48    map_search({status, Status}, {Ws,As}, _Context) ->
 49        {As1,N} = arg(Status, As),
 50        {[["mailer_status = $",N]|Ws], As1};
 51    map_search({message_nr, MsgNr}, {Ws,As}, _Context) ->
 52        {As1,N} = arg(MsgNr, As),
 53        {[["message_nr like ($",N," || '%')"]|Ws], As1};
 54    map_search({template, Tpl}, {Ws,As}, _Context) ->
 55        {As1,N} = arg(Tpl, As),
 56        {[["message_template like ($",N," || '%')"]|Ws], As1};
 57    map_search({to, To}, {Ws,As}, _Context) ->
 58        case z_utils:only_digits(To) of
 59            true ->
 60                {As1,N} = arg(list_to_integer(To), As),
 61                {[["to_id = $",N]|Ws], As1};
 62            false ->
 63                {As1,N} = arg(To, As),
 64                {[["envelop_to like ($",N," || '%')"]|Ws], As1}
 65        end;
 66    map_search({from, From}, {Ws,As}, _Context) ->
 67        case z_utils:only_digits(From) of
 68            true ->
 69                {As1,N} = arg(list_to_integer(From), As),
 70                {[["from_id = $",N]|Ws], As1};
 71            false ->
 72                {As1,N} = arg(From, As),
 73                {[["envelop_from like ($",N," || '%')"]|Ws], As1}
 74        end;
 75    map_search({content, RscId}, {Ws,As}, Context) ->
 76        case m_rsc:rid(RscId, Context) of
 77            undefined -> {["content_id = -1"|Ws], As};
 78            Id -> 
 79                {As1,N} = arg(integer_to_list(Id), As),
 80                {[["content_id = $",N]|Ws], As1}
 81        end;
 82    map_search({other, RscId}, {Ws,As}, Context) ->
 83        case m_rsc:rid(RscId, Context) of
 84            undefined -> {["other_id = -1"|Ws], As};
 85            Id -> 
 86                {As1,N} = arg(integer_to_list(Id), As),
 87                {[["other_id = $",N]|Ws], As1}
 88        end.
 89    
 90    
 91    arg(V, As) ->
 92        As1 = [V|As],
 93        N = integer_to_list(length(As1)),
 94        {As1, N}.
 95        
 96
 97install(Context) ->
 98    case z_db:table_exists(log_email, Context) of
 99        true -> 
100            ok;
101        false ->
102            z_db:q("
103                create table log_email (
104                    id bigserial not null,
105                    severity int not null default 1,
106                    message_nr character varying(32),
107                    mailer_status character varying(32),
108                    mailer_message bytea,
109                    mailer_host character varying(128),
110                    envelop_to character varying(128) not null,
111                    envelop_from character varying(128) not null,
112                    to_id int,
113                    from_id int,
114                    content_id int,
115                    other_id int,
116                    message_template character varying(64),
117                    props bytea,
118                    created timestamp with time zone not null default now(), 
119
120                    constraint log_email_pkey primary key (id)
121                )
122            ", Context),
123            Indices = [
124                       {"log_email_severity", "severity, created"},
125                       {"log_email_envelop_to", "envelop_to, created"},
126                       {"log_email_to_id", "to_id, created"},
127                       {"log_email_from_id", "from_id, created"},
128                       {"log_email_content_id", "content_id, created"},
129                       {"log_email_other_id", "other_id, created"},
130                       {"log_email_created", "created"}
131                      ],
132            [ z_db:q("create index "++Name++" on log_email ("++Cols++")", Context) || {Name, Cols} <- Indices ]
133    end.
134