From 86bfde90b89ff723a5eba44515b1780231098ef2 Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Tue, 2 Dec 2025 21:13:43 +0100 Subject: [PATCH] Improve Performance Signed-off-by: Chaoscaot --- .../src/de/steamwar/routes/AuditLog.kt | 85 +++++++++++++------ 1 file changed, 59 insertions(+), 26 deletions(-) diff --git a/WebsiteBackend/src/de/steamwar/routes/AuditLog.kt b/WebsiteBackend/src/de/steamwar/routes/AuditLog.kt index 27a59c72..4149aeef 100644 --- a/WebsiteBackend/src/de/steamwar/routes/AuditLog.kt +++ b/WebsiteBackend/src/de/steamwar/routes/AuditLog.kt @@ -32,6 +32,7 @@ import io.ktor.server.routing.Route import io.ktor.server.routing.get import io.ktor.server.routing.route import kotlinx.serialization.Serializable +import org.jetbrains.exposed.v1.core.Alias import org.jetbrains.exposed.v1.core.JoinType import org.jetbrains.exposed.v1.core.SortOrder import org.jetbrains.exposed.v1.core.alias @@ -41,8 +42,10 @@ import org.jetbrains.exposed.v1.core.isNull import org.jetbrains.exposed.v1.core.less import org.jetbrains.exposed.v1.core.like import org.jetbrains.exposed.v1.core.or +import org.jetbrains.exposed.v1.jdbc.Query import org.jetbrains.exposed.v1.jdbc.andWhere import org.jetbrains.exposed.v1.jdbc.select +import org.jetbrains.exposed.v1.jdbc.selectAll import java.time.Instant fun Route.configureAuditLog() { @@ -126,34 +129,32 @@ data class PagedAuditLog(val rows: Long, val entries: List) { *AuditLogTable.columns.toTypedArray() ) - - actionText?.let { - query.andWhere { (AuditLogTable.actionText like "%$it%") } - } - - serverText?.let { - query.andWhere { (AuditLogTable.server like "%$it%") } - } - - fullText?.let { - query.andWhere { (AuditLogTable.actionText like "%$it%") or (AuditLogTable.server like "%$it%") } - } - - actor?.let { query.andWhere { actorTable[SteamwarUserTable.uuid] inList actor } } - - actionType?.let { query.andWhere { AuditLogTable.action inList actionType } } - - timeGreater?.let { query.andWhere { AuditLogTable.time greater timeGreater } } - - timeLess?.let { query.andWhere { AuditLogTable.time less timeLess } } - - serverOwner?.let { query.andWhere { serverOwnerTable[SteamwarUserTable.uuid] inList serverOwner } } - - if (velocity == true) query.andWhere { AuditLogTable.serverOwner.isNull() } - PagedAuditLog( - query.count(), + AuditLogTable.selectAll().addAuditLogFilters( + actionText, + serverText, + fullText, + actor, + actionType, + timeGreater, + timeLess, + serverOwner, + velocity + ).count(), query + .addAuditLogFilters( + actionText, + serverText, + fullText, + actor, + actionType, + timeGreater, + timeLess, + serverOwner, + velocity, + actorTable, + serverOwnerTable + ) .limit(limit) .offset((page * limit).toLong()) .orderBy(AuditLogTable.time, SortOrder.valueOf(sorting.uppercase())) @@ -170,6 +171,38 @@ data class PagedAuditLog(val rows: Long, val entries: List) { }, ) } + + private fun Query.addAuditLogFilters( + actionText: String? = null, + serverText: String? = null, + fullText: String? = null, + actor: Set? = null, + actionType: Set? = null, + timeGreater: Instant? = null, + timeLess: Instant? = null, + serverOwner: Set? = null, + velocity: Boolean? = null, + actorTable: Alias? = null, + serverOwnerTable: Alias? = null + ): Query { + actionText?.let { + andWhere { (AuditLogTable.actionText like "%$it%") } + } + serverText?.let { + andWhere { (AuditLogTable.server like "%$it%") } + } + fullText?.let { + andWhere { (AuditLogTable.actionText like "%$it%") or (AuditLogTable.server like "%$it%") } + } + actor?.let { andWhere { actorTable!![SteamwarUserTable.uuid] inList actor } } + actionType?.let { andWhere { AuditLogTable.action inList actionType } } + timeGreater?.let { andWhere { AuditLogTable.time greater timeGreater } } + timeLess?.let { andWhere { AuditLogTable.time less timeLess } } + serverOwner?.let { andWhere { serverOwnerTable!![SteamwarUserTable.uuid] inList serverOwner } } + if (velocity == true) andWhere { AuditLogTable.serverOwner.isNull() } + + return this + } } }