cleaned up server-side stats service, UI now uses per-client calls

pull/1161/merge
Justin Richer 8 years ago
parent 256b79ae51
commit a926a8f0ab

@ -39,13 +39,6 @@ public interface StatsService {
*/ */
public Map<String, Integer> getSummaryStats(); public Map<String, Integer> getSummaryStats();
/**
* Calculate usage count for all clients
*
* @return a map of id of client object to number of approvals
*/
public Map<String, Integer> getByClientId();
/** /**
* Calculate the usage count for a single client * Calculate the usage count for a single client
* *

@ -50,9 +50,6 @@ public class DefaultStatsService implements StatsService {
@Autowired @Autowired
private ApprovedSiteService approvedSiteService; private ApprovedSiteService approvedSiteService;
@Autowired
private ClientDetailsEntityService clientService;
// stats cache // stats cache
private Supplier<Map<String, Integer>> summaryCache = createSummaryCache(); private Supplier<Map<String, Integer>> summaryCache = createSummaryCache();
@ -66,18 +63,6 @@ public class DefaultStatsService implements StatsService {
}, 10, TimeUnit.MINUTES); }, 10, TimeUnit.MINUTES);
} }
private Supplier<Map<String, Integer>> byClientIdCache = createByClientIdCache();
private Supplier<Map<String, Integer>> createByClientIdCache() {
return Suppliers.memoizeWithExpiration(new Supplier<Map<String, Integer>>() {
@Override
public Map<String, Integer> get() {
return computeByClientId();
}
}, 10, TimeUnit.MINUTES);
}
@Override @Override
public Map<String, Integer> getSummaryStats() { public Map<String, Integer> getSummaryStats() {
return summaryCache.get(); return summaryCache.get();
@ -104,66 +89,26 @@ public class DefaultStatsService implements StatsService {
return e; return e;
} }
/* (non-Javadoc)
* @see org.mitre.openid.connect.service.StatsService#calculateByClientId()
*/
@Override
public Map<String, Integer> getByClientId() {
return byClientIdCache.get();
}
private Map<String, Integer> computeByClientId() {
// get all approved sites
Collection<ApprovedSite> allSites = approvedSiteService.getAll();
Multiset<String> clientIds = HashMultiset.create();
for (ApprovedSite approvedSite : allSites) {
clientIds.add(approvedSite.getClientId());
}
Map<String, Integer> counts = getEmptyClientCountMap();
for (String clientId : clientIds) {
ClientDetailsEntity client = clientService.loadClientByClientId(clientId);
counts.put(client.getClientId(), clientIds.count(clientId));
}
return counts;
}
/* (non-Javadoc) /* (non-Javadoc)
* @see org.mitre.openid.connect.service.StatsService#countForClientId(java.lang.String) * @see org.mitre.openid.connect.service.StatsService#countForClientId(java.lang.String)
*/ */
@Override @Override
public ClientStat getCountForClientId(String id) { public ClientStat getCountForClientId(String clientId) {
Collection<ApprovedSite> approvedSites = approvedSiteService.getByClientId(clientId);
Map<String, Integer> counts = getByClientId();
ClientStat stat = new ClientStat(); ClientStat stat = new ClientStat();
stat.setApprovedSiteCount(counts.get(id)); stat.setApprovedSiteCount(approvedSites.size());
return stat; return stat;
} }
/**
* Create a new map of all client ids set to zero
* @return
*/
private Map<String, Integer> getEmptyClientCountMap() {
Map<String, Integer> counts = new HashMap<>();
Collection<ClientDetailsEntity> clients = clientService.getAllClients();
for (ClientDetailsEntity client : clients) {
counts.put(client.getClientId(), 0);
}
return counts;
}
/** /**
* Reset both stats caches on a trigger (before the timer runs out). Resets the timers. * Reset both stats caches on a trigger (before the timer runs out). Resets the timers.
*/ */
@Override @Override
public void resetCache() { public void resetCache() {
summaryCache = createSummaryCache(); summaryCache = createSummaryCache();
byClientIdCache = createByClientIdCache();
} }
} }

Loading…
Cancel
Save