-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Luka Trovic <[email protected]>
- Loading branch information
1 parent
b76a132
commit f798049
Showing
17 changed files
with
184 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
|
||
namespace OCA\Whiteboard\BackgroundJob; | ||
|
||
use OCA\Whiteboard\Service\ConfigService; | ||
use OCA\Whiteboard\Service\StatsService; | ||
use OCP\AppFramework\Utility\ITimeFactory; | ||
use OCP\BackgroundJob\TimedJob; | ||
|
||
class WatchActiveUsers extends TimedJob { | ||
public function __construct( | ||
ITimeFactory $time, | ||
protected bool $isCLI, | ||
protected StatsService $statsService, | ||
protected ConfigService $configService, | ||
) { | ||
parent::__construct($time); | ||
$this->setInterval(300); | ||
} | ||
|
||
protected function run($argument) { | ||
$metricsData = $this->getMetricsData(); | ||
$activeUsers = $metricsData['totalUsers'] ?? 0; | ||
$this->statsService->insertActiveUsersCount($activeUsers); | ||
} | ||
|
||
private function getMetricsData(): array { | ||
$serverUrl = $this->configService->getCollabBackendUrl(); | ||
$metricToken = $this->configService->getCollabBackendMetricsToken(); | ||
|
||
if (!$serverUrl || !$metricToken) { | ||
return []; | ||
} | ||
|
||
$curl = curl_init(); | ||
curl_setopt($curl, CURLOPT_URL, $serverUrl . '/metrics'); | ||
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); | ||
curl_setopt($curl, CURLOPT_HTTPHEADER, [ | ||
'Authorization: Bearer ' . $metricToken, | ||
]); | ||
$response = curl_exec($curl); | ||
curl_close($curl); | ||
|
||
$metrics = [ | ||
'totalUsers' => 0, | ||
]; | ||
|
||
foreach (explode("\n", $response) as $line) { | ||
if (strpos($line, 'whiteboard_room_stats{stat="totalUsers"}') === false) { | ||
continue; | ||
} | ||
$parts = explode(' ', $line); | ||
$metrics['totalUsers'] = (int) $parts[1]; | ||
} | ||
|
||
return $metrics; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
|
||
namespace OCA\Whiteboard\Service; | ||
|
||
use OCP\IDBConnection; | ||
|
||
final class StatsService { | ||
public function __construct( | ||
protected IDBConnection $connection, | ||
) { | ||
} | ||
|
||
public function insertEvent(array $data): void { | ||
$queryBuilder = $this->connection->getQueryBuilder(); | ||
$queryBuilder->insert('whiteboard_events') | ||
->values([ | ||
'user' => $queryBuilder->createNamedParameter($data['user']), | ||
'type' => $queryBuilder->createNamedParameter($data['type']), | ||
'share_token' => $queryBuilder->createNamedParameter($data['share_token']), | ||
'fileid' => $queryBuilder->createNamedParameter($data['fileid']), | ||
'elements' => $queryBuilder->createNamedParameter($data['elements']), | ||
'size' => $queryBuilder->createNamedParameter($data['size']), | ||
'timestamp' => $queryBuilder->createNamedParameter($data['timestamp']), | ||
]) | ||
->executeStatement(); | ||
} | ||
|
||
public function insertActiveUsersCount(int $count): void { | ||
$queryBuilder = $this->connection->getQueryBuilder(); | ||
$queryBuilder->insert('whiteboard_active_users') | ||
->values([ | ||
'total_users' => $queryBuilder->createNamedParameter($count), | ||
'timestamp' => $queryBuilder->createNamedParameter(time()), | ||
]) | ||
->executeStatement(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.