Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2.0 support, circuit network exporter #41

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions Metrics.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,12 @@ Histogram bucket sizes can also be adjusted in the settings.

| Name | Labels | Description |
|-----------------------------------------------------------|----------------------------------------------------|----------------------------------------------------------------------|
| `factorio_logistic_network_all_construction_robots` | force<br/>location (=surface)<br/>network | Total number of construction robots in the network |
| `factorio_logistic_network_available_construction_robots` | force<br/>location (=surface)<br/>network | Number of construction robots in the network available for a new job |
| `factorio_logistic_network_all_logistic_robots` | force<br/>location (=surface)<br/>network | Total number of construction robots in the network |
| `factorio_logistic_network_available_logistic_robots` | force<br/>location (=surface)<br/>network | Number of construction robots in the network available for a new job |
| `factorio_logistic_network_robot_limit` | force<br/>location (=surface)<br/>network | Maximum number of robot the network can work with |
| `factorio_logistic_network_items` | force<br/>location (=surface)<br/>network<br/>name | Number of item `name` in the network |
| `factorio_logistic_network_all_construction_robots` | force<br/>surface<br/>network | Total number of construction robots in the network |
| `factorio_logistic_network_available_construction_robots` | force<br/>surface<br/>network | Number of construction robots in the network available for a new job |
| `factorio_logistic_network_all_logistic_robots` | force<br/>surface<br/>network | Total number of construction robots in the network |
| `factorio_logistic_network_available_logistic_robots` | force<br/>surface<br/>network | Number of construction robots in the network available for a new job |
| `factorio_logistic_network_robot_limit` | force<br/>surface<br/>network | Maximum number of robot the network can work with |
| `factorio_logistic_network_items` | force<br/>surface<br/>network<br/>name | Number of item `name` in the network |


### Other
Expand Down
77 changes: 77 additions & 0 deletions circuit-network.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
local data = {
inited = false,
combinators = {},
}

local function rescan()
data.combinators = {}
for _, player in pairs(game.players) do
for _, surface in pairs(game.surfaces) do
for _, combinator in
pairs(surface.find_entities_filtered({
force = player.force,
type = "constant-combinator",
}))
do
data.combinators[combinator.unit_number] = combinator
end
end
end
data.inited = true
end

function on_circuit_network_build(event)
local entity = event.entity
if entity and entity.name == "constant-combinator" then
data.inited = false
end
end

function on_circuit_network_destroy(event)
local entity = event.entity
if entity.name == "constant-combinator" then
data.inited = false
end
end

function on_circuit_network_init()
data.inited = false
end

function on_circuit_network_load()
data.inited = false
end

function on_circuit_network_tick(event)
if event.tick then
if not data.inited then
rescan()
end

gauge_circuit_network_monitored:reset()
gauge_circuit_network_signal:reset()
local seen = {}
for _, combinator in pairs(data.combinators) do
for _, wire_type in pairs({ defines.wire_connector_id.circuit_red, defines.wire_connector_id.circuit_green }) do
local network = combinator.get_circuit_network(wire_type)
if network ~= nil and seen[network.network_id] == nil and network.signals ~= nil then
seen[network.network_id] = true
local network_id = tostring(network.network_id)
gauge_circuit_network_monitored:set(
1,
{ combinator.force.name, combinator.surface.name, network_id }
)
for _, signal in ipairs(network.signals) do
gauge_circuit_network_signal:set(signal.count, {
combinator.force.name,
combinator.surface.name,
network_id,
signal.signal.name,
signal.signal.quality,
})
end
end
end
end
end
end
89 changes: 62 additions & 27 deletions control.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ require("yarm")
require("events")
require("power")
require("research")
require("circuit-network")

