[RICAPP-181] Update Prediction message (from QP to TS) with serving cell prediciton
[ric-app/qp.git] / qp / database.py
1 # ==================================================================================
2 #       Copyright (c) 2020 AT&T Intellectual Property.
3 #       Copyright (c) 2020 HCL Technologies Limited.
4 #
5 #   Licensed under the Apache License, Version 2.0 (the "License");
6 #   you may not use this file except in compliance with the License.
7 #   You may obtain a copy of the License at
8 #
9 #          http://www.apache.org/licenses/LICENSE-2.0
10 #
11 #   Unless required by applicable law or agreed to in writing, software
12 #   distributed under the License is distributed on an "AS IS" BASIS,
13 #   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 #   See the License for the specific language governing permissions and
15 #   limitations under the License.
16 # ==================================================================================
17 from influxdb import DataFrameClient
18 import pandas as pd
19 import datetime
20
21
22 class NoDataError(Exception):
23     pass
24
25
26 class DATABASE(object):
27
28     def __init__(self, dbname, user='root', password='root', host='r4-influxdb.ricplt', port='8086'):
29         self.client = DataFrameClient(host, port, user, password, dbname)
30         self.data = None
31
32     def read_data(self, meas='ueMeasReport', limit=100000, cellid=False, ueid=False):
33         query = """select * from """ + meas
34
35         if cellid:
36             query += " where nrCellIdentity= '" + cellid + "'"
37
38         if ueid:
39             query += """ where "ue-id"  = \'{}\'""".format(ueid)
40         query += "  ORDER BY DESC LIMIT " + str(limit)
41         result = self.client.query(query)
42         try:
43             if len(result) != 0:
44                 # print("Querying data : " + meas + " : size - " + str(len(result[meas])))
45                 self.data = result[meas]
46                 self.data['measTimeStampRf'] = self.data.index
47             else:
48                 raise NoDataError
49
50         except NoDataError:
51             if cellid:
52                 print('Data not found for ' + meas + ' CellID : '+cellid)
53             elif ueid:
54                 print('Data not found for ' + meas + ' UEID : '+ueid)
55             else:
56                 print('Data not found for ' + meas)
57             pass
58
59     def write_prediction(self, df, meas_name='QP'):
60         df.index = pd.date_range(start=datetime.datetime.now(), freq='10ms', periods=len(df))
61         self.client.write_points(df, meas_name)
62
63
64 class DUMMY:
65
66     def __init__(self):
67         self.ue = pd.DataFrame([[1002, "c2/B13", 8, 69, 65, 113, 0.1, 0.1, "Waiting passenger 9", -882, -959, pd.to_datetime("2021-05-12T07:43:51.652")]], columns=["du-id", "nbCellIdentity", "prb_usage", "rsrp", "rsrq", "rssinr", "throughput", "targetTput", "ue-id", "x", "y", "measTimeStampRf"])
68         self.cell = pd.read_csv('qp/dummy.csv')
69         self.data = None
70
71     def read_data(self, meas='ueMeasReport', limit=100000, cellid=False, ueid=False):
72         if ueid:
73             self.data = self.ue.head(limit)
74         if cellid:
75             self.data = self.cell.head(limit)
76
77     def write_prediction(self, df, meas_name='QP'):
78         pass