commonMain.com.caesarealabs.loggy.database.Searchit.kt Maven / Gradle / Ivy
The newest version!
package com.caesarealabs.loggy.database
import com.caesarealabs.logging.LogSeverity
import com.caesarealabs.loggy.shared.Invocation
import com.caesarealabs.searchit.*
internal fun createSearchit(database: Database) = Searchit(
database,
InvocationDataLens,
listOf(ExactSeverityFilter, AtLeastSeverityFilter)
)
/**
* Allows searchit to inspect an [Invocation]
*/
object InvocationDataLens : DataLens {
override fun hasKeyValue(item: Invocation, key: String, value: String): Boolean {
return item.metadata.any { it.key.equals(key, ignoreCase = true) && it.value.equals(value, ignoreCase = true) }
}
override val directKeys: Set = setOf(Invocation::endpoint.name)
override val indirectKeys: DataKeys = DataKeys.Dynamic { item ->
item.metadata.map { it.key }.toHashSet()
}
override fun getValues(item: Invocation, key: String): List {
return item.metadata.filter { it.key.equals(key, ignoreCase = true) }.map { it.value }
}
override val sortProperty: SortKeyProperty = SortKeyProperty.ofInstant("startTime") { startTime }
override val timeProperty: TimeProperty = TimeProperty.ofInstant("startTime") { startTime }
override fun containsText(item: Invocation, text: String): Boolean {
// check metadata for text
return item.metadata.any {
it.key.contains(text, ignoreCase = true) || it.value.contains(
text,
ignoreCase = true
)
}
// Check logs for text
|| item.logs.any { it.message.contains(text, ignoreCase = true) }
}
}
/**
* Allow searching by the exact importance of an invocation
*/
private val ExactSeverityFilter = SpecialFilter("levelExact") { severityString ->
val severity = resolveSeverity(severityString) ?: return@SpecialFilter null
{ it.importance == severity }
}
/**
* Allow searching by the importance being at least a value
*/
private val AtLeastSeverityFilter = SpecialFilter("level") { severityString ->
val severity = resolveSeverity(severityString) ?: return@SpecialFilter null
{ it.importance >= severity }
}
private fun resolveSeverity(severityString: String): LogSeverity? {
return when (severityString) {
"verbose" -> LogSeverity.Verbose
"debug" -> LogSeverity.Debug
"info" -> LogSeverity.Info
"warn", "warning" -> LogSeverity.Warn
"error" -> LogSeverity.Error
else -> null
}
}