All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.endoscope.storage.gzip.SearchableGzipFileStorage Maven / Gradle / Ivy

There is a newer version: 1.0.9
Show newest version
package com.github.endoscope.storage.gzip;

import com.github.endoscope.core.Stat;
import com.github.endoscope.core.Stats;
import com.github.endoscope.storage.Filters;
import com.github.endoscope.storage.SearchableStatsStorage;
import com.github.endoscope.storage.StatDetails;
import org.slf4j.Logger;

import java.util.Date;

import static org.slf4j.LoggerFactory.getLogger;

/**
 * This storage is for demo purposes as it's not efficient.
 * Notice that it loads complete stats in order to extract just part of it.
 */
public class SearchableGzipFileStorage extends GzipFileStorage implements SearchableStatsStorage {
    private static final Logger log = getLogger(SearchableGzipFileStorage.class);

    public SearchableGzipFileStorage(String dir){
        super(dir);
    }

    @Override
    public Stats topLevel(Date from, Date to, String appGroup, String appType) {
        log.debug("Searching for top level stats from {} to {}", getDateFormat().format(from), getDateFormat().format(to));
        Stats merged = new Stats();
        listParts().stream()
                .peek( statsInfo -> log.debug("Checking {}", statsInfo.getName()))
                .filter(statsInfo -> statsInfo.inRange(from, to))
                .peek( statsInfo -> log.debug("Matches: {}", statsInfo.getName()))
                .map( statsInfo -> load(statsInfo.getName()))
                .forEach(stats -> merged.merge(stats, false));
        return merged;
    }

    @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 Filters filters(Date from, Date to) {
        return new Filters();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy