Add HLS support to listener reports and metrics.

This commit is contained in:
Buster "Silver Eagle" Neece 2022-06-26 13:01:40 -05:00
parent 031d686724
commit ff31677167
No known key found for this signature in database
GPG Key ID: F1D2E64A0005E80E
7 changed files with 113 additions and 9 deletions

View File

@ -51,6 +51,7 @@ final class ListenersAction
private readonly Entity\Repository\ListenerRepository $listenerRepo,
private readonly Entity\Repository\StationMountRepository $mountRepo,
private readonly Entity\Repository\StationRemoteRepository $remoteRepo,
private readonly Entity\Repository\StationHlsStreamRepository $hlsStreamRepo,
) {
}
@ -101,6 +102,7 @@ final class ListenersAction
$mountNames = $this->mountRepo->getDisplayNames($station);
$remoteNames = $this->remoteRepo->getDisplayNames($station);
$hlsStreamNames = $this->hlsStreamRepo->getDisplayNames($station);
/** @var Entity\Api\Listener[] $listeners */
$listeners = [];
@ -139,6 +141,9 @@ final class ListenersAction
if (null !== $listener['mount_id']) {
$api->mount_is_local = true;
$api->mount_name = $mountNames[$listener['mount_id']];
} elseif (null !== $listener['hls_stream_id']) {
$api->mount_is_local = true;
$api->mount_name = $hlsStreamNames[$listener['hls_stream_id']];
} elseif (null !== $listener['remote_id']) {
$api->mount_is_local = false;
$api->mount_name = $remoteNames[$listener['remote_id']];

View File

@ -17,7 +17,8 @@ final class ByStream extends AbstractReportAction
SettingsRepository $settingsRepo,
EntityManagerInterface $em,
private readonly Entity\Repository\StationMountRepository $mountRepo,
private readonly Entity\Repository\StationRemoteRepository $remoteRepo
private readonly Entity\Repository\StationRemoteRepository $remoteRepo,
private readonly Entity\Repository\StationHlsStreamRepository $hlsStreamRepo,
) {
parent::__construct($settingsRepo, $em);
}
@ -44,11 +45,12 @@ final class ByStream extends AbstractReportAction
COUNT(l.listener_hash) AS listeners,
SUM(l.connected_seconds) AS connected_seconds
FROM (
SELECT IF (
mount_id IS NOT NULL,
CONCAT('local_', mount_id),
CONCAT('remote_', remote_id)
) AS stream_id,
SELECT CASE
WHEN mount_id IS NOT NULL THEN CONCAT('local_', mount_id)
WHEN hls_stream_id IS NOT NULL THEN CONCAT('hls_', hls_stream_id)
WHEN remote_id IS NOT NULL THEN CONCAT('remote_', remote_id)
ELSE 'unknown'
END AS stream_id,
SUM(timestamp_end - timestamp_start) AS connected_seconds,
listener_hash
FROM listener
@ -73,6 +75,9 @@ final class ByStream extends AbstractReportAction
foreach ($this->remoteRepo->getDisplayNames($station) as $id => $displayName) {
$streamLookup['remote_' . $id] = $displayName;
}
foreach ($this->hlsStreamRepo->getDisplayNames($station) as $id => $displayName) {
$streamLookup['hls_' . $id] = $displayName;
}
$listenersByStream = [];
$connectedTimeByStream = [];

View File

@ -43,6 +43,13 @@ class Listener implements
#[ORM\JoinColumn(name: 'remote_id', referencedColumnName: 'id', nullable: true, onDelete: 'SET NULL')]
protected ?StationRemote $remote = null;
#[ORM\Column(nullable: true)]
protected ?int $hls_stream_id = null;
#[ORM\ManyToOne(targetEntity: StationHlsStream::class)]
#[ORM\JoinColumn(name: 'hls_stream_id', referencedColumnName: 'id', nullable: true, onDelete: 'SET NULL')]
protected ?StationHlsStream $hls_stream = null;
#[ORM\Column]
protected int $listener_uid;
@ -118,6 +125,21 @@ class Listener implements
$this->remote = $remote;
}
public function getHlsStream(): ?StationHlsStream
{
return $this->hls_stream;
}
public function getHlsStreamId(): ?int
{
return $this->hls_stream_id;
}
public function setHlsStream(?StationHlsStream $hls_stream): void
{
$this->hls_stream = $hls_stream;
}
public function getListenerUid(): int
{
return $this->listener_uid;

View File

@ -0,0 +1,32 @@
<?php
declare(strict_types=1);
namespace App\Entity\Migration;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
final class Version20220626171758 extends AbstractMigration
{
public function getDescription(): string
{
return 'Add HLS stream relation to listener table.';
}
public function up(Schema $schema): void
{
$this->addSql('ALTER TABLE listener ADD hls_stream_id INT DEFAULT NULL');
$this->addSql(
'ALTER TABLE listener ADD CONSTRAINT FK_959C34226FE7D59F FOREIGN KEY (hls_stream_id) REFERENCES station_hls_streams (id) ON DELETE SET NULL'
);
$this->addSql('CREATE INDEX IDX_959C34226FE7D59F ON listener (hls_stream_id)');
}
public function down(Schema $schema): void
{
$this->addSql('ALTER TABLE listener DROP FOREIGN KEY FK_959C34226FE7D59F');
$this->addSql('DROP INDEX IDX_959C34226FE7D59F ON listener');
$this->addSql('ALTER TABLE listener DROP hls_stream_id');
}
}

View File

@ -160,6 +160,8 @@ final class ListenerRepository extends Repository
$record['mount_id'] = (int)$mountId;
} elseif ('remote' === $mountType) {
$record['remote_id'] = (int)$mountId;
} elseif ('hls' === $mountType) {
$record['hls_stream_id'] = (int)$mountId;
}
}
@ -171,7 +173,7 @@ final class ListenerRepository extends Repository
return $record;
}
protected function batchAddDeviceDetails(array $record): array
private function batchAddDeviceDetails(array $record): array
{
$userAgent = $record['listener_user_agent'];
@ -194,7 +196,7 @@ final class ListenerRepository extends Repository
return $record;
}
protected function batchAddLocationDetails(array $record): array
private function batchAddLocationDetails(array $record): array
{
$ip = $record['listener_ip'];

View File

@ -0,0 +1,32 @@
<?php
declare(strict_types=1);
namespace App\Entity\Repository;
use App\Entity;
/**
* @extends AbstractStationBasedRepository<Entity\StationHlsStream>
*/
final class StationHlsStreamRepository extends AbstractStationBasedRepository
{
/**
* @param Entity\Station $station
*
* @return mixed[]
*/
public function getDisplayNames(Entity\Station $station): array
{
$streams = $this->repository->findBy(['station' => $station]);
$displayNames = [];
/** @var Entity\StationHlsStream $stream */
foreach ($streams as $stream) {
$displayNames[$stream->getIdRequired()] = 'HLS: ' . $stream->getName();
}
return $displayNames;
}
}

View File

@ -52,8 +52,12 @@ final class HlsListeners
);
}
$logContents = array_reverse($logContents);
$streamsByName = [];
$clientsByStream = [];
foreach ($hlsStreams as $hlsStream) {
$streamsByName[$hlsStream->getName()] = $hlsStream->getIdRequired();
$clientsByStream[$hlsStream->getName()] = 0;
}
@ -65,8 +69,10 @@ final class HlsListeners
&& isset($clientsByStream[$client->mount])
&& !isset($allClients[$client->uid])
) {
$allClients[$client->uid] = $client;
$clientsByStream[$client->mount]++;
$client->mount = 'hls_' . $streamsByName[$client->mount];
$allClients[$client->uid] = $client;
}
}