From fd741f9b491e7fe0a966ef379d67a46a4e9ad53e Mon Sep 17 00:00:00 2001 From: Danielv123 Date: Mon, 2 Dec 2024 21:05:24 +0000 Subject: [PATCH] Add clusterio_statistics_exporter_platform_mapping This allows queries to map between the platform surfaces tracked in the statistics and the name assigned by the player --- plugins/statistics_exporter/instance.ts | 25 +++++++++++++++++++ plugins/statistics_exporter/module/export.lua | 14 ++++++++++- 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/plugins/statistics_exporter/instance.ts b/plugins/statistics_exporter/instance.ts index e09e131c..d371f735 100644 --- a/plugins/statistics_exporter/instance.ts +++ b/plugins/statistics_exporter/instance.ts @@ -17,6 +17,12 @@ type IpcStats = { } }, } + }, + platforms: { + [key: string]: { + force: string, + surface: string, + } } } @@ -45,6 +51,11 @@ const instanceGameFlowStatistics = new Gauge( "Pollution produced/consumed in the game", { labels: ["instance_id", "surface", "statistic", "direction", "name"] }, ); +const instancePlatformMapping = new Gauge( + "clusterio_statistics_exporter_platform_mapping", + "Mapping of platform name to force, surface and instance", + { labels: ["instance_id", "platform_name", "force", "surface"] } +); function setForceFlowStatistic( instanceId: number, @@ -103,6 +114,19 @@ export class InstancePlugin extends BaseInstancePlugin { instanceGameTicksTotal.labels(String(instanceId)).set(stats.game_tick); instancePlayerCount.labels(String(instanceId)).set(stats.player_count); + // Remove existing platform mappings for this instance + instancePlatformMapping.removeAll({ instance_id: String(instanceId) }); + + // Set new platform mappings + for (let [platformName, platform] of Object.entries(stats.platforms)) { + instancePlatformMapping.labels( + String(instanceId), + platformName, + platform.force, + platform.surface + ).set(1); + } + for (let [surfaceName, surfaceStats] of Object.entries(stats.surface_statistics)) { for (let [forceName, flowStatistics] of Object.entries(surfaceStats.force_flow_statistics)) { for (let [statisticName, statistic] of Object.entries(flowStatistics)) { @@ -151,3 +175,4 @@ export const _instancePlayerCount = instancePlayerCount; export const _instanceGameTicksTotal = instanceGameTicksTotal; export const _instanceForceFlowStatistics = instanceForceFlowStatistics; export const _instanceGameFlowStatistics = instanceGameFlowStatistics; +export const _instancePlatformMapping = instancePlatformMapping; diff --git a/plugins/statistics_exporter/module/export.lua b/plugins/statistics_exporter/module/export.lua index f98c7f21..fe9d4c16 100644 --- a/plugins/statistics_exporter/module/export.lua +++ b/plugins/statistics_exporter/module/export.lua @@ -14,7 +14,8 @@ function statistics_exporter.export() local stats = { game_tick = game.tick, player_count = #game.connected_players, - surface_statistics = {} + surface_statistics = {}, + platforms = {} } for _, surface in pairs(game.surfaces) do @@ -43,5 +44,16 @@ function statistics_exporter.export() stats.surface_statistics[surface.name] = surface_stats end + if v2_stats then + for _, force in pairs(game.forces) do + for _, platform in pairs(force.platforms) do + stats.platforms[platform.name] = { + force = force.name, + surface = platform.surface.name + } + end + end + end + rcon.print(compat.table_to_json(stats)) end