Release 0.0.2
[ric-app/ad.git] / ad / database.py
1 # ==================================================================================
2 #  Copyright (c) 2020 HCL Technologies Limited.
3 #
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
7 #
8 #     http://www.apache.org/licenses/LICENSE-2.0
9 #
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 # ==================================================================================
16
17 from influxdb import DataFrameClient
18 import pandas as pd
19
20
21 class Error(Exception):
22     """Base class for other exceptions"""
23     pass
24
25
26 class NoDataError(Error):
27     """Raised when there is no data available in database for a given measurment"""
28     pass
29
30
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.
34
35
36     Parameters
37     ----------
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')
43         user to connect
44     password: str (default='root')
45         password of the use
46
47     Attributes
48     ----------
49     client: influxDB client
50         DataFrameClient api to connect influxDB
51     data: DataFrame
52         fetched data from database
53     """
54
55     def __init__(self, dbname, user='root', password='root', host="r4-influxdb.ricplt", port='8086'):
56         self.data = None
57         self.client = DataFrameClient(host, port, user, password, dbname)
58
59     def read_data(self, meas, limit=100000):
60         """Read data method for a given measurement and limit
61
62         Parameters
63         ----------
64         meas: str (default='ueMeasReport')
65         limit:int (defualt=100000)
66         """
67
68         result = self.client.query('select * from ' + meas + ' limit ' + str(limit))
69         print("Querying data : " + meas + " : size - " + str(len(result[meas])))
70         try:
71             if len(result[meas]) != 0:
72                 self.data = result[meas]
73                 self.data['measTimeStampRf'] = self.data.index
74             else:
75                 raise NoDataError
76
77         except NoDataError:
78             print('Data not found for ' + meas + ' vnf')
79
80     def write_anomaly(self, df, meas='AD'):
81         """Write data method for a given measurement
82
83         Parameters
84         ----------
85         meas: str (default='AD')
86         """
87         self.client.write_points(df, meas)
88
89
90 class DUMMY:
91
92     def __init__(self):
93         self.ue = pd.read_csv('ad/valid.csv')
94         self.data = None
95
96     def read_data(self, meas='ueMeasReport', limit=100000):
97         if meas == 'valid':
98             self.data = self.ue.head(limit)
99         else:
100             self.data = self.ue.head(limit).drop('Anomaly', axis=1)
101
102     def write_anomaly(self, df, meas_name='QP'):
103         pass