@@ -16,13 +16,21 @@ var DebugManager = Widget.extend({
1616 xmlDependencies : [ '/web/static/src/xml/debug.xml' ] ,
1717 events : {
1818 "click a[data-action]" : "perform_callback" ,
19+ "click .profiling" : "handle_profiling" ,
20+ "change .profiling_param" : "handle_profiling" ,
1921 } ,
2022 init : function ( ) {
2123 this . _super . apply ( this , arguments ) ;
2224 this . _events = null ;
2325 var debug = odoo . debug ;
2426 this . debug_mode = debug ;
2527 this . debug_mode_help = debug && debug !== '1' ? ' (' + debug + ')' : '' ;
28+ this . profile_session = odoo . session_info && odoo . session_info . profile_session || false ;
29+ this . profile_collectors = odoo . session_info && odoo . session_info . profile_collectors ;
30+ if ( ! Array . isArray ( this . profile_collectors ) ) {
31+ this . profile_collectors = [ 'sql' , 'traces_async' ] ; // use default value when not defined in session.
32+ }
33+ this . profile_params = odoo . session_info && odoo . session_info . profile_params || { } ;
2634 } ,
2735 start : function ( ) {
2836 core . bus . on ( 'rpc:result' , this , function ( req , resp ) {
@@ -53,7 +61,62 @@ var DebugManager = Widget.extend({
5361 console . warn ( "No handler for " , callback ) ;
5462 }
5563 } ,
64+ /**
65+ * Manage profiling actions.
66+ * open_profiling_button should not prevent dropdown from closing
67+ * other actions should keep dropdown open and interact with backend to enable or disable profiling features.
68+ * The main switch, 'enable_profiling' is a special case since it will create or remove a profile session
69+ */
70+ handle_profiling : async function ( evt ) {
71+ const $target = $ ( evt . target ) ;
72+ const target = $target [ 0 ] ;
73+ if ( target . id === 'open_profiling_button' ) {
74+ this . do_action ( 'base.action_menu_ir_profile' ) ;
75+ return ;
76+ }
77+ evt . stopPropagation ( ) ; // prevent dropdown from closing
78+ const kwargs = {
79+ params : { }
80+ } ;
81+ if ( target . nodeName == "LABEL" ) {
82+ return
83+ }
84+ kwargs . profile = $ ( '#enable_profiling' ) [ 0 ] . checked ;
85+ kwargs . collectors = $ ( 'input.profile_switch' ) . toArray ( ) . filter ( i => i . checked ) . map ( i => i . id . replace ( / ^ p r o f i l e _ / , '' ) ) ;
86+ $ ( '.profile_param' ) . toArray ( ) . forEach ( i => kwargs . params [ i . id . replace ( / ^ p r o f i l e _ / , '' ) ] = i . value ) ;
5687
88+ try {
89+ const resp = await this . _rpc ( {
90+ model : 'ir.profile' ,
91+ method : 'set_profiling' ,
92+ kwargs : kwargs ,
93+ } ) ;
94+ this . profile_session = resp . session ;
95+ this . profile_collectors = resp . collectors ;
96+ this . profile_params = resp . params ;
97+ } catch ( e ) {
98+ this . profite_session = false ;
99+ }
100+ this . update_profiling_state ( ) ;
101+ } ,
102+ /**
103+ * Update the profiling dropdown menu state after any change to synchronyze server session and client
104+ * This is mainly usefull to avoid desync in case of multiple tabs
105+ */
106+ update_profiling_state : function ( ) {
107+ const self = this ;
108+ this . $ ( '#enable_profiling' ) [ 0 ] . checked = Boolean ( this . profile_session ) ;
109+ this . $ ( '.profile_switch' ) . each ( function ( ) {
110+ this . checked = ( self . profile_collectors . indexOf ( this . id . replace ( / ^ p r o f i l e _ / , '' ) ) !== - 1 ) ;
111+ const params = self . $ ( '.' + this . id ) ;
112+ params . toggleClass ( 'd-none' , ! this . checked ) ;
113+ params . each ( function ( ) {
114+ const params_input = $ ( this ) . find ( '.profile_param' ) [ 0 ] ;
115+ params_input . value = self . profile_params [ params_input . id . replace ( / ^ p r o f i l e _ / , '' ) ] || '' ;
116+ } ) ;
117+ } ) ;
118+ this . $profiling_items . toggleClass ( 'd-none' , ! this . profile_session ) ;
119+ } ,
57120 _debug_events : function ( events ) {
58121 if ( ! this . _events ) {
59122 return ;
@@ -73,6 +136,9 @@ var DebugManager = Widget.extend({
73136 . append ( QWeb . render ( 'WebClient.DebugManager.Global' , {
74137 manager : this ,
75138 } ) ) ;
139+ this . $profiling_items = this . $ ( ".profiling_items" ) ;
140+
141+ this . update_profiling_state ( ) ;
76142 return Promise . resolve ( ) ;
77143 } ,
78144 split_assets : function ( ) {
0 commit comments