/modules/mod_development/resources/resource_wmtrace_conf.erl

http://github.com/zotonic/zotonic · Erlang · 130 lines · 82 code · 17 blank · 31 comment · 1 complexity · b97ea82049afd0165703f299038666c4 MD5 · raw file

  1. %% @author Atilla Erdodi <atilla@maximonster.com>
  2. %% @copyright 2010 Maximonster Interactive Things
  3. %% Date: 2010-10-20
  4. %% @doc Resource to configure tracing of webmachine requests.
  5. %% Copyright 2010 Maximonster Interactive Things
  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. %%
  19. %% Resource
  20. %%
  21. -module(resource_wmtrace_conf).
  22. -export([
  23. is_authorized/2,
  24. event/2
  25. ]).
  26. -include_lib("resource_html.hrl").
  27. -include_lib("webmachine_logger.hrl").
  28. %% GLOBAL
  29. %% only 5xx
  30. %% 4xx and 5xx
  31. %% PER RESOURCE
  32. %% always
  33. is_authorized(ReqData, Context) ->
  34. z_acl:wm_is_authorized(use, mod_development, ReqData, Context).
  35. event(#submit{message={add, _Args}}, Context) ->
  36. case z_acl:is_allowed(use, mod_development, Context) of
  37. true ->
  38. Res = z_context:get_q("resource", Context),
  39. case ets:lookup(?WMTRACE_CONF_TBL, Res) of
  40. [] ->
  41. Eagerness = immediate,
  42. ets:insert(?WMTRACE_CONF_TBL, {list_to_atom(Res), Eagerness}),
  43. ok;
  44. _ ->
  45. already_added
  46. end,
  47. z_render:wire({reload, []}, Context);
  48. false ->
  49. z_render:growl("You don't have permission to change tracing settings.", Context)
  50. end;
  51. event(#postback{message={set_global, _Args}}, Context) ->
  52. case z_acl:is_allowed(use, mod_development, Context) of
  53. true ->
  54. Policy = list_to_atom(z_context:get_q("triggervalue", Context)),
  55. case Policy of
  56. disable ->
  57. ets:delete(?WMTRACE_CONF_TBL, trace_global);
  58. Policy_ ->
  59. ets:delete(?WMTRACE_CONF_TBL, trace_global),
  60. ets:insert(?WMTRACE_CONF_TBL, {trace_global, Policy_})
  61. end,
  62. z_render:growl("Changed global resource tracing setting.", Context);
  63. false ->
  64. z_render:growl("You don't have permission to change tracing settings.", Context)
  65. end;
  66. event(#postback{message={edit, _Args}}, Context) ->
  67. %% not used yet, since it is only meaningful if we need
  68. %% more trace options for per-resource tracing
  69. % Res = proplists:get_value(resource, Args),
  70. % NewOpts = {},
  71. % ets:delete(?WMTRACE_CONF_TBL, Res),
  72. % ets:insert(?WMTRACE_CONF_TBL, {Res, NewOpts}),
  73. Context;
  74. event(#postback{message={delete, Args}}, Context) ->
  75. case z_acl:is_allowed(use, mod_development, Context) of
  76. true ->
  77. Res = proplists:get_value(res_to_del, Args),
  78. ets:delete(?WMTRACE_CONF_TBL, Res),
  79. z_render:wire({reload, []}, Context);
  80. false ->
  81. z_render:growl("You don't have permission to change tracing settings.", Context)
  82. end.
  83. html(Context) ->
  84. Resources_ = get_resources(Context),
  85. TraceConf_ = get_trace_conf(?WMTRACE_CONF_TBL, ets:first(?WMTRACE_CONF_TBL), []),
  86. TraceConf = [Res || {Res, _Eagerness} <- TraceConf_],
  87. Resources = Resources_ -- TraceConf,
  88. TraceGlobal = case ets:lookup(?WMTRACE_CONF_TBL, trace_global) of
  89. [] -> "disabled";
  90. [{trace_global, TrGl}] -> atom_to_list(TrGl)
  91. end,
  92. Vars = [{trace_global, TraceGlobal}, {trace_conf, TraceConf},
  93. {res, Resources}, {page_admin_wmtrace_conf, true}],
  94. Html = z_template:render("wmtrace_conf.tpl", Vars, Context),
  95. z_context:output(Html, Context).
  96. get_trace_conf(Tbl, trace_dir, Acc) ->
  97. get_trace_conf(Tbl, ets:next(Tbl, trace_dir), Acc);
  98. get_trace_conf(Tbl, trace_global, Acc) ->
  99. get_trace_conf(Tbl, ets:next(Tbl, trace_global), Acc);
  100. get_trace_conf(_Tbl, '$end_of_table', Acc) ->
  101. Acc;
  102. get_trace_conf(Tbl, Key, Acc) ->
  103. [Res] = ets:lookup(Tbl, Key),
  104. get_trace_conf(Tbl, ets:next(Tbl, Key), [Res | Acc]).
  105. get_resources(Context) ->
  106. {_Host, _Hostname, _Streamhost, _Smtphost, _Hostaliases, _Redirect, SiteDispatch} = z_dispatcher:dispatchinfo(Context),
  107. Resources = lists:foldl(fun({_, _, Resource, _}, ResList) ->
  108. case lists:member(Resource, ResList) of
  109. false -> [Resource | ResList];
  110. true -> ResList
  111. end
  112. end, [], SiteDispatch),
  113. lists:reverse(Resources).