Release 1.0.2
[ric-app/ad.git] / src / insert.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 """
18 This Module is temporary for pushing data into influxdb before dpeloyment of AD xApp. It will depreciated in future, when data will be coming through KPIMON
19 """
20
21 import datetime
22 import time
23 import pandas as pd
24 from database import DATABASE
25 from configparser import ConfigParser
26
27
28 class INSERTDATA(DATABASE):
29
30     def __init__(self):
31         super().__init__()
32         self.config()
33         self.connect()
34 #        self.dropdb('RIC-Test')
35         self.createdb('RIC-Test')
36
37     def config(self):
38         cfg = ConfigParser()
39         cfg.read('ad_config.ini')
40         for section in cfg.sections():
41             if section == 'influxdb':
42                 self.host = cfg.get(section, "host")
43                 self.port = cfg.get(section, "port")
44                 self.user = cfg.get(section, "user")
45                 self.password = cfg.get(section, "password")
46                 self.path = cfg.get(section, "path")
47                 self.ssl = cfg.get(section, "ssl")
48                 self.dbname = cfg.get(section, "database")
49                 self.meas = cfg.get(section, "measurement")
50
51     def createdb(self, dbname):
52         if dbname not in self.client.get_list_database():
53             print("Create database: " + dbname)
54             self.client.create_database(dbname)
55             self.client.switch_database(dbname)
56
57     def dropdb(self, dbname):
58         if next((item for item in self.client.get_list_database() if item.get("name") == dbname), None) is not None:
59             print("DROP database: " + dbname)
60             self.client.drop_database(dbname)
61
62     def dropmeas(self, measname):
63         print("DROP MEASUREMENT: " + measname)
64         self.client.query('DROP MEASUREMENT '+measname)
65
66     def assign_timestamp(self, df):
67         steps = df['measTimeStampRf'].unique()
68         for timestamp in steps:
69             d = df[df['measTimeStampRf'] == timestamp]
70             d.index = pd.date_range(start=datetime.datetime.now(), freq='1ms', periods=len(d))
71             self.client.write_points(d, self.meas)
72             time.sleep(0.7)
73
74
75 def populatedb():
76     # inintiate connection and create database UEDATA
77     db = INSERTDATA()
78     df = pd.read_csv('ue.csv')
79     while True:
80         db.assign_timestamp(df)
81
82
83 if __name__ == "__main__":
84     populatedb()