Files
wekan/tests/diskHighTracker.test.cjs
T
Lauri Ojansivu b6a3d98736 Admin Panel / Problems: Memory Usage and Disk usage reports.
The RAM and Disk usage pages were designs that no code carried out. Both
monitors now run beside the CPU one and, like it, record only the start and
the end of a sustained high-usage period:

- Memory: RAM from /proc/meminfo's MemAvailable - os.freemem() is MemFree,
  which counts the page cache as used and shows a healthy server as full -
  and swap, which alone also starts a period. Without /proc/meminfo it uses
  what Node reports. Settings WEKAN_RAM_* and WEKAN_SWAP_HIGH_PERCENT.
- Disk: each filesystem WeKan writes to - the files root, its temp directory
  for uploads and the snap's data directory - measured once per device, with
  an episode of its own. WEKAN_DISK_WATCH_PATHS replaces the paths.
- Each report has a live line above its rows and a URL
  (/admin/problems/ram, /admin/problems/disk). The menu and titles use the
  existing memory-usage and sandstorm-disk-usage keys, which every language
  translates, and the Summary names the cpu, ram and disk streams.

Observe and report only, as designed. The three monitors share one event
recorder (server/lib/resourceLog.js) and one intEnv (models/lib/intEnv.js),
which three files had each defined. The pure halves are tested without
Meteor, and resourceMonitorsWiring checks the wiring and that no copy of
either shared piece returns.

Thanks to xet7 !
2026-10-17 01:14:16 +03:00

60 lines
2.8 KiB
JavaScript

'use strict';
// models/lib/diskHighTracker.js - the pure half of the disk-usage monitor
// (docs/Features/Admin-Panel/Problems/Disk-usage.md): statfs numbers to a used
// percentage, the watched paths, and one episode per filesystem.
//
// Run: node tests/diskHighTracker.test.cjs
const assert = require('assert');
const { statfsUsage, watchedPaths, DiskEpisodes } = require('../models/lib/diskHighTracker.js');
let passed = 0;
function test(name, fn) { fn(); passed += 1; console.log(' ok -', name); }
console.log('diskHighTracker:');
test('statfs gives the size, what may still be written and the used percentage', () => {
assert.deepStrictEqual(statfsUsage({ bsize: 4096, blocks: 1000, bavail: 100 }),
{ total: 4096000, available: 409600, usedPct: 90 });
});
test('missing or zero numbers give no figure, never NaN (negative)', () => {
assert.strictEqual(statfsUsage(null), null);
assert.strictEqual(statfsUsage({ bsize: 4096, blocks: 0, bavail: 0 }), null, 'zero-size filesystem');
assert.strictEqual(statfsUsage({ bsize: 4096, blocks: 1000 }), null, 'no free-space field');
assert.strictEqual(statfsUsage({ bsize: 0, blocks: 10, bavail: 1 }), null);
assert.strictEqual(statfsUsage({ bsize: 4096, blocks: 10, bavail: 20 }).usedPct, 0, 'more free than size is capped');
});
test('the default paths are the database, the attachments and the uploads, each once', () => {
assert.deepStrictEqual(watchedPaths({ dataPath: '/var/snap/wekan/common', filesRoot: '/var/snap/wekan/common/files/' }), [
{ path: '/var/snap/wekan/common', role: 'database' },
{ path: '/var/snap/wekan/common/files/', role: 'attachments' },
{ path: '/var/snap/wekan/common/files/temp', role: 'uploads' },
]);
assert.deepStrictEqual(watchedPaths({ filesRoot: '/data/files', dataPath: '' }).map(e => e.role), ['attachments', 'uploads']);
});
test('WEKAN_DISK_WATCH_PATHS replaces the defaults, without repeats', () => {
assert.deepStrictEqual(watchedPaths({ override: '/a: /b ::/a', dataPath: '/x', filesRoot: '/y' }),
[{ path: '/a', role: 'watched' }, { path: '/b', role: 'watched' }]);
assert.strictEqual(watchedPaths({ override: ' ', filesRoot: '/y' })[0].role, 'attachments',
'a blank override keeps the defaults');
});
test('each filesystem has its own episode', () => {
const e = new DiskEpisodes({ highPct: 90, lowPct: 85, enterSamples: 2, exitSamples: 2 });
e.update('sda', 95, 1);
assert.strictEqual(e.update('sda', 96, 2).event, 'start');
assert.strictEqual(e.update('sdb', 50, 2).event, null);
assert.ok(e.isHigh('sda') && !e.isHigh('sdb'), 'a full data disk does not mark the attachments disk');
e.update('sda', 80, 3);
const end = e.update('sda', 80, 4);
assert.strictEqual(end.event, 'end');
assert.strictEqual(end.peak, 96);
assert.strictEqual(e.isHigh('unknown'), false);
});
console.log(`\ndiskHighTracker: ${passed} tests passed`);