Skip to content

Commit

Permalink
Deps/major sadu (#615)
Browse files Browse the repository at this point in the history
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
  • Loading branch information
rainbowdashlabs and renovate[bot] committed Jul 15, 2024
1 parent 16e12bd commit 22d2309
Show file tree
Hide file tree
Showing 57 changed files with 1,185 additions and 1,337 deletions.
1 change: 1 addition & 0 deletions .envrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
use nix
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,4 @@ buildNumber.properties
/out/

/site/
/.direnv
3 changes: 1 addition & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ plugins {
alias(libs.plugins.shadow)
alias(libs.plugins.spotless)
java
`maven-publish`
}

group = "de.chojo"
Expand Down Expand Up @@ -49,7 +48,7 @@ dependencies {

java {
toolchain {
languageVersion.set(JavaLanguageVersion.of(19))
languageVersion.set(JavaLanguageVersion.of(21))
}
withSourcesJar()
withJavadocJar()
Expand Down
2 changes: 1 addition & 1 deletion docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM gradle:jdk19-alpine as build
FROM gradle:jdk21-alpine as build

COPY . .
RUN gradle clean build --no-daemon
Expand Down
2 changes: 1 addition & 1 deletion settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ dependencyResolutionManagement {
versionCatalogs {
create("libs") {
// misc
version("sadu", "1.4.1")
version("sadu", "2.2.0")
library("sadu-queries", "de.chojo.sadu", "sadu-queries").versionRef("sadu")
library("sadu-updater", "de.chojo.sadu", "sadu-updater").versionRef("sadu")
library("sadu-postgresql", "de.chojo.sadu", "sadu-postgresql").versionRef("sadu")
Expand Down
6 changes: 6 additions & 0 deletions shell.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{ pkgs ? import <nixpkgs> {}, ... }:

pkgs.mkShell {
packages = with pkgs; [jdk21];
}

64 changes: 36 additions & 28 deletions src/main/java/de/chojo/repbot/core/Data.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,19 @@
import de.chojo.repbot.dao.access.Analyzer;
import de.chojo.repbot.dao.access.Cleanup;
import de.chojo.repbot.dao.access.Gdpr;
import de.chojo.repbot.dao.access.gdpr.RemovalTask;
import de.chojo.repbot.dao.provider.Guilds;
import de.chojo.repbot.dao.provider.Metrics;
import de.chojo.repbot.dao.provider.Voice;
import de.chojo.repbot.util.LogNotify;
import de.chojo.sadu.databases.PostgreSql;
import de.chojo.sadu.datasource.DataSourceCreator;
import de.chojo.sadu.mapper.RowMapperRegistry;
import de.chojo.sadu.mapper.rowmapper.RowMapper;
import de.chojo.sadu.postgresql.databases.PostgreSql;
import de.chojo.sadu.postgresql.mapper.PostgresqlMapper;
import de.chojo.sadu.queries.configuration.QueryConfiguration;
import de.chojo.sadu.updater.QueryReplacement;
import de.chojo.sadu.updater.SqlUpdater;
import de.chojo.sadu.wrapper.QueryBuilderConfig;
import org.slf4j.Logger;

import java.io.IOException;
Expand Down Expand Up @@ -50,8 +54,8 @@ public static Data create(Threading threading, Configuration configuration) thro
}

public void init() throws SQLException, IOException {
configure();
initConnection();
configure();
updateDatabase();
initDao();
}
Expand All @@ -72,46 +76,50 @@ public void initConnection() {
private void updateDatabase() throws IOException, SQLException {
var schema = configuration.database().schema();
SqlUpdater.builder(dataSource, PostgreSql.get())
.setReplacements(new QueryReplacement("repbot_schema", schema))
.setVersionTable(schema + ".repbot_version")
.setSchemas(schema)
.execute();
.setReplacements(new QueryReplacement("repbot_schema", schema))
.setVersionTable(schema + ".repbot_version")
.setSchemas(schema)
.execute();
}

private void configure() {
log.info("Configuring QueryBuilder");
log.info("Configuring Query Configuration");
var logger = getLogger("DbLogger");
QueryBuilderConfig.setDefault(QueryBuilderConfig.builder()
.withExceptionHandler(err -> logger.error(LogNotify.NOTIFY_ADMIN, "An error occured during a database request", err))
.withExecutor(threading.repBotWorker())
.build());
var registry = new RowMapperRegistry();
registry.register(RowMapper.forClass(RemovalTask.class).mapper(RemovalTask::build).build());
registry.register(PostgresqlMapper.getDefaultMapper());
QueryConfiguration.setDefault(
QueryConfiguration.builder(dataSource)
.setExceptionHandler(err -> logger.error(LogNotify.NOTIFY_ADMIN, "An error occured during a database request", err))
.setRowMapperRegistry(registry)
.build());
}

private void initDao() {
log.info("Creating DAOs");
guilds = new Guilds(dataSource, configuration);
gdpr = new Gdpr(dataSource, configuration);
cleanup = new Cleanup(dataSource);
guilds = new Guilds(configuration);
gdpr = new Gdpr(configuration);
cleanup = new Cleanup();
metrics = new Metrics(dataSource);
analyzer = new Analyzer(dataSource, configuration);
voice = new Voice(dataSource, configuration);
analyzer = new Analyzer(configuration);
voice = new Voice(configuration);
}

private HikariDataSource getConnectionPool() {
log.info("Creating connection pool.");
var data = configuration.database();
return DataSourceCreator.create(PostgreSql.get())
.configure(config -> config
.host(data.host())
.port(data.port())
.user(data.user())
.password(data.password())
.database(data.database()))
.create()
.withMaximumPoolSize(data.poolSize())
.withThreadFactory(Threading.createThreadFactory(threading.hikariGroup()))
.forSchema(data.schema())
.build();
.configure(config -> config
.host(data.host())
.port(data.port())
.user(data.user())
.password(data.password())
.database(data.database()))
.create()
.withMaximumPoolSize(data.poolSize())
.withThreadFactory(Threading.createThreadFactory(threading.hikariGroup()))
.forSchema(data.schema())
.build();
}

public Guilds guilds() {
Expand Down
43 changes: 22 additions & 21 deletions src/main/java/de/chojo/repbot/dao/access/Analyzer.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,40 @@
*/
package de.chojo.repbot.dao.access;

import de.chojo.jdautil.util.Futures;
import de.chojo.repbot.config.Configuration;
import de.chojo.repbot.util.LogNotify;
import de.chojo.sadu.base.QueryFactory;
import de.chojo.sadu.queries.api.query.Query;
import org.slf4j.Logger;

import javax.sql.DataSource;

import static de.chojo.sadu.queries.api.call.Call.call;
import static org.slf4j.LoggerFactory.getLogger;

public class Analyzer extends QueryFactory {
public class Analyzer {
private static final Logger log = getLogger(Analyzer.class);
private final Configuration configuration;

public Analyzer(DataSource dataSource, Configuration configuration) {
super(dataSource);
public Analyzer(Configuration configuration) {
this.configuration = configuration;
}

public void cleanup() {
builder().query("""
DELETE FROM analyzer_results WHERE analyzed < NOW() - ?::interval;
""")
.parameter(stmt -> stmt.setString("%d HOURS".formatted(configuration.cleanup().analyzerLogHours())))
.delete()
.send()
.whenComplete(Futures.whenComplete(res -> log.debug("Deleted {} entries from analyzer log", res.rows()),
err -> log.error(LogNotify.NOTIFY_ADMIN, "Could not cleanup analyzer log.", err)));
builder().query("DELETE FROM reputation_results WHERE submitted < now() - ?::interval")
.parameter(stmt -> stmt.setString("%d HOURS".formatted(configuration.cleanup().analyzerLogHours())))
.delete()
.send()
.whenComplete(Futures.whenComplete(res -> log.debug("Deleted {} entries from reputation results", res.rows()),
err -> log.error(LogNotify.NOTIFY_ADMIN, "Could not cleanup reputation results.", err)));
var delete = Query.query("""
DELETE FROM analyzer_results WHERE analyzed < now() - ?::INTERVAL;
""")
.single(call().bind("%d HOURS".formatted(configuration.cleanup().analyzerLogHours())))
.delete();
if (delete.changed()) {
log.debug("Deleted {} entries from analyzer log", delete.rows());
} else {
delete.exceptions().forEach(e -> log.error(LogNotify.NOTIFY_ADMIN, "Could not cleanup analyzer log.", e));
}
delete = Query.query("DELETE FROM reputation_results WHERE submitted < now() - ?::INTERVAL")
.single(call().bind("%d HOURS".formatted(configuration.cleanup().analyzerLogHours())))
.delete();
if (delete.changed()) {
log.debug("Deleted {} entries from reputation results", delete.rows());
} else {
delete.exceptions().forEach(e -> log.error(LogNotify.NOTIFY_ADMIN, "Could not cleanup analyzer log.", e));
}
}
}
19 changes: 6 additions & 13 deletions src/main/java/de/chojo/repbot/dao/access/Cleanup.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,15 @@
*/
package de.chojo.repbot.dao.access;

import de.chojo.sadu.base.QueryFactory;
import de.chojo.sadu.queries.api.query.Query;

import javax.sql.DataSource;
import java.util.List;

public class Cleanup extends QueryFactory {
public Cleanup(DataSource dataSource) {
super(dataSource);
}

public class Cleanup {
public List<Long> getCleanupList() {
return builder(Long.class)
.queryWithoutParams("""
SELECT guild_id FROM self_cleanup;
""")
.readRow(stmt -> stmt.getLong("guild_id"))
.allSync();
return Query.query("SELECT guild_id FROM self_cleanup;")
.single()
.mapAs(Long.class)
.all();
}
}
70 changes: 34 additions & 36 deletions src/main/java/de/chojo/repbot/dao/access/Gdpr.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,55 +8,52 @@
import de.chojo.repbot.config.Configuration;
import de.chojo.repbot.dao.access.gdpr.GdprUser;
import de.chojo.repbot.dao.access.gdpr.RemovalTask;
import de.chojo.sadu.base.QueryFactory;
import de.chojo.sadu.queries.api.query.Query;
import net.dv8tion.jda.api.entities.User;
import net.dv8tion.jda.api.sharding.ShardManager;
import org.slf4j.Logger;

import javax.sql.DataSource;
import java.util.List;
import java.util.Objects;

import static de.chojo.sadu.queries.api.call.Call.call;
import static org.slf4j.LoggerFactory.getLogger;

public class Gdpr extends QueryFactory {
public class Gdpr {
private static final Logger log = getLogger(Gdpr.class);
private final Configuration configuration;

public Gdpr(DataSource dataSource, Configuration configuration) {
super(dataSource);
public Gdpr(Configuration configuration) {
this.configuration = configuration;
}

public List<RemovalTask> getRemovalTasks() {
return builder(RemovalTask.class)
.queryWithoutParams("""
SELECT
task_id,
user_id,
guild_id
FROM
cleanup_schedule
WHERE delete_after < NOW();
""")
.readRow(rs -> RemovalTask.build(this, rs))
.allSync();
return Query.query("""
SELECT
task_id,
user_id,
guild_id
FROM
cleanup_schedule
WHERE delete_after < now();
""")
.single()
.map(RemovalTask::build)
.all();
}

public void cleanupRequests() {
builder()
.query("""
DELETE FROM gdpr_log
WHERE received IS NOT NULL
AND received < NOW() - ?::INTERVAL;
""", configuration.cleanup().gdprDays())
.parameter(stmt -> stmt.setString("%d DAYS".formatted(configuration.cleanup().gdprDays())))
.update()
.sendSync();
Query.query("""
DELETE FROM gdpr_log
WHERE received IS NOT NULL
AND received < now() - ?::INTERVAL;
""", configuration.cleanup().gdprDays())
.single(call().bind("%d DAYS".formatted(configuration.cleanup().gdprDays())))
.update();
}

public GdprUser request(User user) {
return new GdprUser(this, user);
return new GdprUser(user);
}

/**
Expand All @@ -68,15 +65,16 @@ public GdprUser request(User user) {
* @return list of users
*/
public List<GdprUser> getReportRequests(ShardManager shardManager) {
return builder(GdprUser.class)
.queryWithoutParams("""
SELECT user_id
FROM gdpr_log
WHERE received IS NULL
AND last_attempt < NOW() - (LEAST(48, attempts) || ' HOURS')::interval
""")
.readRow(rs -> GdprUser.build(this, rs, shardManager))
.allSync()
return Query
.query("""
SELECT user_id
FROM gdpr_log
WHERE received IS NULL
AND last_attempt < now() - (least(48, attempts) || ' HOURS')::INTERVAL
""")
.single()
.map(rs -> GdprUser.build(rs, shardManager))
.all()
.stream()
.filter(Objects::nonNull)
.toList();
Expand Down
Loading

0 comments on commit 22d2309

Please sign in to comment.