BangleApps/apps/storageanalyzer/custom.html

139 lines
4.7 KiB
HTML

<html>
<head>
<link rel="stylesheet" href="../../css/spectre.min.css">
</head>
<body>
<script src="../../core/lib/customize.js"></script>
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<!-- Toggle Buttons -->
<div class="btn-group" style="margin: 1em; display: flex; justify-content: center;">
<button id="tableButton" class="btn btn-primary">View Table</button>
<button id="pieChartButton" class="btn">View Pie Chart</button>
</div>
<!-- Table View -->
<div id="storageTable"></div>
<!-- Chart View -->
<div id="storagePieChart" style="display: none; justify-content: center;">
<div id="piechart" style="width: 100%; max-width: 600px; height: 400px;"></div>
</div>
<script>
let globalApps = [];
// Called by the app loader
function onInit(device) {
Util.showModal("Reading Storage...");
Puck.eval(`require("Storage").list(/\\.info$/).map(appInfoName => {
let appInfo = require("Storage").readJSON(appInfoName,1)||{};
var fileSize = 0, dataSize = 0;
appInfo.files.split(",").forEach(f => fileSize += require("Storage").read(f).length);
var data = (appInfo.data||"").split(";");
function wildcardToRegexp(wc) {
return new RegExp("^"+wc.replaceAll(".","\\\\.").replaceAll("?",".*")+"$");
}
if (data[0]) data[0].split(",").forEach(wc => {
require("Storage").list(wildcardToRegexp(wc), {sf:false}).forEach(f => {
dataSize += require("Storage").read(f).length
});
});
if (data[1]) data[1].split(",").forEach(wc => {
require("Storage").list(wildcardToRegexp(wc), {sf:true}).forEach(f => {
dataSize += require("Storage").open(f,"r").getLength();
});
});
return [appInfo.id, fileSize, dataSize];
})`, function(apps) {
Util.hideModal();
globalApps = apps.sort((a,b) => (b[1]+b[2]) - (a[1]+a[2]));
if (apps.length === 0) {
document.getElementById("storageTable").innerHTML = "<p>No apps found</p>";
return;
}
drawTable();
});
}
function drawTable() {
document.getElementById("storageTable").innerHTML = `
<table class="table table-striped">
<thead>
<tr>
<th>App</th>
<th>Code (kb)</th>
<th>Data (kb)</th>
<th>Total (kb)</th>
</tr>
</thead>
<tbody>
${globalApps.map(app => `
<tr>
<td>${app[0]}</td>
<td>${(app[1]/1000).toFixed(1)}</td>
<td>${(app[2]/1000).toFixed(1)}</td>
<td>${((app[1]+app[2])/1000).toFixed(1)}</td>
</tr>`).join("")}
</tbody>
</table>`;
}
function drawChart() {
if (globalApps.length === 0) return;
const chartData = [
['App', 'Total Size']
].concat(globalApps.map(app => [app[0], (app[1] + app[2])/1000]));
const data = google.visualization.arrayToDataTable(chartData);
const options = {
title: 'App Storage Breakdown (in KBs)',
is3D: false,
chartArea: {
width: '90%',
height: '80%'
},
legend: {
position: 'bottom'
}
};
const chart = new google.visualization.PieChart(document.getElementById('piechart'));
chart.draw(data, options);
}
// Load Google Charts and prepare initial chart (in background)
google.charts.load('current', {'packages':['corechart']});
google.charts.setOnLoadCallback(() => {
drawChart();
});
// Toggle view buttons
document.getElementById("pieChartButton").addEventListener("click", function () {
document.getElementById("storageTable").style.display = "none";
document.getElementById("storagePieChart").style.display = "flex";
drawChart();
this.classList.add("btn-primary");
document.getElementById("tableButton").classList.remove("btn-primary");
});
document.getElementById("tableButton").addEventListener("click", function () {
document.getElementById("storageTable").style.display = "block";
document.getElementById("storagePieChart").style.display = "none";
drawTable();
this.classList.add("btn-primary");
document.getElementById("pieChartButton").classList.remove("btn-primary");
});
// Register as app loader's entry point
window.onInit = onInit;
</script>
</body>
</html>