Search in sources :

Example 1 with Functions

use of com.github.davidmoten.rx.Functions in project risky by amsa-code.

the class Formats method transform.

public static Observable<Integer> transform(final File input, final File output, Pattern pattern, final Transformer<HasFix, HasFix> transformer, final Action2<List<HasFix>, File> fixesWriter, final Func1<String, String> renamer) {
    Preconditions.checkNotNull(input);
    Preconditions.checkNotNull(output);
    Preconditions.checkNotNull(pattern);
    Preconditions.checkNotNull(transformer);
    final List<File> files = Files.find(input, pattern);
    long n = 0;
    for (File file : files) n += file.length();
    final long totalSizeBytes = n;
    log.info("transforming " + new DecimalFormat("0.000").format(totalSizeBytes / 1000000.0) + "MB");
    final Action1<File> logger = new Action1<File>() {

        final AtomicInteger count = new AtomicInteger();

        final long startTime = System.currentTimeMillis();

        final AtomicLong size = new AtomicLong();

        @Override
        public void call(File f) {
            long t = System.currentTimeMillis();
            int n = count.incrementAndGet();
            long bytes = size.getAndAdd(f.length());
            double timeToFinishMins;
            if (n > 1) {
                timeToFinishMins = (t - startTime) / (double) (bytes) * (totalSizeBytes - bytes) / 1000.0 / 60.0;
            } else
                timeToFinishMins = -1;
            DecimalFormat df = new DecimalFormat("0.000");
            log.info("transforming " + n + " of " + files.size() + ":" + f + ", sizeMB=" + df.format(f.length() / 1000000.0) + ", finish in mins=" + df.format(timeToFinishMins));
        }
    };
    log.info("converting " + files.size() + " files" + " in " + input);
    return Observable.from(files).flatMap(file -> {
        final File outputFile = rebase(file, input, output);
        outputFile.getParentFile().mkdirs();
        logger.call(file);
        return BinaryFixes.from(file, true, BinaryFixesFormat.WITHOUT_MMSI).toList().flatMapIterable(Functions.<List<Fix>>identity()).compose(transformer).toList().doOnNext(list -> {
            File f = new File(outputFile.getParentFile(), renamer.call(outputFile.getName()));
            fixesWriter.call(list, f);
        }).count();
    });
}
Also used : Logger(org.slf4j.Logger) Transformer(rx.Observable.Transformer) DecimalFormat(java.text.DecimalFormat) LoggerFactory(org.slf4j.LoggerFactory) Action1(rx.functions.Action1) Files(au.gov.amsa.util.Files) Action2(rx.functions.Action2) File(java.io.File) Observable(rx.Observable) VisibleForTesting(com.github.davidmoten.guavamini.annotations.VisibleForTesting) AtomicLong(java.util.concurrent.atomic.AtomicLong) List(java.util.List) Func1(rx.functions.Func1) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Preconditions(com.github.davidmoten.util.Preconditions) Pattern(java.util.regex.Pattern) Functions(com.github.davidmoten.rx.Functions) AtomicLong(java.util.concurrent.atomic.AtomicLong) Action1(rx.functions.Action1) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DecimalFormat(java.text.DecimalFormat) File(java.io.File)

Aggregations

Files (au.gov.amsa.util.Files)1 VisibleForTesting (com.github.davidmoten.guavamini.annotations.VisibleForTesting)1 Functions (com.github.davidmoten.rx.Functions)1 Preconditions (com.github.davidmoten.util.Preconditions)1 File (java.io.File)1 DecimalFormat (java.text.DecimalFormat)1 List (java.util.List)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 Pattern (java.util.regex.Pattern)1 Logger (org.slf4j.Logger)1 LoggerFactory (org.slf4j.LoggerFactory)1 Observable (rx.Observable)1 Transformer (rx.Observable.Transformer)1 Action1 (rx.functions.Action1)1 Action2 (rx.functions.Action2)1 Func1 (rx.functions.Func1)1