Restructuring default config for standards

This commit is contained in:
Dominic Ferrando
2026-07-04 17:41:08 -04:00
parent 376af8ea45
commit a888d24bfd
8 changed files with 59 additions and 48 deletions
+5 -6
View File
@@ -1,6 +1,5 @@
import {
DEFAULT_STANDARDS_DATA,
DEFAULT_STANDARDS_PARAMS,
DEFAULT_STANDARDS_CONFIG,
StandardsDataSchema,
StandardsParamsSchema,
} from "@blade-and-brawn/calculator";
@@ -20,9 +19,9 @@ async function seedStandardsDataset(): Promise<string> {
return existing.id;
}
Value.Assert(StandardsDataSchema, DEFAULT_STANDARDS_DATA);
Value.Assert(StandardsDataSchema, DEFAULT_STANDARDS_CONFIG.data);
const result = await db.insertInto("standards_datasets")
.values({ name: DEFAULT_NAME, data: DEFAULT_STANDARDS_DATA })
.values({ name: DEFAULT_NAME, data: DEFAULT_STANDARDS_CONFIG.data })
.returning("id")
.executeTakeFirstOrThrow();
log.info({ id: result.id }, "seeded default standards dataset");
@@ -44,12 +43,12 @@ async function seedStandardsConfig(datasetId: string): Promise<void> {
.where("is_selected", "=", true)
.executeTakeFirst();
Value.Assert(StandardsParamsSchema, DEFAULT_STANDARDS_PARAMS);
Value.Assert(StandardsParamsSchema, DEFAULT_STANDARDS_CONFIG.params);
const result = await db.insertInto("standards_configs")
.values({
name: DEFAULT_NAME,
dataset_id: datasetId,
params: DEFAULT_STANDARDS_PARAMS,
params: DEFAULT_STANDARDS_CONFIG.params,
is_selected: !hasSelected,
})
.returning("id")
+2 -2
View File
@@ -1,6 +1,6 @@
import {
type Levels,
DEFAULT_STANDARDS_DATA,
DEFAULT_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_DATA[
const standardsByGender = DEFAULT_STANDARDS_CONFIG.data[
activity
].standards.filter((s) => s["metrics"].gender === gender);
const ages = [...new Set(standardsByGender.map((s) => s.metrics.age))];
+15 -12
View File
@@ -1,11 +1,11 @@
import { LevelCalculator, Standards, DEFAULT_STANDARDS_DATA, type StandardsParams, StandardsParamsSchema, StandardsDataSchema } from "@blade-and-brawn/calculator";
import { LevelCalculator, Standards, DEFAULT_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_DATA));
private Calculator = new LevelCalculator(new Standards(DEFAULT_STANDARDS_CONFIG));
Standards: StandardsService = new StandardsService(this.Calculator);
calculate(player: Player, activityPerformances: ActivityPerformance[]) {
@@ -37,11 +37,11 @@ class StandardsConfigService {
}
/** Retrieves the currently selected configuration */
async get(opt: { skipCache?: boolean } = {}): Promise<StandardsParams> {
if (!opt.skipCache) return this.Calculator.Standards.params;
async get(opt: { skipCache?: boolean } = {}): Promise<StandardsConfig> {
if (!opt.skipCache) return this.Calculator.Standards.cfg;
await this.refresh();
return this.Calculator.Standards.params;
return this.Calculator.Standards.cfg;
}
async update(id: string, datasetId: string, params: StandardsParams) {
@@ -58,7 +58,7 @@ class StandardsConfigService {
this.lastRefreshedAt = Date.now();
const config = await db.selectFrom("standards_configs")
const result = await db.selectFrom("standards_configs")
.innerJoin("standards_datasets", "standards_datasets.id", "standards_configs.dataset_id")
.select([
"standards_configs.dataset_id as datasetId",
@@ -68,15 +68,18 @@ class StandardsConfigService {
.where("standards_configs.is_selected", "=", true)
.limit(1)
.executeTakeFirstOrThrow({ errorConstructor: () => new Error("No standards config selected") });
Value.Assert(StandardsParamsSchema, config.params);
Value.Assert(StandardsDataSchema, config.data);
Value.Assert(StandardsParamsSchema, result.params);
Value.Assert(StandardsDataSchema, result.data);
const parametersStringified = JSON.stringify(config.params);
if (parametersStringified === this.cachedParametersStringified && config.datasetId === this.cachedDatasetId) return;
const parametersStringified = JSON.stringify(result.params);
if (parametersStringified === this.cachedParametersStringified && result.datasetId === this.cachedDatasetId) return;
this.Calculator.Standards = new Standards(config.data, config.params);
this.Calculator.Standards = new Standards({
data: result.data,
params: result.params
});
this.cachedParametersStringified = parametersStringified;
this.cachedDatasetId = config.datasetId;
this.cachedDatasetId = result.datasetId;
}
async switch(id: string) {