Make metrics reflect autoscaling policies (Refs #137)
CI / checks (push) Successful in 4m49s
CI / checks (push) Successful in 4m49s
This commit is contained in:
@@ -34,3 +34,59 @@ it('runs reports off the request thread and invalidates cached history after wri
|
||||
expect(changed).toEqual(readMetrics(query, now));
|
||||
expect(changed.summary.playerHours).toBeCloseTo((await first).summary.playerHours * 3);
|
||||
});
|
||||
|
||||
it('refreshes policy signals after policy edits and global pauses without new samples', async () => {
|
||||
const { db } = await import('../../src/lib/server/db/index.js');
|
||||
const { recordMetrics } = await import('../../src/lib/server/fleet/metrics.js');
|
||||
const { metricsReport } = await import('../../src/lib/server/reports/index.js');
|
||||
const now = Date.UTC(2026, 9, 4, 12);
|
||||
vi.spyOn(Date, 'now').mockReturnValue(now);
|
||||
recordMetrics(
|
||||
[{ server: 'one', game: 't5', map: 'Moon', players: 1, capacity: null }],
|
||||
now - 60_000
|
||||
);
|
||||
const query = parseMetricsQuery(new URLSearchParams('days=1'));
|
||||
expect((await metricsReport(query)).maps[0].policy).toBeNull();
|
||||
const settings = {
|
||||
game: 't5',
|
||||
mapSlug: '*',
|
||||
mode: 'create-destroy',
|
||||
minimum: 1,
|
||||
maximum: 1,
|
||||
spares: 1,
|
||||
emptyMinutes: 15
|
||||
};
|
||||
db()
|
||||
.prepare(
|
||||
`INSERT INTO autoscaling_policies
|
||||
(id,game,map_slug,settings,mode,owns_map,updated_at) VALUES(?,?,?,?,?,1,?)`
|
||||
)
|
||||
.run(
|
||||
'moon-policy',
|
||||
't5',
|
||||
'*',
|
||||
JSON.stringify(settings),
|
||||
settings.mode,
|
||||
new Date(now).toISOString()
|
||||
);
|
||||
const atMaximum = (await metricsReport(query)).maps[0];
|
||||
expect(atMaximum.policy?.maximum).toBe(1);
|
||||
expect(atMaximum.noEmptyAtMaximum).toBeCloseTo(1 / 60);
|
||||
db()
|
||||
.prepare('UPDATE autoscaling_policies SET settings=? WHERE id=?')
|
||||
.run(JSON.stringify({ ...settings, maximum: 3 }), 'moon-policy');
|
||||
const belowMaximum = (await metricsReport(query)).maps[0];
|
||||
expect(belowMaximum.policy?.maximum).toBe(3);
|
||||
expect(belowMaximum.noEmptyAtMaximum).toBe(0);
|
||||
expect(belowMaximum.noEmptyBelowMaximum).toBeCloseTo(1 / 60);
|
||||
db()
|
||||
.prepare(
|
||||
"INSERT INTO settings(key,value) VALUES('autoscaling_paused','1') ON CONFLICT(key) DO UPDATE SET value='1'"
|
||||
)
|
||||
.run();
|
||||
expect((await metricsReport(query)).maps[0].policy?.pausedReason).toBeTruthy();
|
||||
db().prepare("UPDATE settings SET value='0' WHERE key='autoscaling_paused'").run();
|
||||
expect((await metricsReport(query)).maps[0].policy?.pausedReason).toBeNull();
|
||||
db().prepare("DELETE FROM autoscaling_policies WHERE id='moon-policy'").run();
|
||||
expect((await metricsReport(query)).maps[0].policy).toBeNull();
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user