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