Finish basic setup for configuration loading
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import {
|
||||
DEFAULT_STANDARDS_CONFIG,
|
||||
FALLBACK_STANDARDS_CONFIG,
|
||||
StandardsDataSchema,
|
||||
StandardsParamsSchema,
|
||||
} from "@blade-and-brawn/calculator";
|
||||
@@ -19,9 +19,9 @@ async function seedStandardsDataset(): Promise<string> {
|
||||
return existing.id;
|
||||
}
|
||||
|
||||
Value.Assert(StandardsDataSchema, DEFAULT_STANDARDS_CONFIG.data);
|
||||
Value.Assert(StandardsDataSchema, FALLBACK_STANDARDS_CONFIG.data);
|
||||
const result = await db.insertInto("standards_datasets")
|
||||
.values({ name: DEFAULT_NAME, data: DEFAULT_STANDARDS_CONFIG.data })
|
||||
.values({ name: DEFAULT_NAME, data: FALLBACK_STANDARDS_CONFIG.data })
|
||||
.returning("id")
|
||||
.executeTakeFirstOrThrow();
|
||||
log.info({ id: result.id }, "seeded default standards dataset");
|
||||
@@ -43,12 +43,12 @@ async function seedStandardsConfig(datasetId: string): Promise<void> {
|
||||
.where("is_selected", "=", true)
|
||||
.executeTakeFirst();
|
||||
|
||||
Value.Assert(StandardsParamsSchema, DEFAULT_STANDARDS_CONFIG.params);
|
||||
Value.Assert(StandardsParamsSchema, FALLBACK_STANDARDS_CONFIG.params);
|
||||
const result = await db.insertInto("standards_configs")
|
||||
.values({
|
||||
name: DEFAULT_NAME,
|
||||
dataset_id: datasetId,
|
||||
params: DEFAULT_STANDARDS_CONFIG.params,
|
||||
params: FALLBACK_STANDARDS_CONFIG.params,
|
||||
is_selected: !hasSelected,
|
||||
})
|
||||
.returning("id")
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import {
|
||||
type Levels,
|
||||
DEFAULT_STANDARDS_CONFIG,
|
||||
FALLBACK_STANDARDS_CONFIG,
|
||||
} from "@blade-and-brawn/calculator";
|
||||
import { Activity, clamp, Gender, range } from "@blade-and-brawn/domain";
|
||||
import { levenbergMarquardt as LM } from "ml-levenberg-marquardt";
|
||||
@@ -15,7 +15,7 @@ for (const activity of Object.values(Activity)) {
|
||||
}
|
||||
|
||||
for (const gender of Object.values(Gender)) {
|
||||
const standardsByGender = DEFAULT_STANDARDS_CONFIG.data[
|
||||
const standardsByGender = FALLBACK_STANDARDS_CONFIG.data[
|
||||
activity
|
||||
].standards.filter((s) => s["metrics"].gender === gender);
|
||||
const ages = [...new Set(standardsByGender.map((s) => s.metrics.age))];
|
||||
|
||||
@@ -104,7 +104,7 @@ export const app = new Elysia()
|
||||
(app) => app
|
||||
// Non-authenticated
|
||||
.post("/calculate", async ({ body }) => {
|
||||
return { levels: s.Calculator.calculate(body.player, body.activityPerformances) };
|
||||
return { levels: await s.Calculator.calculate(body.player, body.activityPerformances) };
|
||||
}, {
|
||||
body: t.Object({
|
||||
player: PlayerSchema,
|
||||
|
||||
@@ -1,14 +1,25 @@
|
||||
import { LevelCalculator, Standards, DEFAULT_STANDARDS_CONFIG, type StandardsParams, StandardsParamsSchema, StandardsDataSchema, type StandardsConfig } from "@blade-and-brawn/calculator";
|
||||
import { LevelCalculator, Standards, FALLBACK_STANDARDS_CONFIG, type StandardsParams, StandardsParamsSchema, StandardsDataSchema, type StandardsConfig } from "@blade-and-brawn/calculator";
|
||||
import type { ActivityPerformance, Player } from "@blade-and-brawn/domain";
|
||||
import { db } from "../database/db";
|
||||
import { Value } from "@sinclair/typebox/value";
|
||||
import { log } from "../util";
|
||||
|
||||
export class CalculatorService {
|
||||
private Calculator = new LevelCalculator(new Standards(DEFAULT_STANDARDS_CONFIG));
|
||||
private Calculator = new LevelCalculator(new Standards(FALLBACK_STANDARDS_CONFIG));
|
||||
Standards: StandardsService = new StandardsService(this.Calculator);
|
||||
private ready: Promise<void>;
|
||||
|
||||
calculate(player: Player, activityPerformances: ActivityPerformance[]) {
|
||||
constructor() {
|
||||
// Load the selected config on startup instead of waiting for the
|
||||
// first `calculate()` call. If it fails (e.g. no config selected
|
||||
// yet, DB unreachable at boot), fall back to DEFAULT_STANDARDS_CONFIG
|
||||
// and keep serving — it'll retry on the next refresh.
|
||||
this.ready = this.Standards.Config.refresh()
|
||||
.catch((err) => log.error({ err }, "failed to load initial standards config, falling back to defaults"));
|
||||
}
|
||||
|
||||
async calculate(player: Player, activityPerformances: ActivityPerformance[]) {
|
||||
await this.ready;
|
||||
this.Standards.Config.refresh({ onlyIfStale: true }).catch((err) => log.error({ err }, "failed to refresh standards config"));
|
||||
return this.Calculator.calculate(player, activityPerformances);
|
||||
}
|
||||
@@ -92,7 +103,7 @@ class StandardsConfigService {
|
||||
.set({ is_selected: true })
|
||||
.where("id", "=", id)
|
||||
.execute();
|
||||
if (result[0]?.numUpdatedRows === 0n) throw new Error(`No config with id "${id}"`);
|
||||
if (result[0]?.numUpdatedRows === 0n) throw new Error(`No standards config with id "${id}"`);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user