From c75da39b870521b9698adb3cd41a8060b6ef6add Mon Sep 17 00:00:00 2001 From: AnthonyAxenov Date: Sat, 1 Nov 2025 00:57:38 +0800 Subject: [PATCH] =?UTF-8?q?=D0=A0=D0=B5=D1=84=D0=B0=D0=BA=D1=82=D0=BE?= =?UTF-8?q?=D1=80=D0=B8=D0=BD=D0=B3=20=D1=81=D1=82=D0=B0=D1=82=D0=B8=D1=81?= =?UTF-8?q?=D1=82=D0=B8=D0=BA=D0=B8.=20=D0=94=D0=BE=D0=B1=D0=B0=D0=B2?= =?UTF-8?q?=D0=BB=D0=B5=D0=BD=20latest?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Core/Bot.php | 29 +++++--- app/Core/StatisticsService.php | 132 +++++++++++++++++++++++++++++++++ 2 files changed, 149 insertions(+), 12 deletions(-) create mode 100644 app/Core/StatisticsService.php diff --git a/app/Core/Bot.php b/app/Core/Bot.php index 88720fe..b8f57f2 100644 --- a/app/Core/Bot.php +++ b/app/Core/Bot.php @@ -351,24 +351,29 @@ class Bot $offlineCh = array_merge($offlineCh, $tmpOffline); $adultCh = array_merge($adultCh, $tmpAdult); } + $stats = new StatisticsService()->get(); $replyText[] = '📊 *Статистика*'; $replyText[] = ''; $replyText[] = '*Список изменён:* ' . $this->escape(ini()->updatedAt()); $replyText[] = ''; - $replyText[] = '*Плейлистов:* ' . count(ini()->getPlaylists()); - $replyText[] = '🟢 Онлайн \- ' . count($onlinePls); - $replyText[] = '🔴 Оффлайн \- ' . count($offlinePls); - $replyText[] = '⚪ В очереди \- ' . count($unknownPls); - $replyText[] = '🔞 Для взрослых \- ' . count($adultPls); - $replyText[] = '⏪ С перемоткой \- ' . count($catchupPls); - $replyText[] = '🗞️ С телепрограммой \- ' . count($tvgPls); - $replyText[] = '🗂️ С группировкой каналов \- ' . count($grouppedPls); + $replyText[] = '*Плейлистов:* ' . $stats['playlists']['all']; + $replyText[] = '🟢 Онлайн \- ' . $stats['playlists']['online']; + $replyText[] = '🔴 Оффлайн \- ' . $stats['playlists']['offline']; + $replyText[] = '⚪ В очереди \- ' . $stats['playlists']['unknown']; + $replyText[] = '🔞 Для взрослых \- ' . $stats['playlists']['adult']; + $replyText[] = '⏪ С перемоткой \- ' . $stats['playlists']['hasCatchup']; + $replyText[] = '🗞️ С телепрограммой \- ' . $stats['playlists']['hasTvg']; + $replyText[] = '🗂️ С группировкой каналов \- ' . $stats['playlists']['groupped']; $replyText[] = ''; - $replyText[] = '*Каналов:* ' . count($allChannels); - $replyText[] = '🟢 Онлайн \- ' . count($onlineCh); - $replyText[] = '🔴 Оффлайн \- ' . count($offlineCh); - $replyText[] = '🔞 Для взрослых \- ' . count($adultCh); + $replyText[] = '*Каналов:* ' . $stats['channels']['all']; + $replyText[] = '🟢 Онлайн \- ' . $stats['channels']['online']; + $replyText[] = '🔴 Оффлайн \- ' . $stats['channels']['offline']; + $replyText[] = '🔞 Для взрослых \- ' . $stats['channels']['adult']; + $replyText[] = ''; + $replyText[] = ''; + $replyText[] = '*Самая свежая проверка*'; + $replyText[] = '🕔 `' . $stats['channels']['latest']['code'] . '` (' . $stats['channels']['latest']['timeFmt'] . ')'; $replyText[] = ''; $replyText[] = ''; diff --git a/app/Core/StatisticsService.php b/app/Core/StatisticsService.php new file mode 100644 index 0000000..2140c22 --- /dev/null +++ b/app/Core/StatisticsService.php @@ -0,0 +1,132 @@ +playlists = ini()->getPlaylists(); + $this->channels = $this->getAllChannels(); + } + + protected function getPlaylistsByField(string $field, int|string|bool|null $value): array + { + return array_filter( + $this->playlists, + static fn (array $pls) => $pls[$field] === $value, + ); + } + + protected function getPlaylistsByTag(string $tag): array + { + return array_filter( + $this->playlists, + static fn (array $pls) => in_array($tag, $pls['tags']), + ); + } + + protected function getPlaylistsWithGroups(): array + { + return array_filter( + $this->playlists, + static fn (array $pls) => !empty($pls['groups']), + ); + } + + protected function getLatestPlaylist(): array + { + $e = array_combine( + array_column($this->playlists, 'code'), + array_column($this->playlists, 'checkedAt'), + ); + $e = array_filter($e); + asort($e); + $latest = array_slice($e, 0, 1); + + return [ + 'code' => array_first(array_keys($latest)), + 'time' => $time = array_first($latest), + 'timeFmt' => date('H:i:s d.m.Y', $time), + ]; + } + + protected function getAllChannels(): array + { + $channels = []; + foreach ($this->playlists as $pls) { + $channels = array_merge($channels, $pls['channels']); + } + + return $channels; + } + + protected function getAllChannelsCount(): int + { + return count($this->channels); + } + + protected function getChannelsByField(string $field, int|string|bool|null $value): array + { + return array_filter( + $this->channels, + static fn (array $channel) => $channel[$field] === $value, + ); + } + + protected function getChannelsByTag(string $tag): array + { + return array_filter( + $this->channels, + static fn (array $channel) => in_array($tag, $channel['tags']), + ); + } + + /** + * Обрабатывает команду /stats + * + * @return bool + * @throws Exception + */ + public function get(): array + { + return [ + 'playlists' => [ + 'all' => count($this->playlists), + 'online' => count($this->getPlaylistsByField('isOnline', true)), + 'offline' => count($this->getPlaylistsByField('isOnline', false)), + 'unknown' => count($this->getPlaylistsByField('isOnline', null)), + 'adult' => count($this->getPlaylistsByTag('adult')), + 'hasCatchup' => count($this->getPlaylistsByField('hasCatchup', true)), + 'hasTvg' => count($this->getPlaylistsByField('hasTvg', true)), + 'groupped' => count($this->getPlaylistsWithGroups()), + 'latest' => $this->getLatestPlaylist(), + ], + 'channels' => [ + 'all' => $this->getAllChannelsCount(), + 'online' => count($this->getChannelsByField('isOnline', true)), + 'offline' => count($this->getChannelsByField('isOnline', false)), + 'adult' => count($this->getChannelsByTag('adult')), + ], + ]; + } +}