Skip to content

Commit

Permalink
fix #17, #16, #18
Browse files Browse the repository at this point in the history
  • Loading branch information
remijouannet committed Jun 21, 2022
1 parent 2b49699 commit fb0b352
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 49 deletions.
11 changes: 8 additions & 3 deletions control.lua
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,14 @@ histogram_train_direct_loop_time = prometheus.histogram("factorio_train_direct_l
gauge_train_arrival_time = prometheus.gauge("factorio_train_arrival_time", "train arrival time", {"station"})
histogram_train_arrival_time = prometheus.histogram("factorio_train_arrival_time_groups", "train arrival time", {"station"}, bucket_settings)

gauge_logistic_network_all_construction_robots = prometheus.gauge("factorio_logistic_network_all_construction_robots", "the number of construction robots available for a job", {"force", "location", "network"})
gauge_logistic_network_available_construction_robots = prometheus.gauge("factorio_logistic_network_available_construction_robots", "the total number of construction robots in the network (idle and active + in roboports)", {"force", "location", "network"})
gauge_logistic_network_all_construction_robots = prometheus.gauge("factorio_logistic_network_all_construction_robots", "the total number of construction robots in the network (idle and active + in roboports)", {"force", "location", "network"})
gauge_logistic_network_available_construction_robots = prometheus.gauge("factorio_logistic_network_available_construction_robots", "the number of construction robots available for a job", {"force", "location", "network"})

gauge_logistic_network_all_logistic_robots = prometheus.gauge("factorio_logistic_network_all_logistic_robots", "the total number of logistic robots in the network (idle and active + in roboports)", {"force", "location", "network"})
gauge_logistic_network_available_logistic_robots = prometheus.gauge("factorio_logistic_network_available_logistic_robots", "the number of logistic robots available for a job", {"force", "location", "network"})

gauge_logistic_network_robot_limit = prometheus.gauge("factorio_logistic_network_robot_limit", "the maximum number of robots the network can work with", {"force", "location", "network"})

gauge_logistic_network_items = prometheus.gauge("factorio_logistic_network_items", "the number of items in a logistic network", {"force", "location", "network", "name"})

gauge_power_production_input = prometheus.gauge("factorio_power_production_input", "power produced", {"force", "name", "network", "surface"})
Expand Down Expand Up @@ -88,7 +91,9 @@ end)
script.on_load(function()

if global.yarm_on_site_update_event_id then
script.on_event(global.yarm_on_site_update_event_id, handleYARM)
if script.get_event_handler(global.yarm_on_site_update_event_id) then
script.on_event(global.yarm_on_site_update_event_id, handleYARM)
end
end

on_power_load()
Expand Down
2 changes: 1 addition & 1 deletion data/prometheus_datasource.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ datasources:
type: prometheus
isDefault: true
url: http://prometheus:9090
timeInterval: 10
timeInterval: "10s"
4 changes: 2 additions & 2 deletions info.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"name": "graftorio2",
"version": "0.0.16",
"version": "0.0.17",
"title": "graftorio2",
"author": "remijouannet",
"description": "visualize metrics from your factorio game in grafana",
"homepage": "https://github.com/remijouannet/graftorio2",
"dependencies": ["? YARM >= 0.8.203"],
"dependencies": ["? YARM >= 0.8.207"],
"factorio_version": "1.1"
}
88 changes: 45 additions & 43 deletions power.lua
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ local function find_entity(unit_number, entity_type)
end

local function rescan_worlds()
print("rescan_worlds")
local networks = script_data.networks
local invalids = {}
local remove = {}
Expand Down Expand Up @@ -133,52 +132,55 @@ function on_power_destroy(event)
end

function on_power_load()
script_data = global.power_data or script_data
script_data.has_checked = false
end

function on_power_init()
global.power_data = global.power_data or script_data
script_data.has_checked = false
end

function on_power_tick(event)
if event.tick then
local ignored = get_ignored_networks_by_switches()

if not script_data.has_checked then
rescan_worlds()
script_data.has_checked = true
end

gauge_power_production_input:reset()
gauge_power_production_output:reset()

for idx, network in pairs(script_data.networks) do
-- reset old style in case it still is old
if network.entity then
network.entity_number = network.entity.unit_number
network.entity = nil
end
local entity = find_entity(network.entity_number, "electric-pole")
if not entity then
rescan_worlds()
entity = find_entity(network.entity_number, "electric-pole")
end
if entity and entity.valid and not ignored[entity.electric_network_id] and entity.electric_network_id == idx then
local force_name = entity.force.name
local surface_name = entity.surface.name
for name, n in pairs(entity.electric_network_statistics.input_counts) do
gauge_power_production_input:set(n, {force_name, name, idx, surface_name})
end
for name, n in pairs(entity.electric_network_statistics.output_counts) do
gauge_power_production_output:set(n, {force_name, name, idx, surface_name})
end
elseif entity and entity.valid and entity.electric_network_id ~= idx then
-- assume this network has been merged with some other so unset
script_data.networks[idx] = nil
elseif entity and not entity.valid then
-- Invalid entity remove anyhow
script_data.networks[idx] = nil
end
end
end
if event.tick then
local ignored = get_ignored_networks_by_switches()

if not script_data.has_checked then
rescan_worlds()
script_data.has_checked = true
end

gauge_power_production_input:reset()
gauge_power_production_output:reset()

for idx, network in pairs(script_data.networks) do
-- reset old style in case it still is old
if network.entity then
network.entity_number = network.entity.unit_number
network.entity = nil
end

local entity = find_entity(network.entity_number, "electric-pole")

if not entity then
rescan_worlds()
entity = find_entity(network.entity_number, "electric-pole")
end

if entity and entity.valid and not ignored[entity.electric_network_id] and entity.electric_network_id == idx then
local force_name = entity.force.name
local surface_name = entity.surface.name
for name, n in pairs(entity.electric_network_statistics.input_counts) do
gauge_power_production_input:set(n, {force_name, name, idx, surface_name})
end
for name, n in pairs(entity.electric_network_statistics.output_counts) do
gauge_power_production_output:set(n, {force_name, name, idx, surface_name})
end
elseif entity and entity.valid and entity.electric_network_id ~= idx then
-- assume this network has been merged with some other so unset
script_data.networks[idx] = nil
elseif entity and not entity.valid then
-- Invalid entity remove anyhow
script_data.networks[idx] = nil
end
end
end
end

0 comments on commit fb0b352

Please sign in to comment.