protected StatDetails createDetails(String detailsId, List<GroupEntity> groups, List<StatEntity> stats) { StatDetails result = new StatDetails(detailsId, null); result.setInfo(storageInfo(null)); groups.forEach( group -> { List<StatEntity> groupStats = stats.stream() .filter( s -> group.getId().equals(s.getGroupId()) ) .collect(toList()); restoreGroupStats(group, groupStats); Stat details = group.getMap().get(detailsId); result.add(details); }); if( result.getMerged() == null ){ result.setMerged(Stat.emptyStat()); } return result; }
@Override public StatDetails stat(String id, Date from, Date to, String appGroup, String appType) { log.debug("Searching for stat {} from {} to {}", id, getDateFormat().format(from), getDateFormat().format(to)); StatDetails result = new StatDetails(id, null); listParts().stream() .peek( fileInfo -> log.debug("Checking {}", fileInfo.getName())) .filter(fileInfo -> fileInfo.inRange(from, to)) .peek( fileInfo -> log.debug("Matches: {}", fileInfo.getName())) .forEach( fileInfo -> { Stats stats = load(fileInfo.getName()); Stat details = stats.getMap().get(id); result.add(details, stats.getStartDate(), stats.getEndDate()); }); if( result.getMerged() == null ){ result.setMerged(Stat.emptyStat()); } return result; }
@Override public StatDetails stat(String rootName, Date from, Date to, String appGroup, String appType) { StatDetails result = new StatDetails(rootName, null); List<GroupEntity> groups = findGroupsInRange(from, to, appGroup, appType); loadTree(groups, rootName, from, to, appGroup, appType); groups.forEach(g -> { Stat details = g.getMap().get(rootName); result.add(details, g.getStartDate(),g.getEndDate()); }); if( result.getMerged() == null ){ result.setMerged(Stat.emptyStat()); } return result; }
@Test public void should_load_details_with_children() throws IOException{ //given Stat rootStat = stat(100); Stat stat1 = stat(10); rootStat.ensureChildrenMap(); rootStat.getChildren().put("1", stat1); Stat stat2 = stat(1); stat1.ensureChildrenMap(); stat1.getChildren().put("11", stat2); Stats stats = stats(dt(year+"-01-01 08:05:00"), dt(year+"-01-01 08:15:00"), rootStat); storage.save(stats, "i1", "t1"); //when StatDetails details = storage.loadDetails(STAT_NAME, dt(year+"-01-01 08:00:00"), dt(year+"-01-01 08:30:00"), null, null); //then assertEquals( 100L, details.getMerged().getAvg()); assertEquals( 10L, details.getMerged().getChild("1").getAvg()); assertEquals( 1L, details.getMerged().getChild("1").getChild("11").getAvg()); }
private StatDetails detailsForRange(String id, TimeRange range) { StatDetails result; if( canSearch(range) ){ result = getStorage().loadDetails(id, range.getFromDate(), range.getToDate(), range.getInstance(), range.getType()); if( inMemoryInRange(range) ){ StatDetails current = detailsInMemory(id); if( current != null ){ //we don't want to merge not set stats as it would reset min value to 0 result.getMerged().merge(current.getMerged(), true); } result.setInfo("Added in-memory data. Original info: " + result.getInfo()); } } else { result = detailsInMemory(id); result.setInfo("in-memory data only. Original info: " + result.getInfo()); } return (result != null) ? result : new StatDetails(id, Stat.emptyStat()); }
@Test public void should_load_details_by_stats_ids() throws IOException{ //given Stats stats1 = stats( dt(year+"-01-01 08:00:00"), dt(year+"-01-01 08:15:00"), stat(100, 200)//hits = 2, avg = 150 ); Stats stats2 = stats( dt(year+"-01-01 08:15:00"), dt(year+"-01-01 08:30:00"), stat(1000)//hits = 1, avg = 1000 ); String id1 = storage.save(stats1, null, null); String id2 = storage.save(stats2, null, null); //when StatDetails details = storage.loadDetails(STAT_NAME, asList(id1, id2)); //then Stat expected = stats1.getMap().get(STAT_NAME).deepCopy(); expected.merge(stats2.getMap().get(STAT_NAME)); assertEquals(STAT_NAME, details.getId() ); assertEquals( expected, details.getMerged()); }
@Test public void should_load_details_by_instance_and_type() throws IOException{ //given Stats stats1 = stats(dt(year+"-01-01 08:05:00"), dt(year+"-01-01 08:15:00"), stat(2)); Stats stats10 = stats(dt(year+"-01-01 08:05:00"), dt(year+"-01-01 08:15:00"), stat(10)); Stats stats100 = 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(stats1, "i1", null); storage.save(stats10, "i1", "t1"); storage.save(stats100, "i2", "t1"); //when StatDetails detailsI = storage.loadDetails(STAT_NAME, dt(year+"-01-01 08:00:00"), dt(year+"-01-01 08:30:00"), "i1", null); StatDetails detailsT = storage.loadDetails(STAT_NAME, dt(year+"-01-01 08:00:00"), dt(year+"-01-01 08:30:00"), null, "t1"); StatDetails detailsIT = storage.loadDetails(STAT_NAME, dt(year+"-01-01 08:00:00"), dt(year+"-01-01 08:30:00"), "i1", "t1"); //then assertEquals( 6, detailsI.getMerged().getAvg()); assertEquals( 55, detailsT.getMerged().getAvg()); assertEquals( 10, detailsIT.getMerged().getAvg()); }
@Test public void should_load_details_by_date_range() throws IOException{ //given Stats stats1 = stats( dt(year+"-01-01 08:00:00"), dt(year+"-01-01 08:15:00"), stat(100, 200)//hits = 2, avg = 150 ); Stats stats2 = stats( dt(year+"-01-01 08:15:00"), dt(year+"-01-01 08:30:00"), stat(1000) //hits =1 avg = 1000 ); Stats stats3 = stats( dt(year+"-01-01 08:30:00"), dt(year+"-01-01 08:45:00"), stat(1000, 1000, 1000)// hits = 3, avg = 1000 ); storage.save(stats1, null, null); storage.save(stats2, null, null); storage.save(stats3, null, null); //when StatDetails details = storage.loadDetails(STAT_NAME, dt(year+"-01-01 08:20:00"), dt(year+"-01-01 08:25:00"), null, null); //then assertEquals(STAT_NAME, details.getId() ); assertEquals( 1, details.getMerged().getHits()); }