Skip to content

Commit 19d9463

Browse files
committed
feat(files_sharing): add basic OpenMetrics exporter for files shares
Signed-off-by: Benjamin Gaussorgues <[email protected]>
1 parent 047d07a commit 19d9463

File tree

4 files changed

+81
-0
lines changed

4 files changed

+81
-0
lines changed

apps/files_sharing/appinfo/info.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,4 +87,8 @@ Turning the feature off removes shared files and folders on the server for all s
8787
<public>
8888
<files>public.php</files>
8989
</public>
90+
91+
<openmetrics>
92+
<exporter>OCA\Files_Sharing\OpenMetrics\SharesCount</exporter>
93+
</openmetrics>
9094
</info>

apps/files_sharing/composer/composer/autoload_classmap.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@
8585
'OCA\\Files_Sharing\\MountProvider' => $baseDir . '/../lib/MountProvider.php',
8686
'OCA\\Files_Sharing\\Notification\\Listener' => $baseDir . '/../lib/Notification/Listener.php',
8787
'OCA\\Files_Sharing\\Notification\\Notifier' => $baseDir . '/../lib/Notification/Notifier.php',
88+
'OCA\\Files_Sharing\\OpenMetrics\\SharesCount' => $baseDir . '/../lib/OpenMetrics/SharesCount.php',
8889
'OCA\\Files_Sharing\\OrphanHelper' => $baseDir . '/../lib/OrphanHelper.php',
8990
'OCA\\Files_Sharing\\ResponseDefinitions' => $baseDir . '/../lib/ResponseDefinitions.php',
9091
'OCA\\Files_Sharing\\Scanner' => $baseDir . '/../lib/Scanner.php',

apps/files_sharing/composer/composer/autoload_static.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ class ComposerStaticInitFiles_Sharing
100100
'OCA\\Files_Sharing\\MountProvider' => __DIR__ . '/..' . '/../lib/MountProvider.php',
101101
'OCA\\Files_Sharing\\Notification\\Listener' => __DIR__ . '/..' . '/../lib/Notification/Listener.php',
102102
'OCA\\Files_Sharing\\Notification\\Notifier' => __DIR__ . '/..' . '/../lib/Notification/Notifier.php',
103+
'OCA\\Files_Sharing\\OpenMetrics\\SharesCount' => __DIR__ . '/..' . '/../lib/OpenMetrics/SharesCount.php',
103104
'OCA\\Files_Sharing\\OrphanHelper' => __DIR__ . '/..' . '/../lib/OrphanHelper.php',
104105
'OCA\\Files_Sharing\\ResponseDefinitions' => __DIR__ . '/..' . '/../lib/ResponseDefinitions.php',
105106
'OCA\\Files_Sharing\\Scanner' => __DIR__ . '/..' . '/../lib/Scanner.php',
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
7+
* SPDX-License-Identifier: AGPL-3.0-or-later
8+
*/
9+
10+
namespace OCA\Files_Sharing\OpenMetrics;
11+
12+
use Generator;
13+
use OCP\DB\QueryBuilder\IQueryBuilder;
14+
use OCP\IDBConnection;
15+
use OCP\OpenMetrics\IMetricFamily;
16+
use OCP\OpenMetrics\Metric;
17+
use OCP\OpenMetrics\MetricTypes;
18+
use OCP\Share\IShare;
19+
use Override;
20+
21+
/**
22+
* Count shares by type
23+
* @since 33.0.0
24+
*/
25+
class SharesCount implements IMetricFamily {
26+
public function __construct(
27+
private IDBConnection $connection,
28+
) {
29+
}
30+
31+
#[Override]
32+
public function name(): string {
33+
return 'shares';
34+
}
35+
36+
#[Override]
37+
public function type(): MetricTypes {
38+
return MetricTypes::gauge;
39+
}
40+
41+
#[Override]
42+
public function unit(): string {
43+
return 'shares';
44+
}
45+
46+
#[Override]
47+
public function help(): string {
48+
return 'Number of shares';
49+
}
50+
51+
#[Override]
52+
public function metrics(): Generator {
53+
$types = [
54+
IShare::TYPE_USER => 'user',
55+
IShare::TYPE_GROUP => 'group',
56+
IShare::TYPE_LINK => 'link',
57+
IShare::TYPE_EMAIL => 'email',
58+
];
59+
$qb = $this->connection->getQueryBuilder();
60+
$result = $qb->select($qb->func()->count('*', 'count'), 'share_type')
61+
->from('share')
62+
->where($qb->expr()->in('share_type', $qb->createNamedParameter(array_keys($types), IQueryBuilder::PARAM_INT_ARRAY)))
63+
->groupBy('share_type')
64+
->executeQuery();
65+
66+
if ($result->rowCount() === 0) {
67+
yield new Metric(0);
68+
return;
69+
}
70+
71+
while ($row = $result->fetch()) {
72+
yield new Metric($row['count'], ['type' => $types[$row['share_type']]]);
73+
}
74+
}
75+
}

0 commit comments

Comments
 (0)