-
-
Notifications
You must be signed in to change notification settings - Fork 5.1k
Expand file tree
/
Copy pathSharesCountMetric.php
More file actions
75 lines (63 loc) · 1.58 KB
/
Copy pathSharesCountMetric.php
File metadata and controls
75 lines (63 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\Files_Sharing\OpenMetrics;
use Generator;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IDBConnection;
use OCP\OpenMetrics\IMetricFamily;
use OCP\OpenMetrics\Metric;
use OCP\OpenMetrics\MetricType;
use OCP\Share\IShare;
use Override;
/**
* Count shares by type
* @since 33.0.0
*/
class SharesCountMetric implements IMetricFamily {
public function __construct(
private IDBConnection $connection,
) {
}
#[Override]
public function name(): string {
return 'shares';
}
#[Override]
public function type(): MetricType {
return MetricType::gauge;
}
#[Override]
public function unit(): string {
return 'shares';
}
#[Override]
public function help(): string {
return 'Number of shares by type';
}
#[Override]
public function metrics(): Generator {
$types = [
IShare::TYPE_USER => 'user',
IShare::TYPE_GROUP => 'group',
IShare::TYPE_LINK => 'link',
IShare::TYPE_EMAIL => 'email',
];
$qb = $this->connection->getQueryBuilder();
$result = $qb->select($qb->func()->count('*', 'count'), 'share_type')
->from('share')
->where($qb->expr()->in('share_type', $qb->createNamedParameter(array_keys($types), IQueryBuilder::PARAM_INT_ARRAY)))
->groupBy('share_type')
->executeQuery();
if ($result->rowCount() === 0) {
yield new Metric(0);
return;
}
foreach ($result->iterateAssociative() as $row) {
yield new Metric($row['count'], ['type' => $types[$row['share_type']]]);
}
}
}