From 73faebe3fa1c353a9ff25690d7e37648c781c36c Mon Sep 17 00:00:00 2001 From: Andreas Tsagkaropoulos Date: Thu, 16 May 2024 15:33:36 +0300 Subject: [PATCH] Bug fix and improvements Fixed the initialization of the Influxdb client Added and improved diagnostic messages Change-Id: Ic8aaa0728a4b936cd4c6e1ed5a0e01ba8f0fb003 --- .../prediction_configuration.properties | 2 +- .../src/runtime/Predictor.py | 2 +- .../runtime/operational_status/ApplicationState.py | 6 ++++-- .../src/runtime/utilities/InfluxDBConnector.py | 10 +++++----- .../src/runtime/utilities/Utilities.py | 14 ++++++++++++-- 5 files changed, 23 insertions(+), 11 deletions(-) diff --git a/exponential-smoothing-predictor/src/r_predictors/prediction_configuration.properties b/exponential-smoothing-predictor/src/r_predictors/prediction_configuration.properties index 9cb3f0d..d24ac5c 100644 --- a/exponential-smoothing-predictor/src/r_predictors/prediction_configuration.properties +++ b/exponential-smoothing-predictor/src/r_predictors/prediction_configuration.properties @@ -1,4 +1,4 @@ -#Tue May 14 15:38:04 UTC 2024 +#Thu May 16 12:31:21 UTC 2024 APP_NAME=default_application METHOD=exponential_smoothing INFLUXDB_HOSTNAME=nebulous-influxdb diff --git a/exponential-smoothing-predictor/src/runtime/Predictor.py b/exponential-smoothing-predictor/src/runtime/Predictor.py index 7eff47c..f2c18e3 100644 --- a/exponential-smoothing-predictor/src/runtime/Predictor.py +++ b/exponential-smoothing-predictor/src/runtime/Predictor.py @@ -60,7 +60,7 @@ def sanitize_prediction_statistics(prediction_confidence_interval, prediction_va confidence_interval_modified = True if confidence_interval_modified: new_prediction_confidence_interval = str(lower_value_prediction_confidence_interval)+","+str(upper_value_prediction_confidence_interval) - print_with_time("The confidence interval "+prediction_confidence_interval+"was modified, becoming "+str(new_prediction_confidence_interval)+", taking into account the values of the metric") + print_with_time("The confidence interval "+prediction_confidence_interval+" was modified, becoming "+str(new_prediction_confidence_interval)+", taking into account the values of the metric") if (prediction_value range(start:-'+time_interval_to_get_data_for+') |> filter(fn: (r) => r["_measurement"] == "'+metric_name+'")' influx_connector = InfluxDBConnector() - print("performing query for application with bucket "+str(self.influxdb_bucket)) + logging.info("performing query for application with bucket "+str(self.influxdb_bucket)) + logging.info("The body of the query is "+query_string) + logging.info("The configuration of the client is "+Utilities.get_fields_and_values(influx_connector)) current_time = time.time() result = influx_connector.client.query_api().query(query_string, EsPredictorState.influxdb_organization) elapsed_time = time.time()-current_time - print("performed query, it took "+str(elapsed_time) + " seconds") + logging.info("performed query, it took "+str(elapsed_time) + " seconds") #print(result.to_values()) with open(self.get_prediction_data_filename(EsPredictorState.configuration_file_location, metric_name), 'w') as file: for table in result: diff --git a/exponential-smoothing-predictor/src/runtime/utilities/InfluxDBConnector.py b/exponential-smoothing-predictor/src/runtime/utilities/InfluxDBConnector.py index 34b65a4..20affb6 100644 --- a/exponential-smoothing-predictor/src/runtime/utilities/InfluxDBConnector.py +++ b/exponential-smoothing-predictor/src/runtime/utilities/InfluxDBConnector.py @@ -1,7 +1,7 @@ from influxdb_client import InfluxDBClient, Point, WritePrecision from datetime import datetime from influxdb_client.client.write_api import SYNCHRONOUS - +import logging from runtime.operational_status.EsPredictorState import EsPredictorState #import influxdb_client, os, time @@ -40,11 +40,11 @@ class InfluxDBConnector: - client = InfluxDBClient(url="http://" + EsPredictorState.influxdb_hostname + ":" + str(EsPredictorState.influxdb_port), token=EsPredictorState.influxdb_token, org=EsPredictorState.influxdb_organization) - write_api = client.write_api(write_options=SYNCHRONOUS) - def InfluxDBConnector(self): - pass + def __init__(self): + self.client = InfluxDBClient(url="http://" + EsPredictorState.influxdb_hostname + ":" + str(EsPredictorState.influxdb_port), token=EsPredictorState.influxdb_token, org=EsPredictorState.influxdb_organization) + self.write_api = self.client.write_api(write_options=SYNCHRONOUS) + logging.info("Successfully created InfluxDB connector, client is "+str(self.client)) def write_data(self,data,bucket): self.write_api.write(bucket=bucket, org=EsPredictorState.influxdb_organization, record=data, write_precision=WritePrecision.S) diff --git a/exponential-smoothing-predictor/src/runtime/utilities/Utilities.py b/exponential-smoothing-predictor/src/runtime/utilities/Utilities.py index 5b2d02c..7292859 100644 --- a/exponential-smoothing-predictor/src/runtime/utilities/Utilities.py +++ b/exponential-smoothing-predictor/src/runtime/utilities/Utilities.py @@ -8,7 +8,7 @@ #from morphemic.dataset import DatasetMaker import datetime import logging,os -from dateutil import parser +import json from influxdb_client import InfluxDBClient from runtime.operational_status.EsPredictorState import EsPredictorState @@ -42,7 +42,7 @@ def load_configuration(): EsPredictorState.influxdb_password = EsPredictorState.configuration_details.get("INFLUXDB_PASSWORD").data EsPredictorState.influxdb_org = EsPredictorState.configuration_details.get("INFLUXDB_ORG").data #This method accesses influx db to retrieve the most recent metric values. - + Utilities.print_with_time("The configuration effective currently is the following\n "+Utilities.get_fields_and_values(EsPredictorState)) @staticmethod def update_influxdb_organization_id(): @@ -63,3 +63,13 @@ def fix_path_ending(path): return path else: return path + os.sep + + @staticmethod + def default_to_string(obj): + return str(obj) + @classmethod + def get_fields_and_values(cls,object): + #Returns those fields that do not start with __ (and their values) + fields_values = {key: value for key, value in object.__dict__.items() if not key.startswith("__")} + return json.dumps(fields_values,indent=4,default=cls.default_to_string) +