/** {@inheritDoc} */ @Override public Map<String, MutableHitCount> getSourceHitCount(EventQueryDefinition query) { Map<String, MutableHitCount> hitRatio = new TreeMap<String, MutableHitCount>(); for (Event event : searchFeatureUsageEvents(query)) { if (!hitRatio.containsKey(event.getSource())) { hitRatio.put(event.getSource(), new MutableHitCount()); } hitRatio.get(event.getSource()).inc(); } return hitRatio; }
/** {@inheritDoc} */ public String toString() { return String.valueOf(get()); }
/** {@inheritDoc} */ @Override public HitCountDto mapRow(ResultSet rs, int rowNum) throws SQLException { return new HitCountDto(rs.getString(columnName), new MutableHitCount(rs.getInt("NB"))); }
/** * 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(); } } }
/** * Create new Serie with existing slots. * * @param idSerie * target serie id */ public void createNewSerie(String idSerie) { // Init new Serie Serie< Map < String, MutableHitCount >> newSerie = new Serie<Map<String,MutableHitCount>>(idSerie); // Populate slots Map < String, MutableHitCount > val = new HashMap<String, MutableHitCount>(); for (String slot : timeSlots) { val.put(slot, new MutableHitCount()); } newSerie.setValue(val); series.put(idSerie, newSerie); }
serie.getValue().get(slotName).inc();
/** {@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; }
/** {@inheritDoc} */ private Map<String, MutableHitCount> computeHitCount(String sqlQuery, String columnName, long from, long to) { Connection sqlConn = null; PreparedStatement ps = null; ResultSet rs = null; Map<String, MutableHitCount> hitCount = new HashMap<String, MutableHitCount>(); try { // Returns features sqlConn = dataSource.getConnection(); ps = sqlConn.prepareStatement(sqlQuery); ps.setTimestamp(1, new Timestamp(from)); ps.setTimestamp(2, new Timestamp(to)); rs = ps.executeQuery(); while (rs.next()) { hitCount.put(rs.getString(columnName), new MutableHitCount(rs.getInt("NB"))); } } catch (SQLException sqlEX) { throw new FeatureAccessException(CANNOT_BUILD_PIE_CHART_FROM_REPOSITORY, sqlEX); } finally { closeResultSet(rs); closeStatement(ps); closeConnection(sqlConn); } return hitCount; }
/** {@inheritDoc} */ @Override public int getFeatureUsageTotalHitCount(EventQueryDefinition q) { Map < String, MutableHitCount > hitRatio = getFeatureUsageHitCount(q); int total = 0; if (hitRatio != null) { for (MutableHitCount hc : hitRatio.values()) { total += hc.get(); } } return total; }
/** {@inheritDoc} */ @Override public Map<String, MutableHitCount> getFeatureUsageHitCount(EventQueryDefinition query) { Map<String, MutableHitCount> hitRatio = new TreeMap<String, MutableHitCount>(); for (Event event : searchFeatureUsageEvents(query)) { if (!hitRatio.containsKey(event.getName())) { hitRatio.put(event.getName(), new MutableHitCount()); } hitRatio.get(event.getName()).inc(); } return hitRatio; }
/** * Convert hicount into pieChart. * * @param hitRatio * current hist ratio * @return * pie chart */ private PieChart renderPieChart(String title, Map < String, MutableHitCount > hitRatio, List < String > colors) { PieChart pieChart = new PieChart(title); int idxColor = 0; for (String key : hitRatio.keySet()) { Serie<Integer> ps = new Serie<Integer>(key, hitRatio.get(key).get(), colors.get(idxColor)); pieChart.getSectors().add(ps); idxColor++; } return pieChart; }
/** {@inheritDoc} */ @Override public Map<String, MutableHitCount> getHostHitCount(EventQueryDefinition query) { Map<String, MutableHitCount> hitRatio = new TreeMap<String, MutableHitCount>(); for (Event event : searchFeatureUsageEvents(query)) { if (!hitRatio.containsKey(event.getHostName())) { hitRatio.put(event.getHostName(), new MutableHitCount()); } hitRatio.get(event.getHostName()).inc(); } return hitRatio; }
/** * Convert hicount into pieChart. * * @param hitRatio * current hist ratio * @return * pie chart */ private BarChart renderBarChart(String title, Map < String, MutableHitCount > hitRatio, List < String > colors) { BarChart barChart = new BarChart(title); int idxColor = 0; for (String key : hitRatio.keySet()) { Serie<Integer> bar = new Serie<Integer>(key, new Double(hitRatio.get(key).get()).intValue(), colors.get(idxColor)); barChart.getChartBars().add(bar); idxColor++; } orderBarDecrecent(barChart); return barChart; }
private Map<String, MutableHitCount> getUsageCount(EventQueryDefinition query, Types type) { Map<String, MutableHitCount> hitCount = new HashMap<>(); // Get events from Redis between the time range. Set<String> events = getEventsFromRedis(query); // Loop through create the buckets. for (String event : events) { Event eventObject = marshallEvent(event); String value = getValueFromAttribute(type, eventObject); MutableHitCount mutableHitCount = hitCount.get(value); if (mutableHitCount != null) { mutableHitCount.inc(); } else { mutableHitCount = new MutableHitCount(1); } hitCount.put(value, mutableHitCount); } return hitCount; }
/** TDD. */ @Test public void testUserHitCount() throws InterruptedException { long start = System.currentTimeMillis(); // When for (int i = 0; i < 8; i++) { Event e1 = new Event(SOURCE_JAVA, TARGET_FEATURE, "f1", ACTION_CHECK_OK); e1.setUser("JOHN"); repo.saveEvent(e1); Thread.sleep(100); Event e2 = new Event(SOURCE_JAVA, TARGET_FEATURE, "f1", ACTION_CHECK_OK); e2.setUser("BOB"); repo.saveEvent(e2); Thread.sleep(100); } Thread.sleep(200); // Then EventQueryDefinition testQuery = new EventQueryDefinition(start - 20, System.currentTimeMillis()); Map<String, MutableHitCount> mapOfHit = repo.getUserHitCount(testQuery); Assert.assertEquals(2, mapOfHit.size()); Assert.assertTrue(mapOfHit.containsKey("JOHN")); Assert.assertTrue(mapOfHit.containsKey("BOB")); Assert.assertEquals(8, mapOfHit.get("BOB").get()); }
/** {@inheritDoc} */ @Override public Map<String, MutableHitCount> getFeatureUsageHitCount(EventQueryDefinition query) { String cqlQuery = getBuilder().cqlFeatureUsageHitCount(query); LOGGER.debug("Query " + cqlQuery); ResultSet rs = conn.getSession().execute(cqlQuery); Map < String, MutableHitCount > hitCount = new HashMap<String, MutableHitCount>(); for (Row row : rs.all()) { String featureName = row.getString(COL_EVENT_NAME); if (hitCount.containsKey(featureName)) { hitCount.get(featureName).inc(); } else { hitCount.put(featureName, new MutableHitCount(1)); } } return hitCount; }
/** TDD. */ @Test public void testSourceHitCount() 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)); repo.saveEvent(new Event(SOURCE_WEB, TARGET_FEATURE, "f2", ACTION_CHECK_OK)); } Thread.sleep(200); repo.saveEvent(new Event(SOURCE_WEBAPI, TARGET_FEATURE, "f1", ACTION_CHECK_OK)); Thread.sleep(200); // Then EventQueryDefinition testQuery = new EventQueryDefinition(start - 20, System.currentTimeMillis()); Map<String, MutableHitCount> mapOfHit = repo.getSourceHitCount(testQuery); Assert.assertEquals(3, mapOfHit.size()); Assert.assertTrue(mapOfHit.containsKey(SOURCE_JAVA)); Assert.assertTrue(mapOfHit.containsKey(SOURCE_WEB)); Assert.assertEquals(1, mapOfHit.get(SOURCE_WEBAPI).get()); }
/** {@inheritDoc} */ @Override public Map<String, MutableHitCount> getUserHitCount(EventQueryDefinition query) { String cqlQuery = getBuilder().cqlUserHitCount(query); LOGGER.debug("Query " + cqlQuery); ResultSet rs = conn.getSession().execute(cqlQuery); Map < String, MutableHitCount > hitCount = new HashMap<String, MutableHitCount>(); for (Row row : rs.all()) { String user = row.getString(COL_EVENT_USER); if (hitCount.containsKey(user)) { hitCount.get(user).inc(); } else { hitCount.put(user, new MutableHitCount(1)); } } return hitCount; }
/** TDD. */ @Test public void testFeatureUsageHitCount() throws InterruptedException { long start = System.currentTimeMillis(); // Given empty event repository // When repo.saveEvent(new Event(SOURCE_JAVA, TARGET_FEATURE, "f1", ACTION_CREATE)); for (int i = 0; i < 8; i++) { Thread.sleep(100); repo.saveEvent(new Event(SOURCE_JAVA, TARGET_FEATURE, "f1", ACTION_CHECK_OK)); repo.saveEvent(new Event(SOURCE_WEB, TARGET_FEATURE, "f2", ACTION_CHECK_OK)); } Thread.sleep(100); // Then EventQueryDefinition testQuery = new EventQueryDefinition(start, System.currentTimeMillis()); // Assert Pie Chart (2 sectors with 8 and 8) Map<String, MutableHitCount> mapOfHit = repo.getFeatureUsageHitCount(testQuery); Assert.assertEquals(2, mapOfHit.size()); Assert.assertTrue(mapOfHit.containsKey("f1")); Assert.assertTrue(mapOfHit.containsKey("f2")); Assert.assertEquals(8, mapOfHit.get("f1").get()); }
/** {@inheritDoc} */ @Override public Map<String, MutableHitCount> getHostHitCount(EventQueryDefinition query) { String cqlQuery = getBuilder().cqlHostHitCount(query); LOGGER.debug("Query " + cqlQuery); ResultSet rs = conn.getSession().execute(cqlQuery); Map < String, MutableHitCount > hitCount = new HashMap<String, MutableHitCount>(); for (Row row : rs.all()) { String hostName = row.getString(COL_EVENT_HOSTNAME); if (hitCount.containsKey(hostName)) { hitCount.get(hostName).inc(); } else { hitCount.put(hostName, new MutableHitCount(1)); } } return hitCount; }