1 # ==================================================================================
2 # Copyright (c) 2020 HCL Technologies Limited.
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 # ==================================================================================
17 from influxdb import DataFrameClient
21 class Error(Exception):
22 """Base class for other exceptions"""
26 class NoDataError(Error):
27 """Raised when there is no data available in database for a given measurment"""
31 class DATABASE(object):
32 r""" DATABASE takes an input as database name. It creates a client connection
33 to influxDB and It reads/ writes UE data for a given dabtabase and a measurement.
38 host: str (default='r4-influxdb.ricplt.svc.cluster.local')
39 hostname to connect to InfluxDB
40 port: int (default='8086')
41 port to connect to InfluxDB
42 username: str (default='root')
44 password: str (default='root')
49 client: influxDB client
50 DataFrameClient api to connect influxDB
52 fetched data from database
55 def __init__(self, dbname, user='root', password='root', host="r4-influxdb.ricplt", port='8086'):
57 self.client = DataFrameClient(host, port, user, password, dbname)
59 def read_data(self, meas, limit=100000):
60 """Read data method for a given measurement and limit
64 meas: str (default='ueMeasReport')
65 limit:int (defualt=100000)
68 result = self.client.query('select * from ' + meas + ' limit ' + str(limit))
69 print("Querying data : " + meas + " : size - " + str(len(result[meas])))
71 if len(result[meas]) != 0:
72 self.data = result[meas]
73 self.data['measTimeStampRf'] = self.data.index
78 print('Data not found for ' + meas + ' vnf')
80 def write_anomaly(self, df, meas='AD'):
81 """Write data method for a given measurement
85 meas: str (default='AD')
87 self.client.write_points(df, meas)
93 self.ue = pd.read_csv('ad/valid.csv')
96 def read_data(self, meas='ueMeasReport', limit=100000):
98 self.data = self.ue.head(limit)
100 self.data = self.ue.head(limit).drop('Anomaly', axis=1)
102 def write_anomaly(self, df, meas_name='QP'):