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
|
# IntelliJ based IDEs
|
||||||
.idea
|
.idea
|
||||||
|
.zed
|
||||||
|
|
||||||
# Finder (MacOS) folder config
|
# Finder (MacOS) folder config
|
||||||
.DS_Store
|
.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 {
|
import {
|
||||||
type Levels,
|
type Levels,
|
||||||
FALLBACK_STANDARDS_CONFIG,
|
|
||||||
} from "@blade-and-brawn/calculator";
|
} from "@blade-and-brawn/calculator";
|
||||||
import { Activity, clamp, Gender, range } from "@blade-and-brawn/domain";
|
import { Activity, clamp, Gender, range } from "@blade-and-brawn/domain";
|
||||||
import { levenbergMarquardt as LM } from "ml-levenberg-marquardt";
|
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)) {
|
for (const activity of Object.values(Activity)) {
|
||||||
const DATASET_MAX_LEVEL = 5;
|
const DATASET_MAX_LEVEL = 5;
|
||||||
const NEW_LEVEL_COUNT = 2;
|
const NEW_LEVEL_COUNT = 2;
|
||||||
@@ -15,7 +17,7 @@ for (const activity of Object.values(Activity)) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for (const gender of Object.values(Gender)) {
|
for (const gender of Object.values(Gender)) {
|
||||||
const standardsByGender = FALLBACK_STANDARDS_CONFIG.data[
|
const standardsByGender = standardsConfig.data[
|
||||||
activity
|
activity
|
||||||
].standards.filter((s) => s["metrics"].gender === gender);
|
].standards.filter((s) => s["metrics"].gender === gender);
|
||||||
const ages = [...new Set(standardsByGender.map((s) => s.metrics.age))];
|
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 { CommerceService } from "./services/commerce";
|
||||||
import cluster from "node:cluster";
|
import cluster from "node:cluster";
|
||||||
import { randomUUIDv7 } from "bun";
|
import { randomUUIDv7 } from "bun";
|
||||||
import { CalculatorService } from "./services/calculator";
|
import { CalculatorService, CalculatorUnavailableError } from "./services/calculator";
|
||||||
import { StandardsParamsSchema } from "@blade-and-brawn/calculator";
|
import { StandardsParamsSchema } from "@blade-and-brawn/calculator";
|
||||||
|
|
||||||
// SERVICES
|
// SERVICES
|
||||||
@@ -47,6 +47,7 @@ export const app = new Elysia()
|
|||||||
.error({
|
.error({
|
||||||
PrintfulError,
|
PrintfulError,
|
||||||
WebflowError,
|
WebflowError,
|
||||||
|
CalculatorUnavailableError,
|
||||||
})
|
})
|
||||||
|
|
||||||
.onError(({ code, error }) => {
|
.onError(({ code, error }) => {
|
||||||
@@ -54,14 +55,17 @@ export const app = new Elysia()
|
|||||||
case "PrintfulError":
|
case "PrintfulError":
|
||||||
case "WebflowError":
|
case "WebflowError":
|
||||||
log.error(
|
log.error(
|
||||||
{ upstreamStatus: error.status, payload: error.payload },
|
{ upstreamStatus: error.upstreamStatus, payload: error.payload },
|
||||||
error.message,
|
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":
|
case "NOT_FOUND":
|
||||||
return status(404, { error: "Not found" });
|
return status(error.status, { error: error.message });
|
||||||
case "VALIDATION":
|
case "VALIDATION":
|
||||||
return status(400, { error: error.message });
|
return status(error.status, { error: error.message });
|
||||||
default:
|
default:
|
||||||
log.error({ err: error }, "unhandled error");
|
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 type { ActivityPerformance, Player } from "@blade-and-brawn/domain";
|
||||||
import { db } from "../database/db";
|
import { db } from "../database/db";
|
||||||
import { Value } from "@sinclair/typebox/value";
|
import { Value } from "@sinclair/typebox/value";
|
||||||
import { log } from "../util";
|
import { log } from "../util";
|
||||||
|
|
||||||
|
export class CalculatorUnavailableError extends Error {
|
||||||
|
status: number = 503
|
||||||
|
}
|
||||||
|
|
||||||
export class CalculatorService {
|
export class CalculatorService {
|
||||||
private Calculator = new LevelCalculator(new Standards(FALLBACK_STANDARDS_CONFIG));
|
private calculator: LevelCalculator | null = null;
|
||||||
Standards: StandardsService = new StandardsService(this.Calculator);
|
private appliedConfigStringified: string | null = null;
|
||||||
private ready: Promise<void>;
|
Standards: StandardsService = new StandardsService();
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
// Load the selected config on startup instead of waiting for the
|
// eagerly load
|
||||||
// first `calculate()` call. If it fails (e.g. no config selected
|
this.refresh().catch((err) => log.error({ err }, "failed to load initial standards config"));
|
||||||
// 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[]) {
|
async calculate(player: Player, activityPerformances: ActivityPerformance[]): Promise<LevelCalculatorOutput> {
|
||||||
await this.ready;
|
await this.refresh();
|
||||||
this.Standards.Config.refresh({ onlyIfStale: true }).catch((err) => log.error({ err }, "failed to refresh standards config"));
|
if (!this.calculator) throw new CalculatorUnavailableError("Level calculator is not ready");
|
||||||
return this.Calculator.calculate(player, activityPerformances);
|
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 {
|
class StandardsService {
|
||||||
Config: StandardsConfigService;
|
Config: StandardsConfigService;
|
||||||
|
|
||||||
constructor(private Calculator: LevelCalculator) {
|
constructor() {
|
||||||
this.Config = new StandardsConfigService(this.Calculator);
|
this.Config = new StandardsConfigService();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class StandardsConfigService {
|
class StandardsConfigService {
|
||||||
private static readonly REFRESH_INTERVAL_MS = 30_000;
|
private cache = {
|
||||||
private cachedParametersStringified: string | null = null;
|
refreshInterval: 30_000,
|
||||||
private cachedDatasetId: string | null = null;
|
lastRefreshedAt: 0,
|
||||||
private lastRefreshedAt = 0;
|
isStale: function () {
|
||||||
|
return Date.now() - this.lastRefreshedAt >= this.refreshInterval;
|
||||||
constructor(private Calculator: LevelCalculator) { }
|
},
|
||||||
|
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) {
|
async create(datasetId: string, params: StandardsParams) {
|
||||||
await db.insertInto("standards_configs")
|
await db.insertInto("standards_configs")
|
||||||
@@ -48,11 +72,10 @@ class StandardsConfigService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/** Retrieves the currently selected configuration */
|
/** Retrieves the currently selected configuration */
|
||||||
async get(opt: { skipCache?: boolean } = {}): Promise<StandardsConfig> {
|
async get(): Promise<StandardsConfig> {
|
||||||
if (!opt.skipCache) return this.Calculator.Standards.cfg;
|
await this.refresh({ onlyIfStale: true });
|
||||||
|
if (!this.cache.data.config) throw new CalculatorUnavailableError("Unable to determine the selected standards configuration");
|
||||||
await this.refresh();
|
return this.cache.data.config;
|
||||||
return this.Calculator.Standards.cfg;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async update(id: string, datasetId: string, params: StandardsParams) {
|
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}"`);
|
if (result[0]?.numUpdatedRows === 0n) throw new Error(`No config with id "${id}"`);
|
||||||
}
|
}
|
||||||
|
|
||||||
async refresh(opt: { onlyIfStale?: boolean } = {}) {
|
async refresh(opt: { onlyIfStale?: boolean } = {}): Promise<void> {
|
||||||
if (opt.onlyIfStale)
|
if (!this.cache.isIncomplete() && (opt.onlyIfStale && !this.cache.isStale())) return;
|
||||||
if (Date.now() - this.lastRefreshedAt < StandardsConfigService.REFRESH_INTERVAL_MS) return;
|
|
||||||
|
|
||||||
this.lastRefreshedAt = Date.now();
|
|
||||||
|
|
||||||
|
try {
|
||||||
const result = await db.selectFrom("standards_configs")
|
const result = await db.selectFrom("standards_configs")
|
||||||
.innerJoin("standards_datasets", "standards_datasets.id", "standards_configs.dataset_id")
|
.innerJoin("standards_datasets", "standards_datasets.id", "standards_configs.dataset_id")
|
||||||
.select([
|
.select([
|
||||||
@@ -82,15 +103,17 @@ class StandardsConfigService {
|
|||||||
Value.Assert(StandardsParamsSchema, result.params);
|
Value.Assert(StandardsParamsSchema, result.params);
|
||||||
Value.Assert(StandardsDataSchema, result.data);
|
Value.Assert(StandardsDataSchema, result.data);
|
||||||
|
|
||||||
const parametersStringified = JSON.stringify(result.params);
|
this.cache.data.datasetId = result.datasetId;
|
||||||
if (parametersStringified === this.cachedParametersStringified && result.datasetId === this.cachedDatasetId) return;
|
this.cache.data.config = {
|
||||||
|
|
||||||
this.Calculator.Standards = new Standards({
|
|
||||||
data: result.data,
|
data: result.data,
|
||||||
params: result.params
|
params: result.params
|
||||||
});
|
};
|
||||||
this.cachedParametersStringified = parametersStringified;
|
// only mark as refreshed once we've actually confirmed the selected
|
||||||
this.cachedDatasetId = result.datasetId;
|
// 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) {
|
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,
|
type Player,
|
||||||
} from "@blade-and-brawn/domain";
|
} from "@blade-and-brawn/domain";
|
||||||
import { levenbergMarquardt as LM } from "ml-levenberg-marquardt";
|
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 { getAvgWeight } from "./avg-weights";
|
||||||
import { StandardsDataSchema, StandardsParamsSchema, type LevelCalculatorOutput, type Levels, type NumberMetric, type Standard, type StandardsConfig, type StandardsData } from "./models";
|
import { type LevelCalculatorOutput, type Levels, type NumberMetric, type Standard, type StandardsConfig, type StandardsData } from "./models";
|
||||||
import { Value } from "@sinclair/typebox/value";
|
|
||||||
|
|
||||||
// SOURCES
|
// SOURCES
|
||||||
// Squat, Bench, Dead Lift:
|
// Squat, Bench, Dead Lift:
|
||||||
@@ -34,13 +31,6 @@ const metricPriority = (m: NumberMetric) => {
|
|||||||
return 2;
|
return 2;
|
||||||
};
|
};
|
||||||
|
|
||||||
Value.Assert(StandardsDataSchema, defaultStandardsDataJson);
|
|
||||||
Value.Assert(StandardsParamsSchema, defaultStandardsParamsJson);
|
|
||||||
export const FALLBACK_STANDARDS_CONFIG: StandardsConfig = {
|
|
||||||
data: defaultStandardsDataJson,
|
|
||||||
params: defaultStandardsParamsJson
|
|
||||||
};
|
|
||||||
|
|
||||||
export class LevelCalculator {
|
export class LevelCalculator {
|
||||||
Standards: Standards;
|
Standards: Standards;
|
||||||
|
|
||||||
|
|||||||
@@ -17,8 +17,9 @@ type Credentials = {
|
|||||||
export class PrintfulError extends Error {
|
export class PrintfulError extends Error {
|
||||||
constructor(
|
constructor(
|
||||||
message: string,
|
message: string,
|
||||||
public status: number,
|
public upstreamStatus: number,
|
||||||
public payload: unknown,
|
public payload: unknown,
|
||||||
|
public status: number = 502
|
||||||
) {
|
) {
|
||||||
super(message);
|
super(message);
|
||||||
this.name = "PrintfulError";
|
this.name = "PrintfulError";
|
||||||
|
|||||||
@@ -22,8 +22,9 @@ type Credentials = {
|
|||||||
export class WebflowError extends Error {
|
export class WebflowError extends Error {
|
||||||
constructor(
|
constructor(
|
||||||
message: string,
|
message: string,
|
||||||
public status: number,
|
public upstreamStatus: number,
|
||||||
public payload: unknown,
|
public payload: unknown,
|
||||||
|
public status: number = 502
|
||||||
) {
|
) {
|
||||||
super(message);
|
super(message);
|
||||||
this.name = "WebflowError";
|
this.name = "WebflowError";
|
||||||
|
|||||||
Reference in New Issue
Block a user