/******************************************************************************* * Copyright 2018 The MIT Internet Trust Consortium * * Portions copyright 2011-2013 The MITRE Corporation * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. *******************************************************************************/ /** * */ package org.mitre.openid.connect.service.impl; import java.util.Collection; import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Set; import java.util.concurrent.TimeUnit; import org.mitre.openid.connect.model.ApprovedSite; import org.mitre.openid.connect.model.ClientStat; import org.mitre.openid.connect.service.ApprovedSiteService; import org.mitre.openid.connect.service.StatsService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.google.common.base.Supplier; import com.google.common.base.Suppliers; /** * @author jricher * */ @Service public class DefaultStatsService implements StatsService { @Autowired private ApprovedSiteService approvedSiteService; // stats cache private Supplier> summaryCache = createSummaryCache(); private Supplier> createSummaryCache() { return Suppliers.memoizeWithExpiration(new Supplier>() { @Override public Map get() { return computeSummaryStats(); } }, 10, TimeUnit.MINUTES); } @Override public Map getSummaryStats() { return summaryCache.get(); } // do the actual computation private Map computeSummaryStats() { // get all approved sites Collection allSites = approvedSiteService.getAll(); // process to find number of unique users and sites Set userIds = new HashSet<>(); Set clientIds = new HashSet<>(); for (ApprovedSite approvedSite : allSites) { userIds.add(approvedSite.getUserId()); clientIds.add(approvedSite.getClientId()); } Map e = new HashMap<>(); e.put("approvalCount", allSites.size()); e.put("userCount", userIds.size()); e.put("clientCount", clientIds.size()); return e; } /* (non-Javadoc) * @see org.mitre.openid.connect.service.StatsService#countForClientId(java.lang.String) */ @Override public ClientStat getCountForClientId(String clientId) { Collection approvedSites = approvedSiteService.getByClientId(clientId); ClientStat stat = new ClientStat(); stat.setApprovedSiteCount(approvedSites.size()); return stat; } /** * Reset both stats caches on a trigger (before the timer runs out). Resets the timers. */ @Override public void resetCache() { summaryCache = createSummaryCache(); } }