Search in sources :

Example 6 with MutableHitCount

use of org.ff4j.audit.MutableHitCount in project ff4j by ff4j.

the class EventRepositoryHBase method computeHitCount.

/**
 * {@inheritDoc}
 */
private Map<String, MutableHitCount> computeHitCount(EventQueryDefinition query, String pColName) {
    Map<String, MutableHitCount> hitCount = new HashMap<String, MutableHitCount>();
    try (Connection hbConn = ConnectionFactory.createConnection(conn.getConfig())) {
        try (Table table = hbConn.getTable(AUDIT_TABLENAME)) {
            query.getActionFilters().add(ACTION_CHECK_OK);
            Scan scanQuery = buildQuery(query, Util.set(pColName), EventConstants.TARGET_FEATURE);
            try (ResultScanner scanner = table.getScanner(scanQuery)) {
                for (Result rr = scanner.next(); rr != null; rr = scanner.next()) {
                    String colValue = Bytes.toString(rr.getValue(B_AUDIT_CF, Bytes.toBytes(pColName)));
                    if (hitCount.containsKey(colValue)) {
                        hitCount.get(colValue).inc();
                    } else {
                        hitCount.put(colValue, new MutableHitCount(1));
                    }
                }
            }
        }
    } catch (IOException e) {
        throw new AuditAccessException("Compute hitcount based on " + pColName, e);
    }
    return hitCount;
}
Also used : Table(org.apache.hadoop.hbase.client.Table) ResultScanner(org.apache.hadoop.hbase.client.ResultScanner) HashMap(java.util.HashMap) HBaseConnection(org.ff4j.hbase.HBaseConnection) Connection(org.apache.hadoop.hbase.client.Connection) Scan(org.apache.hadoop.hbase.client.Scan) IOException(java.io.IOException) MutableHitCount(org.ff4j.audit.MutableHitCount) Result(org.apache.hadoop.hbase.client.Result) AuditAccessException(org.ff4j.exception.AuditAccessException)

Example 7 with MutableHitCount

use of org.ff4j.audit.MutableHitCount in project ff4j by ff4j.

the class InMemoryEventRepository method getUserHitCount.

/**
 * {@inheritDoc}
 */
@Override
public Map<String, MutableHitCount> getUserHitCount(EventQueryDefinition query) {
    Map<String, MutableHitCount> hitRatio = new TreeMap<String, MutableHitCount>();
    for (Event event : searchFeatureUsageEvents(query)) {
        String user = Util.hasLength(event.getUser()) ? event.getUser() : "anonymous";
        if (!hitRatio.containsKey(user)) {
            hitRatio.put(user, new MutableHitCount());
        }
        hitRatio.get(user).inc();
    }
    return hitRatio;
}
Also used : Event(org.ff4j.audit.Event) TreeMap(java.util.TreeMap) MutableHitCount(org.ff4j.audit.MutableHitCount)

Example 8 with MutableHitCount

use of org.ff4j.audit.MutableHitCount in project ff4j by ff4j.

the class InMemoryEventRepository method getFeatureUsageHistory.

/**
 * {@inheritDoc}
 */