bucket_settings = train_buckets(settings.startup["graftorio2-train-histogram-buckets"].value)
nth_tick = settings.startup["graftorio2-nth-tick"].value
Expand All @@ -17,34 +18,34 @@ gauge_total_player_count = prometheus.gauge("factorio_total_player_count", "tota
gauge_seed = prometheus.gauge("factorio_seed", "seed", { "surface" })
gauge_mods = prometheus.gauge("factorio_mods", "mods", { "name", "version" })

gauge_item_production_input = prometheus.gauge("factorio_item_production_input", "items produced", { "force", "name" })
gauge_item_production_input = prometheus.gauge("factorio_item_production_input", "items produced", { "force", "name", "surface" })
gauge_item_production_output =
prometheus.gauge("factorio_item_production_output", "items consumed", { "force", "name" })
prometheus.gauge("factorio_item_production_output", "items consumed", { "force", "name", "surface" })

gauge_fluid_production_input =
prometheus.gauge("factorio_fluid_production_input", "fluids produced", { "force", "name" })
prometheus.gauge("factorio_fluid_production_input", "fluids produced", { "force", "name", "surface" })
gauge_fluid_production_output =
prometheus.gauge("factorio_fluid_production_output", "fluids consumed", { "force", "name" })
prometheus.gauge("factorio_fluid_production_output", "fluids consumed", { "force", "name", "surface" })

gauge_kill_count_input = prometheus.gauge("factorio_kill_count_input", "kills", { "force", "name" })
gauge_kill_count_output = prometheus.gauge("factorio_kill_count_output", "losses", { "force", "name" })
gauge_kill_count_input = prometheus.gauge("factorio_kill_count_input", "kills", { "force", "name", "surface" })
gauge_kill_count_output = prometheus.gauge("factorio_kill_count_output", "losses", { "force", "name", "surface" })

gauge_entity_build_count_input =
prometheus.gauge("factorio_entity_build_count_input", "entities placed", { "force", "name" })
prometheus.gauge("factorio_entity_build_count_input", "entities placed", { "force", "name", "surface" })
gauge_entity_build_count_output =
prometheus.gauge("factorio_entity_build_count_output", "entities removed", { "force", "name" })
prometheus.gauge("factorio_entity_build_count_output", "entities removed", { "force", "name", "surface" })

gauge_pollution_production_input =
prometheus.gauge("factorio_pollution_production_input", "pollutions produced", { "force", "name" })
prometheus.gauge("factorio_pollution_production_input", "pollutions produced", { "name", "surface" })
gauge_pollution_production_output =
prometheus.gauge("factorio_pollution_production_output", "pollutions consumed", { "force", "name" })
prometheus.gauge("factorio_pollution_production_output", "pollutions consumed", { "name", "surface" })

gauge_evolution = prometheus.gauge("factorio_evolution", "evolution", { "force", "type" })
gauge_evolution = prometheus.gauge("factorio_evolution", "evolution", { "force", "type", "surface" })

gauge_research_queue = prometheus.gauge("factorio_research_queue", "research", { "force", "name", "level", "index" })

gauge_items_launched =
prometheus.gauge("factorio_items_launched_total", "items launched in rockets", { "force", "name" })
prometheus.gauge("factorio_items_launched_total", "items launched in rockets", { "force", "name", "quality" })

gauge_yarm_site_amount =
prometheus.gauge("factorio_yarm_site_amount", "YARM - site amount remaining", { "force", "name", "type" })
Expand Down Expand Up @@ -88,35 +89,47 @@ histogram_train_arrival_time =
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" }
{ "force", "surface", "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" }
{ "force", "surface", "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" }
{ "force", "surface", "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" }
{ "force", "surface", "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" }
{ "force", "surface", "network" }
)

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

gauge_circuit_network_signal = prometheus.gauge(
"factorio_circuit_network_signal",
"the value of a signal in a circuit network",
{ "force", "surface", "network", "name", "quality" }
)

gauge_circuit_network_monitored = prometheus.gauge(
"factorio_circuit_network_monitored",
"whether a circuit network with given ID is being monitored",
{ "force", "surface", "network" }
)

gauge_power_production_input =
Expand All @@ -125,12 +138,13 @@ gauge_power_production_output =
prometheus.gauge("factorio_power_production_output", "power consumed", { "force", "name", "network", "surface" })

script.on_init(function()
if game.active_mods["YARM"] then
global.yarm_on_site_update_event_id = remote.call("YARM", "get_on_site_updated_event_id")
script.on_event(global.yarm_on_site_update_event_id, handleYARM)
if script.active_mods["YARM"] then
storage.yarm_on_site_update_event_id = remote.call("YARM", "get_on_site_updated_event_id")
script.on_event(storage.yarm_on_site_update_event_id, handleYARM)
end

on_power_init()
on_circuit_network_init()

script.on_nth_tick(nth_tick, register_events)

Expand All @@ -156,16 +170,28 @@ script.on_init(function()

-- research events
script.on_event(defines.events.on_research_finished, on_research_finished)

-- circuit-network
script.on_event(defines.events.on_built_entity, on_circuit_network_build)
script.on_event(defines.events.on_robot_built_entity, on_circuit_network_build)
script.on_event(defines.events.on_space_platform_built_entity, on_circuit_network_build)
script.on_event(defines.events.script_raised_built, on_circuit_network_build)
script.on_event(defines.events.on_player_mined_entity, on_circuit_network_destroy)
script.on_event(defines.events.on_robot_mined_entity, on_circuit_network_destroy)
script.on_event(defines.events.on_space_platform_mined_entity, on_circuit_network_destroy)
script.on_event(defines.events.on_entity_died, on_circuit_network_destroy)
script.on_event(defines.events.script_raised_destroy, on_circuit_network_destroy)
end)

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

on_power_load()
on_circuit_network_load()

script.on_nth_tick(nth_tick, register_events)

Expand All @@ -191,11 +217,20 @@ script.on_load(function()

-- research events
script.on_event(defines.events.on_research_finished, on_research_finished)

-- circuit-network
script.on_event(defines.events.on_built_entity, on_circuit_network_build)
script.on_event(defines.events.on_robot_built_entity, on_circuit_network_build)
script.on_event(defines.events.script_raised_built, on_circuit_network_build)
script.on_event(defines.events.on_player_mined_entity, on_circuit_network_destroy)
script.on_event(defines.events.on_robot_mined_entity, on_circuit_network_destroy)
script.on_event(defines.events.on_entity_died, on_circuit_network_destroy)
script.on_event(defines.events.script_raised_destroy, on_circuit_network_destroy)
end)

script.on_configuration_changed(function(event)
if game.active_mods["YARM"] then
global.yarm_on_site_update_event_id = remote.call("YARM", "get_on_site_updated_event_id")
script.on_event(global.yarm_on_site_update_event_id, handleYARM)
if script.active_mods["YARM"] then
storage.yarm_on_site_update_event_id = remote.call("YARM", "get_on_site_updated_event_id")
script.on_event(storage.yarm_on_site_update_event_id, handleYARM)
end
end)
Loading