Рефакторинг статистики. Добавлен latest

This commit is contained in:
2025-11-01 00:57:38 +08:00
parent 67349bb909
commit c75da39b87
2 changed files with 149 additions and 12 deletions

View File

@@ -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[] = '';

View File

@@ -0,0 +1,132 @@
<?php
/*
* Copyright (c) 2025, Антон Аксенов
* This file is part of m3u.su project
* MIT License: https://git.axenov.dev/IPTV/web/src/branch/master/LICENSE
*/
declare(strict_types=1);
namespace App\Core;
use Exception;
/**
* Обработчик команд бота
*/
class StatisticsService
{
/**
* @var array[]
*/
protected array $playlists = [];
protected array $channels = [];
public function __construct()
{
$this->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')),
],
];
}
}