Added some statistics on the version parge

Signed-off-by: Louis Vallat <louis@louis-vallat.xyz>
This commit is contained in:
Louis Vallat 2020-10-28 18:11:52 +01:00
parent be254304a8
commit ce748b7c06
4 changed files with 138 additions and 30 deletions

View File

@ -1,9 +1,12 @@
package xyz.vallat.louis.commands;
import discord4j.core.event.domain.message.MessageCreateEvent;
import discord4j.core.object.entity.Guild;
import discord4j.rest.util.Color;
import reactor.core.publisher.Mono;
import xyz.vallat.louis.MovieQuoteBot;
import xyz.vallat.louis.database.SubtitleLineManager;
import xyz.vallat.louis.database.SubtitleManager;
import xyz.vallat.louis.discord.DiscordManager;
public class Version extends Command {
@ -16,12 +19,27 @@ public class Version extends Command {
public Mono<Void> execute(MessageCreateEvent event) {
return event.getMessage().getChannel()
.flatMap(channel -> channel
.createEmbed(embedCreateSpec -> embedCreateSpec
.createEmbed(embedCreateSpec -> {
embedCreateSpec
.setColor(Color.BISMARK)
.setTitle(MovieQuoteBot.NAME)
.setDescription(MovieQuoteBot.DESCRIPTION)
.addField("Version", MovieQuoteBot.VERSION, true)
.addField("Guilds", String.valueOf(DiscordManager.getGuilds().block()), true)
.addField("Subtitles imported", String.valueOf(SubtitleManager.getNumberOfSubtitles()), true)
.addField("Lines imported", String.valueOf(SubtitleLineManager.getNumberOfSubtitleLines()), true);
if (event.getGuildId().isPresent()) {
Guild guild = event.getGuild().block();
if (guild != null)
embedCreateSpec.addField("This guild imported",
SubtitleLineManager
.getNumberOfSubtitleLinesByGuild(guild.getId().asString()) +
" subtitles, from " +
SubtitleManager
.getNumberOfSubtitlesByGuild(guild.getId().asString()) + " subtitles.",
true);
}
embedCreateSpec.addField("Version", MovieQuoteBot.VERSION, true);
}
)
)
.then();

View File

@ -9,13 +9,13 @@ import xyz.vallat.louis.env.EnvironmentVariables;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import static xyz.vallat.louis.database.FilmManager.initializeFilm;
import static xyz.vallat.louis.database.LanguageManager.importLanguageIfNeeded;
import static xyz.vallat.louis.database.LanguageManager.initializeLanguages;
import static xyz.vallat.louis.database.PropertyManager.initializeProperties;
import static xyz.vallat.louis.database.SubtitleLineManager.initializeSubtitleLine;
import static xyz.vallat.louis.database.SubtitleManager.initializeSubtitle;
public final class DBManager {
@ -68,27 +68,4 @@ public final class DBManager {
private static void updateDatabaseIfNeeded() {
PropertyManager.saveProperty(VERSION_PROPERTY, MovieQuoteBot.VERSION);
}
private static void initializeSubtitle(Connection connection) throws SQLException {
logger.debug("Creating subtitle table.");
try (Statement stmt = connection.createStatement()) {
String query = """
CREATE TABLE IF NOT EXISTS subtitles
(
id int GENERATED ALWAYS AS IDENTITY,
film_id int NOT NULL,
language_id int NOT NULL,
importer varchar(37),
importer_guild_id text,
imported_date timestamptz NOT NULL DEFAULT now(),
UNIQUE (film_id, language_id),
PRIMARY KEY (id),
FOREIGN KEY (film_id)
REFERENCES films (id),
FOREIGN KEY (language_id)
REFERENCES languages (id)
);""";
stmt.executeUpdate(query);
}
}
}

View File

@ -2,8 +2,10 @@ package xyz.vallat.louis.database;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import xyz.vallat.louis.codes.ExitCodes;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
@ -14,6 +16,41 @@ public final class SubtitleLineManager {
private SubtitleLineManager() {
}
public static int getNumberOfSubtitleLines() {
logger.debug("Getting the number of subtitle lines in database.");
try (Connection connection = DBManager.getConnection()) {
try (Statement stmt = connection.createStatement()) {
String query = "SELECT COUNT(*) FROM subtitle_lines;";
stmt.executeQuery(query);
stmt.getResultSet().next();
return (stmt.getResultSet().getInt(1));
}
} catch (SQLException e) {
logger.error("Could not connect to the database right now. Reason: {}", e.getMessage());
System.exit(ExitCodes.CANNOT_CONNECT_TO_DB.getValue());
}
return -1;
}
public static int getNumberOfSubtitleLinesByGuild(String guild_id) {
logger.debug("Getting the number of subtitle lines in database from guild '{}'.", guild_id);
try (Connection connection = DBManager.getConnection()) {
String query = "SELECT COUNT(*) FROM subtitle_lines " +
"INNER JOIN subtitles ON subtitles.id = subtitle_lines.subtitle_id " +
"WHERE importer_guild_id = ?;";
try (PreparedStatement stmt = connection.prepareStatement(query)) {
stmt.setString(1, guild_id);
stmt.executeQuery();
stmt.getResultSet().next();
return (stmt.getResultSet().getInt(1));
}
} catch (SQLException e) {
logger.error("Could not connect to the database right now. Reason: {}", e.getMessage());
System.exit(ExitCodes.CANNOT_CONNECT_TO_DB.getValue());
}
return -1;
}
static void initializeSubtitleLine(Connection connection) throws SQLException {
logger.debug("Creating subtitle_line table.");
try (Statement stmt = connection.createStatement()) {

View File

@ -0,0 +1,76 @@
package xyz.vallat.louis.database;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import xyz.vallat.louis.codes.ExitCodes;
import java.nio.charset.StandardCharsets;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
public class SubtitleManager {
private static final Logger logger = LoggerFactory.getLogger(SubtitleManager.class.getCanonicalName());
private SubtitleManager() {
}
public static int getNumberOfSubtitles() {
logger.debug("Getting the number of subtitles in database.");
try (Connection connection = DBManager.getConnection()) {
try (Statement stmt = connection.createStatement()) {
String query = "SELECT COUNT(*) FROM subtitles;";
stmt.executeQuery(query);
stmt.getResultSet().next();
return (stmt.getResultSet().getInt(1));
}
} catch (SQLException e) {
logger.error("Could not connect to the database right now. Reason: {}", e.getMessage());
System.exit(ExitCodes.CANNOT_CONNECT_TO_DB.getValue());
}
return -1;
}
public static int getNumberOfSubtitlesByGuild(String guild_id) {
logger.debug("Getting the number of subtitles in database from guild '{}'.", guild_id);
try (Connection connection = DBManager.getConnection()) {
String query = "SELECT COUNT(*) FROM subtitles " +
"WHERE importer_guild_id = ?;";
try (PreparedStatement stmt = connection.prepareStatement(query)) {
stmt.setString(1, guild_id);
stmt.executeQuery();
stmt.getResultSet().next();
return (stmt.getResultSet().getInt(1));
}
} catch (SQLException e) {
logger.error("Could not connect to the database right now. Reason: {}", e.getMessage());
System.exit(ExitCodes.CANNOT_CONNECT_TO_DB.getValue());
}
return -1;
}
static void initializeSubtitle(Connection connection) throws SQLException {
logger.debug("Creating subtitle table.");
try (Statement stmt = connection.createStatement()) {
String query = """
CREATE TABLE IF NOT EXISTS subtitles
(
id int GENERATED ALWAYS AS IDENTITY,
film_id int NOT NULL,
language_id int NOT NULL,
importer varchar(37),
importer_guild_id text,
imported_date timestamptz NOT NULL DEFAULT now(),
UNIQUE (film_id, language_id),
PRIMARY KEY (id),
FOREIGN KEY (film_id)
REFERENCES films (id),
FOREIGN KEY (language_id)
REFERENCES languages (id)
);""";
stmt.executeUpdate(query);
}
}
}