@Override public StatDetails loadDetails(String detailsId, Date from, Date to, String instance, String type){ List<String> statsIds = find(from, to, instance, type); return loadDetails(detailsId, statsIds); }
@Override public Stats load(String statsId) { try { return readFromGzipFile(statsId); } catch (Exception e) { throw new RuntimeException("Failed to read stats with ID: " + statsId, e); } }
@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; }
@Override public Stats loadAggregated(boolean topLevelOnly, Date from, Date to, String instance, String type){ Stats result = new Stats(); List<String> statsIds = find(from, to, instance, type); if( !statsIds.isEmpty() ){ statsIds.stream() .map( statsId -> load(statsId) ) .forEach( stats -> result.merge(stats, !topLevelOnly ) ); } return result; }
@Override public StatDetails loadDetails(String detailsId, List<String> statsIds){ StatDetails result = new StatDetails(detailsId, null); processStats(statsIds, stats -> { Stat stat = getDetailsOrEmpty(detailsId, stats); result.add(stat); }); return result; }
private File writeToGzipFile(Stats stats, String fileName) throws IOException { File file = buildFile(fileName); OutputStream out = null; try { out = new GZIPOutputStream(new FileOutputStream(file)); jsonUtil.toJson(stats, out); return file; } finally { IOUtils.closeQuietly(out); } }
public void setup(String dirName){ this.dir = toFile(dirName); if( dir.exists() && dir.isFile() ){ throw new RuntimeException("location exists and is a file - cannot use it as storage directory: " + dir.getAbsolutePath()); } if( !dir.exists() && !dir.mkdirs() ){ throw new RuntimeException("cannot create storage directory: " + dir.getAbsolutePath()); } log.info("Using storage directory: {}", dir.getAbsolutePath()); }
@Override public void setup(String dirName) { defaultStorage = new GzipStorage(); defaultStorage.setup(dirName); dailyStorage = new GzipStorage(); dailyStorage.setup(dirName + "/day"); weeklyStorage = new GzipStorage(); weeklyStorage.setup(dirName + "/week"); monthlyStorage = new GzipStorage(); monthlyStorage.setup(dirName + "/month"); } }
private void processStats(List<String> statsIds, Consumer<Stats> consumer){ statsIds.stream() .map( statsId -> load(statsId) ) .filter( stats -> stats != null && stats.getStartDate() != null ) .sorted( (stats1, stats2) -> stats1.getStartDate().compareTo(stats2.getStartDate()) ) .forEach( stats -> consumer.accept(stats)); }
private Stats readFromGzipFile(String fileName) throws IOException { File file = buildFile(fileName); InputStream in = null; try { in = new GZIPInputStream(new FileInputStream(file)); return jsonUtil.fromJson(Stats.class, in); } catch (FileNotFoundException fnf){ return null; } finally { IOUtils.closeQuietly(in); } }