refactor: centralize lake metrics calculations into a utility module with comprehensive unit tests

This commit is contained in:
David Fencl
2026-06-06 11:45:56 +02:00
parent 6d77c20c84
commit dbb22e7972
4 changed files with 130 additions and 20 deletions
+5 -19
View File
@@ -1,6 +1,7 @@
import * as fs from 'fs';
import * as path from 'path';
import { lakesConfig } from './lakesConfig';
import { calculateLakeMetrics } from './utils/calculations';
interface DataRecord {
timestamp: string;
@@ -53,22 +54,7 @@ const lakes = lakesConfig.map(lake => {
}
}
if (volume > 0 && lake.maxVolume && lake.maxVolume > 0) {
capacity = Math.max(0, Math.min(100, Math.round((volume / lake.maxVolume) * 1000) / 10));
} else if (lake.minLevel && lake.maxLevel && currentLevel > 0) {
const percentage = ((currentLevel - lake.minLevel) / (lake.maxLevel - lake.minLevel)) * 100;
capacity = Math.max(0, Math.min(100, Math.round(percentage * 10) / 10)); // Round to 1 decimal place
if (volume === 0) {
volume = Number(((capacity / 100) * (lake.maxVolume || 0)).toFixed(1));
}
} else {
if (volume === 0) volume = lake.maxVolume || 0;
}
let storageDiff = 0;
if (lake.storageLevel && currentLevel > 0) {
storageDiff = Number((currentLevel - lake.storageLevel).toFixed(2));
}
const metrics = calculateLakeMetrics(currentLevel, volume, lake);
return {
id: lake.id,
@@ -76,11 +62,11 @@ const lakes = lakesConfig.map(lake => {
river: lake.text.includes('-') ? lake.text.split('-')[1].trim() : '',
priority: lake.priority || false,
level: currentLevel.toFixed(2),
capacity: capacity,
storageDiff: storageDiff,
capacity: metrics.capacity,
storageDiff: metrics.storageDiff,
inflow: inflow.toFixed(1),
outflow: currentFlow.toFixed(1),
volume: volume,
volume: metrics.volume,
maxVolume: lake.maxVolume || 0,
lat: lake.coords[0],
lng: lake.coords[1],