Batched grafana writes into 30 second intervals

This commit is contained in:
ceikry
2023-08-06 09:36:53 -05:00
committed by Ryan
parent d14b2c7d09
commit 8b6678d4ee
@@ -4,7 +4,11 @@ import core.ServerConstants
import core.api.StartupListener import core.api.StartupListener
import core.game.bots.AIRepository import core.game.bots.AIRepository
import core.integrations.sqlite.SQLiteProvider import core.integrations.sqlite.SQLiteProvider
import core.tools.cycle
import kotlinx.coroutines.Job
import org.json.simple.JSONObject import org.json.simple.JSONObject
import java.util.LinkedList
import java.util.concurrent.LinkedBlockingDeque
class Grafana : StartupListener { class Grafana : StartupListener {
override fun startup() { override fun startup() {
@@ -23,7 +27,9 @@ class Grafana : StartupListener {
val botPulseTime: Int, val botPulseTime: Int,
val otherPulseTime: Int, val otherPulseTime: Int,
val pulseTimes: Array<Map.Entry<String, Int>>, val pulseTimes: Array<Map.Entry<String, Int>>,
val pulseCounts: Array<Map.Entry<String, Int>> val pulseCounts: Array<Map.Entry<String, Int>>,
val botCount: Int,
val timeSecs: Int
) )
companion object { companion object {
@@ -35,6 +41,8 @@ class Grafana : StartupListener {
var botPulseTime = 0 var botPulseTime = 0
var otherPulseTime = 0 var otherPulseTime = 0
lateinit var db: SQLiteProvider lateinit var db: SQLiteProvider
var cycleData = LinkedList<GrafanaData>()
var currentTask: Job? = null
var pulseTimes = HashMap<String, Int>() var pulseTimes = HashMap<String, Int>()
var pulseCounts = HashMap<String, Int>() var pulseCounts = HashMap<String, Int>()
@@ -64,7 +72,7 @@ class Grafana : StartupListener {
fun endTick() { fun endTick() {
if (!this::db.isInitialized) return if (!this::db.isInitialized) return
val cycleData = GrafanaData ( val thisCycle = GrafanaData (
playerTickTime, playerTickTime,
npcTickTime, npcTickTime,
playerRenderTime, playerRenderTime,
@@ -73,52 +81,69 @@ class Grafana : StartupListener {
botPulseTime, botPulseTime,
otherPulseTime, otherPulseTime,
pulseTimes.entries.toTypedArray(), pulseTimes.entries.toTypedArray(),
pulseCounts.entries.toTypedArray() pulseCounts.entries.toTypedArray(),
AIRepository.PulseRepository.size,
getNowTime()
) )
db.runAsync { it -> cycleData.add(thisCycle)
if (cycleData.size < 50) return
if (currentTask?.isActive == true) return
currentTask = db.runAsync {
with (it.prepareStatement(INSERT_TOP_PULSES)) { with (it.prepareStatement(INSERT_TOP_PULSES)) {
val topSorted = cycleData.pulseTimes.sortedByDescending { entry -> entry.value } for (i in 0 until 50) {
val rootObj = JSONObject() val topSorted = cycleData[i].pulseTimes.sortedByDescending { entry -> entry.value }
val contentObj = JSONObject() val rootObj = JSONObject()
for (i in 0 until 5) { val contentObj = JSONObject()
contentObj [topSorted[i].key] = topSorted[i].value for (j in 0 until 5) {
contentObj[topSorted[j].key] = topSorted[j].value
}
rootObj["pulses"] = contentObj
setString(1, rootObj.toJSONString())
setInt(2, cycleData[i].timeSecs)
execute()
} }
rootObj["pulses"] = contentObj
setString(1, rootObj.toJSONString())
setInt(2, getNowTime())
execute()
} }
with (it.prepareStatement(INSERT_PULSE_COUNT)) { with (it.prepareStatement(INSERT_PULSE_COUNT)) {
val topSorted = cycleData.pulseCounts.sortedByDescending { entry -> entry.value } for (i in 0 until 50) {
val rootObj = JSONObject() val topSorted = cycleData[i].pulseCounts.sortedByDescending { entry -> entry.value }
val contentObj = JSONObject() val rootObj = JSONObject()
for (i in 0 until 5) { val contentObj = JSONObject()
contentObj [topSorted[i].key] = topSorted[i].value for (j in 0 until 5) {
contentObj[topSorted[j].key] = topSorted[j].value
}
rootObj["pulses"] = contentObj
setString(1, rootObj.toJSONString())
setInt(2, cycleData[i].timeSecs)
execute()
} }
rootObj["pulses"] = contentObj
setString(1, rootObj.toJSONString())
setInt(2, getNowTime())
execute()
} }
with (it.prepareStatement(INSERT_BOT_COUNT)) { with (it.prepareStatement(INSERT_BOT_COUNT)) {
setInt(1, AIRepository.PulseRepository.size) for (i in 0 until 50) {
setInt(2, getNowTime()) setInt(1, cycleData[i].botCount)
execute() setInt(2, cycleData[i].timeSecs)
execute()
}
} }
with (it.prepareStatement(INSERT_TICK_MEAS)) { with (it.prepareStatement(INSERT_TICK_MEAS)) {
setInt(1, cycleData.botPulseTime) for (i in 0 until 50) {
setInt(2, cycleData.otherPulseTime) setInt(1, cycleData[i].botPulseTime)
setInt(3, cycleData.npcTickTime) setInt(2, cycleData[i].otherPulseTime)
setInt(4, cycleData.playerTickTime) setInt(3, cycleData[i].npcTickTime)
setInt(5, cycleData.playerRenderTime) setInt(4, cycleData[i].playerTickTime)
setInt(6, cycleData.packetProcessTime) setInt(5, cycleData[i].playerRenderTime)
setInt(7, cycleData.totalTickTime - (cycleData.botPulseTime + cycleData.otherPulseTime + cycleData.npcTickTime + cycleData.playerTickTime + cycleData.playerRenderTime + cycleData.packetProcessTime)) setInt(6, cycleData[i].packetProcessTime)
setInt(8, getNowTime()) setInt(7, cycleData[i].totalTickTime - (cycleData[i].botPulseTime + cycleData[i].otherPulseTime + cycleData[i].npcTickTime + cycleData[i].playerTickTime + cycleData[i].playerRenderTime + cycleData[i].packetProcessTime))
execute() setInt(8, cycleData[i].timeSecs)
execute()
}
} }
cycleData.removeAll(cycleData.subList(0, 49).toSet())
} }
} }