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;
}
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;
}
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;
}
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();
}
}
}
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());
}
Aggregations