PageRenderTime 35ms CodeModel.GetById 10ms app.highlight 20ms RepoModel.GetById 1ms app.codeStats 0ms

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