Remove standards config fallback and implement caching
This commit is contained in:
@@ -31,6 +31,7 @@ report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json
|
||||
|
||||
# IntelliJ based IDEs
|
||||
.idea
|
||||
.zed
|
||||
|
||||
# Finder (MacOS) folder config
|
||||
.DS_Store
|
||||
|
||||
@@ -1,8 +0,0 @@
|
||||
// Folder-specific settings
|
||||
//
|
||||
// For a full list of overridable settings, and general information on folder-specific settings,
|
||||
// see the documentation: https://zed.dev/docs/configuring-zed#settings-files
|
||||
{
|
||||
"tab_size": 4,
|
||||
"hard_tabs": false
|
||||
}
|
||||
@@ -1,10 +1,12 @@
|
||||
import {
|
||||
type Levels,
|
||||
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";
|
||||
import { CalculatorService } from "../services/calculator";
|
||||
|
||||
const calculator = new CalculatorService();
|
||||
const standardsConfig = await calculator.Standards.Config.get();
|
||||
for (const activity of Object.values(Activity)) {
|
||||
const DATASET_MAX_LEVEL = 5;
|
||||
const NEW_LEVEL_COUNT = 2;
|
||||
@@ -15,7 +17,7 @@ for (const activity of Object.values(Activity)) {
|
||||
}
|
||||
|
||||
for (const gender of Object.values(Gender)) {
|
||||
const standardsByGender = FALLBACK_STANDARDS_CONFIG.data[
|
||||
const standardsByGender = standardsConfig.data[
|
||||
activity
|
||||
].standards.filter((s) => s["metrics"].gender === gender);
|
||||
const ages = [...new Set(standardsByGender.map((s) => s.metrics.age))];
|
||||
|
||||
@@ -17,7 +17,7 @@ import jwt from "@elysia/jwt";
|
||||
import { CommerceService } from "./services/commerce";
|
||||
import cluster from "node:cluster";
|
||||
import { randomUUIDv7 } from "bun";
|
||||
import { CalculatorService } from "./services/calculator";
|
||||
import { CalculatorService, CalculatorUnavailableError } from "./services/calculator";
|
||||
import { StandardsParamsSchema } from "@blade-and-brawn/calculator";
|
||||
|
||||
// SERVICES
|
||||
@@ -47,6 +47,7 @@ export const app = new Elysia()
|
||||
.error({
|
||||
PrintfulError,
|
||||
WebflowError,
|
||||
CalculatorUnavailableError,
|
||||
})
|
||||
|
||||
.onError(({ code, error }) => {
|
||||
@@ -54,14 +55,17 @@ export const app = new Elysia()
|
||||
case "PrintfulError":
|
||||
case "WebflowError":
|
||||
log.error(
|
||||
{ upstreamStatus: error.status, payload: error.payload },
|
||||
{ upstreamStatus: error.upstreamStatus, payload: error.payload },
|
||||
error.message,
|
||||
);
|
||||
return status(502, { error: error.message });
|
||||
return status(error.status, { error: error.message });
|
||||
case "CalculatorUnavailableError":
|
||||
log.error({ err: error.cause }, error.message);
|
||||
return status(error.status, { error: error.message });
|
||||
case "NOT_FOUND":
|
||||
return status(404, { error: "Not found" });
|
||||
return status(error.status, { error: error.message });
|
||||
case "VALIDATION":
|
||||
return status(400, { error: error.message });
|
||||
return status(error.status, { error: error.message });
|
||||
default:
|
||||
log.error({ err: error }, "unhandled error");
|
||||
}
|
||||
|
||||
@@ -1,45 +1,69 @@
|
||||
import { LevelCalculator, Standards, FALLBACK_STANDARDS_CONFIG, type StandardsParams, StandardsParamsSchema, StandardsDataSchema, type StandardsConfig } from "@blade-and-brawn/calculator";
|
||||
import { LevelCalculator, Standards, type StandardsParams, StandardsParamsSchema, StandardsDataSchema, type StandardsConfig, type LevelCalculatorOutput } 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 CalculatorUnavailableError extends Error {
|
||||
status: number = 503
|
||||
}
|
||||
|
||||
export class CalculatorService {
|
||||
private Calculator = new LevelCalculator(new Standards(FALLBACK_STANDARDS_CONFIG));
|
||||
Standards: StandardsService = new StandardsService(this.Calculator);
|
||||
private ready: Promise<void>;
|
||||
private calculator: LevelCalculator | null = null;
|
||||
private appliedConfigStringified: string | null = null;
|
||||
Standards: StandardsService = new StandardsService();
|
||||
|
||||
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"));
|
||||
// eagerly load
|
||||
this.refresh().catch((err) => log.error({ err }, "failed to load initial standards config"));
|
||||
}
|
||||
|
||||
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);
|
||||
async calculate(player: Player, activityPerformances: ActivityPerformance[]): Promise<LevelCalculatorOutput> {
|
||||
await this.refresh();
|
||||
if (!this.calculator) throw new CalculatorUnavailableError("Level calculator is not ready");
|
||||
return this.calculator.calculate(player, activityPerformances);
|
||||
}
|
||||
|
||||
private async refresh(): Promise<void> {
|
||||
const standardsConfig = await this.Standards.Config.get();
|
||||
const standardsConfigStringified = JSON.stringify(standardsConfig);
|
||||
|
||||
if (this.calculator) {
|
||||
// avoid instatiating a new Standards object if nothing changed
|
||||
if (standardsConfigStringified === this.appliedConfigStringified) return;
|
||||
|
||||
this.calculator.Standards = new Standards(standardsConfig);
|
||||
}
|
||||
else {
|
||||
this.calculator = new LevelCalculator(new Standards(standardsConfig));
|
||||
}
|
||||
this.appliedConfigStringified = standardsConfigStringified;
|
||||
}
|
||||
}
|
||||
|
||||
class StandardsService {
|
||||
Config: StandardsConfigService;
|
||||
|
||||
constructor(private Calculator: LevelCalculator) {
|
||||
this.Config = new StandardsConfigService(this.Calculator);
|
||||
constructor() {
|
||||
this.Config = new StandardsConfigService();
|
||||
}
|
||||
}
|
||||
|
||||
class StandardsConfigService {
|
||||
private static readonly REFRESH_INTERVAL_MS = 30_000;
|
||||
private cachedParametersStringified: string | null = null;
|
||||
private cachedDatasetId: string | null = null;
|
||||
private lastRefreshedAt = 0;
|
||||
|
||||
constructor(private Calculator: LevelCalculator) { }
|
||||
private cache = {
|
||||
refreshInterval: 30_000,
|
||||
lastRefreshedAt: 0,
|
||||
isStale: function () {
|
||||
return Date.now() - this.lastRefreshedAt >= this.refreshInterval;
|
||||
},
|
||||
isIncomplete: function () {
|
||||
return !this.data.datasetId || !this.data.config
|
||||
},
|
||||
data: {
|
||||
datasetId: null as string | null,
|
||||
config: null as StandardsConfig | null
|
||||
},
|
||||
}
|
||||
|
||||
async create(datasetId: string, params: StandardsParams) {
|
||||
await db.insertInto("standards_configs")
|
||||
@@ -48,11 +72,10 @@ class StandardsConfigService {
|
||||
}
|
||||
|
||||
/** Retrieves the currently selected configuration */
|
||||
async get(opt: { skipCache?: boolean } = {}): Promise<StandardsConfig> {
|
||||
if (!opt.skipCache) return this.Calculator.Standards.cfg;
|
||||
|
||||
await this.refresh();
|
||||
return this.Calculator.Standards.cfg;
|
||||
async get(): Promise<StandardsConfig> {
|
||||
await this.refresh({ onlyIfStale: true });
|
||||
if (!this.cache.data.config) throw new CalculatorUnavailableError("Unable to determine the selected standards configuration");
|
||||
return this.cache.data.config;
|
||||
}
|
||||
|
||||
async update(id: string, datasetId: string, params: StandardsParams) {
|
||||
@@ -63,12 +86,10 @@ class StandardsConfigService {
|
||||
if (result[0]?.numUpdatedRows === 0n) throw new Error(`No config with id "${id}"`);
|
||||
}
|
||||
|
||||
async refresh(opt: { onlyIfStale?: boolean } = {}) {
|
||||
if (opt.onlyIfStale)
|
||||
if (Date.now() - this.lastRefreshedAt < StandardsConfigService.REFRESH_INTERVAL_MS) return;
|
||||
|
||||
this.lastRefreshedAt = Date.now();
|
||||
async refresh(opt: { onlyIfStale?: boolean } = {}): Promise<void> {
|
||||
if (!this.cache.isIncomplete() && (opt.onlyIfStale && !this.cache.isStale())) return;
|
||||
|
||||
try {
|
||||
const result = await db.selectFrom("standards_configs")
|
||||
.innerJoin("standards_datasets", "standards_datasets.id", "standards_configs.dataset_id")
|
||||
.select([
|
||||
@@ -82,15 +103,17 @@ class StandardsConfigService {
|
||||
Value.Assert(StandardsParamsSchema, result.params);
|
||||
Value.Assert(StandardsDataSchema, result.data);
|
||||
|
||||
const parametersStringified = JSON.stringify(result.params);
|
||||
if (parametersStringified === this.cachedParametersStringified && result.datasetId === this.cachedDatasetId) return;
|
||||
|
||||
this.Calculator.Standards = new Standards({
|
||||
this.cache.data.datasetId = result.datasetId;
|
||||
this.cache.data.config = {
|
||||
data: result.data,
|
||||
params: result.params
|
||||
});
|
||||
this.cachedParametersStringified = parametersStringified;
|
||||
this.cachedDatasetId = result.datasetId;
|
||||
};
|
||||
// only mark as refreshed once we've actually confirmed the selected
|
||||
// config; a failed attempt must not make stale data look fresh
|
||||
this.cache.lastRefreshedAt = Date.now();
|
||||
} catch (err) {
|
||||
throw new CalculatorUnavailableError("Unable to determine the selected standards configuration", { cause: err });
|
||||
}
|
||||
}
|
||||
|
||||
async switch(id: string) {
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,79 +0,0 @@
|
||||
{
|
||||
"global": {
|
||||
"maxLevel": 100
|
||||
},
|
||||
"activity": {
|
||||
"BackSquat": {
|
||||
"enableGeneration": true,
|
||||
"weightModifier": 0,
|
||||
"weightSkew": 0,
|
||||
"ageModifier": 0,
|
||||
"difficultyModifier": 0.3,
|
||||
"peakAge": 0,
|
||||
"stretch": {
|
||||
"upper": 0,
|
||||
"lower": 1
|
||||
}
|
||||
},
|
||||
"BenchPress": {
|
||||
"enableGeneration": true,
|
||||
"weightModifier": 0,
|
||||
"weightSkew": 0,
|
||||
"ageModifier": 0,
|
||||
"difficultyModifier": 0.05,
|
||||
"peakAge": 0,
|
||||
"stretch": {
|
||||
"upper": 1,
|
||||
"lower": 0
|
||||
}
|
||||
},
|
||||
"Deadlift": {
|
||||
"enableGeneration": true,
|
||||
"weightModifier": 0,
|
||||
"weightSkew": 0,
|
||||
"ageModifier": 0,
|
||||
"difficultyModifier": -0.05,
|
||||
"peakAge": 0,
|
||||
"stretch": {
|
||||
"upper": 1,
|
||||
"lower": 0
|
||||
}
|
||||
},
|
||||
"Run": {
|
||||
"enableGeneration": true,
|
||||
"weightModifier": 0.1,
|
||||
"weightSkew": 0,
|
||||
"ageModifier": 0,
|
||||
"difficultyModifier": 0.35,
|
||||
"peakAge": 0,
|
||||
"stretch": {
|
||||
"upper": 1,
|
||||
"lower": 1
|
||||
}
|
||||
},
|
||||
"BroadJump": {
|
||||
"enableGeneration": true,
|
||||
"weightModifier": -0.1,
|
||||
"weightSkew": 0,
|
||||
"ageModifier": -0.25,
|
||||
"difficultyModifier": -0.15,
|
||||
"peakAge": 23,
|
||||
"stretch": {
|
||||
"upper": 3,
|
||||
"lower": 1
|
||||
}
|
||||
},
|
||||
"ConeDrill": {
|
||||
"enableGeneration": true,
|
||||
"weightModifier": -0.1,
|
||||
"weightSkew": 0,
|
||||
"ageModifier": -0.25,
|
||||
"difficultyModifier": 0.15,
|
||||
"peakAge": 23,
|
||||
"stretch": {
|
||||
"upper": 0,
|
||||
"lower": 2
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -10,11 +10,8 @@ import {
|
||||
type Player,
|
||||
} from "@blade-and-brawn/domain";
|
||||
import { levenbergMarquardt as LM } from "ml-levenberg-marquardt";
|
||||
import defaultStandardsParamsJson from "./data/standards/fallback-params.json" with { type: "json" };
|
||||
import defaultStandardsDataJson from "./data/standards/fallback-data.json" with { type: "json" };
|
||||
import { getAvgWeight } from "./avg-weights";
|
||||
import { StandardsDataSchema, StandardsParamsSchema, type LevelCalculatorOutput, type Levels, type NumberMetric, type Standard, type StandardsConfig, type StandardsData } from "./models";
|
||||
import { Value } from "@sinclair/typebox/value";
|
||||
import { type LevelCalculatorOutput, type Levels, type NumberMetric, type Standard, type StandardsConfig, type StandardsData } from "./models";
|
||||
|
||||
// SOURCES
|
||||
// Squat, Bench, Dead Lift:
|
||||
@@ -34,13 +31,6 @@ const metricPriority = (m: NumberMetric) => {
|
||||
return 2;
|
||||
};
|
||||
|
||||
Value.Assert(StandardsDataSchema, defaultStandardsDataJson);
|
||||
Value.Assert(StandardsParamsSchema, defaultStandardsParamsJson);
|
||||
export const FALLBACK_STANDARDS_CONFIG: StandardsConfig = {
|
||||
data: defaultStandardsDataJson,
|
||||
params: defaultStandardsParamsJson
|
||||
};
|
||||
|
||||
export class LevelCalculator {
|
||||
Standards: Standards;
|
||||
|
||||
|
||||
@@ -17,8 +17,9 @@ type Credentials = {
|
||||
export class PrintfulError extends Error {
|
||||
constructor(
|
||||
message: string,
|
||||
public status: number,
|
||||
public upstreamStatus: number,
|
||||
public payload: unknown,
|
||||
public status: number = 502
|
||||
) {
|
||||
super(message);
|
||||
this.name = "PrintfulError";
|
||||
|
||||
@@ -22,8 +22,9 @@ type Credentials = {
|
||||
export class WebflowError extends Error {
|
||||
constructor(
|
||||
message: string,
|
||||
public status: number,
|
||||
public upstreamStatus: number,
|
||||
public payload: unknown,
|
||||
public status: number = 502
|
||||
) {
|
||||
super(message);
|
||||
this.name = "WebflowError";
|
||||
|
||||
Reference in New Issue
Block a user