From 050e33547f4bc43c7a95bed336ce69c3c6183ce8 Mon Sep 17 00:00:00 2001 From: "sunil.n" Date: Fri, 21 Nov 2025 11:04:34 +0530 Subject: [PATCH] Implement connect api in data.py - to connect influxdb, fetch NSSI performance reports for predictions Change-Id: I9f3eaf636aa41318582bda00e23d8a93c819eaac Signed-off-by: sunil.n --- .../rapp-slice-prb-prediction/README.md | 37 +++++++++++++++------- .../rapp-slice-prb-prediction/src/data.py | 19 +++++++++++ 2 files changed, 45 insertions(+), 11 deletions(-) diff --git a/sample-rapp-generator/rapp-slice-prb-prediction/README.md b/sample-rapp-generator/rapp-slice-prb-prediction/README.md index f7a0c2b..2543946 100644 --- a/sample-rapp-generator/rapp-slice-prb-prediction/README.md +++ b/sample-rapp-generator/rapp-slice-prb-prediction/README.md @@ -133,7 +133,12 @@ The `config.json` file contains all necessary configuration parameters for the R ## Database Management (`src/data.py`) -The `data.py` module implements a comprehensive database configuration and management class that centralizes all InfluxDB connection settings and configuration loading for the RAPP. This component provides a unified interface for database operations and configuration management. +The `data.py` module implements a comprehensive database configuration and management class that centralizes all InfluxDB connection settings and configuration loading for the RAPP. This component provides a unified interface for database operations and configuration management with enhanced connection handling and robust error management. + +**Key Import Enhancements:** +- **`influxdb_client`**: Direct import of the InfluxDB client library for database connectivity +- **`time`**: Added for implementing retry logic and connection delay mechanisms +- **`RequestException`**: Specific exception handling for HTTP and network-related errors ### Key Features @@ -141,7 +146,9 @@ The `data.py` module implements a comprehensive database configuration and manag - **Environment Variable Support**: Supports INFLUX_TOKEN environment variable for secure credential management - **Flexible Configuration**: Handles both static configuration and dynamic service discovery parameters - **Pandas Integration**: Configures pandas display options for optimal data visualization -- **Error Handling**: Includes logging for configuration loading and validation +- **Connection Management**: Robust InfluxDB connection handling with automatic reconnection and retry logic +- **Error Handling**: Comprehensive error handling for configuration loading, connection issues, and database operations +- **Version Verification**: Automatic InfluxDB server version detection for compatibility checking ### DATABASE Class @@ -211,12 +218,28 @@ pd.set_option('display.width', 1000) # Adjust width to avoid line brea pd.set_option('display.colheader_justify', 'left') # Align headers left ``` -#### Usage Example +#### Connection Management + +The DATABASE class provides robust connection management with automatic reconnection and error handling: + ```python # Initialize database configuration db = DATABASE() + +# Establish connection to InfluxDB +if db.connect(): + logger.info("Successfully connected to InfluxDB") + # Perform database operations +else: + logger.error("Failed to connect to InfluxDB") ``` +**Error Handling:** +- Catches `RequestException` and `ConnectionError` for network-related issues +- Provides detailed error logging for troubleshooting +- Implements automatic retry delay to handle temporary network issues +- Logs InfluxDB version information for debugging compatibility issues + #### Integration with RAPP Architecture The DATABASE class serves as the foundation for all data operations in the RAPP: @@ -226,14 +249,6 @@ The DATABASE class serves as the foundation for all data operations in the RAPP: 3. **Performance Monitoring**: Enables KPI data collection and analysis 4. **Service Discovery**: Supports SME integration for dynamic endpoint resolution -#### Error Handling and Logging - -The class includes comprehensive error handling: -- Configuration file validation and error reporting -- Environment variable detection and fallback handling -- Logging for configuration loading status -- Graceful handling of missing configuration parameters - #### Benefits - **Centralized Management**: All database configuration in one place diff --git a/sample-rapp-generator/rapp-slice-prb-prediction/src/data.py b/sample-rapp-generator/rapp-slice-prb-prediction/src/data.py index a340996..fc85456 100644 --- a/sample-rapp-generator/rapp-slice-prb-prediction/src/data.py +++ b/sample-rapp-generator/rapp-slice-prb-prediction/src/data.py @@ -1,9 +1,13 @@ import logging +import time +import influxdb_client import pandas as pd import json import os +from requests import RequestException + logger = logging.getLogger(__name__) class DATABASE(object): @@ -62,3 +66,18 @@ class DATABASE(object): self.window_size = int(window_size_str) self.tag_slice_type = influx_config.get("tag_slice_type") self.tag_nssi_id = influx_config.get("tag_nssi_id") + + # Connect with influxdb + def connect(self): + if self.client is not None: + self.client.close() + + try: + self.client = influxdb_client.InfluxDBClient(url=self.address, org=self.org, token=self.token) + version = self.client.version() + logger.info("Connected to Influx Database, InfluxDB version : {}".format(version)) + return True + + except (RequestException, ConnectionError): + logger.error("Failed to establish a new connection with InflulxDB, Please check your url/hostname") + time.sleep(120) \ No newline at end of file -- 2.16.6