-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathMetricsRepository.java
More file actions
74 lines (62 loc) · 2.72 KB
/
Copy pathMetricsRepository.java
File metadata and controls
74 lines (62 loc) · 2.72 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
package myconext.repository;
import com.mongodb.client.AggregateIterable;
import myconext.model.IdpScoping;
import org.bson.Document;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public class MetricsRepository {
private final MongoTemplate mongoTemplate;
public MetricsRepository(MongoTemplate mongoTemplate) {
this.mongoTemplate = mongoTemplate;
}
public Integer countTotalLinkedAccounts() {
return doInCollection("users",
List.of(
"{ \"$unwind\": \"$linkedAccounts\" }",
"{ \"$count\": \"totalLinkedAccounts\" }"
), "totalLinkedAccounts");
}
public Integer countTotalAppRegistrations() {
return doInCollection("registrations",
List.of(
"{ \"$count\": \"totalAppRegistrations\" }"
), "totalAppRegistrations");
}
public Integer countTotalExternalLinkedAccountsByType(IdpScoping idpScoping) {
return doInCollection("users",
List.of(
"{ \"$unwind\": \"$externalLinkedAccounts\" }",
"{ \"$match\": { \"externalLinkedAccounts.idpScoping\": \"" + idpScoping.name() + "\" } },",
"{ \"$count\": \"countExternalLinkedAccounts\" }"
), "countExternalLinkedAccounts");
}
public Integer countTotalExternalLinkedAccounts() {
return doInCollection("users",
List.of(
"{ \"$unwind\": \"$externalLinkedAccounts\" }",
"{ \"$count\": \"totalExternalLinkedAccounts\" }"
), "totalExternalLinkedAccounts");
}
public Integer countTotalUsedServices() {
return doInCollection("users",
List.of(
"{ \"$unwind\": \"$eduIDS\" }",
"{ \"$unwind\": \"$eduIDS.services\" }",
"{ \"$group\": { \"_id\": \"$eduIDS.services.entityId\" } },",
"{ \"$count\": \"countTotalUsedServices\" }"
), "countTotalUsedServices");
}
private Integer doInCollection(String collectionName, List<String> pipeLines, String resultKeyWord) {
return mongoTemplate.execute(collectionName, collection -> {
List<Document> documents = pipeLines
.stream()
.map(Document::parse)
.toList();
AggregateIterable<Document> result = collection.aggregate(documents);
Document doc = result.first();
return doc != null ? doc.getInteger(resultKeyWord) : 0;
});
}
}