-
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
4aadede
commit a29ff7c
Showing
19 changed files
with
621 additions
and
1 deletion.
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
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,38 @@ | ||
<?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 PruneOldStatisticsData extends TimedJob { | ||
public function __construct( | ||
ITimeFactory $time, | ||
protected bool $isCLI, | ||
protected StatsService $statsService, | ||
protected ConfigService $configService, | ||
) { | ||
parent::__construct($time); | ||
$this->setInterval(24 * 60 * 60); | ||
} | ||
|
||
protected function run($argument) { | ||
$lifeTimeInDays = $this->configService->getStatisticsDataLifetime(); | ||
|
||
if (!$lifeTimeInDays) { | ||
return; | ||
} | ||
|
||
$beforeTime = time() - $lifeTimeInDays * 24 * 60 * 60; | ||
$this->statsService->pruneData($beforeTime); | ||
} | ||
} |
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,75 @@ | ||
<?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; | ||
use OCP\Http\Client\IClientService; | ||
|
||
class WatchActiveUsers extends TimedJob { | ||
public function __construct( | ||
ITimeFactory $time, | ||
protected bool $isCLI, | ||
protected StatsService $statsService, | ||
protected ConfigService $configService, | ||
protected IClientService $clientService, | ||
) { | ||
parent::__construct($time); | ||
$this->setInterval(300); | ||
} | ||
|
||
protected function run($argument) { | ||
if (!$this->configService->getWhiteboardEnableStatistics()) { | ||
return; | ||
} | ||
$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 []; | ||
} | ||
|
||
$client = $this->clientService->newClient(); | ||
$response = $client->get($serverUrl . '/metrics', [ | ||
'headers' => [ | ||
'Accept' => 'application/json', | ||
'Content-Type' => 'application/json', | ||
'Authorization' => 'Bearer ' . $metricToken, | ||
], | ||
]); | ||
$responseBody = $response->getBody(); | ||
|
||
$metrics = [ | ||
'totalUsers' => 10, | ||
]; | ||
|
||
if (!is_string($responseBody)) { | ||
return $metrics; | ||
} | ||
|
||
foreach (explode("\n", $responseBody) as $line) { | ||
if (strpos($line, 'socket_io_connected') === 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
|
||
namespace OCA\Whiteboard\Events; | ||
|
||
use OCA\Whiteboard\Model\User; | ||
use OCP\EventDispatcher\Event; | ||
use OCP\Files\File; | ||
|
||
abstract class AbstractWhiteboardEvent extends Event { | ||
public function __construct( | ||
private File $file, | ||
private User $user, | ||
private array $data, | ||
) { | ||
} | ||
|
||
public function getFile(): File { | ||
return $this->file; | ||
} | ||
|
||
public function getUser(): User { | ||
return $this->user; | ||
} | ||
|
||
public function getData(): array { | ||
return $this->data; | ||
} | ||
} |
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,13 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
|
||
namespace OCA\Whiteboard\Events; | ||
|
||
class WhiteboardOpenedEvent extends AbstractWhiteboardEvent { | ||
} |
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,13 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
|
||
namespace OCA\Whiteboard\Events; | ||
|
||
class WhiteboardUpdatedEvent extends AbstractWhiteboardEvent { | ||
} |
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,56 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
|
||
namespace OCA\Whiteboard\Listener; | ||
|
||
use OCA\Whiteboard\Service\ConfigService; | ||
use OCA\Whiteboard\Service\StatsService; | ||
use OCP\EventDispatcher\Event; | ||
use OCP\EventDispatcher\IEventListener; | ||
use OCP\Files\Events\Node\NodeCreatedEvent; | ||
use OCP\IUserSession; | ||
|
||
/** @template-implements IEventListener<NodeCreatedEvent|Event> */ | ||
/** | ||
* @psalm-suppress UndefinedClass | ||
* @psalm-suppress MissingTemplateParam | ||
*/ | ||
final class FileCreatedListener implements IEventListener { | ||
public function __construct( | ||
protected StatsService $statsService, | ||
protected IUserSession $userSession, | ||
protected ConfigService $configService, | ||
) { | ||
} | ||
|
||
public function handle(Event $event): void { | ||
if (!($event instanceof NodeCreatedEvent) || !$this->configService->getWhiteboardEnableStatistics()) { | ||
return; | ||
} | ||
|
||
$node = $event->getNode(); | ||
|
||
if ($node->getExtension() !== 'whiteboard') { | ||
return; | ||
} | ||
|
||
$currentUser = $this->userSession->getUser(); | ||
$ownerUser = $node->getOwner(); | ||
|
||
$this->statsService->insertEvent([ | ||
'user' => $currentUser ? $currentUser->getUID() : ($ownerUser ? $ownerUser->getUID() : null), | ||
'type' => 'created', | ||
'share_token' => '', | ||
'fileid' => $node->getId(), | ||
'elements' => json_encode([]), | ||
'size' => 0, | ||
'timestamp' => time(), | ||
]); | ||
} | ||
} |
Oops, something went wrong.