index.phtml 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. declare(strict_types=1);
  3. /** @var FreshRSS_ViewStats $this */
  4. $this->partial('aside_subscription');
  5. ?>
  6. <main class="post">
  7. <h1><?= _t('admin.stats.main') ?></h1>
  8. <div class="stat-grid">
  9. <div class="stat half">
  10. <h2><?= _t('admin.stats.entry_repartition') ?></h2>
  11. <div class="table-wrapper scrollbar-thin">
  12. <table>
  13. <thead>
  14. <tr>
  15. <th> </th>
  16. <th><?= _t('admin.stats.main_stream') ?></th>
  17. <th><?= _t('admin.stats.all_feeds') ?></th>
  18. </tr>
  19. </thead>
  20. <tbody>
  21. <tr>
  22. <th><?= _t('admin.stats.status_total') ?></th>
  23. <td class="numeric"><?= format_number($this->repartitions['main_stream']['total'] ?? -1) ?></td>
  24. <td class="numeric"><?= format_number($this->repartitions['all_feeds']['total'] ?? -1) ?></td>
  25. </tr>
  26. <tr>
  27. <th><?= _t('admin.stats.status_read') ?></th>
  28. <td class="numeric"><?= format_number($this->repartitions['main_stream']['count_reads'] ?? -1) ?></td>
  29. <td class="numeric"><?= format_number($this->repartitions['all_feeds']['count_reads'] ?? -1) ?></td>
  30. </tr>
  31. <tr>
  32. <th><?= _t('admin.stats.status_unread') ?></th>
  33. <td class="numeric"><?= format_number($this->repartitions['main_stream']['count_unreads'] ?? -1) ?></td>
  34. <td class="numeric"><?= format_number($this->repartitions['all_feeds']['count_unreads'] ?? -1) ?></td>
  35. </tr>
  36. <tr>
  37. <th><?= _t('admin.stats.status_favorites') ?></th>
  38. <td class="numeric"><?= format_number($this->repartitions['main_stream']['count_favorites'] ?? -1) ?></td>
  39. <td class="numeric"><?= format_number($this->repartitions['all_feeds']['count_favorites'] ?? -1) ?></td>
  40. </tr>
  41. </tbody>
  42. </table>
  43. </div>
  44. </div>
  45. <div class="stat half">
  46. <h2><?= _t('admin.stats.top_feed') ?></h2>
  47. <div class="table-wrapper scrollbar-thin">
  48. <table>
  49. <thead>
  50. <tr>
  51. <th><?= _t('admin.stats.feed') ?></th>
  52. <th><?= _t('admin.stats.category') ?></th>
  53. <th><?= _t('admin.stats.entry_count') ?></th>
  54. <th><?= _t('admin.stats.percent_of_total') ?></th>
  55. </tr>
  56. </thead>
  57. <tbody>
  58. <?php foreach ($this->topFeed as $feed): ?>
  59. <tr>
  60. <td><a href="<?= _url('stats', 'repartition', 'id', $feed['id']) ?>"><?= $feed['name'] ?></a></td>
  61. <td><?= $feed['category'] ?></td>
  62. <td class="numeric"><?= format_number($feed['count']) ?></td>
  63. <td class="numeric"><?php
  64. if (!empty($this->repartitions['all_feeds']['total'])) {
  65. echo format_number($feed['count'] / $this->repartitions['all_feeds']['total'] * 100, 1);
  66. }
  67. ?></td>
  68. </tr>
  69. <?php endforeach; ?>
  70. </tbody>
  71. </table>
  72. </div>
  73. </div>
  74. <div class="stat">
  75. <h2><?= _t('admin.stats.entry_per_day') ?></h2>
  76. <div>
  77. <canvas id="statsEntriesPerDay"></canvas>
  78. <script class="jsonData-stats" type="application/json">
  79. <?= json_encode([
  80. 'canvasID' => 'statsEntriesPerDay',
  81. 'charttype' => 'barWithAverage',
  82. 'labelBarChart' => _t('admin.stats.entry_count'),
  83. 'dataBarChart' => $this->entryCount,
  84. 'labelAverage' => 'Average (' . $this->average . ')',
  85. 'dataAverage' => $this->average,
  86. 'xAxisLabels' => $this->last30DaysLabels,
  87. ], JSON_UNESCAPED_UNICODE)
  88. ?></script>
  89. </div>
  90. </div>
  91. <?php
  92. // Function to generate a color palette
  93. /**
  94. * Generate a color palette.
  95. *
  96. * @param int $count The number of colors to generate.
  97. * @return array<int,string> An array of HSL color strings.
  98. */
  99. function generateColorPalette(int $count): array {
  100. $colors = [];
  101. for ($i = 0; $i < $count; $i++) {
  102. $hue = ($i / $count) * 360; // Distribute colors evenly around the color wheel
  103. $saturation = 70; // Fixed saturation
  104. $lightness = 50; // Fixed lightness
  105. $colors[] = "hsl($hue, {$saturation}%, {$lightness}%)";
  106. }
  107. return $colors;
  108. }
  109. // 1. Get all unique category labels and sort them
  110. $allLabels = array_unique(array_merge($this->feedByCategory['label'], $this->entryByCategory['label']));
  111. sort($allLabels); // Ensure consistent order
  112. // 2. Generate a color palette based on the number of unique categories
  113. $colorPalette = generateColorPalette(count($allLabels));
  114. // 3. Map categories to colors
  115. $colorMap = array_combine($allLabels, $colorPalette);
  116. // 4. Align data and labels for both charts
  117. $feedData = array_fill_keys($allLabels, 0); // Initialize data with all categories
  118. foreach ($this->feedByCategory['label'] as $index => $label) {
  119. $feedData[$label] = $this->feedByCategory['data'][$index];
  120. }
  121. $entryData = array_fill_keys($allLabels, 0); // Initialize data with all categories
  122. foreach ($this->entryByCategory['label'] as $index => $label) {
  123. $entryData[$label] = $this->entryByCategory['data'][$index];
  124. }
  125. // Final data and labels
  126. $feedLabels = array_keys($feedData);
  127. $feedColors = array_map(fn($label) => $colorMap[$label], $feedLabels);
  128. $feedValues = array_values($feedData);
  129. $entryLabels = array_keys($entryData);
  130. $entryColors = array_map(fn($label) => $colorMap[$label], $entryLabels);
  131. $entryValues = array_values($entryData);
  132. ?>
  133. <div class="stat half">
  134. <h2><?= _t('admin.stats.feed_per_category') ?></h2>
  135. <div>
  136. <canvas id="statsFeedsPerCategory"></canvas>
  137. <script class="jsonData-stats" type="application/json">
  138. <?= json_encode([
  139. 'canvasID' => 'statsFeedsPerCategory',
  140. 'charttype' => 'doughnut',
  141. 'data' => $feedValues,
  142. 'labels' => $feedLabels,
  143. 'backgroundColor' => $feedColors,
  144. ], JSON_UNESCAPED_UNICODE); ?>
  145. </script>
  146. </div>
  147. </div>
  148. <div class="stat half">
  149. <h2><?= _t('admin.stats.entry_per_category') ?></h2>
  150. <div>
  151. <canvas id="statsEntriesPerCategory"></canvas>
  152. <script class="jsonData-stats" type="application/json">
  153. <?= json_encode([
  154. 'canvasID' => 'statsEntriesPerCategory',
  155. 'charttype' => 'doughnut',
  156. 'data' => $entryValues,
  157. 'labels' => $entryLabels,
  158. 'backgroundColor' => $entryColors,
  159. ], JSON_UNESCAPED_UNICODE); ?>
  160. </script>
  161. </div>
  162. </div>
  163. </div>
  164. </main>
  165. <script src="../scripts/statsWithChartjs.js?<?= @filemtime(PUBLIC_PATH . '/scripts/statsWithChartjs.js') ?>"></script>