11package myconext .repository ;
22
33import com .mongodb .client .AggregateIterable ;
4- import myconext .model .User ;
5- import org .springframework .beans .factory .annotation .Autowired ;
6- import org .springframework .data .mongodb .core .MongoTemplate ;
7- import org .springframework .data .mongodb .core .aggregation .*;
4+ import myconext .model .IdpScoping ;
85import org .bson .Document ;
6+ import org .springframework .data .mongodb .core .MongoTemplate ;
97import org .springframework .stereotype .Repository ;
108
119import java .util .Arrays ;
1210import java .util .List ;
13- import java .util .Map ;
1411
1512@ Repository
1613public class MetricsRepository {
@@ -21,44 +18,43 @@ public MetricsRepository(MongoTemplate mongoTemplate) {
2118 this .mongoTemplate = mongoTemplate ;
2219 }
2320
24- public Number getTotalLinkedAccountCount () {
25- UnwindOperation unwindLinkedAccounts = Aggregation .unwind ("linkedAccounts" );
26- CountOperation countTotal = Aggregation .count ().as ("totalLinkedAccounts" );
27- TypedAggregation <User > aggregation = Aggregation .newAggregation (
28- User .class ,
29- unwindLinkedAccounts ,
30- countTotal
31- );
32- AggregationResults <Map > results = mongoTemplate .aggregate (
33- aggregation ,
34- Map .class
35- );
36- return (Number ) results .getUniqueMappedResult ().get ("totalLinkedAccounts" );
21+ public Integer countTotalLinkedAccounts () {
22+ return doInCollection ("users" ,
23+ List .of (
24+ "{ \" $unwind\" : \" $linkedAccounts\" }" ,
25+ "{ \" $count\" : \" totalLinkedAccounts\" }"
26+ ), "totalLinkedAccounts" );
3727 }
3828
39- // Alternative approach: Using MongoTemplate's native execute method
40- public Number countTotalLinkedAccountsNativeExecute () {
41- /*
42- users.aggregate([
43- {
44- "$unwind": "$linkedAccounts"
45- },
46- {
47- "$count": "totalLinkedAccounts"
48- }
49- ])
29+ public Integer countTotalExternalLinkedAccountsByType (IdpScoping idpScoping ) {
30+ return doInCollection ("users" ,
31+ List .of (
32+ "{ \" $unwind\" : \" $externalLinkedAccounts\" }" ,
33+ "{ \" $match\" : { \" externalLinkedAccounts.idpScoping\" : \" " + idpScoping .name () + "\" } }," ,
34+ "{ \" $count\" : \" countExternalLinkedAccounts\" }"
35+ ), "countExternalLinkedAccounts" );
36+ }
5037
51- */
52- return mongoTemplate .execute ("users" , collection -> {
53- List <Document > pipeline = Arrays .asList (
54- Document .parse ("{ \" $unwind\" : \" $linkedAccounts\" }" ),
55- Document .parse ("{ \" $count\" : \" totalLinkedAccounts\" }" )
56- );
38+ public Integer countTotalRegisteredApps () {
39+ return doInCollection ("users" ,
40+ List .of (
41+ "{ \" $unwind\" : \" $eduIDS\" }" ,
42+ "{ \" $unwind\" : \" $eduIDS.services\" }" ,
43+ "{ \" $group\" : { \" _id\" : \" $eduIDS.services.entityId\" } }," ,
44+ "{ \" $count\" : \" countTotalRegisteredApps\" }"
45+ ), "countTotalRegisteredApps" );
46+ }
5747
58- AggregateIterable <Document > result = collection .aggregate (pipeline );
48+ private Integer doInCollection (String collectionName , List <String > pipeLines , String resultKeyWord ) {
49+ return mongoTemplate .execute (collectionName , collection -> {
50+ List <Document > documents = pipeLines
51+ .stream ()
52+ .map (Document ::parse )
53+ .toList ();
54+ AggregateIterable <Document > result = collection .aggregate (documents );
5955 Document doc = result .first ();
60-
61- return doc != null ? doc .getInteger ("totalLinkedAccounts" ) : 0 ;
56+ return doc != null ? doc .getInteger (resultKeyWord ) : 0 ;
6257 });
58+
6359 }
6460}
0 commit comments