mirror of
https://github.com/kalarina-bit/Sparkledns.git
synced 2026-11-22 07:36:09 +00:00
Release v1.1.2
This commit is contained in:
@@ -16,14 +16,6 @@ class QueryEntry(
|
||||
val latencyMs: Int,
|
||||
)
|
||||
|
||||
data class QueryStats(
|
||||
val total: Long,
|
||||
val cached: Long,
|
||||
val blocked: Long,
|
||||
val failed: Long,
|
||||
val avgLatencyMs: Int,
|
||||
)
|
||||
|
||||
// In-memory only: nothing is written to disk or sent anywhere. StatsStore keeps hourly
|
||||
// numbers without domains for the charts.
|
||||
object QueryLog {
|
||||
@@ -31,14 +23,12 @@ object QueryLog {
|
||||
|
||||
private val entries = ArrayDeque<QueryEntry>()
|
||||
private var nextId = 0L
|
||||
private var total = 0L
|
||||
private var cached = 0L
|
||||
private var blocked = 0L
|
||||
private var failed = 0L
|
||||
private var latencySum = 0L
|
||||
private var latencyCount = 0L
|
||||
private val version = MutableStateFlow(0L)
|
||||
|
||||
// The last snapshot, handed out again while the log has not changed
|
||||
private var lastSnapshot: List<QueryEntry> = emptyList()
|
||||
private var snapshotVersion = 0L
|
||||
|
||||
// Fires right away, then at most once a second while queries keep coming: a busy log refreshes
|
||||
// the screen no more often than the old one-second polling did, and an idle one not at all
|
||||
fun changes(): Flow<Long> = version.transform {
|
||||
@@ -48,42 +38,32 @@ object QueryLog {
|
||||
|
||||
fun add(question: DnsMessage.Question?, status: QueryStatus, latencyMs: Int) {
|
||||
if (question == null) return
|
||||
val time = System.currentTimeMillis()
|
||||
synchronized(this) {
|
||||
if (entries.size >= MAX_ENTRIES) entries.removeFirst()
|
||||
entries.addLast(
|
||||
QueryEntry(nextId++, System.currentTimeMillis(), question.name.ifEmpty { "." }, question.type, status, latencyMs)
|
||||
)
|
||||
total++
|
||||
when (status) {
|
||||
QueryStatus.OK -> { latencySum += latencyMs; latencyCount++ }
|
||||
QueryStatus.CACHED -> cached++
|
||||
QueryStatus.BLOCKED -> blocked++
|
||||
QueryStatus.FAILED -> failed++
|
||||
}
|
||||
entries.addLast(QueryEntry(nextId++, time, question.name.ifEmpty { "." }, question.type, status, latencyMs))
|
||||
version.value++
|
||||
}
|
||||
// Hourly numbers for the charts; outside the lock above, as it takes a lock of its own
|
||||
StatsStore.record(status, latencyMs, Companies.of(question.name))
|
||||
StatsStore.record(status, latencyMs, Companies.of(question.name), time)
|
||||
}
|
||||
|
||||
// Newest first. Builds a real copy: on API 35+ List.reversed() resolves to a live JDK view,
|
||||
// and on older devices that method does not exist at all
|
||||
// and on older devices that method does not exist at all. While the log does not change the
|
||||
// same list comes back, so a screen refreshed with it has nothing to update.
|
||||
@Synchronized
|
||||
fun snapshot(): List<QueryEntry> = List(entries.size) { entries[entries.size - 1 - it] }
|
||||
|
||||
@Synchronized
|
||||
fun stats() = QueryStats(
|
||||
total = total,
|
||||
cached = cached,
|
||||
blocked = blocked,
|
||||
failed = failed,
|
||||
avgLatencyMs = if (latencyCount == 0L) 0 else (latencySum / latencyCount).toInt(),
|
||||
)
|
||||
fun snapshot(): List<QueryEntry> {
|
||||
val current = version.value
|
||||
if (snapshotVersion != current) {
|
||||
lastSnapshot = List(entries.size) { entries[entries.size - 1 - it] }
|
||||
snapshotVersion = current
|
||||
}
|
||||
return lastSnapshot
|
||||
}
|
||||
|
||||
@Synchronized
|
||||
fun clear() {
|
||||
entries.clear()
|
||||
total = 0; cached = 0; blocked = 0; failed = 0; latencySum = 0; latencyCount = 0
|
||||
version.value++
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user