|
1 | | -console.log("Hi, I am a script loaded from statistics module"); |
| 1 | +// Dashboard chart bootstrap. The server renders the series as JSON inside |
| 2 | +// `<script id="dashboard-data">` so this module has a single parse step and |
| 3 | +// no template-interpolation inside JS (safer, easier to cache, no CSP hacks). |
| 4 | + |
| 5 | +import Chart from "chart.js/auto"; |
| 6 | + |
| 7 | +function readData() { |
| 8 | + const el = document.getElementById("dashboard-data"); |
| 9 | + if (!el) return null; |
| 10 | + try { |
| 11 | + return JSON.parse(el.textContent || "{}"); |
| 12 | + } catch (e) { |
| 13 | + console.error("dashboard: bad JSON payload", e); |
| 14 | + return null; |
| 15 | + } |
| 16 | +} |
| 17 | + |
| 18 | +function renderUploads(data) { |
| 19 | + const canvas = document.getElementById("uploadsChart"); |
| 20 | + if (!canvas || !data.months?.length) return; |
| 21 | + new Chart(canvas.getContext("2d"), { |
| 22 | + type: "bar", |
| 23 | + data: { |
| 24 | + labels: data.months, |
| 25 | + datasets: [ |
| 26 | + { |
| 27 | + label: "Datasets uploaded", |
| 28 | + data: data.uploads || [], |
| 29 | + backgroundColor: "rgba(54, 153, 255, 0.2)", |
| 30 | + borderColor: "rgba(54, 153, 255, 1)", |
| 31 | + borderWidth: 2, |
| 32 | + borderRadius: 4, |
| 33 | + }, |
| 34 | + ], |
| 35 | + }, |
| 36 | + options: { |
| 37 | + responsive: true, |
| 38 | + plugins: { legend: { display: false } }, |
| 39 | + scales: { y: { beginAtZero: true, ticks: { stepSize: 1 } } }, |
| 40 | + }, |
| 41 | + }); |
| 42 | +} |
| 43 | + |
| 44 | +function renderActivity(data) { |
| 45 | + const canvas = document.getElementById("activityChart"); |
| 46 | + if (!canvas || !data.months?.length) return; |
| 47 | + new Chart(canvas.getContext("2d"), { |
| 48 | + type: "line", |
| 49 | + data: { |
| 50 | + labels: data.months, |
| 51 | + datasets: [ |
| 52 | + { |
| 53 | + label: "Downloads", |
| 54 | + data: data.downloads || [], |
| 55 | + backgroundColor: "rgba(246, 78, 96, 0.1)", |
| 56 | + borderColor: "rgba(246, 78, 96, 1)", |
| 57 | + borderWidth: 2, |
| 58 | + tension: 0.3, |
| 59 | + fill: true, |
| 60 | + pointRadius: 4, |
| 61 | + }, |
| 62 | + { |
| 63 | + label: "Views", |
| 64 | + data: data.views || [], |
| 65 | + backgroundColor: "rgba(80, 205, 137, 0.1)", |
| 66 | + borderColor: "rgba(80, 205, 137, 1)", |
| 67 | + borderWidth: 2, |
| 68 | + tension: 0.3, |
| 69 | + fill: true, |
| 70 | + pointRadius: 4, |
| 71 | + }, |
| 72 | + ], |
| 73 | + }, |
| 74 | + options: { |
| 75 | + responsive: true, |
| 76 | + plugins: { legend: { display: true, position: "top" } }, |
| 77 | + scales: { y: { beginAtZero: true, ticks: { stepSize: 1 } } }, |
| 78 | + }, |
| 79 | + }); |
| 80 | +} |
| 81 | + |
| 82 | +function init() { |
| 83 | + const data = readData(); |
| 84 | + if (!data) return; |
| 85 | + renderUploads(data); |
| 86 | + renderActivity(data); |
| 87 | +} |
| 88 | + |
| 89 | +if (document.readyState === "loading") { |
| 90 | + document.addEventListener("DOMContentLoaded", init, { once: true }); |
| 91 | +} else { |
| 92 | + init(); |
| 93 | +} |
0 commit comments