mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-21 16:13:12 +09:00
Port remaining plugins to Typescript (#499)
* .js to .ts * Allow access to _items entries; getItemCount return number * add permission field to PluginPage interface * port plugins to typescript * update Request classes * fix IpcFlowType
This commit is contained in:
1 parent
50f12c719b
commit
0b3550b472
51 files changed
+1033
-1026
No files matched your search
@@ -1,5 +1,4 @@
|
||||
"use strict";
|
||||
const lib = require("@clusterio/lib");
|
||||
import * as lib from "@clusterio/lib";
|
||||
|
||||
class InstanceConfigGroup extends lib.PluginConfigGroup {}
|
||||
InstanceConfigGroup.defaultAccess = ["controller", "host", "control"];
|
||||
@@ -13,12 +12,14 @@ InstanceConfigGroup.define({
|
||||
});
|
||||
InstanceConfigGroup.finalize();
|
||||
|
||||
module.exports.default = {
|
||||
const info: lib.PluginInfo = {
|
||||
name: "statistics_exporter",
|
||||
title: "Statistics Exporter",
|
||||
description:
|
||||
"Provides in-game item/fluid production, builds, kills, and pollution "+
|
||||
"statistics to the cluster's Prometheus endpoint.",
|
||||
instanceEntrypoint: "instance",
|
||||
instanceEntrypoint: "dist/plugin/instance",
|
||||
InstanceConfigGroup,
|
||||
};
|
||||
|
||||
export default info;
|
||||
@@ -1,7 +1,23 @@
|
||||
"use strict";
|
||||
const lib = require("@clusterio/lib");
|
||||
import * as lib from "@clusterio/lib";
|
||||
const { Gauge } = lib;
|
||||
|
||||
type IpcStats = {
|
||||
game_tick: number,
|
||||
player_count: number,
|
||||
game_flow_statistics: {
|
||||
pollution_statistics: IpcFlowType,
|
||||
},
|
||||
force_flow_statistics: {
|
||||
[key:string]: {
|
||||
[key:string]: IpcFlowType,
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
type IpcFlowType = {
|
||||
"input": Record<string, number>,
|
||||
"output": Record<string, number>,
|
||||
}
|
||||
|
||||
const instancePlayerCount = new Gauge(
|
||||
"clusterio_statistics_exporter_instance_player_count",
|
||||
@@ -24,7 +40,14 @@ const instanceGameFlowStatistics = new Gauge(
|
||||
{ labels: ["instance_id", "statistic", "direction", "name"] },
|
||||
);
|
||||
|
||||
function setForceFlowStatistic(instanceId, forceName, statisticName, direction, item, value) {
|
||||
function setForceFlowStatistic(
|
||||
instanceId: number,
|
||||
forceName: string,
|
||||
statisticName: string,
|
||||
direction: string,
|
||||
item: string,
|
||||
value: number,
|
||||
) {
|
||||
instanceForceFlowStatistics.labels(String(instanceId), forceName, statisticName, direction, item).set(value);
|
||||
|
||||
// For item and fluid statistics it's useful to compare the input flow with the
|
||||
@@ -36,7 +59,7 @@ function setForceFlowStatistic(instanceId, forceName, statisticName, direction,
|
||||
}
|
||||
|
||||
|
||||
class InstancePlugin extends lib.BaseInstancePlugin {
|
||||
export class InstancePlugin extends lib.BaseInstancePlugin {
|
||||
async init() {
|
||||
if (!this.instance.config.get("factorio.enable_save_patching")) {
|
||||
throw new Error("statistics_exporter plugin requires save patching.");
|
||||
@@ -45,10 +68,10 @@ class InstancePlugin extends lib.BaseInstancePlugin {
|
||||
|
||||
async gatherMetrics() {
|
||||
let string = await this.sendRcon("/sc statistics_exporter.export()");
|
||||
let stats;
|
||||
let stats: IpcStats;
|
||||
try {
|
||||
stats = JSON.parse(string);
|
||||
} catch (err) {
|
||||
} catch (err: any) {
|
||||
throw new Error(`Error parsing statistics JSON: ${err.message}, content "${string}"`);
|
||||
}
|
||||
|
||||
@@ -85,16 +108,11 @@ class InstancePlugin extends lib.BaseInstancePlugin {
|
||||
// stream is overloaded. Should the timeout be exceeded the
|
||||
// previous values for the metrics will end up being sent to controller.
|
||||
let timeout = this.instance.config.get("statistics_exporter.command_timeout") * 1000;
|
||||
await lib.timeout(this.gatherMetrics(), timeout);
|
||||
await lib.timeout(this.gatherMetrics(), timeout, undefined);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
InstancePlugin,
|
||||
|
||||
// For testing only
|
||||
_instancePlayerCount: instancePlayerCount,
|
||||
_instanceGameTicksTotal: instanceGameTicksTotal,
|
||||
_instanceForceFlowStatistics: instanceForceFlowStatistics,
|
||||
_instanceGameFlowStatistics: instanceGameFlowStatistics,
|
||||
};
|
||||
export const _instancePlayerCount = instancePlayerCount;
|
||||
export const _instanceGameTicksTotal = instanceGameTicksTotal;
|
||||
export const _instanceForceFlowStatistics = instanceForceFlowStatistics;
|
||||
export const _instanceGameFlowStatistics = instanceGameFlowStatistics;
|
||||
@@ -6,9 +6,9 @@
|
||||
"homepage": "https://github.com/clusterio/clusterio#readme",
|
||||
"license": "MIT",
|
||||
"repository": "clusterio/clusterio",
|
||||
"main": "./info.js",
|
||||
"main": "dist/plugin/info.js",
|
||||
"scripts": {
|
||||
"prepare": "webpack-cli --env production",
|
||||
"prepare": "tsc --build && webpack-cli --env production",
|
||||
"test": "echo \"Error: run tests from root\" && exit 1"
|
||||
},
|
||||
"bugs": {
|
||||
|
||||
@@ -2,8 +2,8 @@
|
||||
const assert = require("assert").strict;
|
||||
|
||||
const mock = require("../../../test/mock");
|
||||
const instance = require("../instance");
|
||||
const info = require("../info").default;
|
||||
const instance = require("../dist/plugin/instance");
|
||||
const info = require("../dist/plugin/info").default;
|
||||
|
||||
|
||||
describe("statistics_exporter plugin", function() {
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
{
|
||||
"include": ["./**/*.ts"],
|
||||
"exclude": ["test/*", "./dist/*"],
|
||||
"compilerOptions": {
|
||||
"lib": ["es2023"],
|
||||
"module": "commonjs",
|
||||
"target": "es2022",
|
||||
"rootDir": ".",
|
||||
"outDir": "dist/plugin",
|
||||
"sourceMap": true,
|
||||
"strict": true,
|
||||
"alwaysStrict": true,
|
||||
"esModuleInterop": true,
|
||||
"skipLibCheck": true
|
||||
}
|
||||
}
|
||||
@@ -16,7 +16,7 @@ module.exports = (env = {}) => merge(common(env), {
|
||||
name: "statistics_exporter",
|
||||
library: { type: "var", name: "plugin_statistics_exporter" },
|
||||
exposes: {
|
||||
"./info": "./info.js",
|
||||
"./info": "./dist/plugin/info.js",
|
||||
"./package.json": "./package.json",
|
||||
},
|
||||
shared: {
|
||||
|
||||
Reference in new issue
Block a user