Publish lagometer JSON file

If the szutil_lagometer_publish_json setting is enabled, write a
JSON file to the world path with lagometer data every publish
cycle, so that external systems can access it.

Also fix formatting issues in the lagometer format string.
This commit is contained in:
Aaron Suen 2024-08-20 07:00:39 -04:00
parent 1ccad0ca3c
commit c0e717fe27

View File

@ -1,10 +1,10 @@
-- LUALOCALS < ---------------------------------------------------------
local ipairs, loadstring, math, minetest, pairs, string, tonumber
= ipairs, loadstring, math, minetest, pairs, string, tonumber
local math_ceil, math_floor, string_format, string_gsub, string_rep,
string_sub
= math.ceil, math.floor, string.format, string.gsub, string.rep,
string.sub
local ipairs, loadstring, math, minetest, os, pairs, string, tonumber
= ipairs, loadstring, math, minetest, os, pairs, string, tonumber
local math_ceil, math_floor, os_time, string_format, string_gsub,
string_rep, string_sub
= math.ceil, math.floor, os.time, string.format, string.gsub,
string.rep, string.sub
-- LUALOCALS > ---------------------------------------------------------
local modname = minetest.get_current_modname()
@ -121,6 +121,7 @@ local graphbar = string_rep("|", graphbar_width)
-- Function to publish current lag values to all receiving parties.
local hud_elem_type = minetest.features.hud_def_type_field and "type" or "hud_elem_type"
local meterfmt = "%2.2f %s %2.2f%s"
local function publish()
-- Expire old periods, and accumulate current ones.
local accum = newperiod()
@ -141,7 +142,7 @@ local function publish()
for bucket = 1, bucket_max do
local qty = accum[bucket]
local line = qty <= 0 and "" or string_format(" % 2.2f % s % 2.2f % s", qty,
local line = qty <= 0 and "" or string_format(meterfmt, qty,
-- Maximum width of a graph bar corresponds to 50% of the total
-- time in the window, so that there will never be 2 bars of the
-- same length that don't have the same amount of time, even if
@ -194,6 +195,22 @@ local function publish()
end
end
end
-- Publish a JSON dump for external use.
if minetest.settings:get_bool(modname .. "_publish_json") then
minetest.safe_file_write(
minetest.get_worldpath() .. "/lagometer.json",
minetest.write_json({
timestamp = os_time(),
interval = interval,
period_length = period_length,
period_count = period_count,
bucket_step = bucket_step,
bucket_max = bucket_max,
buckets = accum
}))
end
end
-- Run the publish method on a timer, so that player displays