PluginProbe
Plugin Memory Usage / 1.2.3
Plugin Memory Usage v1.2.3
trunk 1.0 1.0.2 1.1.0 1.1.2 1.1.3 1.2.0 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 1.3.0
plugin-memory-usage / plugin-memory-usage.js

plugin-memory-usage.js in Plugin Memory Usage 1.2.3, at plugin-memory-usage.js

315 lines 12.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1
2 var wpmem_ajaxurl = wpmemData.wpmem_ajaxurl;
3 var wpmemNonce = wpmemData.nonce;
4 var lastMemoryUsage = wpmemData.initialMemoryUsage;
5
6 console.log('inside js ');
7
8
9 var pluginMemoryUsage = {};
10
11 document.addEventListener('DOMContentLoaded', function() {
12 var toggleButtons = document.querySelectorAll('.toggle-plugin');
13 toggleButtons.forEach(function(button) {
14 button.addEventListener('click', function() {
15 var plugin = this.dataset.plugin;
16 var action = this.dataset.action;
17 wpmem_togglePlugin(plugin, action, this);
18 });
19 });
20
21 document.getElementById('refresh-memory').addEventListener('click', function() {
22 wpmem_refreshMemoryUsage(null, null);
23 });
24
25
26 wpmem_displayAllPluginHistory();
27
28
29 });
30
31 function wpmem_togglePlugin(plugin, action, button) {
32 var xhr = new XMLHttpRequest();
33 xhr.open('POST', wpmem_ajaxurl, true);
34 xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
35
36 xhr.onreadystatechange = function() {
37 if (xhr.readyState === 4) {
38 console.log('Raw AJAX response:', xhr.responseText);
39 if (xhr.status === 200) {
40 try {
41 var response = JSON.parse(xhr.responseText);
42 if (response.success) {
43 button.textContent = action === 'activate' ? 'Deactivate' : 'Activate';
44 button.dataset.action = action === 'activate' ? 'deactivate' : 'activate';
45 button.closest('li').classList.toggle('plugin-active');
46 button.closest('li').classList.toggle('plugin-inactive');
47 wpmem_refreshMemoryUsage(plugin, action);
48 wpmem_updateAverageMemory(plugin);
49 } else {
50 console.error('Error toggling plugin:', response.data);
51 alert('Error toggling plugin: ' + response.data);
52 }
53 } catch (e) {
54 console.error('Error parsing JSON:', e);
55 console.log('Response that caused the error:', xhr.responseText);
56 alert('Error parsing server response');
57 }
58 } else {
59 console.error('AJAX request failed with status:', xhr.status);
60 console.log('Response text:', xhr.responseText);
61 alert('AJAX request failed');
62 }
63 }
64 };
65
66 xhr.send('action=toggle_plugin&plugin=' + encodeURIComponent(plugin) + '&toggle_action=' + action + '&nonce=' + wpmemNonce);
67 }
68
69
70
71 function wpmem_updateMemoryHistory(plugin, action, memoryDifference) {
72 var historyList = document.getElementById('memory-history-list');
73 var listItem = document.createElement('li');
74 listItem.textContent = wpmem_createMemoryHistoryEntry(plugin, action, memoryDifference);
75 historyList.insertBefore(listItem, historyList.firstChild);
76 }
77
78 function wpmem_updatePluginMemoryUsage(plugin, memoryDifference) {
79 var absDifference = Math.abs(memoryDifference);
80 pluginMemoryUsage[plugin] = (pluginMemoryUsage[plugin] || 0) + absDifference;
81 var pluginItem = document.querySelector('.plugin-list li .toggle-plugin[data-plugin="' + plugin + '"]').closest('li');
82 var memoryBar = pluginItem.querySelector('.plugin-memory-fill');
83 var memoryText = pluginItem.querySelector('.plugin-memory-text');
84 var avgMemorySpan = pluginItem.querySelector('.avg-memory');
85 var percentage = (absDifference / lastMemoryUsage) * 100;
86 percentage = Math.max(0, Math.min(100, percentage)); // Ensure percentage is between 0 and 100
87 memoryBar.style.width = percentage + '%';
88 memoryText.textContent = wpmem_formatMemoryDifference(absDifference);
89
90 // Update average memory
91 var avgMemory = pluginMemoryUsage[plugin] / (1024 * 1024); // Convert to MB
92 avgMemorySpan.textContent = '(' + avgMemory.toFixed(2) + ' MB)';
93 }
94
95
96 function wpmem_createMemoryHistoryEntry(pluginName, action, memoryDifference) {
97 var timestamp = new Date().toLocaleString();
98 if (action === 'increase_memory') {
99 return `${timestamp} - Memory limit increased to ${memoryDifference}`;
100 } else if (action === 'increase_memory_failed') {
101 return `${timestamp} - Failed to increase memory limit: ${memoryDifference}`;
102 } else if (action === 'increase_memory_error') {
103 return `${timestamp} - Error while trying to increase memory limit: ${memoryDifference}`;
104 } else {
105 return `${timestamp} - ${pluginName} (${action}): ${wpmem_formatMemoryDifference(memoryDifference)}`;
106 }
107 }
108
109 function wpmem_formatMemoryDifference(diffBytes) {
110 var absValue = Math.abs(diffBytes);
111 var sign = diffBytes >= 0 ? '+' : '-';
112 var mbValue = absValue / (1024 * 1024); // Convert to MB
113 return `${sign}${mbValue.toFixed(2)} MB`;
114 }
115
116
117
118
119 function wpmem_refreshMemoryUsage(plugin, action) {
120 var refreshButton = document.getElementById('refresh-memory');
121 refreshButton.textContent = 'Refreshing...';
122 refreshButton.disabled = true;
123
124 var xhr = new XMLHttpRequest();
125 xhr.open('POST', wpmem_ajaxurl, true);
126 xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
127
128 xhr.onreadystatechange = function() {
129 if (xhr.readyState === 4) {
130 console.log('Raw AJAX response (refresh):', xhr.responseText);
131 if (xhr.status === 200) {
132 try {
133 var response = JSON.parse(xhr.responseText);
134 if (response.success) {
135 refreshButton.textContent = 'Refresh Memory Usage';
136 refreshButton.disabled = false;
137
138 document.getElementById('current-memory').textContent = response.data.current_memory;
139 document.getElementById('memory-bar-fill').style.width = response.data.percentage + '%';
140 document.getElementById('memory-percentage').textContent = response.data.percentage + '%';
141
142 var memoryDifference = response.data.current_memory_bytes - lastMemoryUsage;
143 lastMemoryUsage = response.data.current_memory_bytes;
144
145 if (plugin && action) {
146 wpmem_updateMemoryHistory(plugin, action, memoryDifference);
147 wpmem_updatePluginMemoryUsage(plugin, memoryDifference);
148 wpmem_fetchAndDisplayPluginHistory(plugin);
149 wpmem_updateAverageMemory(plugin);
150 }
151
152 } else {
153 console.error('Error refreshing memory usage:', response.data);
154 }
155 } catch (e) {
156 console.error('Error parsing JSON:', e);
157 console.log('Response that caused the error:', xhr.responseText);
158 }
159 } else {
160 console.error('AJAX request failed with status:', xhr.status);
161 console.log('Response text:', xhr.responseText);
162 }
163 refreshButton.textContent = 'Refresh Memory Usage';
164 refreshButton.disabled = false;
165 }
166 };
167
168 var data = 'action=refresh_memory_usage&nonce=' + wpmemNonce;
169 if (plugin && action) {
170 data += '&plugin=' + encodeURIComponent(plugin) + '&toggle_action=' + action + '&previous_memory=' + lastMemoryUsage;
171 }
172 xhr.send(data);
173 }
174
175 function wpmem_fetchAndDisplayPluginHistory(plugin) {
176 var xhr = new XMLHttpRequest();
177 xhr.open('POST', wpmem_ajaxurl, true);
178 xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
179
180 xhr.onreadystatechange = function() {
181 if (xhr.readyState === 4 && xhr.status === 200) {
182 var response = JSON.parse(xhr.responseText);
183 if (response.success) {
184 wpmem_displayPluginHistory(plugin, response.data);
185 } else {
186 console.error('Error fetching plugin history:', response.data);
187 }
188 }
189 };
190
191 xhr.send('action=get_plugin_history&plugin=' + encodeURIComponent(plugin) + '&nonce=' + wpmemNonce);
192 }
193
194
195
196 function wpmem_displayPluginHistory(plugin, history) {
197 var pluginItem = document.querySelector('.plugin-list li .toggle-plugin[data-plugin="' + plugin + '"]');
198 if (!pluginItem) return;
199
200 var historyContainer = pluginItem.closest('li').querySelector('.plugin-history-container');
201
202 if (!historyContainer) {
203 historyContainer = document.createElement('div');
204 historyContainer.className = 'plugin-history-container';
205 pluginItem.closest('li').appendChild(historyContainer);
206 }
207
208 historyContainer.innerHTML = '';
209
210 history.slice(0, 3).forEach(function(entry, index) {
211 var historyBar = document.createElement('div');
212 historyBar.className = 'plugin-history-bar';
213 var percentage = (Math.abs(entry.memory_change) / lastMemoryUsage) * 100;
214 percentage = Math.max(0, Math.min(100, percentage));
215 historyBar.style.width = percentage + '%';
216 historyBar.style.backgroundColor = wpmem_getHistoryColor(index);
217 historyBar.title = wpmem_formatMemoryDifference(entry.memory_change) + ' on ' + entry.timestamp;
218 historyContainer.appendChild(historyBar);
219 });
220
221 // Ensure the history container is visible
222 historyContainer.style.display = 'block';
223 }
224
225
226
227
228
229 function wpmem_getHistoryColor(index) {
230 var colors = ['#4CAF50', '#8BC34A', '#CDDC39'];
231 return colors[index] || colors[colors.length - 1];
232 }
233
234
235 function wpmem_displayAllPluginHistory() {
236 var pluginHistory = wpmemData.pluginHistory;
237 for (var plugin in pluginHistory) {
238 if (pluginHistory.hasOwnProperty(plugin)) {
239 wpmem_displayPluginHistory(plugin, pluginHistory[plugin]);
240 }
241 }
242 }
243
244
245
246
247 function wpmem_updateAverageMemory(plugin) {
248 jQuery.ajax({
249 url: wpmem_ajaxurl,
250 type: 'POST',
251 data: {
252 action: 'update_average_memory',
253 plugin_path: plugin,
254 nonce: wpmemNonce
255 },
256 success: function(response) {
257 if (response.success) {
258 var avgMemorySpan = jQuery('.plugin-list li .toggle-plugin[data-plugin="' + plugin + '"]')
259 .closest('li')
260 .find('.avg-memory');
261 avgMemorySpan.text('(' + response.data.avg_memory + ' MB)');
262 } else {
263 console.error('Error updating average memory:', response.data);
264 }
265 },
266 error: function(xhr, status, error) {
267 console.error('AJAX request failed:', status, error);
268 }
269 });
270 }
271
272
273
274
275
276
277
278 jQuery(document).ready(function($) {
279 $('#increase-memory-limit').on('click', function() {
280 $.ajax({
281 url: wpmemData.wpmem_ajaxurl,
282 type: 'POST',
283 data: {
284 action: 'increase_memory_limit',
285 nonce: wpmemData.nonce
286 },
287 success: function(response) {
288 if (response.success) {
289 $('#memory-increase-result').text('Memory limit increased to ' + response.data.new_limit);
290 // Add to memory history
291 wpmem_updateMemoryHistory('System', 'increase_memory', response.data.new_limit);
292 // Refresh the memory usage display
293
294 // Refresh the page after a short delay
295 setTimeout(function() {
296 location.reload();
297 }, 2500);
298
299
300 } else {
301 $('#memory-increase-result').text('Failed to increase memory limit: ' + response.data.message);
302 // Add failure to memory history
303 wpmem_updateMemoryHistory('System', 'increase_memory_failed', response.data.message);
304 }
305 },
306 error: function(xhr, status, error) {
307 console.error('AJAX request failed:', status, error);
308 // Add error to memory history
309 wpmem_updateMemoryHistory('System', 'increase_memory_error', 'AJAX request failed');
310 }
311 });
312 });
313 });
314
315