File size: 22,432 Bytes
fa5b595 970aabc fa5b595 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 | <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Pest Geospatial Analytics | AgriTech Suite</title>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Outfit:wght@300;400;500;600;700&display=swap" rel="stylesheet">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.1/font/bootstrap-icons.css">
<style>
:root {
--primary-green: #1a5d3a;
--accent-green: #198754;
--light-bg: #f8f9fa;
--text-dark: #212529;
--card-shadow: 0 10px 40px rgba(0, 0, 0, 0.05);
}
body {
font-family: 'Outfit', sans-serif;
background-color: var(--light-bg);
color: var(--text-dark);
min-height: 100vh;
display: flex;
flex-direction: column;
}
.analytics-header {
background-color: var(--primary-green);
color: white;
padding: 3rem 1rem 6rem;
text-align: center;
border-bottom-left-radius: 50% 20px;
border-bottom-right-radius: 50% 20px;
margin-bottom: 2rem;
}
.header-title {
font-weight: 700;
font-size: 2rem;
text-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
}
.dashboard-container {
margin-top: -5rem;
padding: 0 2rem 3rem;
flex: 1;
display: flex;
justify-content: center;
}
.dashboard-card {
background: white;
border-radius: 24px;
box-shadow: var(--card-shadow);
width: 100%;
max-width: 1200px;
overflow: hidden;
display: grid;
grid-template-columns: 320px 1fr;
min-height: 600px;
border: 1px solid #eef0f3;
}
.sidebar-controls {
background: #fdfdfd;
border-right: 1px solid #f1f3f5;
padding: 2rem;
display: flex;
flex-direction: column;
}
.sidebar-title {
font-size: 1rem;
font-weight: 700;
color: var(--primary-green);
margin-bottom: 2rem;
display: flex;
align-items: center;
gap: 0.5rem;
padding-bottom: 1rem;
border-bottom: 2px solid #f0f0f0;
}
.form-group {
margin-bottom: 1.5rem;
}
.form-label-custom {
font-size: 0.8rem;
font-weight: 600;
color: #6c757d;
text-transform: uppercase;
letter-spacing: 0.5px;
margin-bottom: 0.5rem;
display: block;
}
.form-select-custom {
width: 100%;
border: 1px solid #e2e8f0;
border-radius: 10px;
padding: 0.8rem 1rem;
font-size: 0.95rem;
font-weight: 500;
background-color: white;
transition: all 0.2s;
}
.form-select-custom:focus {
border-color: var(--accent-green);
box-shadow: 0 0 0 4px rgba(25, 135, 84, 0.1);
outline: none;
}
.form-select-custom option:disabled {
color: #adb5bd;
background-color: #f8f9fa;
}
.btn-visualize {
background-color: var(--accent-green);
color: white;
border: none;
border-radius: 10px;
padding: 1rem;
font-weight: 600;
width: 100%;
display: flex;
align-items: center;
justify-content: center;
gap: 0.5rem;
margin-top: auto;
transition: all 0.2s;
box-shadow: 0 4px 15px rgba(25, 135, 84, 0.2);
}
.btn-visualize:hover:not(:disabled) {
background-color: #146c43;
transform: translateY(-2px);
box-shadow: 0 6px 20px rgba(25, 135, 84, 0.3);
}
.btn-visualize:disabled {
opacity: 0.55;
cursor: not-allowed;
}
.map-viewport {
background-color: #f8fafc;
padding: 2rem;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
position: relative;
}
.map-frame {
background: white;
padding: 1rem;
border-radius: 16px;
box-shadow: 0 10px 30px rgba(0, 0, 0, 0.08);
max-width: 100%;
}
.map-image {
max-width: 100%;
max-height: 550px;
border-radius: 8px;
display: block;
}
.result-meta {
position: absolute;
top: 1.5rem;
left: 1.5rem;
background: white;
padding: 0.5rem 1rem;
border-radius: 50px;
box-shadow: 0 4px 15px rgba(0, 0, 0, 0.05);
display: flex;
gap: 1rem;
z-index: 5;
font-size: 0.85rem;
font-weight: 500;
color: var(--text-dark);
}
.meta-item {
display: flex;
align-items: center;
gap: 0.5rem;
}
.meta-item i {
color: var(--accent-green);
}
.empty-state {
text-align: center;
color: #adb5bd;
}
.empty-icon {
font-size: 4rem;
margin-bottom: 1rem;
color: #e9ecef;
}
/* Status badges inside dropdowns */
.week-loading-badge {
display: inline-flex;
align-items: center;
gap: 0.4rem;
font-size: 0.75rem;
color: #6c757d;
margin-top: 0.3rem;
}
.availability-hint {
font-size: 0.72rem;
color: #adb5bd;
margin-top: 0.25rem;
}
@media (max-width: 992px) {
.dashboard-card {
grid-template-columns: 1fr;
}
.sidebar-controls {
border-right: none;
border-bottom: 1px solid #f1f3f5;
}
.map-image {
max-height: 400px;
}
}
#loadingOverlay {
position: absolute;
inset: 0;
background: rgba(255, 255, 255, 0.9);
display: none;
justify-content: center;
align-items: center;
z-index: 50;
border-radius: 20px;
}
/* Spinner inside dropdowns while checking */
.select-spinner {
position: relative;
}
.select-spinner::after {
content: '';
position: absolute;
right: 2.5rem;
top: 50%;
transform: translateY(-50%);
width: 14px;
height: 14px;
border: 2px solid #dee2e6;
border-top-color: var(--accent-green);
border-radius: 50%;
animation: spin 0.6s linear infinite;
}
@keyframes spin {
from { transform: translateY(-50%) rotate(0deg); }
to { transform: translateY(-50%) rotate(360deg); }
}
</style>
</head>
<body>
<div class="analytics-header">
<h1 class="header-title">Geospatial Intelligence Dashboard</h1>
<p class="opacity-75">Analysis of pest distribution patterns over space and time</p>
</div>
<div class="dashboard-container">
<div class="dashboard-card">
<aside class="sidebar-controls">
<div class="sidebar-title">
<i class="bi bi-sliders2"></i> FILTER PARAMETERS
</div>
<form method="GET" action="/" id="analyticsForm">
<div class="form-group">
<label class="form-label-custom">Select Crop</label>
<select id="crop" name="crop" class="form-select-custom" onchange="onCropChange('')">
<option value="">Choose Crop...</option>
{% for c in crops %}
<option value="{{ c }}" {% if selected_crop==c %}selected{% endif %}>{{ c }}</option>
{% endfor %}
</select>
</div>
<div class="form-group">
<label class="form-label-custom">Select Pest</label>
<select id="pest" name="pest" class="form-select-custom" onchange="onPestOrYearChange()">
<option value="">Choose Pest...</option>
</select>
</div>
<div class="row">
<div class="col-6 form-group">
<label class="form-label-custom">Year</label>
<select id="year" name="year" class="form-select-custom" onchange="onPestOrYearChange()">
<option value="">Year...</option>
{% for y in years %}
<option value="{{ y }}" {% if selected_year==y %}selected{% endif %}>{{ y }}</option>
{% endfor %}
</select>
</div>
<div class="col-6 form-group">
<label class="form-label-custom">Week</label>
<select id="week" name="week" class="form-select-custom" onchange="onWeekChange()">
<option value="">Week...</option>
{% if selected_week %}
<option value="{{ selected_week }}" selected>Week {{ selected_week }}</option>
{% endif %}
</select>
<span id="weekHint" class="availability-hint"></span>
</div>
</div>
<div class="form-group mb-5">
<label class="form-label-custom">Analysis Metric</label>
<select id="param" name="param" class="form-select-custom">
<option value="">Select Metric...</option>
{% for code, label in params.items() %}
<option value="{{ code }}" {% if selected_param==code %}selected{% endif %}>{{ label }}</option>
{% endfor %}
</select>
<span id="paramHint" class="availability-hint"></span>
</div>
<button type="submit" class="btn-visualize" id="submitBtn">
Generate Map <i class="bi bi-arrow-right-circle"></i>
</button>
</form>
</aside>
<main class="map-viewport">
<div id="loadingOverlay">
<div class="text-center">
<div class="spinner-border text-success mb-3" role="status"></div>
<h5 class="text-muted">Rendering Geospatial Data...</h5>
</div>
</div>
{% if image_url %}
<div class="result-meta">
<div class="meta-item"><i class="bi bi-calendar-event"></i> {{ selected_year }} (W{{ selected_week }})</div>
<div class="d-none d-md-flex meta-item text-muted">|</div>
<div class="meta-item"><i class="bi bi-bug"></i> {{ selected_pest }}</div>
</div>
<div class="map-frame" id="mapContainer">
<img src="{{ image_url }}" alt="Heatmap Result" class="map-image" onerror="handleImageError(this)">
<div id="dataNotAvailable" class="p-5 text-center d-none">
<i class="bi bi-database-x fs-1 text-danger mb-3"></i>
<h5 class="text-secondary">Data Unavailable</h5>
<p class="text-muted small">No records found for this parameter combination.</p>
</div>
</div>
{% elif not data_available %}
<div class="empty-state">
<div class="empty-icon"><i class="bi bi-database-x text-danger"></i></div>
<h4 class="text-secondary">Data Unavailable</h4>
<p class="text-muted">No image data found for the selected combination.<br>Try a different week, year, or metric.</p>
</div>
{% else %}
<div class="empty-state">
<div class="empty-icon"><i class="bi bi-map"></i></div>
<h4>Map Visualization</h4>
<p>Configure filters on the left to generate insights.</p>
</div>
{% endif %}
</main>
</div>
</div>
<!-- Server-side data passed via data attributes β keeps <script> Jinja-free -->
<div id="appData"
data-crop-to-pests='{{ crop_to_pests | tojson }}'
data-init-crop="{{ selected_crop | e }}"
data-init-pest="{{ selected_pest | e }}"
data-init-year="{{ selected_year | e }}"
data-init-week="{{ selected_week | e }}"
data-init-param="{{ selected_param | e }}"
style="display:none"
></div>
<script>
const _appData = document.getElementById('appData').dataset;
const cropToPests = JSON.parse(_appData.cropToPests);
// Server-rendered selections (used to restore state after JS rebuilds dropdowns)
const INIT_CROP = _appData.initCrop;
const INIT_PEST = _appData.initPest;
const INIT_YEAR = _appData.initYear;
const INIT_WEEK = _appData.initWeek;
const INIT_PARAM = _appData.initParam;
// Track in-flight availability request so we can cancel stale results
let availabilityController = null;
// ββ Crop changed β repopulate pest dropdown ββββββββββββββββββββββββββββββ
function onCropChange(restorePest) {
const crop = document.getElementById("crop").value;
const pestSelect = document.getElementById("pest");
pestSelect.innerHTML = '<option value="">Choose Pest...</option>';
if (crop && cropToPests[crop]) {
cropToPests[crop].forEach(p => {
const opt = document.createElement("option");
opt.value = p;
opt.textContent = p;
pestSelect.appendChild(opt);
});
}
// Restore a previously selected pest if requested (page-load restore)
if (restorePest && restorePest !== "") {
pestSelect.value = restorePest;
}
clearWeeks();
clearParamAvailability();
// Only fetch weeks if both pest and year are already set
const year = document.getElementById("year").value;
if (crop && pestSelect.value && year) {
fetchWeeks();
}
}
// ββ Pest or Year changed β refresh weeks βββββββββββββββββββββββββββββββββ
function onPestOrYearChange() {
clearWeeks();
clearParamAvailability();
const crop = document.getElementById("crop").value;
const pest = document.getElementById("pest").value;
const year = document.getElementById("year").value;
if (crop && pest && year) fetchWeeks();
}
// ββ Week changed β check param availability βββββββββββββββββββββββββββββββ
function onWeekChange() {
clearParamAvailability();
const crop = document.getElementById("crop").value;
const pest = document.getElementById("pest").value;
const year = document.getElementById("year").value;
const week = document.getElementById("week").value;
if (crop && pest && year && week) checkParamAvailability(crop, pest, year, week);
}
// ββ Fetch available weeks from backend ββββββββββββββββββββββββββββββββββββ
function fetchWeeks(restoreWeek) {
const crop = document.getElementById("crop").value;
const pest = document.getElementById("pest").value;
const year = document.getElementById("year").value;
const weekSelect = document.getElementById("week");
const hint = document.getElementById("weekHint");
weekSelect.innerHTML = '<option value="">Checking availability...</option>';
weekSelect.disabled = true;
hint.textContent = "Scanning weeks with data...";
hint.style.color = "#6c757d";
fetch(`/fetch_weeks?crop=${encodeURIComponent(crop)}&pest=${encodeURIComponent(pest)}&year=${encodeURIComponent(year)}`)
.then(res => {
if (!res.ok) throw new Error("Network response was not ok");
return res.json();
})
.then(data => {
weekSelect.innerHTML = '<option value="">Week...</option>';
weekSelect.disabled = false;
const weeks = Array.isArray(data.weeks) ? data.weeks : [];
if (weeks.length > 0) {
weeks.forEach(w => {
const opt = document.createElement("option");
opt.value = String(w);
opt.textContent = `Week ${w}`;
weekSelect.appendChild(opt);
});
hint.textContent = `${weeks.length} week(s) with data available`;
hint.style.color = "#198754";
// Restore a previously selected week (page-load) or keep current selection
const target = restoreWeek || INIT_WEEK;
if (target !== "" && weeks.map(String).includes(String(target))) {
weekSelect.value = String(target);
}
} else {
hint.textContent = "No data found for this combination";
hint.style.color = "#dc3545";
}
// If a week ended up selected, check param availability
if (weekSelect.value) {
checkParamAvailability(crop, pest, year, weekSelect.value);
}
})
.catch(() => {
weekSelect.innerHTML = '<option value="">Week...</option>';
weekSelect.disabled = false;
hint.textContent = "Could not load weeks β check connection";
hint.style.color = "#dc3545";
});
}
// ββ Check which params are available for selected combination βββββββββββββ
function checkParamAvailability(crop, pest, year, week) {
const paramSelect = document.getElementById("param");
const hint = document.getElementById("paramHint");
const submitBtn = document.getElementById("submitBtn");
// Abort any previous in-flight request
if (availabilityController) availabilityController.abort();
availabilityController = new AbortController();
hint.textContent = "Checking metric availability...";
hint.style.color = "#6c757d";
submitBtn.disabled = true;
fetch(
`/check_availability?crop=${encodeURIComponent(crop)}&pest=${encodeURIComponent(pest)}&year=${encodeURIComponent(year)}&week=${encodeURIComponent(week)}`,
{ signal: availabilityController.signal }
)
.then(res => {
if (!res.ok) throw new Error("Network response was not ok");
return res.json();
})
.then(data => {
availabilityController = null;
const availability = data.availability || {};
let availableCount = 0;
Array.from(paramSelect.options).forEach(opt => {
if (!opt.value) return; // skip placeholder option
const isAvailable = availability[opt.value] === true;
opt.disabled = !isAvailable;
// Keep label clean β strip any stale "(unavailable)" suffix first
opt.textContent = opt.textContent.replace(/\s*\(unavailable\)$/, "");
if (isAvailable) {
availableCount++;
} else {
opt.textContent += " (unavailable)";
// Auto-deselect if this option was selected
if (opt.selected) {
opt.selected = false;
}
}
});
// If the previously selected param is now deselected, reset to placeholder
if (paramSelect.selectedIndex > 0 && paramSelect.options[paramSelect.selectedIndex].disabled) {
paramSelect.value = "";
}
if (availableCount === 0) {
hint.textContent = "No metrics available for this week";
hint.style.color = "#dc3545";
submitBtn.disabled = true;
} else {
hint.textContent = `${availableCount} metric(s) available`;
hint.style.color = "#198754";
submitBtn.disabled = false;
}
})
.catch(err => {
if (err.name === "AbortError") return; // stale request β ignore
availabilityController = null;
hint.textContent = "";
submitBtn.disabled = false;
});
}
// ββ Helpers βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
function clearWeeks() {
const weekSelect = document.getElementById("week");
weekSelect.innerHTML = '<option value="">Week...</option>';
weekSelect.disabled = false;
const hint = document.getElementById("weekHint");
hint.textContent = "";
}
function clearParamAvailability() {
// Abort any in-flight availability check
if (availabilityController) {
availabilityController.abort();
availabilityController = null;
}
const paramSelect = document.getElementById("param");
Array.from(paramSelect.options).forEach(opt => {
opt.disabled = false;
opt.textContent = opt.textContent.replace(/\s*\(unavailable\)$/, "");
});
document.getElementById("paramHint").textContent = "";
document.getElementById("submitBtn").disabled = false;
}
function handleImageError(img) {
img.style.display = 'none';
const unavailableEl = document.getElementById('dataNotAvailable');
if (unavailableEl) unavailableEl.classList.remove('d-none');
}
// ββ Form submit guard βββββββββββββββββββββββββββββββββββββββββββββββββββββ
document.getElementById('analyticsForm').addEventListener('submit', (e) => {
const fields = ['crop', 'pest', 'year', 'week', 'param'];
const missing = fields.filter(id => !document.getElementById(id).value);
if (missing.length > 0) {
e.preventDefault();
alert(`Please select: ${missing.join(', ')}`);
return;
}
document.getElementById('loadingOverlay').style.display = 'flex';
});
// ββ Page-load restore βββββββββββββββββββββββββββββββββββββββββββββββββββββ
window.addEventListener('DOMContentLoaded', () => {
// Step 1: restore crop selection (already set by server-rendered HTML)
// Step 2: rebuild pest dropdown and restore pest selection
onCropChange(INIT_PEST);
// Step 3: restore year (already set by server-rendered HTML)
// Step 4: fetch weeks and restore week + param selections
if (INIT_CROP && INIT_PEST && INIT_YEAR) {
fetchWeeks(INIT_WEEK);
}
// Restore param selection after availability check completes (handled inside fetchWeeks β checkParamAvailability)
// But if no week is set yet, at least restore the param visually
if (INIT_PARAM) {
document.getElementById("param").value = INIT_PARAM;
}
});
</script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>
|