<commit_before>package com.gmail.nossr50.runnables.backups;

import java.io.File;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

import org.bukkit.scheduler.BukkitRunnable;

import com.gmail.nossr50.config.Config;
import com.gmail.nossr50.mcMMO;

public class CleanBackupsTask extends BukkitRunnable {
    private static final String BACKUP_DIRECTORY = mcMMO.getMainDirectory() + "backup" + File.separator;
    private static final File BACKUP_DIR = new File(BACKUP_DIRECTORY);

    @Override
    public void run() {
        List<Integer> savedDays = new ArrayList<Integer>();
        List<Integer> savedWeeks = new ArrayList<Integer>();
        List<File> toDelete = new ArrayList<File>();
        int amountTotal = 0;
        int amountDeleted = 0;

        // Check files in backup folder from oldest to newest
        for (File file : BACKUP_DIR.listFiles()) {
            if (!file.isFile()) {
                continue;
            }

            amountTotal++;
            String fileName = file.getName();
            fileName = fileName.split("[.]")[0];

            Date date = getDate(fileName);

            if (date == null) {
                mcMMO.p.debug("Could not determine date for file: " + fileName);
                continue;
            }

            Calendar cal = Calendar.getInstance();
            cal.setTime(date);
            int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
            int weekOfYear = cal.get(Calendar.WEEK_OF_YEAR);

            if (isPast24Hours(date) && Config.getInstance().getKeepLast24Hours()) {
                // Keep all files from the last 24 hours
                continue;
            }
            else if (isLastWeek(date) && !savedDays.contains(dayOfWeek)  && Config.getInstance().getKeepDailyLastWeek()) {
                // Keep daily backups of the past week
                savedDays.add(dayOfWeek);
                continue;
            }
            else if (!savedWeeks.contains(weekOfYear) && Config.getInstance().getKeepWeeklyPastMonth()) {
                // Keep one backup of each week
                savedWeeks.add(weekOfYear);
                continue;
            }

            amountDeleted++;
            toDelete.add(file);
        }

        mcMMO.p.getLogger().info("Cleaning backup files... (" + amountDeleted + "/" + amountTotal + ")");

        for (File file : toDelete) {
            mcMMO.p.debug("Deleted: " + file.getName());
            file.delete();
        }
    }

    /**
     * Check if date is within last 24 hours
     *
     * @param date date to check
     *
     * @return true is date is within last 24 hours, false if otherwise
     */
    private boolean isPast24Hours(Date date) {
        Date modifiedDate = new Date(System.currentTimeMillis() - TimeUnit.MILLISECONDS.convert(24, TimeUnit.HOURS));
        return date.after(modifiedDate);
    }

    /**
     * Check if date is within the last week
     *
     * @param date date to check<commit_msg>Don't send a message to the console if no backups were deleted<commit_after>package com.gmail.nossr50.runnables.backups;

import java.io.File;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

import org.bukkit.scheduler.BukkitRunnable;

import com.gmail.nossr50.config.Config;
import com.gmail.nossr50.mcMMO;

public class CleanBackupsTask extends BukkitRunnable {
    private static final String BACKUP_DIRECTORY = mcMMO.getMainDirectory() + "backup" + File.separator;
    private static final File BACKUP_DIR = new File(BACKUP_DIRECTORY);

    @Override
    public void run() {
        List<Integer> savedDays = new ArrayList<Integer>();
        List<Integer> savedWeeks = new ArrayList<Integer>();
        List<File> toDelete = new ArrayList<File>();
        int amountTotal = 0;
        int amountDeleted = 0;

        // Check files in backup folder from oldest to newest
        for (File file : BACKUP_DIR.listFiles()) {
            if (!file.isFile()) {
                continue;
            }

            amountTotal++;
            String fileName = file.getName();
            fileName = fileName.split("[.]")[0];

            Date date = getDate(fileName);

            if (date == null) {
                mcMMO.p.debug("Could not determine date for file: " + fileName);
                continue;
            }

            Calendar cal = Calendar.getInstance();
            cal.setTime(date);
            int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
            int weekOfYear = cal.get(Calendar.WEEK_OF_YEAR);

            if (isPast24Hours(date) && Config.getInstance().getKeepLast24Hours()) {
                // Keep all files from the last 24 hours
                continue;
            }
            else if (isLastWeek(date) && !savedDays.contains(dayOfWeek)  && Config.getInstance().getKeepDailyLastWeek()) {
                // Keep daily backups of the past week
                savedDays.add(dayOfWeek);
                continue;
            }
            else if (!savedWeeks.contains(weekOfYear) && Config.getInstance().getKeepWeeklyPastMonth()) {
                // Keep one backup of each week
                savedWeeks.add(weekOfYear);
                continue;
            }

            amountDeleted++;
            toDelete.add(file);
        }

        if (toDelete.isEmpty()) {
            return;
        }

        mcMMO.p.getLogger().info("Cleaning backup files... (" + amountDeleted + "/" + amountTotal + ")");

        for (File file : toDelete) {
            mcMMO.p.debug("Deleted: " + file.getName());
            file.delete();
        }
    }

    /**
     * Check if date is within last 24 hours
     *
     * @param date date to check
     *
     * @return true is date is within last 24 hours, false if otherwise
     */
    private boolean isPast24Hours(Date date) {
        Date modifiedDate = new Date(System.currentTimeMillis() - TimeUnit.MILLISECONDS.convert(24, TimeUnit.HOURS));
        return date.after(modifiedDate);
    }

    /**
     * Check if date is within the last week
     *
     * @param date date to check
{'fraction_non_alphanumeric': 0.10689546599496222, 'fraction_numerical': 0.007241813602015114, 'mean_word_length': 2.0765133171912833, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1695715', 'n_tokens_mistral': 1847, 'n_tokens_neox': 1746, 'n_words': 441}