mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-22 00:13:12 +09:00
Provide production statistics for all surfaces
This commit is contained in:
1 parent
1090197acc
commit
7934e471a7
2 files changed
+78
-43
No files matched your search
@@ -6,14 +6,18 @@ const { Gauge } = lib;
|
|||||||
type IpcStats = {
|
type IpcStats = {
|
||||||
game_tick: number,
|
game_tick: number,
|
||||||
player_count: number,
|
player_count: number,
|
||||||
game_flow_statistics: {
|
surface_statistics: {
|
||||||
pollution_statistics: IpcFlowType,
|
[key: string]: {
|
||||||
},
|
game_flow_statistics: {
|
||||||
force_flow_statistics: {
|
pollution_statistics: IpcFlowType,
|
||||||
[key:string]: {
|
},
|
||||||
[key:string]: IpcFlowType,
|
force_flow_statistics: {
|
||||||
|
[key:string]: {
|
||||||
|
[key:string]: IpcFlowType,
|
||||||
|
}
|
||||||
|
},
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type IpcFlowType = {
|
type IpcFlowType = {
|
||||||
@@ -34,29 +38,44 @@ const instanceGameTicksTotal = new Gauge(
|
|||||||
const instanceForceFlowStatistics = new Gauge(
|
const instanceForceFlowStatistics = new Gauge(
|
||||||
"clusterio_statistics_exporter_instance_force_flow_statistics",
|
"clusterio_statistics_exporter_instance_force_flow_statistics",
|
||||||
"Items/fluids/enemies/buildings produced/built/killed by a force",
|
"Items/fluids/enemies/buildings produced/built/killed by a force",
|
||||||
{ labels: ["instance_id", "force", "statistic", "direction", "name"] },
|
{ labels: ["instance_id", "surface", "force", "statistic", "direction", "name"] },
|
||||||
);
|
);
|
||||||
const instanceGameFlowStatistics = new Gauge(
|
const instanceGameFlowStatistics = new Gauge(
|
||||||
"clusterio_statistics_exporter_instance_game_flow_statistics",
|
"clusterio_statistics_exporter_instance_game_flow_statistics",
|
||||||
"Pollution produced/consumed in the game",
|
"Pollution produced/consumed in the game",
|
||||||
{ labels: ["instance_id", "statistic", "direction", "name"] },
|
{ labels: ["instance_id", "surface", "statistic", "direction", "name"] },
|
||||||
);
|
);
|
||||||
|
|
||||||
function setForceFlowStatistic(
|
function setForceFlowStatistic(
|
||||||
instanceId: number,
|
instanceId: number,
|
||||||
|
surfaceName: string,
|
||||||
forceName: string,
|
forceName: string,
|
||||||
statisticName: string,
|
statisticName: string,
|
||||||
direction: string,
|
direction: string,
|
||||||
item: string,
|
item: string,
|
||||||
value: number,
|
value: number,
|
||||||
) {
|
) {
|
||||||
instanceForceFlowStatistics.labels(String(instanceId), forceName, statisticName, direction, item).set(value);
|
instanceForceFlowStatistics.labels(
|
||||||
|
String(instanceId),
|
||||||
|
surfaceName,
|
||||||
|
forceName,
|
||||||
|
statisticName,
|
||||||
|
direction,
|
||||||
|
item
|
||||||
|
).set(value);
|
||||||
|
|
||||||
// For item and fluid statistics it's useful to compare the input flow with the
|
// For item and fluid statistics it's useful to compare the input flow with the
|
||||||
// output flow, to simplify the comparison ensure both directions have a value.
|
// output flow, to simplify the comparison ensure both directions have a value.
|
||||||
if (["item_production_statistics", "fluid_production_statistic"].includes(statisticName)) {
|
if (["item_production_statistics", "fluid_production_statistic"].includes(statisticName)) {
|
||||||
let reverseDirection = direction === "input" ? "output" : "input";
|
let reverseDirection = direction === "input" ? "output" : "input";
|
||||||
instanceForceFlowStatistics.labels(String(instanceId), forceName, statisticName, reverseDirection, item);
|
instanceForceFlowStatistics.labels(
|
||||||
|
String(instanceId),
|
||||||
|
surfaceName,
|
||||||
|
forceName,
|
||||||
|
statisticName,
|
||||||
|
reverseDirection,
|
||||||
|
item
|
||||||
|
).set(value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -84,21 +103,32 @@ export class InstancePlugin extends BaseInstancePlugin {
|
|||||||
instanceGameTicksTotal.labels(String(instanceId)).set(stats.game_tick);
|
instanceGameTicksTotal.labels(String(instanceId)).set(stats.game_tick);
|
||||||
instancePlayerCount.labels(String(instanceId)).set(stats.player_count);
|
instancePlayerCount.labels(String(instanceId)).set(stats.player_count);
|
||||||
|
|
||||||
for (let [forceName, flowStatistics] of Object.entries(stats.force_flow_statistics)) {
|
for (let [surfaceName, surfaceStats] of Object.entries(stats.surface_statistics)) {
|
||||||
for (let [statisticName, statistic] of Object.entries(flowStatistics)) {
|
for (let [forceName, flowStatistics] of Object.entries(surfaceStats.force_flow_statistics)) {
|
||||||
for (let [direction, counts] of Object.entries(statistic)) {
|
for (let [statisticName, statistic] of Object.entries(flowStatistics)) {
|
||||||
for (let [item, value] of Object.entries(counts)) {
|
for (let [direction, counts] of Object.entries(statistic)) {
|
||||||
setForceFlowStatistic(instanceId, forceName, statisticName, direction, item, value);
|
// eslint-disable-next-line max-depth
|
||||||
|
for (let [item, value] of Object.entries(counts)) {
|
||||||
|
setForceFlowStatistic(
|
||||||
|
instanceId,
|
||||||
|
surfaceName,
|
||||||
|
forceName,
|
||||||
|
statisticName,
|
||||||
|
direction,
|
||||||
|
item,
|
||||||
|
value
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
for (let [direction, counts] of Object.entries(stats.game_flow_statistics.pollution_statistics)) {
|
for (let [direction, counts] of Object.entries(surfaceStats.game_flow_statistics.pollution_statistics)) {
|
||||||
for (let [item, value] of Object.entries(counts)) {
|
for (let [item, value] of Object.entries(counts)) {
|
||||||
instanceGameFlowStatistics.labels(
|
instanceGameFlowStatistics.labels(
|
||||||
String(instanceId), "pollution_statistics", direction, item
|
String(instanceId), surfaceName, "pollution_statistics", direction, item
|
||||||
).set(value);
|
).set(value);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,32 +11,37 @@ local statistics = {
|
|||||||
|
|
||||||
statistics_exporter = {}
|
statistics_exporter = {}
|
||||||
function statistics_exporter.export()
|
function statistics_exporter.export()
|
||||||
--- @diagnostic disable-next-line undefined-field
|
|
||||||
local pollution = v2_stats and game.get_pollution_statistics(1) or game.pollution_statistics
|
|
||||||
local stats = {
|
local stats = {
|
||||||
game_tick = game.tick,
|
game_tick = game.tick,
|
||||||
player_count = #game.connected_players,
|
player_count = #game.connected_players,
|
||||||
game_flow_statistics = {
|
surface_statistics = {}
|
||||||
pollution_statistics = {
|
|
||||||
--- TODO support multi surface pollution statistics
|
|
||||||
input = pollution.input_counts,
|
|
||||||
output = pollution.output_counts,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
force_flow_statistics = {}
|
|
||||||
}
|
}
|
||||||
for _, force in pairs(game.forces) do
|
|
||||||
local flow_statistics = {}
|
|
||||||
for _, statName in pairs(statistics) do
|
|
||||||
local stat = v2_stats and force["get_" .. statName](1) or force[statName]
|
|
||||||
flow_statistics[statName] = {
|
|
||||||
input = stat.input_counts,
|
|
||||||
output = stat.output_counts,
|
|
||||||
}
|
|
||||||
end
|
|
||||||
stats.force_flow_statistics[force.name] = flow_statistics
|
|
||||||
end
|
|
||||||
|
|
||||||
|
for _, surface in pairs(game.surfaces) do
|
||||||
|
local surface_stats = {
|
||||||
|
game_flow_statistics = {
|
||||||
|
pollution_statistics = {
|
||||||
|
input = (v2_stats and game.get_pollution_statistics(surface.index) or surface.pollution_statistics).input_counts,
|
||||||
|
output = (v2_stats and game.get_pollution_statistics(surface.index) or surface.pollution_statistics).output_counts,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
force_flow_statistics = {}
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, force in pairs(game.forces) do
|
||||||
|
local flow_statistics = {}
|
||||||
|
for _, statName in pairs(statistics) do
|
||||||
|
local stat = v2_stats and force["get_" .. statName](surface.index) or force[statName]
|
||||||
|
flow_statistics[statName] = {
|
||||||
|
input = stat.input_counts,
|
||||||
|
output = stat.output_counts,
|
||||||
|
}
|
||||||
|
end
|
||||||
|
surface_stats.force_flow_statistics[force.name] = flow_statistics
|
||||||
|
end
|
||||||
|
|
||||||
|
stats.surface_statistics[surface.name] = surface_stats
|
||||||
|
end
|
||||||
|
|
||||||
rcon.print(compat.table_to_json(stats))
|
rcon.print(compat.table_to_json(stats))
|
||||||
end
|
end
|
||||||
Reference in new issue
Block a user