Skip to content

Commit 503933a

Browse files
feat(statistics): data collect
Signed-off-by: Luka Trovic <[email protected]>
1 parent f798049 commit 503933a

25 files changed

+526
-935
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
/css/
88
/vendor/
99
/node_modules/
10-
/backup/
1110

1211
.php-cs-fixer.cache
1312
.phpunit.result.cache

README.md

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ docker build -t nextcloud-whiteboard-server -f Dockerfile .
8484

8585
### Reverse proxy
8686

87-
#### Apache < 2.4.47
87+
#### Apache
8888

8989
```
9090
ProxyPass /whiteboard http://localhost:3002/
@@ -94,12 +94,6 @@ RewriteCond %{HTTP:Connection} upgrade [NC]
9494
RewriteRule ^/?whiteboard/(.*) "ws://localhost:3002/$1" [P,L]
9595
```
9696

97-
#### Apache >= 2.4.47
98-
99-
```
100-
ProxyPass /whiteboard http://localhost:3002/ upgrade=websocket
101-
```
102-
10397
#### Nginx
10498

10599
```

appinfo/info.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ The official whiteboard app for Nextcloud. It allows users to create and share w
4545

4646
<background-jobs>
4747
<job>OCA\Whiteboard\BackgroundJob\WatchActiveUsers</job>
48+
<job>OCA\Whiteboard\BackgroundJob\PruneOldStatisticsData</job>
4849
</background-jobs>
4950

5051
<settings>

lib/BackgrounJob/WatchActiveUsers.php

Lines changed: 0 additions & 65 deletions
This file was deleted.
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
7+
* SPDX-License-Identifier: AGPL-3.0-or-later
8+
*/
9+
10+
namespace OCA\Whiteboard\BackgroundJob;
11+
12+
use OCA\Whiteboard\Service\ConfigService;
13+
use OCA\Whiteboard\Service\StatsService;
14+
use OCP\AppFramework\Utility\ITimeFactory;
15+
use OCP\BackgroundJob\TimedJob;
16+
17+
class PruneOldStatisticsData extends TimedJob {
18+
public function __construct(
19+
ITimeFactory $time,
20+
protected bool $isCLI,
21+
protected StatsService $statsService,
22+
protected ConfigService $configService,
23+
) {
24+
parent::__construct($time);
25+
$this->setInterval(24 * 60 * 60);
26+
}
27+
28+
protected function run($argument) {
29+
$lifeTimeInDays = $this->configService->getStatisticsDataLifetime();
30+
31+
if (!$lifeTimeInDays) {
32+
return;
33+
}
34+
35+
$beforeTime = time() - $lifeTimeInDays * 24 * 60 * 60;
36+
$this->statsService->pruneData($beforeTime);
37+
}
38+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
7+
* SPDX-License-Identifier: AGPL-3.0-or-later
8+
*/
9+
10+
namespace OCA\Whiteboard\BackgroundJob;
11+
12+
use OCA\Whiteboard\Service\ConfigService;
13+
use OCA\Whiteboard\Service\StatsService;
14+
use OCP\AppFramework\Utility\ITimeFactory;
15+
use OCP\BackgroundJob\TimedJob;
16+
17+
class WatchActiveUsers extends TimedJob {
18+
public function __construct(
19+
ITimeFactory $time,
20+
protected bool $isCLI,
21+
protected StatsService $statsService,
22+
protected ConfigService $configService,
23+
) {
24+
parent::__construct($time);
25+
$this->setInterval(300);
26+
}
27+
28+
protected function run($argument) {
29+
$metricsData = $this->getMetricsData();
30+
$activeUsers = $metricsData['totalUsers'] ?? 0;
31+
$this->statsService->insertActiveUsersCount($activeUsers);
32+
}
33+
34+
private function getMetricsData(): array {
35+
$serverUrl = $this->configService->getCollabBackendUrl();
36+
$metricToken = $this->configService->getCollabBackendMetricsToken();
37+
38+
if (!$serverUrl || !$metricToken) {
39+
return [];
40+
}
41+
42+
$curl = curl_init();
43+
curl_setopt($curl, CURLOPT_URL, $serverUrl . '/metrics');
44+
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
45+
curl_setopt($curl, CURLOPT_HTTPHEADER, [
46+
'Authorization: Bearer ' . $metricToken,
47+
]);
48+
$response = curl_exec($curl);
49+
curl_close($curl);
50+
51+
$metrics = [
52+
'totalUsers' => 0,
53+
];
54+
55+
foreach (explode("\n", $response) as $line) {
56+
if (strpos($line, 'whiteboard_room_stats{stat="totalUsers"}') === false) {
57+
continue;
58+
}
59+
$parts = explode(' ', $line);
60+
$metrics['totalUsers'] = (int) $parts[1];
61+
}
62+
63+
return $metrics;
64+
}
65+
}

lib/Controller/SettingsController.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public function update(): DataResponse {
3737
$secret = $this->request->getParam('secret');
3838
$enableStatistics = $this->request->getParam('enableStatistics');
3939
$metricsToken = $this->request->getParam('metricsToken');
40+
$statisticsDataLifetime = $this->request->getParam('statisticsDataLifetime');
4041

4142
if ($serverUrl !== null) {
4243
$this->configService->setCollabBackendUrl($serverUrl);
@@ -54,6 +55,10 @@ public function update(): DataResponse {
5455
$this->configService->setCollabBackendMetricsToken($metricsToken);
5556
}
5657

58+
if ($statisticsDataLifetime !== null) {
59+
$this->configService->setStatisticsDataLifetime((int) $statisticsDataLifetime);
60+
}
61+
5762
return new DataResponse([
5863
'jwt' => $this->jwtService->generateJWTFromPayload([ 'serverUrl' => $serverUrl ])
5964
]);

lib/Events/AbstractWhiteboardEvent.php

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,22 @@
1414
use OCP\Files\File;
1515

1616
abstract class AbstractWhiteboardEvent extends Event {
17-
public function __construct(
18-
private File $file,
19-
private User $user,
20-
private array $data,
21-
) {
22-
}
23-
24-
public function getFile(): File {
25-
return $this->file;
26-
}
27-
28-
public function getUser(): User {
29-
return $this->user;
30-
}
31-
32-
public function getData(): array {
33-
return $this->data;
34-
}
17+
public function __construct(
18+
private File $file,
19+
private User $user,
20+
private array $data,
21+
) {
22+
}
23+
24+
public function getFile(): File {
25+
return $this->file;
26+
}
27+
28+
public function getUser(): User {
29+
return $this->user;
30+
}
31+
32+
public function getData(): array {
33+
return $this->data;
34+
}
3535
}

lib/Listener/FileCreatedListener.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public function handle(Event $event): void {
4343
'type' => 'created',
4444
'share_token' => '',
4545
'fileid' => $node->getId(),
46-
'elements' => '',
46+
'elements' => json_encode([]),
4747
'size' => 0,
4848
'timestamp' => time(),
4949
]);

lib/Listener/WhiteboardOpenedListener.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public function handle(Event $event): void {
4141
'type' => 'opened',
4242
'share_token' => $user instanceof PublicSharingUser ? $user->getPublicSharingToken() : '',
4343
'fileid' => $file->getId(),
44-
'elements' => $data['elements'] ?? '',
44+
'elements' => json_encode($data['elements'] ?? []),
4545
'size' => $file->getSize(),
4646
'timestamp' => time(),
4747
]);

0 commit comments

Comments
 (0)