commonMain.com.caesarealabs.loggy.database.LogService.kt Maven / Gradle / Ivy
The newest version!
package com.caesarealabs.loggy.database
import com.caesarealabs.loggy.shared.*
import com.caesarealabs.rpc4k.generated.LoggyServiceEventInvoker
import com.caesarealabs.searchit.DataLens
import com.caesarealabs.searchit.Database
import com.caesarealabs.searchit.SearchitResult
import com.caesarealabs.searchit.TimeRange
import kotlinx.datetime.TimeZone
interface LoggyDatabase : Database {
suspend fun getEndpoints(): List
class InMemory(items: List, lens: DataLens) : LoggyDatabase {
private val db = Database.InMemory.create(items, lens)
override suspend fun getEndpoints(): List {
return db.query(null, mapOf()).map { it.endpoint }.distinct()
}
override suspend fun query(timeRange: TimeRange?, keyValues: Map): List {
return db.query(timeRange, keyValues)
}
}
}
// TO DO: lib only needs the events part, so once it is split properly in rpc4k, only use events here and the entire impl should be reserved to
// :service
/**
* Rpc service in charge of providing log information after it has been stored.
*/
class LoggyServiceImpl(
// We may use this invoker later
@Suppress("unused") private val invoker: LoggyServiceEventInvoker,
private val database: LoggyDatabase
) : LoggyService {
private val searchit = createSearchit(database)
companion object;
override suspend fun getEndpoints(): List {
return database.getEndpoints()
}
/**
* Get invocations matching the [query].
* The results will be sorted from most recent to least recent.
*/
override suspend fun getInvocations(query: Query, userTimeZone: String): SearchResult {
return searchit.search(query.text, query.page, TimeZone.of(userTimeZone), query.maxItemsPerPage).toGeneral()
}
override suspend fun getDataValuesForKeyInQuery(query: String, key: String, userTimeZone: String): List? {
return searchit.getValuesForKeyInQuery(query, key, TimeZone.of(userTimeZone))
}
override fun onInvocationAdded(
invocation: Invocation,
/*@EventTarget */endpoint: String?,
query: Query
): Invocation {
//TODO: unfortunately currently there is no way to specify that an event shouldn't be dispatched based on the query
return invocation
}
}
fun SearchitResult.toGeneral(): SearchResult = SearchResult(error = error,
success = success?.let { SearchSuccess(it.pageCount, it.items, it.searchKeys) }
)