@Override
public TimeSeriesChart getFeatureUsageHistory(EventQueryDefinition query, TimeUnit units) {
    // Create the interval depending on units
    TimeSeriesChart tsc = new TimeSeriesChart(query.getFrom(), query.getTo(), units);
    for (String currentDay : getCandidateDays(query.getFrom(), query.getTo())) {
        // There are some event this day
        if (featureUsageEvents.containsKey(currentDay)) {
            for (Map.Entry<String, EventSeries> entry : featureUsageEvents.get(currentDay).entrySet()) {
                String currentFeatureName = entry.getKey();
                // Filter feature names if required
                Set<String> filteredFeatures = query.getNamesFilter();
                if (filteredFeatures == null || filteredFeatures.isEmpty() || filteredFeatures.contains(currentFeatureName)) {
                    // Loop over events
                    for (Event evt : entry.getValue()) {
                        // Between bounds (keydate not enough)
                        if (isEventInInterval(evt, query.getFrom(), query.getTo())) {
                            // Create new serie if new feature Name
                            if (!tsc.getSeries().containsKey((currentFeatureName))) {
                                tsc.createNewSerie(currentFeatureName);
                            }
                            // Match FeatureName
                            Serie<Map<String, MutableHitCount>> serie = tsc.getSeries().get(currentFeatureName);
                            // Match SlotName
                            String slotName = tsc.getSdf().format(new Date(evt.getTimestamp()));
                            // Should be always 'true' as the tsc.getsdf().format() will get a slotName.
                            if (serie.getValue().containsKey(slotName)) {
                                // Fast Increment
                                serie.getValue().get(slotName).inc();
                            }
                        }
                    }
                }
            }
        }
    }
    // Recolor series
    List<String> colors = Util.generateHSVGradient("ee1100", "442299", tsc.getSeries().size());
    int idxColor = 0;
    for (Map.Entry<String, Serie<Map<String, MutableHitCount>>> serie : tsc.getSeries().entrySet()) {
        serie.getValue().setColor(colors.get(idxColor));
        idxColor++;
    }
    return tsc;
}
Also used : TimeSeriesChart(org.ff4j.audit.chart.TimeSeriesChart) EventSeries(org.ff4j.audit.EventSeries) Date(java.util.Date) MutableHitCount(org.ff4j.audit.MutableHitCount) Event(org.ff4j.audit.Event) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) TreeMap(java.util.TreeMap) Map(java.util.Map) Serie(org.ff4j.audit.chart.Serie)

Example 9 with MutableHitCount

use of org.ff4j.audit.MutableHitCount in project ff4j by ff4j.

the class TimeSeriesChart method addEvent.

/**
 * Add Event to chart.
 *
 * @param evt
 *      current event
 */
public void addEvent(Event evt) {
    if (!series.containsKey(evt.getName())) {
        createNewSerie(evt.getName());
    }
    String targetSlot = sdf.format(new Date(evt.getTimestamp()));
    Serie<Map<String, MutableHitCount>> targetSerie = series.get(evt.getName());
    if (targetSerie != null) {
        MutableHitCount mhc = targetSerie.getValue().get(targetSlot);
        if (mhc != null) {
            mhc.inc();
        }
    }
}
Also used : Map(java.util.Map) HashMap(java.util.HashMap) Date(java.util.Date) MutableHitCount(org.ff4j.audit.MutableHitCount)

Example 10 with MutableHitCount

use of org.ff4j.audit.MutableHitCount in project ff4j by ff4j.

the class AbstractEventRepositoryTest method testHostHitCount.

/**
 * TDD.
 */
@Test
public void testHostHitCount() throws InterruptedException {
    long start = System.currentTimeMillis();
    // When
    for (int i = 0; i < 8; i++) {
        Thread.sleep(100);
        repo.saveEvent(new Event(SOURCE_JAVA, TARGET_FEATURE, "f1", ACTION_CHECK_OK));
    }
    Thread.sleep(200);
    // Then
    EventQueryDefinition testQuery = new EventQueryDefinition(start, System.currentTimeMillis());
    Map<String, MutableHitCount> mapOfHit = repo.getHostHitCount(testQuery);
    Assert.assertEquals(1, mapOfHit.size());
    Assert.assertEquals(1, mapOfHit.values().size());
}
Also used : EventQueryDefinition(org.ff4j.audit.EventQueryDefinition) Event(org.ff4j.audit.Event) MutableHitCount(org.ff4j.audit.MutableHitCount) Test(org.junit.Test)

Aggregations

MutableHitCount (org.ff4j.audit.MutableHitCount)27 Event (org.ff4j.audit.Event)15 HashMap (java.util.HashMap)14 Test (org.junit.Test)10 EventQueryDefinition (org.ff4j.audit.EventQueryDefinition)8 ResultSet (com.datastax.driver.core.ResultSet)4 Row (com.datastax.driver.core.Row)4 JestResult (io.searchbox.client.JestResult)4 Map (java.util.Map)3 Timestamp (java.sql.Timestamp)2 Date (java.util.Date)2 TreeMap (java.util.TreeMap)2 Serie (org.ff4j.audit.chart.Serie)2 TimeSeriesChart (org.ff4j.audit.chart.TimeSeriesChart)2 IOException (java.io.IOException)1 Connection (java.sql.Connection)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 SimpleDateFormat (java.text.SimpleDateFormat)1