More refactoring and integration of standards dataset database
This commit is contained in:
@@ -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