mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-16 15:30:41 +09:00
Add startedAtMs to all processes
This commit is contained in:
12 files changed
+118
-9
No files matched your search
@@ -96,6 +96,7 @@ export default class Controller {
|
||||
|
||||
devMiddleware: any | null = null;
|
||||
|
||||
startedAtMs = Date.now();
|
||||
autosaveInterval?: ReturnType<typeof setInterval>;
|
||||
systemMetricsInterval?: ReturnType<typeof setInterval>;
|
||||
|
||||
@@ -397,6 +398,7 @@ export default class Controller {
|
||||
this.onSystemMetricsIntervalChanged();
|
||||
|
||||
logger.info("Started controller");
|
||||
this.startedAtMs = Date.now();
|
||||
this._state = "running";
|
||||
}
|
||||
|
||||
@@ -553,7 +555,7 @@ export default class Controller {
|
||||
requests.push(hostConnection.send(new lib.SystemInfoRequest()));
|
||||
}
|
||||
const restartRequired = await this.checkRestartRequired();
|
||||
requests.push(lib.gatherSystemInfo("controller", this.canRestart, restartRequired));
|
||||
requests.push(lib.gatherSystemInfo("controller", this.canRestart, restartRequired, this.startedAtMs));
|
||||
const newMetrics = await Promise.all(requests);
|
||||
for (const metric of newMetrics) {
|
||||
this.systems.set(metric);
|
||||
@@ -663,6 +665,12 @@ export default class Controller {
|
||||
if (!json.restartRequired) { // Added in 2.0.0.alpha.21
|
||||
json.restartRequired = false;
|
||||
}
|
||||
if (!json.systemStartedAtMs) { // Added in 2.0.0.alpha.23
|
||||
json.systemStartedAtMs = Date.now();
|
||||
}
|
||||
if (!json.processStartedAtMs) { // Added in 2.0.0.alpha.23
|
||||
json.processStartedAtMs = Date.now();
|
||||
}
|
||||
return json;
|
||||
});
|
||||
}
|
||||
|
||||
@@ -207,6 +207,11 @@ export default class HostConnection extends BaseConnection {
|
||||
|
||||
let prev = instance.status;
|
||||
instance.status = request.status;
|
||||
// If the instance has transitioned to running then updated the started at time
|
||||
if (request.status === "running" && prev === "starting") {
|
||||
instance.startedAtMs = Date.now();
|
||||
}
|
||||
|
||||
instance.gamePort = request.gamePort;
|
||||
instance.factorioVersion = request.factorioVersion;
|
||||
this._controller.instances.set(instance);
|
||||
@@ -246,6 +251,9 @@ export default class HostConnection extends BaseConnection {
|
||||
controllerInstance.status = instanceData.status;
|
||||
controllerInstance.gamePort = instanceData.gamePort;
|
||||
controllerInstance.factorioVersion = instanceData.factorioVersion;
|
||||
if (instanceData.status === "running" && prev === "starting") {
|
||||
controllerInstance.startedAtMs = Date.now();
|
||||
}
|
||||
instanceUpdates.push(controllerInstance);
|
||||
logger.verbose(`Instance ${instanceConfig.get("instance.name")} State: ${instanceData.status}`);
|
||||
await lib.invokeHook(
|
||||
@@ -261,6 +269,7 @@ export default class HostConnection extends BaseConnection {
|
||||
instanceData.status,
|
||||
instanceData.gamePort,
|
||||
instanceData.factorioVersion,
|
||||
0,
|
||||
Date.now(),
|
||||
);
|
||||
instanceUpdates.push(newInstance);
|
||||
|
||||
@@ -12,6 +12,7 @@ export default class InstanceInfo {
|
||||
public status: lib.InstanceStatus,
|
||||
public gamePort?: number,
|
||||
public factorioVersion?: lib.TargetVersion,
|
||||
public startedAtMs = 0,
|
||||
public updatedAtMs = 0,
|
||||
) {
|
||||
this.config = config;
|
||||
@@ -22,6 +23,7 @@ export default class InstanceInfo {
|
||||
"config": lib.InstanceConfig.jsonSchema,
|
||||
"status": lib.InstanceStatus,
|
||||
"gamePort": Type.Optional(Type.Number()),
|
||||
"startedAtMs": Type.Optional(Type.Number()),
|
||||
"updatedAtMs": Type.Optional(Type.Number()),
|
||||
"factorioVersion": Type.Optional(lib.TargetVersionSchema),
|
||||
});
|
||||
@@ -32,6 +34,7 @@ export default class InstanceInfo {
|
||||
json.status,
|
||||
json.gamePort,
|
||||
json.factorioVersion,
|
||||
json.startedAtMs,
|
||||
json.updatedAtMs,
|
||||
);
|
||||
}
|
||||
@@ -44,6 +47,7 @@ export default class InstanceInfo {
|
||||
this.gamePort,
|
||||
this.status,
|
||||
this.factorioVersion,
|
||||
this.startedAtMs,
|
||||
this.updatedAtMs,
|
||||
this.config.get("instance.exclude_from_start_all"),
|
||||
);
|
||||
|
||||
@@ -291,6 +291,7 @@ async function startHost() {
|
||||
});
|
||||
|
||||
await hostConnector.connect();
|
||||
host.startedAtMs = Date.now();
|
||||
logger.info("Started host");
|
||||
}
|
||||
|
||||
|
||||
@@ -278,6 +278,8 @@ export default class Host extends lib.Link {
|
||||
_disconnecting = false;
|
||||
_shuttingDown = false;
|
||||
|
||||
startedAtMs = Date.now();
|
||||
|
||||
static instanceConfigWarning = {
|
||||
"_warning": "Changes to this file will be overwritten by the controller's copy.",
|
||||
};
|
||||
@@ -807,7 +809,7 @@ export default class Host extends lib.Link {
|
||||
|
||||
async handleSystemInfoRequest() {
|
||||
const restartRequired = await this.checkRestartRequired();
|
||||
return lib.gatherSystemInfo(this.config.get("host.id"), this.canRestart, restartRequired);
|
||||
return lib.gatherSystemInfo(this.config.get("host.id"), this.canRestart, restartRequired, this.startedAtMs);
|
||||
}
|
||||
|
||||
async checkRestartRequired() {
|
||||
|
||||
@@ -260,6 +260,8 @@ export class SystemInfo {
|
||||
public diskAvailable: number,
|
||||
public canRestart: boolean,
|
||||
public restartRequired: boolean,
|
||||
public systemStartedAtMs: number,
|
||||
public processStartedAtMs: number,
|
||||
/** Millisecond Unix timestamp this entry was last updated at */
|
||||
public updatedAtMs: number,
|
||||
public isDeleted: boolean,
|
||||
@@ -279,6 +281,8 @@ export class SystemInfo {
|
||||
"diskAvailable": Type.Number(),
|
||||
"canRestart": Type.Boolean(),
|
||||
"restartRequired": Type.Boolean(),
|
||||
"systemStartedAtMs": Type.Number(),
|
||||
"processStartedAtMs": Type.Number(),
|
||||
"updatedAtMs": Type.Number(),
|
||||
"isDeleted": Type.Boolean(),
|
||||
});
|
||||
@@ -298,6 +302,8 @@ export class SystemInfo {
|
||||
json.diskAvailable,
|
||||
json.canRestart,
|
||||
json.restartRequired,
|
||||
json.systemStartedAtMs,
|
||||
json.processStartedAtMs,
|
||||
json.updatedAtMs,
|
||||
json.isDeleted,
|
||||
);
|
||||
|
||||
@@ -44,6 +44,7 @@ export class InstanceDetails {
|
||||
public gamePort: number | undefined,
|
||||
public status: InstanceStatus,
|
||||
public factorioVersion: TargetVersion | undefined,
|
||||
public startedAtMs = 0,
|
||||
/** Millisecond Unix timestamp this entry was last updated at */
|
||||
public updatedAtMs = 0,
|
||||
public excludeFromStartAll = false,
|
||||
@@ -59,6 +60,7 @@ export class InstanceDetails {
|
||||
"creating_save", "exporting_data", "deleted",
|
||||
]),
|
||||
"factorioVersion": Type.Optional(TargetVersionSchema),
|
||||
"startedAtMs": Type.Optional(Type.Number()),
|
||||
"updatedAtMs": Type.Optional(Type.Number()),
|
||||
"excludeFromStartAll": Type.Optional(Type.Boolean()),
|
||||
});
|
||||
@@ -71,6 +73,7 @@ export class InstanceDetails {
|
||||
json.gamePort,
|
||||
json.status,
|
||||
json.factorioVersion,
|
||||
json.startedAtMs,
|
||||
json.updatedAtMs,
|
||||
json.excludeFromStartAll,
|
||||
);
|
||||
|
||||
@@ -134,7 +134,12 @@ function minZip<T>(a: T[], b: T[]): [T, T][] {
|
||||
|
||||
let previousTotalCpuMs: number[] = [];
|
||||
let previousIdleCpuMs: number[] = [];
|
||||
export async function gatherSystemInfo(id: SystemInfo["id"], canRestart: boolean, restartRequired: boolean) {
|
||||
export async function gatherSystemInfo(
|
||||
id: SystemInfo["id"],
|
||||
canRestart: boolean,
|
||||
restartRequired: boolean,
|
||||
startedAtMs: number
|
||||
) {
|
||||
const cpus = os.cpus();
|
||||
const currentTotalCpuMs = cpus.map(({ times }) => times.user + times.idle + times.irq + times.nice + times.sys);
|
||||
const currentIdleCpuMs = cpus.map(({ times }) => times.idle);
|
||||
@@ -152,6 +157,7 @@ export async function gatherSystemInfo(id: SystemInfo["id"], canRestart: boolean
|
||||
diskAvailable = stats.bavail * stats.bsize;
|
||||
}
|
||||
|
||||
const now = Date.now();
|
||||
return new SystemInfo(
|
||||
id,
|
||||
os.hostname(),
|
||||
@@ -166,7 +172,9 @@ export async function gatherSystemInfo(id: SystemInfo["id"], canRestart: boolean
|
||||
diskAvailable,
|
||||
canRestart,
|
||||
restartRequired,
|
||||
Date.now(),
|
||||
now - (os.uptime() * 1000),
|
||||
startedAtMs,
|
||||
now,
|
||||
false,
|
||||
);
|
||||
}
|
||||
@@ -8,7 +8,7 @@ import PluginExtra from "./PluginExtra";
|
||||
import LogConsole, { SelectMaxLogLevel } from "./LogConsole";
|
||||
import {
|
||||
MetricCpuRatio, MetricCpuUsed, MetricMemoryRatio, MetricMemoryUsed,
|
||||
MetricDiskUsed, MetricDiskRatio,
|
||||
MetricDiskUsed, MetricDiskRatio, MetricRelativeDate,
|
||||
} from "./system_metrics";
|
||||
import { useAccount } from "../model/account";
|
||||
import { useSystems } from "../model/system";
|
||||
@@ -101,6 +101,12 @@ export default function ControllerPage() {
|
||||
<Descriptions.Item label="Node.js">{system?.node}</Descriptions.Item>
|
||||
<Descriptions.Item label="OS Kernel">{system?.kernel}</Descriptions.Item>
|
||||
<Descriptions.Item label="Machine">{system?.machine}</Descriptions.Item>
|
||||
<Descriptions.Item label="System Uptime">
|
||||
<MetricRelativeDate timeMs={system?.systemStartedAtMs}/>
|
||||
</Descriptions.Item>
|
||||
<Descriptions.Item label="Process Uptime">
|
||||
<MetricRelativeDate timeMs={system?.processStartedAtMs}/>
|
||||
</Descriptions.Item>
|
||||
<Descriptions.Item label="Hostname" span={2}>{system?.hostname}</Descriptions.Item>
|
||||
<Descriptions.Item label="CPU Model" span={2}>{system?.cpuModel}</Descriptions.Item>
|
||||
<Descriptions.Item label="CPU Usage"><MetricCpuRatio system={system} /></Descriptions.Item>
|
||||
|
||||
@@ -17,7 +17,7 @@ import PageLayout from "./PageLayout";
|
||||
import PluginExtra from "./PluginExtra";
|
||||
import {
|
||||
MetricCpuRatio, MetricCpuUsed, MetricMemoryRatio, MetricMemoryUsed,
|
||||
MetricDiskUsed, MetricDiskRatio,
|
||||
MetricDiskUsed, MetricDiskRatio, MetricRelativeDate,
|
||||
} from "./system_metrics";
|
||||
import { formatTimestamp } from "../util/time_format";
|
||||
import { useSystems } from "../model/system";
|
||||
@@ -147,8 +147,14 @@ export default function HostViewPage() {
|
||||
<Descriptions.Item label="Node.js">{system?.node}</Descriptions.Item>
|
||||
<Descriptions.Item label="OS Kernel">{system?.kernel}</Descriptions.Item>
|
||||
<Descriptions.Item label="Machine">{system?.machine}</Descriptions.Item>
|
||||
<Descriptions.Item label="Hostname" span={2}>{system?.hostname}</Descriptions.Item>
|
||||
<Descriptions.Item label="Connected From" span={2}>{host.remoteAddress}</Descriptions.Item>
|
||||
<Descriptions.Item label="System Uptime">
|
||||
<MetricRelativeDate timeMs={system?.systemStartedAtMs}/>
|
||||
</Descriptions.Item>
|
||||
<Descriptions.Item label="Process Uptime">
|
||||
<MetricRelativeDate timeMs={system?.processStartedAtMs}/>
|
||||
</Descriptions.Item>
|
||||
<Descriptions.Item label="Hostname">{system?.hostname}</Descriptions.Item>
|
||||
<Descriptions.Item label="Connected From">{host.remoteAddress}</Descriptions.Item>
|
||||
<Descriptions.Item label="CPU Model" span={2}>{system?.cpuModel}</Descriptions.Item>
|
||||
<Descriptions.Item label="CPU Usage"><MetricCpuRatio system={system} /></Descriptions.Item>
|
||||
<Descriptions.Item label="Cores"><MetricCpuUsed system={system} /></Descriptions.Item>
|
||||
|
||||
@@ -25,6 +25,7 @@ import { useHost } from "../model/host";
|
||||
import InstanceStatusTag from "./InstanceStatusTag";
|
||||
import Link from "./Link";
|
||||
import { instancePublicAddress } from "../util/instance";
|
||||
import { MetricRelativeDate } from "./system_metrics";
|
||||
|
||||
type MenuItem = Required<MenuProps>["items"][number];
|
||||
const { Title } = Typography;
|
||||
@@ -41,6 +42,7 @@ function InstanceDescription(props: InstanceDescriptionProps) {
|
||||
return <Descriptions
|
||||
bordered
|
||||
size="small"
|
||||
column={{ xs: 1, md: 2, lg: 2, xl: 2, xxl: 2 }}
|
||||
>
|
||||
<Descriptions.Item label="Host">
|
||||
{!assigned
|
||||
@@ -64,7 +66,12 @@ function InstanceDescription(props: InstanceDescriptionProps) {
|
||||
<Descriptions.Item label="Version">
|
||||
{instance.factorioVersion ?? "unknown"}
|
||||
</Descriptions.Item>
|
||||
<Descriptions.Item label="Status"><InstanceStatusTag status={instance.status!} /></Descriptions.Item>
|
||||
<Descriptions.Item label="Status">
|
||||
<InstanceStatusTag status={instance.status!} />
|
||||
</Descriptions.Item>
|
||||
<Descriptions.Item label="Start Time">
|
||||
<MetricRelativeDate timeMs={instance.startedAtMs}/>
|
||||
</Descriptions.Item>
|
||||
</Descriptions>;
|
||||
}
|
||||
|
||||
|
||||
@@ -63,3 +63,52 @@ export function RestartRequired(props: { system?: lib.SystemInfo }) {
|
||||
<ReloadOutlined style={{ color: "yellow" }}/>
|
||||
</Tooltip>;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a human-readable duration between two dates.
|
||||
*
|
||||
* The output includes up to two most significant time units and omits
|
||||
* leading zero units. Supported units are weeks, days, hours, minutes,
|
||||
* and seconds.
|
||||
*
|
||||
* Examples: "1w 2d", "2d 3h", "1h 2m", "45s"
|
||||
*
|
||||
* @param date1 - The starting date.
|
||||
* @param date2 - The ending date.
|
||||
* @returns A duration string (e.g. "2d 3h").
|
||||
*/
|
||||
function humanAbsTimeDiff(date1: number, date2 = 0) {
|
||||
let diffMs = Math.abs(date2 - date1);
|
||||
const totalSeconds = Math.floor(diffMs / 1000);
|
||||
|
||||
const weeks = Math.floor(totalSeconds / 604800); // 7 * 24 * 60 * 60
|
||||
const days = Math.floor((totalSeconds % 604800) / 86400);
|
||||
const hours = Math.floor((totalSeconds % 86400) / 3600);
|
||||
const minutes = Math.floor((totalSeconds % 3600) / 60);
|
||||
const seconds = totalSeconds % 60;
|
||||
|
||||
if (weeks > 0) {
|
||||
return days > 0 ? `${weeks}w ${days}d` : `${weeks}w`;
|
||||
}
|
||||
|
||||
if (days > 0) {
|
||||
return hours > 0 ? `${days}d ${hours}h` : `${days}d`;
|
||||
}
|
||||
|
||||
if (hours > 0) {
|
||||
return minutes > 0 ? `${hours}h ${minutes}m` : `${hours}h`;
|
||||
}
|
||||
|
||||
if (minutes > 0) {
|
||||
return seconds > 0 ? `${minutes}m ${seconds}s` : `${minutes}m`;
|
||||
}
|
||||
|
||||
return `${seconds}s`;
|
||||
}
|
||||
|
||||
export function MetricRelativeDate(props: { timeMs?: number }) {
|
||||
if (!props.timeMs || props.timeMs === 0) {
|
||||
return "N/A";
|
||||
}
|
||||
return `${new Date(props.timeMs).toLocaleString()} (${humanAbsTimeDiff(props.timeMs, Date.now())})`;
|
||||
}
|
||||
Reference in new issue
Block a user