index.phtml 6.6 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="box">
  9. <div class="box-title"><h2><?= _t('admin.stats.entry_repartition') ?></h2></div>
  10. <div class="box-content scrollbar-thin">
  11. <table>
  12. <thead>
  13. <tr>
  14. <th> </th>
  15. <th><?= _t('admin.stats.main_stream') ?></th>
  16. <th><?= _t('admin.stats.all_feeds') ?></th>
  17. </tr>
  18. </thead>
  19. <tbody>
  20. <tr>
  21. <th><?= _t('admin.stats.status_total') ?></th>
  22. <td class="numeric"><?= format_number($this->repartitions['main_stream']['total'] ?? -1) ?></td>
  23. <td class="numeric"><?= format_number($this->repartitions['all_feeds']['total'] ?? -1) ?></td>
  24. </tr>
  25. <tr>
  26. <th><?= _t('admin.stats.status_read') ?></th>
  27. <td class="numeric"><?= format_number($this->repartitions['main_stream']['count_reads'] ?? -1) ?></td>
  28. <td class="numeric"><?= format_number($this->repartitions['all_feeds']['count_reads'] ?? -1) ?></td>
  29. </tr>
  30. <tr>
  31. <th><?= _t('admin.stats.status_unread') ?></th>
  32. <td class="numeric"><?= format_number($this->repartitions['main_stream']['count_unreads'] ?? -1) ?></td>
  33. <td class="numeric"><?= format_number($this->repartitions['all_feeds']['count_unreads'] ?? -1) ?></td>
  34. </tr>
  35. <tr>
  36. <th><?= _t('admin.stats.status_favorites') ?></th>
  37. <td class="numeric"><?= format_number($this->repartitions['main_stream']['count_favorites'] ?? -1) ?></td>
  38. <td class="numeric"><?= format_number($this->repartitions['all_feeds']['count_favorites'] ?? -1) ?></td>
  39. </tr>
  40. </tbody>
  41. </table>
  42. </div>
  43. </div>
  44. <div class="box double-height">
  45. <div class="box-title"><h2><?= _t('admin.stats.top_feed') ?></h2></div>
  46. <div class="box-content scrollbar-thin">
  47. <table>
  48. <thead>
  49. <tr>
  50. <th><?= _t('admin.stats.feed') ?></th>
  51. <th><?= _t('admin.stats.category') ?></th>
  52. <th><?= _t('admin.stats.entry_count') ?></th>
  53. <th><?= _t('admin.stats.percent_of_total') ?></th>
  54. </tr>
  55. </thead>
  56. <tbody>
  57. <?php foreach ($this->topFeed as $feed): ?>
  58. <tr>
  59. <td><a href="<?= _url('stats', 'repartition', 'id', $feed['id']) ?>"><?= $feed['name'] ?></a></td>
  60. <td><?= $feed['category'] ?></td>
  61. <td class="numeric"><?= format_number($feed['count']) ?></td>
  62. <td class="numeric"><?php
  63. if (!empty($this->repartitions['all_feeds']['total'])) {
  64. echo format_number($feed['count'] / $this->repartitions['all_feeds']['total'] * 100, 1);
  65. }
  66. ?></td>
  67. </tr>
  68. <?php endforeach; ?>
  69. </tbody>
  70. </table>
  71. </div>
  72. </div>
  73. <br />
  74. <div class="box double-width double-height">
  75. <div class="box-title"><h2><?= _t('admin.stats.entry_per_day') ?></h2></div>
  76. <div class="box-content scrollbar-thin">
  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. <br id="stats_per_category" />
  134. <div class="box double-height" id="feed_per_category">
  135. <div class="box-title"><h2><?= _t('admin.stats.feed_per_category') ?></h2><a href="#feed_per_category" class="btn target-hidden">+</a><a href="#stats_per_category" class="btn target-visible">-</a></div>
  136. <div class="box-content scrollbar-thin">
  137. <canvas id="statsFeedsPerCategory"></canvas>
  138. <script class="jsonData-stats" type="application/json">
  139. <?= json_encode([
  140. 'canvasID' => 'statsFeedsPerCategory',
  141. 'charttype' => 'doughnut',
  142. 'data' => $feedValues,
  143. 'labels' => $feedLabels,
  144. 'backgroundColor' => $feedColors,
  145. ], JSON_UNESCAPED_UNICODE); ?>
  146. </script>
  147. </div>
  148. </div>
  149. <div class="box double-height" id="entry_per_category">
  150. <div class="box-title"><h2><?= _t('admin.stats.entry_per_category') ?></h2><a href="#entry_per_category" class="btn target-hidden">+</a><a href="#stats_per_category" class="btn target-visible">-</a></div>
  151. <div class="box-content scrollbar-thin">
  152. <canvas id="statsEntriesPerCategory"></canvas>
  153. <script class="jsonData-stats" type="application/json">
  154. <?= json_encode([
  155. 'canvasID' => 'statsEntriesPerCategory',
  156. 'charttype' => 'doughnut',
  157. 'data' => $entryValues,
  158. 'labels' => $entryLabels,
  159. 'backgroundColor' => $entryColors,
  160. ], JSON_UNESCAPED_UNICODE); ?>
  161. </script>
  162. </div>
  163. </div>
  164. </div>
  165. </main>
  166. <script src="../scripts/statsWithChartjs.js?<?= @filemtime(PUBLIC_PATH . '/scripts/statsWithChartjs.js') ?>"></script>