More models organization
This commit is contained in:
@@ -38,7 +38,7 @@ export async function up(db: Kysely<any>): Promise<void> {
|
||||
await db.schema.createTable("standards_configs")
|
||||
.$call(addDefaultColumns)
|
||||
.addColumn("name", "text", (cb) => cb.notNull().defaultTo(""))
|
||||
.addColumn("parameters", "jsonb", (cb) => cb.notNull())
|
||||
.addColumn("params", "jsonb", (cb) => cb.notNull())
|
||||
.addColumn("is_selected", "boolean", (cb) => cb.notNull().defaultTo(false))
|
||||
.addColumn("dataset_id", "bigint", (cb) => cb.notNull())
|
||||
.addForeignKeyConstraint(
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import {
|
||||
type Levels,
|
||||
DEFAULT_STANDARDS_DATASET,
|
||||
DEFAULT_STANDARDS_DATA,
|
||||
} 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_DATASET[
|
||||
const standardsByGender = DEFAULT_STANDARDS_DATA[
|
||||
activity
|
||||
].standards.filter((s) => s["metrics"].gender === gender);
|
||||
const ages = [...new Set(standardsByGender.map((s) => s.metrics.age))];
|
||||
|
||||
@@ -18,7 +18,7 @@ import { CommerceService } from "./services/commerce";
|
||||
import cluster from "node:cluster";
|
||||
import { randomUUIDv7 } from "bun";
|
||||
import { CalculatorService } from "./services/calculator";
|
||||
import { StandardsParametersSchema } from "@blade-and-brawn/calculator";
|
||||
import { StandardsParamsSchema } from "@blade-and-brawn/calculator";
|
||||
|
||||
// SERVICES
|
||||
// -----------
|
||||
@@ -117,16 +117,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: { datasetId, parameters } }) => {
|
||||
.put("/config/:id", async ({ params: { id }, body: { datasetId, params: parameters } }) => {
|
||||
await s.Calculator.Standards.Config.update(id, datasetId, parameters);
|
||||
}, {
|
||||
params: t.Object({ id: t.String() }),
|
||||
body: t.Object({ datasetId: t.String(), parameters: StandardsParametersSchema })
|
||||
body: t.Object({ datasetId: t.String(), params: StandardsParamsSchema })
|
||||
})
|
||||
.post("/config", async ({ body: { datasetId, parameters } }) => {
|
||||
await s.Calculator.Standards.Config.create(datasetId, parameters);
|
||||
.post("/config", async ({ body: { datasetId, params } }) => {
|
||||
await s.Calculator.Standards.Config.create(datasetId, params);
|
||||
}, {
|
||||
body: t.Object({ datasetId: t.String(), parameters: StandardsParametersSchema })
|
||||
body: t.Object({ datasetId: t.String(), params: StandardsParamsSchema })
|
||||
})
|
||||
.post("/config/:id/switch", async ({ params: { id } }) => {
|
||||
await s.Calculator.Standards.Config.switch(id);
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
import { LevelCalculator, Standards, DEFAULT_STANDARDS_DATASET, type StandardsParameters, StandardsParametersSchema, StandardsDatasetSchema } from "@blade-and-brawn/calculator";
|
||||
import { LevelCalculator, Standards, DEFAULT_STANDARDS_DATA, type StandardsParams, StandardsParamsSchema, StandardsDataSchema } 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_DATASET));
|
||||
private Calculator = new LevelCalculator(new Standards(DEFAULT_STANDARDS_DATA));
|
||||
Standards: StandardsService = new StandardsService(this.Calculator);
|
||||
|
||||
calculate(player: Player, activityPerformances: ActivityPerformance[]) {
|
||||
@@ -30,23 +30,23 @@ class StandardsConfigService {
|
||||
|
||||
constructor(private Calculator: LevelCalculator) { }
|
||||
|
||||
async create(datasetId: string, parameters: StandardsParameters) {
|
||||
async create(datasetId: string, params: StandardsParams) {
|
||||
await db.insertInto("standards_configs")
|
||||
.values({ parameters, dataset_id: datasetId })
|
||||
.values({ params, dataset_id: datasetId })
|
||||
.execute();
|
||||
}
|
||||
|
||||
/** Retrieves the currently selected configuration */
|
||||
async get(opt: { skipCache?: boolean } = {}): Promise<StandardsParameters> {
|
||||
async get(opt: { skipCache?: boolean } = {}): Promise<StandardsParams> {
|
||||
if (!opt.skipCache) return this.Calculator.Standards.params;
|
||||
|
||||
await this.refresh();
|
||||
return this.Calculator.Standards.params;
|
||||
}
|
||||
|
||||
async update(id: string, datasetId: string, parameters: StandardsParameters) {
|
||||
async update(id: string, datasetId: string, params: StandardsParams) {
|
||||
const result = await db.updateTable("standards_configs")
|
||||
.set({ parameters, dataset_id: datasetId })
|
||||
.set({ params, dataset_id: datasetId })
|
||||
.where("id", "=", id)
|
||||
.execute();
|
||||
if (result[0]?.numUpdatedRows === 0n) throw new Error(`No config with id "${id}"`);
|
||||
@@ -62,19 +62,19 @@ class StandardsConfigService {
|
||||
.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",
|
||||
"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(StandardsParametersSchema, config.parameters);
|
||||
Value.Assert(StandardsDatasetSchema, config.dataset);
|
||||
Value.Assert(StandardsParamsSchema, config.params);
|
||||
Value.Assert(StandardsDataSchema, config.data);
|
||||
|
||||
const parametersStringified = JSON.stringify(config.parameters);
|
||||
const parametersStringified = JSON.stringify(config.params);
|
||||
if (parametersStringified === this.cachedParametersStringified && config.datasetId === this.cachedDatasetId) return;
|
||||
|
||||
this.Calculator.Standards = new Standards(config.dataset, config.parameters);
|
||||
this.Calculator.Standards = new Standards(config.data, config.params);
|
||||
this.cachedParametersStringified = parametersStringified;
|
||||
this.cachedDatasetId = config.datasetId;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user