Skip to content

Commit

Permalink
feat(statistics): data collect
Browse files Browse the repository at this point in the history
Signed-off-by: Luka Trovic <[email protected]>
  • Loading branch information
luka-nextcloud committed Jan 6, 2025
1 parent 4aadede commit a29ff7c
Show file tree
Hide file tree
Showing 19 changed files with 621 additions and 1 deletion.
5 changes: 5 additions & 0 deletions appinfo/info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ The official whiteboard app for Nextcloud. It allows users to create and share w
<nextcloud min-version="28" max-version="30"/>
</dependencies>

<background-jobs>
<job>OCA\Whiteboard\BackgroundJob\WatchActiveUsers</job>
<job>OCA\Whiteboard\BackgroundJob\PruneOldStatisticsData</job>
</background-jobs>

<settings>
<admin>OCA\Whiteboard\Settings\Admin</admin>
<admin-section>OCA\Whiteboard\Settings\Section</admin-section>
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"license": "AGPL",
"require": {
"php": "^8.0",
"firebase/php-jwt": "^6.10"
"firebase/php-jwt": "^6.10",
"ext-curl": "*"
},
"require-dev": {
"nextcloud/coding-standard": "^1.0",
Expand Down
9 changes: 9 additions & 0 deletions lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,20 @@

use OCA\Files_Sharing\Event\BeforeTemplateRenderedEvent;
use OCA\Viewer\Event\LoadViewer;
use OCA\Whiteboard\Events\WhiteboardOpenedEvent;
use OCA\Whiteboard\Events\WhiteboardUpdatedEvent;
use OCA\Whiteboard\Listener\AddContentSecurityPolicyListener;
use OCA\Whiteboard\Listener\BeforeTemplateRenderedListener;
use OCA\Whiteboard\Listener\FileCreatedListener;
use OCA\Whiteboard\Listener\LoadViewerListener;
use OCA\Whiteboard\Listener\RegisterTemplateCreatorListener;
use OCA\Whiteboard\Listener\WhiteboardOpenedListener;
use OCA\Whiteboard\Listener\WhiteboardUpdatedListener;
use OCP\AppFramework\App;
use OCP\AppFramework\Bootstrap\IBootContext;
use OCP\AppFramework\Bootstrap\IBootstrap;
use OCP\AppFramework\Bootstrap\IRegistrationContext;
use OCP\Files\Events\Node\NodeCreatedEvent;
use OCP\Files\Template\ITemplateManager;
use OCP\Files\Template\RegisterTemplateCreatorEvent;
use OCP\IL10N;
Expand All @@ -44,6 +50,9 @@ public function register(IRegistrationContext $context): void {
$context->registerEventListener(LoadViewer::class, LoadViewerListener::class);
$context->registerEventListener(RegisterTemplateCreatorEvent::class, RegisterTemplateCreatorListener::class);
$context->registerEventListener(BeforeTemplateRenderedEvent::class, BeforeTemplateRenderedListener::class);
$context->registerEventListener(NodeCreatedEvent::class, FileCreatedListener::class);
$context->registerEventListener(WhiteboardOpenedEvent::class, WhiteboardOpenedListener::class);
$context->registerEventListener(WhiteboardUpdatedEvent::class, WhiteboardUpdatedListener::class);
}

public function boot(IBootContext $context): void {
Expand Down
38 changes: 38 additions & 0 deletions lib/BackgroundJob/PruneOldStatisticsData.php
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);
}
}
75 changes: 75 additions & 0 deletions lib/BackgroundJob/WatchActiveUsers.php
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;
}
}
15 changes: 15 additions & 0 deletions lib/Controller/SettingsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ public function update(): DataResponse {
try {
$serverUrl = $this->request->getParam('serverUrl');
$secret = $this->request->getParam('secret');
$enableStatistics = $this->request->getParam('enableStatistics');
$metricsToken = $this->request->getParam('metricsToken');
$statisticsDataLifetime = $this->request->getParam('statisticsDataLifetime');

if ($serverUrl !== null) {
$this->configService->setCollabBackendUrl($serverUrl);
Expand All @@ -44,6 +47,18 @@ public function update(): DataResponse {
$this->configService->setWhiteboardSharedSecret($secret);
}

if ($enableStatistics !== null) {
$this->configService->setWhiteboardEnableStatistics($enableStatistics);
}

if ($metricsToken !== null) {
$this->configService->setCollabBackendMetricsToken($metricsToken);
}

if ($statisticsDataLifetime !== null) {
$this->configService->setStatisticsDataLifetime((int)$statisticsDataLifetime);
}

return new DataResponse([
'jwt' => $this->jwtService->generateJWTFromPayload([ 'serverUrl' => $serverUrl ])
]);
Expand Down
10 changes: 10 additions & 0 deletions lib/Controller/WhiteboardController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
namespace OCA\Whiteboard\Controller;

use Exception;
use OCA\Whiteboard\Events\WhiteboardOpenedEvent;
use OCA\Whiteboard\Events\WhiteboardUpdatedEvent;
use OCA\Whiteboard\Exception\InvalidUserException;
use OCA\Whiteboard\Exception\UnauthorizedException;
use OCA\Whiteboard\Service\Authentication\GetUserFromIdServiceFactory;
Expand All @@ -23,6 +25,7 @@
use OCP\AppFramework\Http\Attribute\NoCSRFRequired;
use OCP\AppFramework\Http\Attribute\PublicPage;
use OCP\AppFramework\Http\DataResponse;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\IRequest;

/**
Expand All @@ -39,6 +42,7 @@ public function __construct(
private WhiteboardContentService $contentService,
private ExceptionService $exceptionService,
private ConfigService $configService,
private IEventDispatcher $dispatcher,
) {
parent::__construct($appName, $request);
}
Expand All @@ -58,6 +62,9 @@ public function show(int $fileId): DataResponse {

$data = $this->contentService->getContent($file);

$event = new WhiteboardOpenedEvent($file, $user, $data);
$this->dispatcher->dispatchTyped($event);

return new DataResponse(['data' => $data]);
} catch (Exception $e) {
return $this->exceptionService->handleException($e);
Expand All @@ -79,6 +86,9 @@ public function update(int $fileId, array $data): DataResponse {

$this->contentService->updateContent($file, $data);

$event = new WhiteboardUpdatedEvent($file, $user, $data);
$this->dispatcher->dispatchTyped($event);

return new DataResponse(['status' => 'success']);
} catch (Exception $e) {
return $this->exceptionService->handleException($e);
Expand Down
35 changes: 35 additions & 0 deletions lib/Events/AbstractWhiteboardEvent.php
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;
}
}
13 changes: 13 additions & 0 deletions lib/Events/WhiteboardOpenedEvent.php
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 {
}
13 changes: 13 additions & 0 deletions lib/Events/WhiteboardUpdatedEvent.php
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 {
}
56 changes: 56 additions & 0 deletions lib/Listener/FileCreatedListener.php
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(),
]);
}
}
Loading

0 comments on commit a29ff7c

Please sign in to comment.