Skip to content

Commit

Permalink
feat: improve logs and timestamp format for /get-server-details (#120)
Browse files Browse the repository at this point in the history
  • Loading branch information
DarkAtra authored May 28, 2024
1 parent 1908eae commit 4f8c0d9
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 6 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

<groupId>de.darkatra</groupId>
<artifactId>v-rising-discord-bot</artifactId>
<version>2.9.5</version>
<version>2.10.0</version>
<packaging>jar</packaging>

<licenses>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ class GetServerDetailsCommand(
value = when (serverStatusMonitor.recentErrors.isEmpty()) {
true -> "-"
false -> serverStatusMonitor.recentErrors.joinToString("\n") {
"${it.timestamp}```${StringUtils.truncate(it.message, botProperties.maxCharactersPerError)}```"
"<t:${it.timestamp}:R>```${StringUtils.truncate(it.message, botProperties.maxCharactersPerError)}```"
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package de.darkatra.vrising.discord.migration

import de.darkatra.vrising.discord.serverstatus.model.ServerStatusMonitor
import de.darkatra.vrising.discord.serverstatus.model.ServerStatusMonitorStatus
import org.dizitart.no2.Document
import org.dizitart.no2.Nitrite
import org.dizitart.no2.objects.filters.ObjectFilters
import org.dizitart.no2.util.ObjectUtils
Expand Down Expand Up @@ -78,6 +79,22 @@ class DatabaseMigrationService(
documentAction = { document ->
document["embedEnabled"] = true
}
),
DatabaseMigration(
description = "Serialize error timestamp as long (epochSecond).",
isApplicable = { currentSchemaVersion -> currentSchemaVersion.major < 2 || (currentSchemaVersion.major == 2 && currentSchemaVersion.minor <= 9) },
documentAction = { document ->
val recentErrors = document["recentErrors"]
if (recentErrors is List<*>) {
recentErrors.filterIsInstance<Document>().forEach { error ->
if (error["timestamp"] is String) {
error["timestamp"] = Instant.parse(error["timestamp"] as String).epochSecond
}
}
} else {
document["recentErrors"] = emptyList<Any>()
}
}
)
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import dev.kord.core.exception.EntityNotFoundException
import dev.kord.rest.builder.message.EmbedBuilder
import dev.kord.rest.builder.message.embed
import org.slf4j.LoggerFactory
import org.slf4j.MDC
import org.springframework.stereotype.Service
import java.time.Instant
import java.time.temporal.ChronoUnit
Expand All @@ -32,6 +33,9 @@ class ServerStatusMonitorService(

suspend fun updateServerStatusMonitors(kord: Kord) {
serverStatusMonitorRepository.getServerStatusMonitors(status = ServerStatusMonitorStatus.ACTIVE).forEach { serverStatusMonitor ->

MDC.put("server-status-monitor-id", serverStatusMonitor.id)

updateServerStatusMonitor(kord, serverStatusMonitor)
updatePlayerActivityFeed(kord, serverStatusMonitor)
updatePvpKillFeed(kord, serverStatusMonitor)
Expand All @@ -40,6 +44,8 @@ class ServerStatusMonitorService(
} catch (e: OutdatedServerStatusMonitorException) {
logger.debug("Server status monitor was updated or deleted by another thread. Will ignore this exception and proceed as usual.", e)
}

MDC.clear()
}
}

Expand Down Expand Up @@ -110,7 +116,7 @@ class ServerStatusMonitorService(
add(
Error(
message = "${e::class.simpleName}: ${e.message}",
timestamp = Instant.now().toString()
timestamp = Instant.now().epochSecond
)
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ package de.darkatra.vrising.discord.serverstatus.model

data class Error(
val message: String,
val timestamp: String
val timestamp: Long
)
2 changes: 2 additions & 0 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,7 @@ logging:
level:
root: info
com.ibasco.agql: warn
pattern:
console: "%clr(%d{yyyy-MM-dd'T'HH:mm:ss.SSSXXX}){faint} %clr(%5p) %clr(${PID:-}){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m %mdc%n%wEx"

version: @project.version@
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,17 @@ class DatabaseMigrationServiceTest {
repository.insert(Schema(appVersion = "V2.2.0"))
repository.insert(Schema(appVersion = "V2.3.0"))
repository.insert(Schema(appVersion = "V2.9.0"))
repository.insert(Schema(appVersion = "V2.10.0"))

val databaseMigrationService = DatabaseMigrationService(
database = database,
appVersionFromPom = "2.9.0"
appVersionFromPom = "2.10.0"
)

assertThat(databaseMigrationService.migrateToLatestVersion()).isFalse()

val schemas = repository.find().toList()
assertThat(schemas).hasSize(7)
assertThat(schemas).hasSize(8)
}

@Test
Expand Down

0 comments on commit 4f8c0d9

Please sign in to comment.