|
12 | 12 | );
|
13 | 13 | const refreshBtn = document.getElementById('statify_refresh');
|
14 | 14 |
|
| 15 | + const chartElemDaily = document.getElementById('statify_chart_daily'); |
15 | 16 | const chartElemMonthly = document.getElementById('statify_chart_monthly');
|
16 | 17 | const chartElemYearly = document.getElementById('statify_chart_yearly');
|
17 | 18 | const yearlyTable = document.getElementById('statify-table-yearly');
|
| 19 | + const dailyTable = document.getElementById('statify-table-daily'); |
18 | 20 |
|
19 | 21 | /**
|
20 | 22 | * Update the dashboard widget
|
|
63 | 65 | });
|
64 | 66 | }
|
65 | 67 |
|
| 68 | + /** |
| 69 | + * Render monthly statistics. |
| 70 | + * |
| 71 | + * @param {number} year Year to load data for. |
| 72 | + * |
| 73 | + * @return {Promise<{[key: string]: number}>} Data promise from API. |
| 74 | + */ |
| 75 | + function loadDaily(year) { |
| 76 | + year = encodeURIComponent(year); |
| 77 | + |
| 78 | + // Load data from API. |
| 79 | + return wp.apiFetch({ |
| 80 | + path: `/statify/v1/stats/extended?scope=day&year=${year}`, |
| 81 | + }); |
| 82 | + } |
| 83 | + |
66 | 84 | /**
|
67 | 85 | * Render monthly statistics.
|
68 | 86 | *
|
|
73 | 91 | return wp.apiFetch({ path: '/statify/v1/stats/extended?scope=month' });
|
74 | 92 | }
|
75 | 93 |
|
| 94 | + /** |
| 95 | + * Render daily statistics. |
| 96 | + * |
| 97 | + * @param {HTMLElement} root Root element. |
| 98 | + * @param {{[key: string]: number}} data Data from API. |
| 99 | + */ |
| 100 | + function renderDaily(root, data) { |
| 101 | + const labels = Object.keys(data); |
| 102 | + const values = Object.values(data); |
| 103 | + |
| 104 | + render(root, labels, values); |
| 105 | + } |
| 106 | + |
76 | 107 | /**
|
77 | 108 | * Render monthly statistics.
|
78 | 109 | *
|
|
317 | 348 | }
|
318 | 349 |
|
319 | 350 | col = document.createElement('TD');
|
| 351 | + col.classList.add('statify-table-sum'); |
320 | 352 | col.innerText = sum;
|
321 | 353 | row.appendChild(col);
|
322 | 354 |
|
323 | 355 | tbody.insertBefore(row, tbody.firstChild);
|
324 | 356 | }
|
325 | 357 | }
|
326 | 358 |
|
| 359 | + /** |
| 360 | + * Render yearly table. |
| 361 | + * |
| 362 | + * @param {HTMLElement} table Root element. |
| 363 | + * @param {any} data Data from API. |
| 364 | + */ |
| 365 | + function renderDailyTable(table, data) { |
| 366 | + const rows = Array.from(table.querySelectorAll('tbody > tr')); |
| 367 | + const cols = rows.map((row) => Array.from(row.querySelectorAll('td'))); |
| 368 | + let out = cols.slice(0, 31); |
| 369 | + |
| 370 | + const sum = Array(12).fill(0); |
| 371 | + const vls = Array(12).fill(0); |
| 372 | + const min = Array(12).fill(Number.MAX_SAFE_INTEGER); |
| 373 | + const max = Array(12).fill(0); |
| 374 | + |
| 375 | + for (const [day, count] of Object.entries(data)) { |
| 376 | + const d = new Date(day); |
| 377 | + const m = d.getMonth(); |
| 378 | + sum[m] += count; |
| 379 | + ++vls[m]; |
| 380 | + min[m] = Math.min(min[m], count); |
| 381 | + max[m] = Math.max(max[m], count); |
| 382 | + out[d.getDate() - 1][m].innerText = count; |
| 383 | + } |
| 384 | + |
| 385 | + out = |
| 386 | + cols[ |
| 387 | + rows.findIndex((row) => |
| 388 | + row.classList.contains('statify-table-sum') |
| 389 | + ) |
| 390 | + ]; |
| 391 | + const avg = |
| 392 | + cols[ |
| 393 | + rows.findIndex((row) => |
| 394 | + row.classList.contains('statify-table-avg') |
| 395 | + ) |
| 396 | + ]; |
| 397 | + for (const [m, s] of sum.entries()) { |
| 398 | + if (vls[m] > 0) { |
| 399 | + out[m].innerText = s; |
| 400 | + avg[m].innerText = Math.round(s / vls[m]); |
| 401 | + } else { |
| 402 | + out[m].innerText = '-'; |
| 403 | + avg[m].innerText = '-'; |
| 404 | + } |
| 405 | + } |
| 406 | + |
| 407 | + out = |
| 408 | + cols[ |
| 409 | + rows.findIndex((row) => |
| 410 | + row.classList.contains('statify-table-min') |
| 411 | + ) |
| 412 | + ]; |
| 413 | + for (const [m, s] of min.entries()) { |
| 414 | + out[m].innerText = vls[m] > 0 ? s : '-'; |
| 415 | + } |
| 416 | + |
| 417 | + out = |
| 418 | + cols[ |
| 419 | + rows.findIndex((row) => |
| 420 | + row.classList.contains('statify-table-max') |
| 421 | + ) |
| 422 | + ]; |
| 423 | + for (const [m, s] of max.entries()) { |
| 424 | + out[m].innerText = vls[m] > 0 ? s : '-'; |
| 425 | + } |
| 426 | + |
| 427 | + for (const row of rows) { |
| 428 | + row.classList.remove('placeholder'); |
| 429 | + } |
| 430 | + } |
| 431 | + |
| 432 | + /** |
| 433 | + * Convert daily to monthly data. |
| 434 | + * |
| 435 | + * @param {{[key: string]: number}} data Daily data. |
| 436 | + * @return {{visits: {[key: string]: {[key: string]: number}}}} Monthly data. |
| 437 | + */ |
| 438 | + function dailyToMonthly(data) { |
| 439 | + const monthly = { visits: {} }; |
| 440 | + for (const [day, count] of Object.entries(data)) { |
| 441 | + const date = new Date(day); |
| 442 | + const year = date.getFullYear(); |
| 443 | + const month = date.getMonth(); |
| 444 | + |
| 445 | + if (!(year in monthly.visits)) { |
| 446 | + monthly.visits[year] = {}; |
| 447 | + } |
| 448 | + monthly.visits[year][month] = |
| 449 | + count + (monthly.visits[year][month] || 0); |
| 450 | + } |
| 451 | + return monthly; |
| 452 | + } |
| 453 | + |
327 | 454 | // Abort if config or target element is not present.
|
328 | 455 | if (chartElem) {
|
329 | 456 | // Bind update function to "refresh" button.
|
|
340 | 467 | updateDashboard(false);
|
341 | 468 | }
|
342 | 469 |
|
343 |
| - if (chartElemMonthly) { |
| 470 | + if (chartElemDaily) { |
| 471 | + loadDaily(chartElemDaily.dataset.year).then((data) => { |
| 472 | + renderDaily(chartElemDaily, data); |
| 473 | + |
| 474 | + if (chartElemMonthly) { |
| 475 | + renderMonthly(chartElemMonthly, dailyToMonthly(data)); |
| 476 | + } |
| 477 | + |
| 478 | + if (dailyTable) { |
| 479 | + renderDailyTable(dailyTable, data); |
| 480 | + } |
| 481 | + }); |
| 482 | + } else if (chartElemMonthly) { |
344 | 483 | loadMonthly()
|
345 | 484 | .then((data) => {
|
346 | 485 | renderMonthly(chartElemMonthly, data);
|
|
0 commit comments