More refactoring and integration of standards dataset database
This commit is contained in:
@@ -16,7 +16,7 @@ function processResults(label: string, { error, results }: MigrationResultSet):
|
||||
if (it.status === 'Success') {
|
||||
message = `migration ${label} succeeded: "${it.migrationName}"`;
|
||||
} else if (it.status === 'Error') {
|
||||
throw new Error(`migration ${label} failed: "${it.migrationName}"`);
|
||||
throw error ?? new Error(`migration ${label} failed: "${it.migrationName}"`);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
@@ -27,11 +27,27 @@ export async function up(db: Kysely<any>): Promise<void> {
|
||||
.nullsNotDistinct()
|
||||
.execute();
|
||||
|
||||
// TABLE: STANDARDS_DATASETS
|
||||
await db.schema.createTable("standards_datasets")
|
||||
.$call(addDefaultColumns)
|
||||
.addColumn("name", "text", (cb) => cb.notNull().defaultTo(""))
|
||||
.addColumn("data", "jsonb", (cb) => cb.notNull())
|
||||
.execute();
|
||||
|
||||
// TABLE: STANDARDS_CONFIGS
|
||||
await db.schema.createTable("standards_configs")
|
||||
.$call(addDefaultColumns)
|
||||
.addColumn("name", "text", (cb) => cb.notNull().defaultTo(""))
|
||||
.addColumn("parameters", "jsonb", (cb) => cb.notNull())
|
||||
.addColumn("is_selected", "boolean", (cb) => cb.notNull().defaultTo(false))
|
||||
.addColumn("dataset_id", "bigint", (cb) => cb.notNull())
|
||||
.addForeignKeyConstraint(
|
||||
"fk_standards_configs_dataset_id",
|
||||
["dataset_id"],
|
||||
"standards_datasets",
|
||||
["id"],
|
||||
(cb) => cb.onDelete("restrict")
|
||||
)
|
||||
.execute();
|
||||
|
||||
// INDEX: ONE_SELECTED_STANDARDS_CONFIG
|
||||
@@ -50,9 +66,12 @@ export async function down(db: Kysely<any>): Promise<void> {
|
||||
// INDEX: ONE_ACTIVE_PRODUCT_SYNC
|
||||
await db.schema.dropIndex("idx_one_active_product_sync").ifExists().execute();
|
||||
|
||||
// INDEX: ONE_SELECTED_STANDARDS_CONFIG
|
||||
await db.schema.dropIndex("idx_one_selected_standards_config").ifExists().execute();
|
||||
|
||||
// TABLE: STANDARDS_CONFIGS
|
||||
await db.schema.dropTable("standards_configs").ifExists().execute()
|
||||
|
||||
// INDEX: ONE_SELECTED_STANDARDS_CONFIG
|
||||
await db.schema.dropIndex("idx_one_selected_standards_config").ifExists().execute();
|
||||
// TABLE: STANDARDS_DATASETS
|
||||
await db.schema.dropTable("standards_datasets").ifExists().execute()
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import {
|
||||
type Levels,
|
||||
DEFAULT_STANDARDS_DATA,
|
||||
DEFAULT_STANDARDS_DATASET,
|
||||
} 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_DATASET[
|
||||
activity
|
||||
].standards.filter((s) => s["metrics"].gender === gender);
|
||||
const ages = [...new Set(standardsByGender.map((s) => s.metrics.age))];
|
||||
|
||||
@@ -116,16 +116,16 @@ export const app = new Elysia()
|
||||
if (!token || !token.sessionId) throw status(401, "Unauthorized");
|
||||
return { sessionId: token.sessionId.toString() };
|
||||
})
|
||||
.put("/config/:id", async ({ params: { id }, body: { standardsConfig } }) => {
|
||||
await calculatorService.standards.setConfig(id, standardsConfig);
|
||||
.put("/config/:id", async ({ params: { id }, body: { datasetId, parameters } }) => {
|
||||
await calculatorService.standards.setConfig(id, datasetId, parameters);
|
||||
}, {
|
||||
params: t.Object({ id: t.String() }),
|
||||
body: t.Object({ standardsConfig: StandardsParametersSchema })
|
||||
body: t.Object({ datasetId: t.String(), parameters: StandardsParametersSchema })
|
||||
})
|
||||
.post("/config", async ({ body: { standardsConfig } }) => {
|
||||
await calculatorService.standards.createConfig(standardsConfig);
|
||||
.post("/config", async ({ body: { datasetId, parameters } }) => {
|
||||
await calculatorService.standards.createConfig(datasetId, parameters);
|
||||
}, {
|
||||
body: t.Object({ standardsConfig: StandardsParametersSchema })
|
||||
body: t.Object({ datasetId: t.String(), parameters: StandardsParametersSchema })
|
||||
})
|
||||
.post("/config/:id/select", async ({ params: { id } }) => {
|
||||
await calculatorService.standards.selectConfig(id);
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
import { LevelCalculator, Standards, DEFAULT_STANDARDS_DATA, type StandardsParameters, StandardsParametersSchema } from "@blade-and-brawn/calculator";
|
||||
import { LevelCalculator, Standards, DEFAULT_STANDARDS_DATASET, type StandardsParameters, StandardsParametersSchema, StandardsDatasetSchema } 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";
|
||||
|
||||
let calculator = new LevelCalculator(new Standards(DEFAULT_STANDARDS_DATA));
|
||||
let calculator = new LevelCalculator(new Standards(DEFAULT_STANDARDS_DATASET));
|
||||
|
||||
export class CalculatorService {
|
||||
standards: StandardsSubService = new StandardsSubService();
|
||||
@@ -17,7 +17,8 @@ export class CalculatorService {
|
||||
|
||||
class StandardsSubService {
|
||||
private static readonly CONFIG_REFRESH_INTERVAL_MS = 30_000;
|
||||
private cachedStandardsConfigData: string | null = null;
|
||||
private cachedParametersStringified: string | null = null;
|
||||
private cachedDatasetId: string | null = null;
|
||||
private lastRefreshedAt = 0;
|
||||
|
||||
async getSelectedConfig(opt: { skipCache?: boolean } = {}): Promise<StandardsParameters> {
|
||||
@@ -41,17 +42,17 @@ class StandardsSubService {
|
||||
});
|
||||
}
|
||||
|
||||
async setConfig(id: string, parameters: StandardsParameters) {
|
||||
async setConfig(id: string, datasetId: string, parameters: StandardsParameters) {
|
||||
const result = await db.updateTable("standards_configs")
|
||||
.set({ parameters })
|
||||
.set({ parameters, dataset_id: datasetId })
|
||||
.where("id", "=", id)
|
||||
.execute();
|
||||
if (result[0]?.numUpdatedRows === 0n) throw new Error(`No config with id "${id}"`);
|
||||
}
|
||||
|
||||
async createConfig(parameters: StandardsParameters) {
|
||||
async createConfig(datasetId: string, parameters: StandardsParameters) {
|
||||
await db.insertInto("standards_configs")
|
||||
.values({ parameters })
|
||||
.values({ parameters, dataset_id: datasetId })
|
||||
.execute();
|
||||
}
|
||||
|
||||
@@ -61,17 +62,24 @@ class StandardsSubService {
|
||||
|
||||
this.lastRefreshedAt = Date.now();
|
||||
|
||||
const { parameters } = await db.selectFrom("standards_configs")
|
||||
.select(["parameters"])
|
||||
.where("is_selected", "=", true)
|
||||
const config = await db.selectFrom("standards_configs")
|
||||
.innerJoin("standards_datasets", "standards_datasets.id", "standards_configs.dataset_id")
|
||||
.select([
|
||||
"standards_configs.dataset_id as datasetId",
|
||||
"standards_configs.parameters as parameters",
|
||||
"standards_datasets.data as dataset",
|
||||
])
|
||||
.where("standards_configs.is_selected", "=", true)
|
||||
.limit(1)
|
||||
.executeTakeFirstOrThrow({ errorConstructor: () => new Error("No standards config selected") });
|
||||
Value.Assert(StandardsParametersSchema, parameters);
|
||||
Value.Assert(StandardsParametersSchema, config.parameters);
|
||||
Value.Assert(StandardsDatasetSchema, config.dataset);
|
||||
|
||||
const standardsConfigData = JSON.stringify(parameters);
|
||||
if (standardsConfigData === this.cachedStandardsConfigData) return;
|
||||
const parametersStringified = JSON.stringify(config.parameters);
|
||||
if (parametersStringified === this.cachedParametersStringified && config.datasetId === this.cachedDatasetId) return;
|
||||
|
||||
calculator = new LevelCalculator(new Standards(DEFAULT_STANDARDS_DATA, parameters));
|
||||
this.cachedStandardsConfigData = standardsConfigData;
|
||||
calculator = new LevelCalculator(new Standards(config.dataset, config.parameters));
|
||||
this.cachedParametersStringified = parametersStringified;
|
||||
this.cachedDatasetId = config.datasetId;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user