refactor: centralize lake metrics calculations into a utility module with comprehensive unit tests
This commit is contained in:
+5
-19
@@ -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],
|
||||
|
||||
Reference in New Issue
Block a user