Clean up Calculator/Standards servicees
This commit is contained in:
@@ -4,28 +4,29 @@ 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";
|
||||||
|
|
||||||
let calculator = new LevelCalculator(new Standards(DEFAULT_STANDARDS_DATASET));
|
|
||||||
|
|
||||||
export class CalculatorService {
|
export class CalculatorService {
|
||||||
standards: StandardsSubService = new StandardsSubService();
|
private calculator: LevelCalculator = new LevelCalculator(new Standards(DEFAULT_STANDARDS_DATASET));
|
||||||
|
standards: StandardsService = new StandardsService(this.calculator);
|
||||||
|
|
||||||
calculate(player: Player, activityPerformances: ActivityPerformance[]) {
|
calculate(player: Player, activityPerformances: ActivityPerformance[]) {
|
||||||
this.standards.refreshConfig({ onlyIfStale: true }).catch((err) => log.error({ err }, "failed to refresh standards config"));
|
this.standards.refreshConfig({ onlyIfStale: true }).catch((err) => log.error({ err }, "failed to refresh standards config"));
|
||||||
return calculator.calculate(player, activityPerformances);
|
return this.calculator.calculate(player, activityPerformances);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class StandardsSubService {
|
class StandardsService {
|
||||||
private static readonly CONFIG_REFRESH_INTERVAL_MS = 30_000;
|
private static readonly CONFIG_REFRESH_INTERVAL_MS = 30_000;
|
||||||
private cachedParametersStringified: string | null = null;
|
private cachedParametersStringified: string | null = null;
|
||||||
private cachedDatasetId: string | null = null;
|
private cachedDatasetId: string | null = null;
|
||||||
private lastRefreshedAt = 0;
|
private lastRefreshedAt = 0;
|
||||||
|
|
||||||
|
constructor(private calculator: LevelCalculator) { }
|
||||||
|
|
||||||
async getSelectedConfig(opt: { skipCache?: boolean } = {}): Promise<StandardsParameters> {
|
async getSelectedConfig(opt: { skipCache?: boolean } = {}): Promise<StandardsParameters> {
|
||||||
if (!opt.skipCache) return calculator.standards.params;
|
if (!opt.skipCache) return this.calculator.standards.params;
|
||||||
|
|
||||||
await this.refreshConfig();
|
await this.refreshConfig();
|
||||||
return calculator.standards.params;
|
return this.calculator.standards.params;
|
||||||
}
|
}
|
||||||
|
|
||||||
async selectConfig(id: string) {
|
async selectConfig(id: string) {
|
||||||
@@ -58,7 +59,7 @@ class StandardsSubService {
|
|||||||
|
|
||||||
async refreshConfig(opt: { onlyIfStale?: boolean } = {}) {
|
async refreshConfig(opt: { onlyIfStale?: boolean } = {}) {
|
||||||
if (opt.onlyIfStale)
|
if (opt.onlyIfStale)
|
||||||
if (Date.now() - this.lastRefreshedAt < StandardsSubService.CONFIG_REFRESH_INTERVAL_MS) return;
|
if (Date.now() - this.lastRefreshedAt < StandardsService.CONFIG_REFRESH_INTERVAL_MS) return;
|
||||||
|
|
||||||
this.lastRefreshedAt = Date.now();
|
this.lastRefreshedAt = Date.now();
|
||||||
|
|
||||||
@@ -78,7 +79,7 @@ class StandardsSubService {
|
|||||||
const parametersStringified = JSON.stringify(config.parameters);
|
const parametersStringified = JSON.stringify(config.parameters);
|
||||||
if (parametersStringified === this.cachedParametersStringified && config.datasetId === this.cachedDatasetId) return;
|
if (parametersStringified === this.cachedParametersStringified && config.datasetId === this.cachedDatasetId) return;
|
||||||
|
|
||||||
calculator = new LevelCalculator(new Standards(config.dataset, config.parameters));
|
this.calculator.standards = new Standards(config.dataset, config.parameters);
|
||||||
this.cachedParametersStringified = parametersStringified;
|
this.cachedParametersStringified = parametersStringified;
|
||||||
this.cachedDatasetId = config.datasetId;
|
this.cachedDatasetId = config.datasetId;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -73,7 +73,7 @@ const metricPriority = (m: NumberMetric) => {
|
|||||||
export const DEFAULT_STANDARDS_DATASET = defaultStandardsDataset as StandardsDataset;
|
export const DEFAULT_STANDARDS_DATASET = defaultStandardsDataset as StandardsDataset;
|
||||||
|
|
||||||
export class LevelCalculator {
|
export class LevelCalculator {
|
||||||
readonly standards: Standards;
|
standards: Standards;
|
||||||
|
|
||||||
public constructor(standards: Standards) {
|
public constructor(standards: Standards) {
|
||||||
this.standards = standards;
|
this.standards = standards;
|
||||||
|
|||||||
Reference in New Issue
Block a user