Files
wekan/server/statistics.js
T
Lauri Ojansivu dcc6c8fe5d Version pane: five tables with a heading each, and what WeKan is installed as.
Admin Panel / Settings / Version was ONE table of 38 rows. The WeKan version,
the OS load average, the DDP transport and a V8 heap counter were the same kind
of thing in it: one flat list, read top to bottom, with no way to jump to the
part you came for.

Five tables now, each under a small heading, in the order somebody debugging
reads them - Platform (what this IS), OS (what it runs on), Meteor (how it talks
to the client: reactivity, reactivity order, DDP transport), Database (type,
versions, commits, storage engine, OpLog, sessions) and Node (version, heap,
memory). Reactivity and DDP moved to Meteor, where they belong: they are how the
client is fed, and they were the two rows most often read as database settings.
Whether an OpLog EXISTS stays with the database, because that is a property of
it. No row was dropped; a test lists all 38 by name and requires each to still
be shown.

NEW ROW: what WeKan is installed as - bundle.zip, Snap, Docker or Sandstorm. It
is the first thing a support answer turns on, and the pane never said it: the
same version keeps its data somewhere else, carries a different database and
gives the admin different reach in each of the four. models/lib/platformPackaging.js
detects it, most-specific-first: an explicit WEKAN_PACKAGING wins (a packaging
that knows what it is should say so, and it is how a future .deb or chart names
itself), then Sandstorm, then snapd's own SNAP/SNAP_NAME, then a container
runtime's marker file, then the bundle. Sandstorm and Snap are asked BEFORE the
container markers on purpose: a grain is a container, and a confined snap can
look like one, so the other order answers "Docker" for both. It claims nothing
it cannot know - there is no "source checkout" answer, because a `meteor run`
and an unpacked bundle are identical from inside the process.

The detection is a pure function taking the environment, the Sandstorm flag and
a file-exists callback, so all nine of its tests run here without a snap, a
container or a grain - including that an empty SNAP is not a snap, that an empty
WEKAN_PACKAGING falls through rather than blanking the field, and that an
unreadable root answers bundle.zip instead of throwing an admin's Version page
away.

The headings are SMALLER than the pane title above them, and a test compares the
two font sizes rather than trusting the CSS to stay that way: five headings at
1.25rem would read as five pane titles and "Version" would be lost among them,
and an unstyled h3 - the browser default here - is larger still. Six new English
strings; the other languages fall back to English until translated, as usual.

tests/clientBundleImports.test.cjs caught the new stylesheet before it could
matter: package.json sets meteor.mainModule, so a .css nobody imports is simply
not in the bundle. It is imported from client/features/settings.js now.

Thanks to xet7 !
2026-08-10 03:40:12 +03:00

218 lines
9.5 KiB
JavaScript

