<commit_before>package stream.flarebot.flarebot;

import com.datastax.driver.core.PreparedStatement;
import com.datastax.driver.core.ResultSet;
import com.datastax.driver.core.Row;
import com.datastax.driver.core.Statement;
import io.github.binaryoverload.JSONConfig;
import net.dv8tion.jda.core.entities.TextChannel;
import net.dv8tion.jda.core.entities.User;
import stream.flarebot.flarebot.api.ApiRequester;
import stream.flarebot.flarebot.database.CassandraController;
import stream.flarebot.flarebot.database.CassandraTask;
import stream.flarebot.flarebot.objects.GuildWrapper;
import stream.flarebot.flarebot.objects.GuildWrapperBuilder;
import stream.flarebot.flarebot.scheduler.FlarebotTask;
import stream.flarebot.flarebot.util.expiringmap.ExpiredEvent;
import stream.flarebot.flarebot.util.expiringmap.ExpiringMap;
import stream.flarebot.flarebot.util.MessageUtils;

import java.awt.Color;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;

public class FlareBotManager {

    private static FlareBotManager instance;

    private Map<Language.Locales, JSONConfig> configs = new ConcurrentHashMap<>();
    // Command - reason
    private Map<String, String> disabledCommands = new ConcurrentHashMap<>();

    private ExpiringMap<String, GuildWrapper> guilds;
    private final long GUILD_EXPIRE = TimeUnit.MINUTES.toMillis(15);
    private final long INACTIVITY_CHECK = TimeUnit.MINUTES.toMillis(2);

    private final String GUILD_DATA_TABLE;

    private PreparedStatement saveGuildStatement;

    public FlareBotManager() {
        instance = this;
        GUILD_DATA_TABLE = (FlareBot.getInstance().isTestBot() ? "flarebot.guild_data_test" : "flarebot.guild_data");
    }

    public static FlareBotManager getInstance() {
        return instance;
    }

    public void executeCreations() {
        // Note to self: Figure out a way to order this but where I can still update the damn last_retrieved key
        CassandraController.executeAsync("CREATE TABLE IF NOT EXISTS " + GUILD_DATA_TABLE + " (" +
                "guild_id varchar, " +
                "data text, " +
                "last_retrieved timestamp, " +
                "PRIMARY KEY(guild_id))");
        CassandraController.executeAsync("CREATE TABLE IF NOT EXISTS flarebot.playlist (" +
                "playlist_name varchar, " +
                "guild_id varchar, " +
                "owner varchar, " +
                "songs list<varchar>, " +
                "scope varchar, " +
                "times_played int, " +
                "PRIMARY KEY(playlist_name, guild_id))");

                // Can't seem to cluster this because times_played is a bitch to have as a primary key.
                //"PRIMARY KEY((playlist_name, guild_id), times_played)) " +
                //"WITH CLUSTERING ORDER BY (times_played DESC)");

        initGuildSaving();
    }

    private void initGuildSaving() {
        guilds = new ExpiringMap<>(GUILD_EXPIRE, new ExpiredEvent<String, GuildWrapper>() {
            @Override
            public void run(String guildId, GuildWrapper guildWrapper, long expired, long last_retrieved) {
                //ApiRequester.requestAsync(ApiRoute.UNLOAD, );
                if((System.currentTimeMillis() - INACTIVITY_CHECK) > guilds.getLastRetrieved(guildId))
                    saveGuild(guildId, guildWrapper, last_retrieved, true);
                else {
                    setCancelled(true);
                    guilds.resetTime(guildId);
                    saveGuild(guildId, guildWrapper, last_retrieved, false);
                }
            }
        });
        new FlarebotTask() {
            @Override
            public void run() {
                guilds.purge();
            }
        }.repeat(30_000, 30_000);
    }

    // Do not use this method!
    protected void saveGuild(String guildId, GuildWrapper guildWrapper, final long last_retrieved, boolean unload) {
        long last_r = (last_retrieved == -1 ? System.currentTimeMillis() : last_retrieved);
        CassandraController.runTask(session -> {
            if(saveGuildStatement == null) saveGuildStatement = session.prepare("UPDATE " + GUILD_DATA_TABLE
                    + "SET last_retrieved = ?, data = ? WHERE guild_id = ?");
            session.executeAsync(saveGuildStatement.bind()
                    .setTimestamp(0, new Date(last_r))
                    .setString(1, FlareBot.GSON.toJson(guildWrapper)).setString(2, guildId));
        });
        FlareBot.LOGGER.info("Guild " + guildId + "'s data got saved! Last retrieved: " + last_r
                + " (" + new Date(last_r) + ")");
        if(unload)
            FlareBot.getInstance().getChannelByID(FlareBot.GUILD_LOG).sendMessage(MessageUtils.getEmbed()
                    .setColor(Color.MAGENTA).setTitle("Guild unloaded!", null)
                    .setDescription("Guild " + guildId + " unloaded!").addField("Time", "Millis: " + System.currentTimeMillis()
                            + "\nTime: " + LocalDateTime.now().toString() + "\nLast retrieved: " + last_r, false)
                    .build()).queue();
    }

