Commit c2c8ba4e authored by Sergio Gonzalez Diaz's avatar Sergio Gonzalez Diaz
Browse files

Create monitoring table at initialization

parent b646c676
Loading
Loading
Loading
Loading
+17 −6
Original line number Diff line number Diff line
@@ -28,6 +28,7 @@ class MetricsDB():
    self.ilp_port=int(ilp_port)
    self.rest_port=rest_port
    self.table=table
    self.create_table()

  def write_KPI(self,time,kpi_id,kpi_sample_type,device_id,endpoint_id,service_id,kpi_value):
    self.socket.connect((self.host,self.ilp_port))
@@ -47,9 +48,19 @@ class MetricsDB():
  def run_query(self, sql_query):
    query_params = {'query': sql_query, 'fmt' : 'json'}
    url = f"http://{self.host}:{self.rest_port}/exec"
    try:
    response = requests.get(url, params=query_params)
    json_response = json.loads(response.text)
        LOGGER.info(json_response)
    except requests.exceptions.RequestException as e:
        LOGGER.info(f'Error: {e}', file=sys.stderr)
    LOGGER.info(f"Query executed, result:{json_response}")
  
  def create_table(self):
    query = f'CREATE TABLE IF NOT EXISTS {self.table}'\
    '(kpi_id SYMBOL,'\
    'kpi_sample_type SYMBOL,'\
    'device_id SYMBOL,'\
    'endpoint_id SYMBOL,'\
    'service_id SYMBOL,'\
    'timestamp TIMESTAMP,'\
    'kpi_value DOUBLE)'\
    'TIMESTAMP(timestamp);'
    self.run_query(query)
    LOGGER.info(f"Table {self.table} created")