|
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
|
|
61 | 63 | });
|
62 | 64 | }
|
63 | 65 |
|
| 66 | + /** |
| 67 | + * Render monthly statistics. |
| 68 | + * |
| 69 | + * @param {number} year Year to load data for. |
| 70 | + * |
| 71 | + * @return {Promise<{[key: string]: number}>} Data promise from API. |
| 72 | + */ |
| 73 | + function loadDaily(year) { |
| 74 | + year = encodeURIComponent(year); |
| 75 | + |
| 76 | + // Load data from API. |
| 77 | + return wp.apiFetch({ |
| 78 | + path: `/statify/v1/stats/extended?scope=day&year=${year}`, |
| 79 | + }); |
| 80 | + } |
| 81 | + |
64 | 82 | /**
|
65 | 83 | * Render monthly statistics.
|
66 | 84 | *
|
|
71 | 89 | return wp.apiFetch({ path: '/statify/v1/stats/extended?scope=month' });
|
72 | 90 | }
|
73 | 91 |
|
| 92 | + /** |
| 93 | + * Render daily statistics. |
| 94 | + * |
| 95 | + * @param {HTMLElement} root Root element. |
| 96 | + * @param {{[key: string]: number}} data Data from API. |
| 97 | + */ |
| 98 | + function renderDaily(root, data) { |
| 99 | + const labels = Object.keys(data); |
| 100 | + const values = Object.values(data); |
| 101 | + |
| 102 | + render(root, labels, values); |
| 103 | + } |
| 104 | + |
74 | 105 | /**
|
75 | 106 | * Render monthly statistics.
|
76 | 107 | *
|
|
308 | 339 | }
|
309 | 340 |
|
310 | 341 | col = document.createElement('TD');
|
| 342 | + col.classList.add('statify-table-sum'); |
311 | 343 | col.innerText = sum;
|
312 | 344 | row.appendChild(col);
|
313 | 345 |
|
314 | 346 | tbody.insertBefore(row, tbody.firstChild);
|
315 | 347 | }
|
316 | 348 | }
|
317 | 349 |
|
| 350 | + /** |
| 351 | + * Render yearly table. |
| 352 | + * |
| 353 | + * @param {HTMLElement} table Root element. |
| 354 | + * @param {any} data Data from API. |
| 355 | + */ |
| 356 | + function renderDailyTable(table, data) { |
| 357 | + const rows = Array.from(table.querySelectorAll('tbody > tr')); |
| 358 | + const cols = rows.map((row) => Array.from(row.querySelectorAll('td'))); |
| 359 | + let out = cols.slice(0, 31); |
| 360 | + |
| 361 | + const sum = Array(12).fill(0); |
| 362 | + const vls = Array(12).fill(0); |
| 363 | + const min = Array(12).fill(Number.MAX_SAFE_INTEGER); |
| 364 | + const max = Array(12).fill(0); |
| 365 | + |
| 366 | + for (const [day, count] of Object.entries(data)) { |
| 367 | + const d = new Date(day); |
| 368 | + const m = d.getMonth(); |
| 369 | + sum[m] += count; |
| 370 | + ++vls[m]; |
| 371 | + min[m] = Math.min(min[m], count); |
| 372 | + max[m] = Math.max(max[m], count); |
| 373 | + out[d.getDate() - 1][m].innerText = count; |
| 374 | + } |
| 375 | + |
| 376 | + out = |
| 377 | + cols[ |
| 378 | + rows.findIndex((row) => |
| 379 | + row.classList.contains('statify-table-sum') |
| 380 | + ) |
| 381 | + ]; |
| 382 | + const avg = |
| 383 | + cols[ |
| 384 | + rows.findIndex((row) => |
| 385 | + row.classList.contains('statify-table-avg') |
| 386 | + ) |
| 387 | + ]; |
| 388 | + for (const [m, s] of sum.entries()) { |
| 389 | + if (vls[m] > 0) { |
| 390 | + out[m].innerText = s; |
| 391 | + avg[m].innerText = Math.round(s / vls[m]); |
| 392 | + } else { |
| 393 | + out[m].innerText = '-'; |
| 394 | + avg[m].innerText = '-'; |
| 395 | + } |
| 396 | + } |
| 397 | + |
| 398 | + out = |
| 399 | + cols[ |
| 400 | + rows.findIndex((row) => |
| 401 | + row.classList.contains('statify-table-min') |
| 402 | + ) |
| 403 | + ]; |
| 404 | + for (const [m, s] of min.entries()) { |
| 405 | + out[m].innerText = vls[m] > 0 ? s : '-'; |
| 406 | + } |
| 407 | + |
| 408 | + out = |
| 409 | + cols[ |
| 410 | + rows.findIndex((row) => |
| 411 | + row.classList.contains('statify-table-max') |
| 412 | + ) |
| 413 | + ]; |
| 414 | + for (const [m, s] of max.entries()) { |
| 415 | + out[m].innerText = vls[m] > 0 ? s : '-'; |
| 416 | + } |
| 417 | + |
| 418 | + for (const row of rows) { |
| 419 | + row.classList.remove('placeholder'); |
| 420 | + } |
| 421 | + } |
| 422 | + |
| 423 | + /** |
| 424 | + * Convert daily to monthly data. |
| 425 | + * |
| 426 | + * @param {{[key: string]: number}} data Daily data. |
| 427 | + * @return {{visits: {[key: string]: {[key: string]: number}}}} Monthly data. |
| 428 | + */ |
| 429 | + function dailyToMonthly(data) { |
| 430 | + const monthly = { visits: {} }; |
| 431 | + for (const [day, count] of Object.entries(data)) { |
| 432 | + const date = new Date(day); |
| 433 | + const year = date.getFullYear(); |
| 434 | + const month = date.getMonth(); |
| 435 | + |
| 436 | + if (!(year in monthly.visits)) { |
| 437 | + monthly.visits[year] = {}; |
| 438 | + } |
| 439 | + monthly.visits[year][month] = |
| 440 | + count + (monthly.visits[year][month] || 0); |
| 441 | + } |
| 442 | + return monthly; |
| 443 | + } |
| 444 | + |
318 | 445 | // Abort if config or target element is not present.
|
319 | 446 | if (typeof statifyDashboard !== 'undefined') {
|
320 | 447 | if (chartElem) {
|
|
332 | 459 | updateDashboard(false);
|
333 | 460 | }
|
334 | 461 |
|
335 |
| - if (chartElemMonthly) { |
| 462 | + if (chartElemDaily) { |
| 463 | + loadDaily(chartElemDaily.dataset.year).then((data) => { |
| 464 | + renderDaily(chartElemDaily, data); |
| 465 | + |
| 466 | + if (chartElemMonthly) { |
| 467 | + renderMonthly(chartElemMonthly, dailyToMonthly(data)); |
| 468 | + } |
| 469 | + |
| 470 | + if (dailyTable) { |
| 471 | + renderDailyTable(dailyTable, data); |
| 472 | + } |
| 473 | + }); |
| 474 | + } else if (chartElemMonthly) { |
336 | 475 | loadMonthly()
|
337 | 476 | .then((data) => {
|
338 | 477 | renderMonthly(chartElemMonthly, data);
|
|
0 commit comments