// Copyright 2019 Oath Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.config.proxy.filedistribution;

import com.yahoo.io.IOUtils;
import com.yahoo.log.LogLevel;
import com.yahoo.vespa.filedistribution.FileDownloader;

import java.io.File;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.file.attribute.BasicFileAttributes;
import java.time.Duration;
import java.time.Instant;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import java.util.logging.Logger;
import java.util.stream.Collectors;

import static java.nio.file.Files.readAttributes;

/**
 * Deletes cached file references and url downloads that have not been used for some time
 *
 * @author hmusum
 */
class CachedFilesMaintainer implements Runnable {

    private final static Logger log = Logger.getLogger(CachedFilesMaintainer.class.getName());

    private static final File defaultUrlDownloadDir = UrlDownloadRpcServer.downloadDir;
    private static final File defaultFileReferencesDownloadDir = FileDownloader.defaultDownloadDirectory;
    private static final Duration defaultDurationToKeepFiles = Duration.ofDays(30);

    private final File urlDownloadDir;
    private final File fileReferencesDownloadDir;
    private final Duration durationToKeepFiles;

    CachedFilesMaintainer() {
        this(defaultFileReferencesDownloadDir, defaultUrlDownloadDir, defaultDurationToKeepFiles);
    }

    CachedFilesMaintainer(File fileReferencesDownloadDir, File urlDownloadDir, Duration durationToKeepFiles) {
        this.fileReferencesDownloadDir = fileReferencesDownloadDir;
        this.urlDownloadDir = urlDownloadDir;
        this.durationToKeepFiles = durationToKeepFiles;
    }

    @Override
    public void run() {
        deleteUnusedFiles(fileReferencesDownloadDir);
        deleteUnusedFiles(urlDownloadDir);
    }

    private void deleteUnusedFiles(File directory) {
        Instant deleteNotUsedSinceInstant = Instant.now().minus(durationToKeepFiles);
        Set<String> filesOnDisk = new HashSet<>();
        File[] files = directory.listFiles();
        if (files != null)
            filesOnDisk.addAll(Arrays.stream(files).map(File::getName).collect(Collectors.toSet()));
        log.log(LogLevel.DEBUG, "Files on disk (in " + directory + "): " + filesOnDisk);

        Set<String> filesToDelete = filesOnDisk
                .stream()
                .filter(fileReference -> isFileLastModifiedBefore(new File(directory, fileReference), deleteNotUsedSinceInstant))
                .collect(Collectors.toSet());
        if (filesToDelete.size() > 0) {
            log.log(LogLevel.INFO, "Files that can be deleted in " + directory + " (not used since " + deleteNotUsedSinceInstant + "): " + filesToDelete);
            filesToDelete.forEach(fileReference -> {
                File file = new File(directory, fileReference);
                if (!IOUtils.recursiveDeleteDir(file))
                    log.log(LogLevel.WARNING, "Could not delete " + file.getAbsolutePath());
            });
        }
    }

    private boolean isFileLastModifiedBefore(File fileReference, Instant instant) {
        BasicFileAttributes fileAttributes;
        try {
            fileAttributes = readAttributes(fileReference.toPath(), BasicFileAttributes.class);
            return fileAttributes.lastModifiedTime().toInstant().isBefore(instant);
        } catch (IOException e) {
            throw new UncheckedIOException(e);
        }
    }

}

@@ -539,24 +539,56 @@
 a.util.Set;%0A
+import java.util.logging.Level;%0A
 import java.
@@ -1837,24 +1837,42 @@
 oid run() %7B%0A
+        try %7B%0A    
         dele
@@ -1913,32 +1913,36 @@
 adDir);%0A        
+    
 deleteUnusedFile
@@ -1956,24 +1956,139 @@
 wnloadDir);%0A
+        %7D catch (Throwable t) %7B%0A            log.log(Level.WARNING, %22Deleting unused files failed. %22, t);%0A        %7D%0A
     %7D%0A%0A    p

Handle any exceptions when running executor service