Remove standards config fallback and implement caching
This commit is contained in:
@@ -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,34 +86,34 @@ 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;
|
||||
async refresh(opt: { onlyIfStale?: boolean } = {}): Promise<void> {
|
||||
if (!this.cache.isIncomplete() && (opt.onlyIfStale && !this.cache.isStale())) return;
|
||||
|
||||
this.lastRefreshedAt = Date.now();
|
||||
try {
|
||||
const result = await db.selectFrom("standards_configs")
|
||||
.innerJoin("standards_datasets", "standards_datasets.id", "standards_configs.dataset_id")
|
||||
.select([
|
||||
"standards_configs.dataset_id as datasetId",
|
||||
"standards_configs.params as params",
|
||||
"standards_datasets.data as data",
|
||||
])
|
||||
.where("standards_configs.is_selected", "=", true)
|
||||
.limit(1)
|
||||
.executeTakeFirstOrThrow({ errorConstructor: () => new Error("No standards config selected") });
|
||||
Value.Assert(StandardsParamsSchema, result.params);
|
||||
Value.Assert(StandardsDataSchema, result.data);
|
||||
|
||||
const result = await db.selectFrom("standards_configs")
|
||||
.innerJoin("standards_datasets", "standards_datasets.id", "standards_configs.dataset_id")
|
||||
.select([
|
||||
"standards_configs.dataset_id as datasetId",
|
||||
"standards_configs.params as params",
|
||||
"standards_datasets.data as data",
|
||||
])
|
||||
.where("standards_configs.is_selected", "=", true)
|
||||
.limit(1)
|
||||
.executeTakeFirstOrThrow({ errorConstructor: () => new Error("No standards config selected") });
|
||||
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({
|
||||
data: result.data,
|
||||
params: result.params
|
||||
});
|
||||
this.cachedParametersStringified = parametersStringified;
|
||||
this.cachedDatasetId = result.datasetId;
|
||||
this.cache.data.datasetId = result.datasetId;
|
||||
this.cache.data.config = {
|
||||
data: result.data,
|
||||
params: result.params
|
||||
};
|
||||
// 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) {
|
||||
|
||||
Reference in New Issue
Block a user