use of io.jans.as.common.model.stat.StatEntry in project jans by JanssenProject.
the class StatResponseService method buildItem.
private StatResponseItem buildItem(String month) {
try {
String monthlyDn = String.format("ou=%s,%s", escapeLog(month), statService.getBaseDn());
final List<StatEntry> entries = entryManager.findEntries(monthlyDn, StatEntry.class, Filter.createPresenceFilter("jansId"));
if (entries == null || entries.isEmpty()) {
log.trace("Can't find stat entries for month: {}", monthlyDn);
return null;
}
final StatResponseItem responseItem = new StatResponseItem();
responseItem.setMonthlyActiveUsers(userCardinality(entries));
responseItem.setMonth(month);
unionTokenMapIntoResponseItem(entries, responseItem);
return responseItem;
} catch (Exception e) {
log.error(e.getMessage(), e);
return null;
}
}
use of io.jans.as.common.model.stat.StatEntry in project jans by JanssenProject.
the class StatService method setupCurrentEntry.
private void setupCurrentEntry(Date now) {
final String month = periodDateFormat.format(now);
// jansId=<id>,ou=yyyyMM,ou=stat,o=gluu
String dn = String.format("jansId=%s,%s", nodeId, monthlyDn);
if (currentEntry != null && month.equals(currentEntry.getStat().getMonth())) {
return;
}
try {
StatEntry entryFromPersistence = entryManager.find(StatEntry.class, dn);
if (entryFromPersistence != null && month.equals(entryFromPersistence.getStat().getMonth())) {
hll = HLL.fromBytes(Base64.getDecoder().decode(entryFromPersistence.getUserHllData()));
tokenCounters = new ConcurrentHashMap<>(entryFromPersistence.getStat().getTokenCountPerGrantType());
currentEntry = entryFromPersistence;
log.trace("Stat entry loaded.");
return;
}
} catch (EntryPersistenceException e) {
log.trace("Stat entry is not found in persistence.");
}
if (currentEntry == null) {
log.trace("Creating stat entry ...");
hll = newHll();
tokenCounters = new ConcurrentHashMap<>();
currentEntry = new StatEntry();
currentEntry.setId(nodeId);
currentEntry.setDn(dn);
currentEntry.setUserHllData(Base64.getEncoder().encodeToString(hll.toBytes()));
currentEntry.getStat().setMonth(periodDateFormat.format(new Date()));
entryManager.persist(currentEntry);
log.trace("Created stat entry.");
}
}
use of io.jans.as.common.model.stat.StatEntry in project jans by JanssenProject.
the class StatResponseService method unionTokenMapIntoResponseItem.
private void unionTokenMapIntoResponseItem(List<StatEntry> entries, StatResponseItem responseItem) {
for (StatEntry entry : entries) {
entry.getStat().getTokenCountPerGrantType().entrySet().stream().filter(en -> en.getValue() != null).forEach(en -> {
final Map<String, Long> tokenMap = responseItem.getTokenCountPerGrantType().get(en.getKey());
if (tokenMap == null) {
responseItem.getTokenCountPerGrantType().put(en.getKey(), en.getValue());
return;
}
for (Map.Entry<String, Long> tokenEntry : en.getValue().entrySet()) {
final Long counter = tokenMap.get(tokenEntry.getKey());
if (counter == null) {
tokenMap.put(tokenEntry.getKey(), tokenEntry.getValue());
continue;
}
tokenMap.put(tokenEntry.getKey(), counter + tokenEntry.getValue());
}
});
}
}
Aggregations