Files
web/app/Core/StatisticsService.php
2026-01-03 01:12:18 +08:00

134 lines
3.7 KiB
PHP

<?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();
}
/**
* Обрабатывает команду /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')),
],
];
}
protected function getPlaylistsByField(string $field, bool|int|string|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, bool|int|string|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']),
);
}
}