Search in sources :

Example 1 with StatEntry

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;
    }
}
Also used : StatResponseItem(io.jans.as.server.ws.rs.stat.StatResponseItem) StatEntry(io.jans.as.common.model.stat.StatEntry)

Example 2 with StatEntry

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.");
    }
}
Also used : StatEntry(io.jans.as.common.model.stat.StatEntry) EntryPersistenceException(io.jans.orm.exception.EntryPersistenceException) Date(java.util.Date)

Example 3 with StatEntry

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());
            }
        });
    }
}
Also used : Util.escapeLog(io.jans.as.model.util.Util.escapeLog) StatResponse(io.jans.as.server.ws.rs.stat.StatResponse) Logger(org.slf4j.Logger) Filter(io.jans.orm.search.filter.Filter) HLL(net.agkn.hll.HLL) PersistenceEntryManager(io.jans.orm.PersistenceEntryManager) Inject(javax.inject.Inject) TimeUnit(java.util.concurrent.TimeUnit) DependsOn(javax.ejb.DependsOn) Base64(java.util.Base64) List(java.util.List) StatEntry(io.jans.as.common.model.stat.StatEntry) Map(java.util.Map) ApplicationScoped(javax.enterprise.context.ApplicationScoped) CacheBuilder(com.google.common.cache.CacheBuilder) StatResponseItem(io.jans.as.server.ws.rs.stat.StatResponseItem) Named(javax.inject.Named) Cache(com.google.common.cache.Cache) StatEntry(io.jans.as.common.model.stat.StatEntry) Map(java.util.Map)

Aggregations

StatEntry (io.jans.as.common.model.stat.StatEntry)3 StatResponseItem (io.jans.as.server.ws.rs.stat.StatResponseItem)2 Cache (com.google.common.cache.Cache)1 CacheBuilder (com.google.common.cache.CacheBuilder)1 Util.escapeLog (io.jans.as.model.util.Util.escapeLog)1 StatResponse (io.jans.as.server.ws.rs.stat.StatResponse)1 PersistenceEntryManager (io.jans.orm.PersistenceEntryManager)1 EntryPersistenceException (io.jans.orm.exception.EntryPersistenceException)1 Filter (io.jans.orm.search.filter.Filter)1 Base64 (java.util.Base64)1 Date (java.util.Date)1 List (java.util.List)1 Map (java.util.Map)1 TimeUnit (java.util.concurrent.TimeUnit)1 DependsOn (javax.ejb.DependsOn)1 ApplicationScoped (javax.enterprise.context.ApplicationScoped)1 Inject (javax.inject.Inject)1 Named (javax.inject.Named)1 HLL (net.agkn.hll.HLL)1 Logger (org.slf4j.Logger)1