add(worker)

This commit is contained in:
kevin 2025-06-27 11:16:24 -04:00
parent 3849ba2b10
commit d55062679a
3 changed files with 82 additions and 3 deletions

View File

@ -24,15 +24,38 @@ $table = isset($_GET['table']) ? $_GET['table'] : '';
<script src="../js/popper.min.js"></script>
</head>
<script>
const table = "<?php echo htmlspecialchars($table); ?>";
if (window.Worker) {
const statsWorker = new Worker('stats.worker.js');
statsWorker.postMessage({
table: table
});
statsWorker.onmessage = function(e) {
const stats = e.data;
document.getElementById('stats').innerHTML = `
<span class="badge bg-info text-dark me-2">
<b>Instruction</b> - Min: ${stats.instruction.min}, Max: ${stats.instruction.max}, Prom: ${stats.instruction.avg}
</span>
<span class="badge bg-info text-dark me-2">
<b>Input</b> - Min: ${stats.input.min}, Max: ${stats.input.max}, Prom: ${stats.input.avg}
</span>
<span class="badge bg-info text-dark">
<b>Output</b> - Min: ${stats.output.min}, Max: ${stats.output.max}, Prom: ${stats.output.avg}
</span>
`;
};
}
</script>
<body style="background-color:#fdfdfd;">
<br>
<h1 align="center" style="color:color(srgb 0.52 0.59 0.7);font-weight:bold;">Datatables server side processing with PHP and MYSQL </h1>
<br>
<div class="container">
<div class="mb-3">
<div class="mb-3 d-flex align-items-center gap-3">
<a href="add_dataset.php?table=<?php echo htmlspecialchars($table); ?>" class="btn btn-primary">
<i class="bi bi-plus-circle"></i> Agregar Dataset
</a>
<div id="stats" class="ms-3"></div>
</div>
<table id="example" class="display" style="width:100%">
<thead>

View File

@ -0,0 +1,12 @@
self.onmessage = function(e) {
const table = e.data.table;
function fetchStats() {
fetch(`stats_worker.php?table=${encodeURIComponent(table)}`)
.then(response => response.json())
.then(data => {
self.postMessage(data);
setTimeout(fetchStats, 5000); // Consulta cada 5 segundos
});
}
fetchStats();
};

View File

@ -0,0 +1,44 @@
<?php
include("../db.php");
$table = isset($_GET['table']) ? $_GET['table'] : '';
$result = [
'instruction' => ['min' => '-', 'max' => '-', 'avg' => '-'],
'input' => ['min' => '-', 'max' => '-', 'avg' => '-'],
'output' => ['min' => '-', 'max' => '-', 'avg' => '-']
];
if ($table && preg_match('/^[a-zA-Z0-9_]+$/', $table)) {
$sql = "SELECT
MIN(CHAR_LENGTH(instruction)) as min_instruction,
MAX(CHAR_LENGTH(instruction)) as max_instruction,
AVG(CHAR_LENGTH(instruction)) as avg_instruction,
MIN(CHAR_LENGTH(input)) as min_input,
MAX(CHAR_LENGTH(input)) as max_input,
AVG(CHAR_LENGTH(input)) as avg_input,
MIN(CHAR_LENGTH(output)) as min_output,
MAX(CHAR_LENGTH(output)) as max_output,
AVG(CHAR_LENGTH(output)) as avg_output
FROM `$table`";
$res = mysqli_query($conn, $sql);
if ($res && $row = mysqli_fetch_assoc($res)) {
$result['instruction'] = [
'min' => $row['min_instruction'] !== null ? $row['min_instruction'] : '-',
'max' => $row['max_instruction'] !== null ? $row['max_instruction'] : '-',
'avg' => $row['avg_instruction'] !== null ? round($row['avg_instruction'], 2) : '-'
];
$result['input'] = [
'min' => $row['min_input'] !== null ? $row['min_input'] : '-',
'max' => $row['max_input'] !== null ? $row['max_input'] : '-',
'avg' => $row['avg_input'] !== null ? round($row['avg_input'], 2) : '-'
];
$result['output'] = [
'min' => $row['min_output'] !== null ? $row['min_output'] : '-',
'max' => $row['max_output'] !== null ? $row['max_output'] : '-',
'avg' => $row['avg_output'] !== null ? round($row['avg_output'], 2) : '-'
];
}
}
header('Content-Type: application/json');
echo json_encode($result);