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