12 KiB
title, description, status
| title | description | status |
|---|---|---|
| 06 — Runtime View | Describe runtime aspects: request flows, lifecycle of key interactions, and runtime components. | draft |
06 — Runtime View
Overview
The runtime view focuses on the dynamic behavior of the CalMiner application during execution. It illustrates how various components interact to fulfill user requests, process data, and generate outputs. Key runtime scenarios include scenario management, parameter input handling, cost tracking, consumption tracking, production output recording, equipment management, maintenance logging, Monte Carlo simulations, and reporting.
Request Flow
- User Interaction: A user interacts with the web application through the UI, triggering actions such as creating a scenario, inputting parameters, or generating reports.
- API Request: The frontend sends HTTP requests (GET, POST, PUT, DELETE) to the appropriate API endpoints defined in the
routes/directory. - Routing: The FastAPI framework routes the incoming requests to the corresponding route handlers.
- Service Layer: Route handlers invoke services from the
services/directory to process the business logic. - Database Interaction: Services interact with the database via ORM models defined in the
models/directory to perform CRUD operations. - Response Generation: After processing, services return data to the route handlers, which format the response (JSON or HTML) and send it back to the frontend.
- UI Update: The frontend updates the UI based on the response, rendering new data or updating existing views.
- Reporting Pipeline: For reporting, data is aggregated from various sources, processed to generate statistics, and presented in the dashboard using Chart.js.
- Monte Carlo Simulations: Stochastic simulations are executed in the backend, generating probabilistic outcomes that are stored temporarily and used for risk analysis in reports.
- Error Handling: Throughout the process, error handling mechanisms ensure that exceptions are caught and appropriate responses are sent back to the user.
Request flow diagram:
sequenceDiagram
participant User
participant Frontend
participant API
participant Service
participant Database
User->>Frontend: Interact with UI
Frontend->>API: Send HTTP Request
API->>Service: Route to Handler
Service->>Database: Perform CRUD Operation
Database-->>Service: Return Data
Service-->>API: Return Processed Data
API-->>Frontend: Send Response
Frontend-->>User: Update UI
participant Reporting
Service->>Reporting: Aggregate Data
Reporting-->>Service: Return Report Data
Service-->>API: Return Report Response
API-->>Frontend: Send Report Data
Frontend-->>User: Render Report
participant Simulation
Service->>Simulation: Execute Monte Carlo Simulation
Simulation-->>Service: Return Simulation Results
Service-->>API: Return Simulation Data
API-->>Frontend: Send Simulation Data
Frontend-->>User: Display Simulation Results
Key Runtime Scenarios
Scenario Management
- User accesses the scenario list via the UI.
- The frontend sends a GET request to
/api/scenarios. - The
ScenarioServiceretrieves scenarios from the database. - The response is rendered in the UI.
- For scenario creation, the user submits a form, triggering a POST request to
/api/scenarios, which theScenarioServiceprocesses to create a new scenario in the database. - The UI updates to reflect the new scenario.
Scenario management diagram:
sequenceDiagram
participant User
participant Frontend
participant API
participant ScenarioService
participant Database
User->>Frontend: Access Scenario List
Frontend->>API: GET /api/scenarios
API->>ScenarioService: Route to Handler
ScenarioService->>Database: Retrieve Scenarios
Database-->>ScenarioService: Return Scenarios
ScenarioService-->>API: Return Scenario Data
API-->>Frontend: Send Response
Frontend-->>User: Render Scenario List
User->>Frontend: Submit New Scenario Form
Frontend->>API: POST /api/scenarios
API->>ScenarioService: Route to Handler
ScenarioService->>Database: Create New Scenario
Database-->>ScenarioService: Confirm Creation
ScenarioService-->>API: Return New Scenario Data
API-->>Frontend: Send Response
Frontend-->>User: Update UI with New Scenario
Process Parameter Input
- User navigates to the parameter input form.
- The frontend fetches existing parameters via a GET request to
/api/parameters. - The
ParameterServiceretrieves parameters from the database. - The response is rendered in the UI.
- For parameter updates, the user submits a form, triggering a PUT request to
/api/parameters/:id, which theParameterServiceprocesses to update the parameter in the database. - The UI updates to reflect the changes.
Parameter input diagram:
sequenceDiagram
participant User
participant Frontend
participant API
participant ParameterService
participant Database
User->>Frontend: Navigate to Parameter Input Form
Frontend->>API: GET /api/parameters
API->>ParameterService: Route to Handler
ParameterService->>Database: Retrieve Parameters
Database-->>ParameterService: Return Parameters
ParameterService-->>API: Return Parameter Data
API-->>Frontend: Send Response
Frontend-->>User: Render Parameter Form
User->>Frontend: Submit Parameter Update Form
Frontend->>API: PUT /api/parameters/:id
API->>ParameterService: Route to Handler
ParameterService->>Database: Update Parameter
Database-->>ParameterService: Confirm Update
ParameterService-->>API: Return Updated Parameter Data
API-->>Frontend: Send Response
Frontend-->>User: Update UI with Updated Parameter
Cost Tracking
- User accesses the cost tracking view.
- The frontend sends a GET request to
/api/coststo fetch existing cost records. - The
CostServiceretrieves cost data from the database. - The response is rendered in the UI.
- For cost updates, the user submits a form, triggering a PUT request to
/api/costs/:id, which theCostServiceprocesses to update the cost record in the database. - The UI updates to reflect the changes.
Cost tracking diagram:
sequenceDiagram
participant User
participant Frontend
participant API
participant CostService
participant Database
User->>Frontend: Access Cost Tracking View
Frontend->>API: GET /api/costs
API->>CostService: Route to Handler
CostService->>Database: Retrieve Cost Records
Database-->>CostService: Return Cost Data
CostService-->>API: Return Cost Data
API-->>Frontend: Send Response
Frontend-->>User: Render Cost Tracking View
User->>Frontend: Submit Cost Update Form
Frontend->>API: PUT /api/costs/:id
API->>CostService: Route to Handler
CostService->>Database: Update Cost Record
Database-->>CostService: Confirm Update
CostService-->>API: Return Updated Cost Data
API-->>Frontend: Send Response
Frontend-->>User: Update UI with Updated Cost Data
Reporting Pipeline and UI Integration
-
Data Sources
- Scenario-linked calculations (costs, consumption, production) produce raw figures stored in dedicated tables (
capex,opex,consumption,production_output). - Monte Carlo simulations (currently transient) generate arrays of
{ "result": float }tuples that the dashboard or downstream tooling passes directly to reporting endpoints.
- Scenario-linked calculations (costs, consumption, production) produce raw figures stored in dedicated tables (
-
API Contract
POST /api/reporting/summaryaccepts a JSON array of result objects and validates shape through_validate_payloadinroutes/reporting.py.- On success it returns a structured payload (
ReportSummary) containing count, mean, median, min/max, standard deviation, and percentile values, all as floats.
-
Service Layer
services/reporting.generate_reportconverts the sanitized payload into descriptive statistics using Python’s standard library (statisticsmodule) to avoid external dependencies.- The service remains stateless; no database read/write occurs, which keeps summary calculations deterministic and idempotent.
- Extended KPIs (surfaced in the API and dashboard):
variance: population variance computed as the square of the population standard deviation.percentile_5andpercentile_95: lower and upper tail interpolated percentiles for sensitivity bounds.value_at_risk_95: 5th percentile threshold representing the minimum outcome within a 95% confidence band.expected_shortfall_95: mean of all outcomes at or below thevalue_at_risk_95, highlighting tail exposure.
-
UI Consumption
templates/Dashboard.htmlposts the user-provided dataset to the summary endpoint, renders metric cards for each field, and charts the distribution using Chart.js.SUMMARY_FIELDSnow includes variance, 5th/10th/90th/95th percentiles, and tail-risk metrics (VaR/Expected Shortfall at 95%); tooltip annotations surface the tail metrics alongside the percentile line chart.- Error handling surfaces HTTP failures inline so users can address malformed JSON or backend availability issues without leaving the page.
Reporting pipeline diagram:
sequenceDiagram
participant User
participant Frontend
participant API
participant ReportingService
User->>Frontend: Input Data for Reporting
Frontend->>API: POST /api/reporting/summary
API->>ReportingService: Route to Handler
ReportingService->>ReportingService: Validate Payload
ReportingService->>ReportingService: Compute Statistics
ReportingService-->>API: Return Report Summary
API-->>Frontend: Send Report Summary
Frontend-->>User: Render Report Metrics and Charts
Monte Carlo Simulation Execution
- User initiates a Monte Carlo simulation via the UI.
- The frontend sends a POST request to
/api/simulations/runwith simulation parameters. - The
SimulationServiceexecutes the Monte Carlo logic, generating stochastic results. - The results are temporarily stored and returned to the frontend.
- The UI displays the simulation results and allows users to trigger reporting based on these outcomes.
- The reporting pipeline processes the simulation results as described above.
- Error handling ensures that any issues during simulation execution are communicated back to the user.
- Monte Carlo simulation diagram:
sequenceDiagram
participant User
participant Frontend
participant API
participant SimulationService
User->>Frontend: Input Simulation Parameters
Frontend->>API: POST /api/simulations/run
API->>SimulationService: Route to Handler
SimulationService->>SimulationService: Execute Monte Carlo Logic
SimulationService-->>API: Return Simulation Results
API-->>Frontend: Send Simulation Results
Frontend-->>User: Render Simulation Results
Error Handling
Throughout the runtime processes, error handling mechanisms are implemented to catch exceptions and provide meaningful feedback to users. Common error scenarios include:
- Invalid input data
- Database connection issues
- Simulation execution errors
- Reporting calculation failures
- API endpoint unavailability
- Timeouts during long-running operations
- Unauthorized access attempts
- Data validation failures
- Resource not found errors
Error handling diagram:
sequenceDiagram
participant User
participant Frontend
participant API
participant Service
User->>Frontend: Perform Action
Frontend->>API: Send Request
API->>Service: Route to Handler
Service->>Service: Process Request
alt Success
Service-->>API: Return Data
API-->>Frontend: Send Response
Frontend-->>User: Update UI
else Error
Service-->>API: Return Error
API-->>Frontend: Send Error Response
Frontend-->>User: Display Error Message
end