import { ReactiveCache } from '/imports/reactiveCache';
import { Meteor } from 'meteor/meteor';
import { MongoInternals } from 'meteor/mongo';
import fs from 'fs';
import { detectPackaging } from '/models/lib/platformPackaging';
// Sandstorm context is detected using the METEOR_SETTINGS environment variable
// in the package definition.
const isSandstorm =
Meteor.settings && Meteor.settings.public && Meteor.settings.public.sandstorm;
Meteor.methods({
async getStatistics() {
const currentUser = await ReactiveCache.getCurrentUser();
if (currentUser?.isAdmin) {
const os = require('os');
const pjson = require('/package.json');
const statistics = {};
let wekanVersion = pjson.version;
wekanVersion = wekanVersion.replace('v', '');
statistics.version = wekanVersion;
// HOW this WeKan was installed - bundle.zip, Snap, Docker or Sandstorm.
// The same version behaves differently in each (where its data lives, what
// database it carries, what the admin can reach), so the Version pane says
// which one rather than leaving it to be asked on every issue. The
// detection is a pure function so it can be tested without a snap, a
// container or a grain; see models/lib/platformPackaging.js.
statistics.platform = {
packaging: detectPackaging({
env: process.env,
isSandstorm,
fileExists: p => fs.existsSync(p),
}),
};
statistics.os = {
type: os.type(),
platform: os.platform(),
arch: os.arch(),
release: os.release(),
uptime: os.uptime(),
loadavg: os.loadavg(),
totalmem: os.totalmem(),
freemem: os.freemem(),
cpus: os.cpus(),
};
let nodeVersion = process.version;
nodeVersion = nodeVersion.replace('v', '');
statistics.process = {
nodeVersion,
pid: process.pid,
uptime: process.uptime(),
};
if (!isSandstorm) {
const v8 = require('v8');
statistics.nodeHeapStats = {
totalHeapSize: v8.getHeapStatistics().total_heap_size,
totalHeapSizeExecutable: v8.getHeapStatistics().total_heap_size_executable,
totalPhysicalSize: v8.getHeapStatistics().total_physical_size,
totalAvailableSize: v8.getHeapStatistics().total_available_size,
usedHeapSize: v8.getHeapStatistics().used_heap_size,
heapSizeLimit: v8.getHeapStatistics().heap_size_limit,
mallocedMemory: v8.getHeapStatistics().malloced_memory,
peakMallocedMemory: v8.getHeapStatistics().peak_malloced_memory,
doesZapGarbage: v8.getHeapStatistics().does_zap_garbage,
numberOfNativeContexts: v8.getHeapStatistics().number_of_native_contexts,
numberOfDetachedContexts: v8.getHeapStatistics().number_of_detached_contexts,
};
let memoryUsage = process.memoryUsage();
statistics.nodeMemoryUsage = {
rss: memoryUsage.rss,
heapTotal: memoryUsage.heapTotal,
heapUsed: memoryUsage.heapUsed,
external: memoryUsage.external,
};
}
let meteorVersion = Meteor.release;
meteorVersion = meteorVersion.replace('METEOR@', '');
statistics.meteor = {
meteorVersion,
};
// Database facts shown in Admin Panel / Version:
// - databaseType: MongoDB or FerretDB (WeKan can run on either; FerretDB
// v1 speaks the MongoDB wire protocol, so most of this looks the same).
// - mongoVersion: the MongoDB-compatible (wire) version. FerretDB reports
// one too, so WeKan/Meteor treat it like MongoDB.
// - databaseCommit: the server's git commit (buildInfo.gitVersion).
// - ferretdbVersion / ferretdbCommit: FerretDB's own version + commit,
// only present when the server is FerretDB.
// - mongoStorageEngine: WiredTiger for MongoDB; SQLite (or PostgreSQL) for
// FerretDB.
// - reactivity: how live updates reach clients right now — 'oplog' (Meteor
// oplog tailing), 'changeStreams', or 'polling' (poll-and-diff, the
// fallback FerretDB uses since it has no replica-set oplog).
let databaseType = 'MongoDB';
let mongoVersion = 'unknown';
let databaseCommit = 'unknown';
let ferretdbVersion;
let ferretdbCommit;
let mongoStorageEngine = 'unknown';
let mongoOplogEnabled = false;
let reactivity = 'polling';
try {
const { mongo } = MongoInternals.defaultRemoteCollectionDriver();
// Reactivity driver: Meteor uses oplog tailing when an oplog handle is
// active, otherwise poll-and-diff. A change-streams driver (if ever used)
// is detected by its handle name.
const oplogHandle = mongo._oplogHandle;
mongoOplogEnabled = Boolean(oplogHandle && oplogHandle.onOplogEntry);
if (mongoOplogEnabled) {
const handleName = oplogHandle?.constructor?.name || '';
reactivity = /changestream/i.test(handleName) ? 'changeStreams' : 'oplog';
} else {
reactivity = 'polling';
}
// buildInfo carries the version + git commit, and — when the server is
// FerretDB rather than MongoDB — a `ferretdb` sub-document with FerretDB's
// own version/commit.
const buildInfo = await mongo.db.command({ buildInfo: 1 });
mongoVersion = buildInfo.version || 'unknown';
databaseCommit = buildInfo.gitVersion || 'unknown';
// The wekan/FerretDB v1 fork reports a TOP-LEVEL `ferretdbVersion` string
// (e.g. "v1.24.2-60-gb5523566") plus `ferretdbFeatures`, and puts its own
// git commit in `gitVersion`. Upstream/other builds may instead use a
// `ferretdb` sub-document ({version, commit}). Detect either.
if (buildInfo.ferretdb || buildInfo.ferretdbVersion || buildInfo.ferretdbFeatures) {
databaseType = 'FerretDB';
ferretdbVersion =
buildInfo.ferretdbVersion ||
(buildInfo.ferretdb && buildInfo.ferretdb.version) ||
'unknown';
ferretdbCommit =
(buildInfo.ferretdb &&
(buildInfo.ferretdb.commit || buildInfo.ferretdb.gitVersion)) ||
buildInfo.gitVersion ||
'unknown';
}
// Storage engine: MongoDB reports it in serverStatus. That command needs
// the cluster-level `clusterMonitor` role, which a per-database WeKan user
// (readWrite on the wekan db only, as in the FOSS/Docker/Meteor3 docs)
// does NOT have — so it is rejected and the engine stays 'unknown'.
try {
const serverStatus = await mongo.db.command({ serverStatus: 1 });
if (serverStatus.storageEngine && serverStatus.storageEngine.name) {
mongoStorageEngine = serverStatus.storageEngine.name;
}
} catch (e) {
// Not authorized (per-db credentials) or FerretDB has no serverStatus.
}
// Fallback that works with plain readWrite credentials: $collStats returns
// storageStats with a `wiredTiger` (or `inMemory`) sub-document naming the
// engine backing the collection, and requires only the collStats action
// that the read/readWrite roles already grant. Used when serverStatus was
// denied, so per-database MongoDB users still see the real engine instead
// of 'unknown'.
if (mongoStorageEngine === 'unknown' && databaseType === 'MongoDB') {
try {
const cols = await mongo.db
.listCollections({}, { nameOnly: true })
.toArray();
const target = cols
.map(c => c.name)
.find(n => n && !n.startsWith('system.'));
if (target) {
const stats = await mongo.db
.collection(target)
.aggregate([{ $collStats: { storageStats: {} } }])
.toArray();
const storageStats = stats[0] && stats[0].storageStats;
if (storageStats) {
if (storageStats.wiredTiger) mongoStorageEngine = 'wiredTiger';
else if (storageStats.inMemory) mongoStorageEngine = 'inMemory';
}
}
} catch (e) {
// collStats also unavailable — leave 'unknown'.
}
}
if (mongoStorageEngine === 'unknown' && databaseType === 'FerretDB') {
// WeKan bundles FerretDB with its embedded SQLite backend.
mongoStorageEngine = 'SQLite';
}
} catch (e) {
// Leave the defaults above (all 'unknown'/false/'polling').
}
statistics.mongo = {
databaseType,
mongoVersion,
databaseCommit,
ferretdbVersion,
ferretdbCommit,
mongoStorageEngine,
mongoOplogEnabled,
reactivity,
// Configured environment (what was REQUESTED) shown next to `reactivity`
// (what is actually LIVE): the driver priority order Meteor tries
// left-to-right (OpLog only when tailing works, else polling) and the DDP
// transport. Empty env shows as '(unset)'.
reactivityOrder:
process.env.METEOR_REACTIVITY_ORDER ||
process.env.DEFAULT_METEOR_REACTIVITY_ORDER ||
'(unset)',
ddpTransport: process.env.DDP_TRANSPORT || '(unset)',
};
const client = MongoInternals.defaultRemoteCollectionDriver()?.mongo?.client;
const sessionsCount = client?.s?.activeSessions?.size;
statistics.session = {
sessionsCount,
};
return statistics;
} else {
return false;
}
},
});