Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions appinfo/info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -167,4 +167,10 @@
<plugin type="collaborator-search" share-type="SHARE_TYPE_ROOM">OCA\Talk\Collaboration\Collaborators\RoomPlugin</plugin>
</plugins>
</collaboration>

<openmetrics>
<exporter>OCA\Talk\OpenMetrics\ActiveCalls</exporter>
<exporter>OCA\Talk\OpenMetrics\InCallSessions</exporter>
</openmetrics>

</info>
31 changes: 7 additions & 24 deletions lib/Command/Monitor/HasActiveCalls.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,14 @@
namespace OCA\Talk\Command\Monitor;

use OC\Core\Command\Base;
use OCA\Talk\Participant;
use OCP\IDBConnection;
use OCA\Talk\Service\MetricsService;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class HasActiveCalls extends Base {

public function __construct(
protected IDBConnection $connection,
protected MetricsService $metricsService,
) {
parent::__construct();
}
Expand All @@ -33,15 +32,7 @@ protected function configure(): void {
}

protected function execute(InputInterface $input, OutputInterface $output): int {
$query = $this->connection->getQueryBuilder();

$query->select($query->func()->count('*', 'num_calls'))
->from('talk_rooms')
->where($query->expr()->isNotNull('active_since'));

$result = $query->executeQuery();
$numCalls = (int)$result->fetchColumn();
$result->closeCursor();
$numCalls = $this->metricsService->getNumberOfActiveCalls();

if ($numCalls === 0) {
if ($input->getOption('output') === 'plain') {
Expand All @@ -53,21 +44,13 @@ protected function execute(InputInterface $input, OutputInterface $output): int
return 0;
}

$query = $this->connection->getQueryBuilder();
$query->select($query->func()->count('*', 'num_participants'))
->from('talk_sessions')
->where($query->expr()->gt('in_call', $query->createNamedParameter(Participant::FLAG_DISCONNECTED)))
->andWhere($query->expr()->gt('last_ping', $query->createNamedParameter(time() - 60)));

$result = $query->executeQuery();
$numParticipants = (int)$result->fetchColumn();
$result->closeCursor();

$numSessions = $this->metricsService->getNumberOfSessionsInCalls();

// We keep "participants" here, to not break scripting done with that command
if ($input->getOption('output') === 'plain') {
$output->writeln(sprintf('<error>There are currently %1$d calls in progress with %2$d participants</error>', $numCalls, $numParticipants));
$output->writeln(sprintf('<error>There are currently %1$d calls in progress with %2$d participants</error>', $numCalls, $numSessions));
} else {
$data = ['calls' => $numCalls, 'participants' => $numParticipants];
$data = ['calls' => $numCalls, 'participants' => $numSessions];
$this->writeArrayInOutputFormat($input, $output, $data);
}
return 1;
Expand Down
49 changes: 49 additions & 0 deletions lib/OpenMetrics/ActiveCalls.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

declare(strict_types=1);

/**
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCA\Talk\OpenMetrics;

use Generator;
use OCA\Talk\Service\MetricsService;
use OCP\OpenMetrics\IMetricFamily;
use OCP\OpenMetrics\Metric;
use OCP\OpenMetrics\MetricType;
use Override;

class ActiveCalls implements IMetricFamily {
public function __construct(
private MetricsService $metricsService,
) {
}

#[Override]
public function name(): string {
return 'talk_active_calls';
}

#[Override]
public function type(): MetricType {
return MetricType::gauge;
}

#[Override]
public function unit(): string {
return 'calls';
}

#[Override]
public function help(): string {
return 'Number of active calls in talk';
}

#[Override]
public function metrics(): Generator {
yield new Metric($this->metricsService->getNumberOfActiveCalls(), [], time());
}
}
49 changes: 49 additions & 0 deletions lib/OpenMetrics/InCallSessions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

declare(strict_types=1);

/**
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCA\Talk\OpenMetrics;

use Generator;
use OCA\Talk\Service\MetricsService;
use OCP\OpenMetrics\IMetricFamily;
use OCP\OpenMetrics\Metric;
use OCP\OpenMetrics\MetricType;
use Override;

class InCallSessions implements IMetricFamily {
public function __construct(
private MetricsService $metricsService,
) {
}

#[Override]
public function name(): string {
return 'talk_in_call_sessions';
}

#[Override]
public function type(): MetricType {
return MetricType::gauge;
}

#[Override]
public function unit(): string {
return 'sessions';
}

#[Override]
public function help(): string {
return 'Number of sessions in calls in talk';
}

#[Override]
public function metrics(): Generator {
yield new Metric($this->metricsService->getNumberOfSessionsInCalls(), [], time());
}
}
52 changes: 52 additions & 0 deletions lib/Service/MetricsService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

declare(strict_types=1);

/**
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCA\Talk\Service;

use OCA\Talk\Participant;
use OCP\IDBConnection;
use Psr\Log\LoggerInterface;

class MetricsService {

public function __construct(
protected LoggerInterface $logger,
protected IDBConnection $connection,
) {
}

public function getNumberOfActiveCalls(): int {
$query = $this->connection->getQueryBuilder();

$query->select($query->func()->count('*', 'num_calls'))
->from('talk_rooms')
->where($query->expr()->isNotNull('active_since'));

$result = $query->executeQuery();
$numCalls = (int)$result->fetchOne();
$result->closeCursor();

return $numCalls;
}

public function getNumberOfSessionsInCalls(): int {
$query = $this->connection->getQueryBuilder();
$query->select($query->func()->count('*', 'num_sessions'))
->from('talk_sessions')
->where($query->expr()->gt('in_call', $query->createNamedParameter(Participant::FLAG_DISCONNECTED)))
->andWhere($query->expr()->gt('last_ping', $query->createNamedParameter(time() - 60)));

$result = $query->executeQuery();
$numSessions = (int)$result->fetchColumn();
$result->closeCursor();

return $numSessions;
}

}
Loading