private Histogram histogramForRange(String id, TimeRange range, String lastGroupId) { Histogram result; if( canSearch(range) ){ result = getStorage().loadHistogram(id, range.getFromDate(), range.getToDate(), range.getInstance(), range.getType(), lastGroupId); if( inMemoryInRange(range) && result.isLastHistogramPart() ){ Histogram current = histogramInMemory(id); if( current != null ){ result.getHistogram().addAll(current.getHistogram()); } result.setInfo("Added in-memory data. Original info: " + result.getInfo()); } } else { result = histogramInMemory(id); result.setInfo("in-memory data only. Original info: " + result.getInfo()); } result.setInfo(result.getInfo() + ", " + range.toString() ); return (result != null) ? result : new Histogram(id); }
protected Histogram createHistogram(List<GroupEntity> groups, List<StatEntity> topLevelStats) { Histogram result = new Histogram(); groups.forEach( group -> { Stat details = topLevelStats.stream() .filter( s -> group.getId().equals(s.getGroupId()) ) .filter( s -> s != null ) .map( s-> s.getStat()) .findFirst() .orElseGet(() -> Stat.emptyStat()); result.add(details, group.getStartDate(),group.getEndDate()); }); return result; }
@Override public Histogram loadHistogram(String detailsId, Date from, Date to, String instance, String type, String lastGroupId){ List<GroupEntity> allGroups = findGroupsInRange(from, to, instance, type); List<GroupEntity> tailGroups = tailAfter(allGroups, lastGroupId); List<GroupEntity> groups = limitHistogramSize(tailGroups); lastGroupId = (groups.size() == tailGroups.size()) ? null : groups.get(HISTOGRAM_PAGE-1).getId(); List<String> groupIds = extractGroupIds(groups); List<StatEntity> stats = loadGroupStats(groupIds, detailsId, true);//topLevel only Histogram result = createHistogram(groups, stats); result.setId(detailsId); result.setInfo(storageInfo(null)); result.setLastGroupId(lastGroupId); result.setStartDate(allGroups.stream().map(GroupEntity::getStartDate).min(Date::compareTo).orElseGet(() -> new Date())); result.setEndDate(allGroups.stream().map(GroupEntity::getEndDate).max(Date::compareTo).orElseGet(() -> new Date())); return result; }
private Histogram histogramInMemory(String id) { return Endoscope.processStats(stats -> { Stat s = stats.getMap().get(id); if( s == null ){//might happen when stats get saved and/or reset in the mean time return null; } Histogram result = new Histogram(id); result.getHistogram().add( new StatHistory(s, stats.getStartDate(), new Date()) ); return result; }); }
assertEquals(STAT_NAME, histogram.getId() ); assertEquals( 1, histogram.getHistogram().size()); assertEquals( 1000L, histogram.getHistogram().get(0).getAvg() ); assertEquals( dt(year+"-01-01 08:15:00"), histogram.getHistogram().get(0).getStartDate() ); assertEquals( dt(year+"-01-01 08:30:00"), histogram.getHistogram().get(0).getEndDate() ); assertEquals( 1, histogram.getHistogram().get(0).getHits() );
@Transient public void add(Stat details, Date startDate, Date endDate){ if( details == null ){ return; } getHistogram().add( new StatHistory(details, startDate, endDate) ); }
@Test public void should_load_histogram_by_instance_and_type() throws IOException{ //given Stats stats = stats(dt(year+"-01-01 08:05:00"), dt(year+"-01-01 08:15:00"), stat(100)); //save the same but with different instance and type storage.save(stats, "i1", null); storage.save(stats, "i1", "t1"); storage.save(stats, "i2", "t1"); //when Histogram histogramI = storage.loadHistogram(STAT_NAME, dt(year+"-01-01 08:00:00"), dt(year+"-01-01 08:30:00"), "i1", null, null); Histogram histogramT = storage.loadHistogram(STAT_NAME, dt(year+"-01-01 08:00:00"), dt(year+"-01-01 08:30:00"), null, "t1", null); Histogram histogramIT = storage.loadHistogram(STAT_NAME, dt(year+"-01-01 08:00:00"), dt(year+"-01-01 08:30:00"), "i1", "t1", null); //then assertEquals( 2, histogramI.getHistogram().size()); assertEquals( 2, histogramT.getHistogram().size()); assertEquals( 1, histogramIT.getHistogram().size()); }
@Override public Histogram loadHistogram(String detailsId, Date from, Date to, String instance, String type, String lastGroupId){ List<String> statsIds = find(from, to, instance, type); Histogram result = new Histogram(detailsId); processStats(statsIds, stats -> { Stat stat = getDetailsOrEmpty(detailsId, stats); result.add(stat, stats.getStartDate(),stats.getEndDate()); }); return result; }