    public void savePlaylist(TextChannel channel, String owner, String name, List<String> songs) {
        CassandraController.runTask(session -> {
            PreparedStatement exists = session
                    .prepare("SELECT * FROM flarebot.playlist WHERE playlist_name = ? AND guild_id = ?");
            ResultSet set = session.execute(exists.bind().setString(0, name).setString(1, channel.getGuild().getId()));
            if (set.one() != null) {
                channel.sendMessage("That name is already taken!").queue();
                return;<commit_msg>Fix query<commit_after>package stream.flarebot.flarebot;

import com.datastax.driver.core.PreparedStatement;
import com.datastax.driver.core.ResultSet;
import com.datastax.driver.core.Row;
import com.datastax.driver.core.Statement;
import io.github.binaryoverload.JSONConfig;
import net.dv8tion.jda.core.entities.TextChannel;
import net.dv8tion.jda.core.entities.User;
import stream.flarebot.flarebot.api.ApiRequester;
import stream.flarebot.flarebot.database.CassandraController;
import stream.flarebot.flarebot.database.CassandraTask;
import stream.flarebot.flarebot.objects.GuildWrapper;
import stream.flarebot.flarebot.objects.GuildWrapperBuilder;
import stream.flarebot.flarebot.scheduler.FlarebotTask;
import stream.flarebot.flarebot.util.expiringmap.ExpiredEvent;
import stream.flarebot.flarebot.util.expiringmap.ExpiringMap;
import stream.flarebot.flarebot.util.MessageUtils;

import java.awt.Color;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;

public class FlareBotManager {

    private static FlareBotManager instance;

    private Map<Language.Locales, JSONConfig> configs = new ConcurrentHashMap<>();
    // Command - reason
    private Map<String, String> disabledCommands = new ConcurrentHashMap<>();

    private ExpiringMap<String, GuildWrapper> guilds;
    private final long GUILD_EXPIRE = TimeUnit.MINUTES.toMillis(15);
    private final long INACTIVITY_CHECK = TimeUnit.MINUTES.toMillis(2);

    private final String GUILD_DATA_TABLE;

    private PreparedStatement saveGuildStatement;

    public FlareBotManager() {
        instance = this;
        GUILD_DATA_TABLE = (FlareBot.getInstance().isTestBot() ? "flarebot.guild_data_test" : "flarebot.guild_data");
    }

    public static FlareBotManager getInstance() {
        return instance;
    }

    public void executeCreations() {
        // Note to self: Figure out a way to order this but where I can still update the damn last_retrieved key
        CassandraController.executeAsync("CREATE TABLE IF NOT EXISTS " + GUILD_DATA_TABLE + " (" +
                "guild_id varchar, " +
                "data text, " +
                "last_retrieved timestamp, " +
                "PRIMARY KEY(guild_id))");
        CassandraController.executeAsync("CREATE TABLE IF NOT EXISTS flarebot.playlist (" +
                "playlist_name varchar, " +
                "guild_id varchar, " +
                "owner varchar, " +
                "songs list<varchar>, " +
                "scope varchar, " +
                "times_played int, " +
                "PRIMARY KEY(playlist_name, guild_id))");

                // Can't seem to cluster this because times_played is a bitch to have as a primary key.
                //"PRIMARY KEY((playlist_name, guild_id), times_played)) " +
                //"WITH CLUSTERING ORDER BY (times_played DESC)");

        initGuildSaving();
    }

    private void initGuildSaving() {
        guilds = new ExpiringMap<>(GUILD_EXPIRE, new ExpiredEvent<String, GuildWrapper>() {
            @Override
            public void run(String guildId, GuildWrapper guildWrapper, long expired, long last_retrieved) {
                //ApiRequester.requestAsync(ApiRoute.UNLOAD, );
                if((System.currentTimeMillis() - INACTIVITY_CHECK) > guilds.getLastRetrieved(guildId))
                    saveGuild(guildId, guildWrapper, last_retrieved, true);
                else {
                    setCancelled(true);
                    guilds.resetTime(guildId);
                    saveGuild(guildId, guildWrapper, last_retrieved, false);
                }
            }
        });
        new FlarebotTask() {
            @Override
            public void run() {
                guilds.purge();
            }
        }.repeat(30_000, 30_000);
    }

    // Do not use this method!
    protected void saveGuild(String guildId, GuildWrapper guildWrapper, final long last_retrieved, boolean unload) {
        long last_r = (last_retrieved == -1 ? System.currentTimeMillis() : last_retrieved);
        CassandraController.runTask(session -> {
            if(saveGuildStatement == null) saveGuildStatement = session.prepare("UPDATE " + GUILD_DATA_TABLE
                    + " SET last_retrieved = ?, data = ? WHERE guild_id = ?");
            session.executeAsync(saveGuildStatement.bind()
                    .setTimestamp(0, new Date(last_r))
                    .setString(1, FlareBot.GSON.toJson(guildWrapper)).setString(2, guildId));
        });
        FlareBot.LOGGER.info("Guild " + guildId + "'s data got saved! Last retrieved: " + last_r
                + " (" + new Date(last_r) + ")");
        if(unload)
            FlareBot.getInstance().getChannelByID(FlareBot.GUILD_LOG).sendMessage(MessageUtils.getEmbed()
                    .setColor(Color.MAGENTA).setTitle("Guild unloaded!", null)
                    .setDescription("Guild " + guildId + " unloaded!").addField("Time", "Millis: " + System.currentTimeMillis()
                            + "\nTime: " + LocalDateTime.now().toString() + "\nLast retrieved: " + last_r, false)
                    .build()).queue();
    }

    public void savePlaylist(TextChannel channel, String owner, String name, List<String> songs) {
        CassandraController.runTask(session -> {
            PreparedStatement exists = session
                    .prepare("SELECT * FROM flarebot.playlist WHERE playlist_name = ? AND guild_id = ?");
            ResultSet set = session.execute(exists.bind().setString(0, name).setString(1, channel.getGuild().getId()));
            if (set.one() != null) {
                channel.sendMessage("That name is already taken!").queue();
                return;
{'fraction_non_alphanumeric': 0.10672576932912795, 'fraction_numerical': 0.0035802574375586056, 'mean_word_length': 2.640086875581756, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 27, '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': 2, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5561465', 'n_tokens_mistral': 3540, 'n_tokens_neox': 3304, 'n_words': 684}