001/******************************************************************************* 002 * Copyright 2017 The MIT Internet Trust Consortium 003 * 004 * Portions copyright 2011-2013 The MITRE Corporation 005 * 006 * Licensed under the Apache License, Version 2.0 (the "License"); 007 * you may not use this file except in compliance with the License. 008 * You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 *******************************************************************************/ 018/** 019 * 020 */ 021package org.mitre.openid.connect.service.impl; 022 023import java.util.Collection; 024import java.util.HashMap; 025import java.util.HashSet; 026import java.util.Map; 027import java.util.Set; 028import java.util.concurrent.TimeUnit; 029 030import org.mitre.openid.connect.model.ApprovedSite; 031import org.mitre.openid.connect.model.ClientStat; 032import org.mitre.openid.connect.service.ApprovedSiteService; 033import org.mitre.openid.connect.service.StatsService; 034import org.springframework.beans.factory.annotation.Autowired; 035import org.springframework.stereotype.Service; 036 037import com.google.common.base.Supplier; 038import com.google.common.base.Suppliers; 039 040/** 041 * @author jricher 042 * 043 */ 044@Service 045public class DefaultStatsService implements StatsService { 046 047 @Autowired 048 private ApprovedSiteService approvedSiteService; 049 050 // stats cache 051 private Supplier<Map<String, Integer>> summaryCache = createSummaryCache(); 052 053 private Supplier<Map<String, Integer>> createSummaryCache() { 054 return Suppliers.memoizeWithExpiration(new Supplier<Map<String, Integer>>() { 055 @Override 056 public Map<String, Integer> get() { 057 return computeSummaryStats(); 058 } 059 060 }, 10, TimeUnit.MINUTES); 061 } 062 063 @Override 064 public Map<String, Integer> getSummaryStats() { 065 return summaryCache.get(); 066 } 067 068 // do the actual computation 069 private Map<String, Integer> computeSummaryStats() { 070 // get all approved sites 071 Collection<ApprovedSite> allSites = approvedSiteService.getAll(); 072 073 // process to find number of unique users and sites 074 Set<String> userIds = new HashSet<>(); 075 Set<String> clientIds = new HashSet<>(); 076 for (ApprovedSite approvedSite : allSites) { 077 userIds.add(approvedSite.getUserId()); 078 clientIds.add(approvedSite.getClientId()); 079 } 080 081 Map<String, Integer> e = new HashMap<>(); 082 083 e.put("approvalCount", allSites.size()); 084 e.put("userCount", userIds.size()); 085 e.put("clientCount", clientIds.size()); 086 return e; 087 } 088 089 /* (non-Javadoc) 090 * @see org.mitre.openid.connect.service.StatsService#countForClientId(java.lang.String) 091 */ 092 @Override 093 public ClientStat getCountForClientId(String clientId) { 094 095 Collection<ApprovedSite> approvedSites = approvedSiteService.getByClientId(clientId); 096 097 ClientStat stat = new ClientStat(); 098 stat.setApprovedSiteCount(approvedSites.size()); 099 100 return stat; 101 } 102 103 /** 104 * Reset both stats caches on a trigger (before the timer runs out). Resets the timers. 105 */ 106 @Override 107 public void resetCache() { 108 summaryCache = createSummaryCache(); 109 } 110 111}