Private
Public Access
1
0
Files
lua-prometheus-exporter/power-stats.lua

75 lines
2.3 KiB
Lua

function AddPowerPole(event)
local e = event.entity
if e then
storage.representativePoles[e.unit_number] = e
-- Update cache with new network
storage.networkCache = nil
end
end
function RemovePowerPole(event)
local e = event.entity
if e then
storage.representativePoles[e.unit_number] = nil
-- Invalidate cache - force rebuild to find new representative if needed
storage.networkCache = nil
end
end
function GetNetworks()
-- Return cached networks if available, otherwise rebuild
if storage.networkCache then
return storage.networkCache
end
local networks = {}
for _, pole in pairs(storage.representativePoles) do
if pole.valid then
local netID = pole.electric_network_id
if netID then
networks[netID] = pole
end
end
end
storage.networkCache = networks
return networks
end
function ScanNetworks()
storage.representativePoles = {}
storage.networkCache = {}
for _, surface in pairs(game.surfaces) do
for _, pole in pairs(surface.find_entities_filtered{type = "electric-pole"}) do
if pole.valid and pole.electric_network_id then
storage.representativePoles[pole.unit_number] = pole
storage.networkCache[pole.electric_network_id] = pole
end
end
end
end
function SendPowerStats()
if options.enablePower then
local powerPart = {}
powerPart[#powerPart+1] = "---power-stats---\n"
for _, pole in pairs(GetNetworks()) do
if pole.valid and pole.type == "electric-pole" then
local input = pole.electric_network_statistics.input_counts
local output = pole.electric_network_statistics.output_counts
local surfaceName = pole.surface.name
for item,value in pairs(input) do
powerPart[#powerPart+1] = ("%s:%d:in:%s:%d"):format(surfaceName, pole.electric_network_id, item, value)
end
for item, value in pairs(output) do
powerPart[#powerPart+1] = ("%s:%d:out:%s:%d"):format(surfaceName,pole.electric_network_id, item, value)
end
end
end
helpers.send_udp(udpAddress,table.concat(powerPart,"\n"),serverIndex)
end
end