- Updated README.md to reflect new features and usage instructions. - Removed deprecated Dashboard.html component and integrated dashboard functionality directly into the main application. - Revised architecture documentation for clarity and added module map and request flow diagrams. - Enhanced maintenance model to include equipment association and cost tracking. - Updated requirements.txt to include new dependencies (httpx, pandas, numpy). - Improved consumption, maintenance, production, and reporting routes with better validation and response handling. - Added unit tests for maintenance and production routes, ensuring proper CRUD operations and validation. - Enhanced reporting service to calculate and return detailed summary statistics. - Redesigned Dashboard.html for improved user experience and integrated Chart.js for visualizing simulation results.
58 lines
1.7 KiB
Python
58 lines
1.7 KiB
Python
from statistics import mean, median, pstdev
|
|
from typing import Dict, Iterable, List, Union
|
|
|
|
|
|
def _extract_results(simulation_results: Iterable[Dict[str, float]]) -> List[float]:
|
|
values: List[float] = []
|
|
for item in simulation_results:
|
|
if not isinstance(item, dict):
|
|
continue
|
|
value = item.get("result")
|
|
if isinstance(value, (int, float)):
|
|
values.append(float(value))
|
|
return values
|
|
|
|
|
|
def _percentile(values: List[float], percentile: float) -> float:
|
|
if not values:
|
|
return 0.0
|
|
sorted_values = sorted(values)
|
|
if len(sorted_values) == 1:
|
|
return sorted_values[0]
|
|
index = (percentile / 100) * (len(sorted_values) - 1)
|
|
lower = int(index)
|
|
upper = min(lower + 1, len(sorted_values) - 1)
|
|
weight = index - lower
|
|
return sorted_values[lower] * (1 - weight) + sorted_values[upper] * weight
|
|
|
|
|
|
def generate_report(simulation_results: List[Dict[str, float]]) -> Dict[str, Union[float, int]]:
|
|
"""Aggregate basic statistics for simulation outputs."""
|
|
|
|
values = _extract_results(simulation_results)
|
|
|
|
if not values:
|
|
return {
|
|
"count": 0,
|
|
"mean": 0.0,
|
|
"median": 0.0,
|
|
"min": 0.0,
|
|
"max": 0.0,
|
|
"std_dev": 0.0,
|
|
"percentile_10": 0.0,
|
|
"percentile_90": 0.0,
|
|
}
|
|
|
|
summary: Dict[str, Union[float, int]] = {
|
|
"count": len(values),
|
|
"mean": mean(values),
|
|
"median": median(values),
|
|
"min": min(values),
|
|
"max": max(values),
|
|
"percentile_10": _percentile(values, 10),
|
|
"percentile_90": _percentile(values, 90),
|
|
}
|
|
|
|
summary["std_dev"] = pstdev(values) if len(values) > 1 else 0.0
|
|
return summary
|