mirror of
https://github.com/Telecominfraproject/wlan-lanforge-scripts.git
synced 2025-11-02 19:58:03 +00:00
kpi_csv_sq.py : updates from lf_dash.py
Signed-off-by: Chuck SmileyRekiere <chuck.smileyrekiere@candelatech.com>
This commit is contained in:
@@ -1,7 +1,10 @@
|
||||
#!/usr/bin/env python3
|
||||
'''
|
||||
File: read kpi.csv place in sql database, create png of historical kpi and present graph on dashboard
|
||||
Usage: kpi_csv_sq.py --path <path to directories to traverse> --database <name of database> --show --png --store
|
||||
Usage: kpi_csv_sq.py --store --png --show --path <path to directories to traverse> --database <name of database>
|
||||
Example: kpi_csv_sq.py --show (show dashboard generated from database)
|
||||
Example: kpi_csv_sq.py --store --png --show --path <path to read kpi.csv> (read kpi.csv store to database, write png, show dashboard )
|
||||
|
||||
'''
|
||||
# visit http://127.0.0.1:8050/ in your web browser.
|
||||
|
||||
@@ -30,6 +33,7 @@ class csv_sqlite_dash():
|
||||
self.database = _database
|
||||
self.table = _table
|
||||
self.png = _png
|
||||
self.png_generated = False
|
||||
self.kpi_list = []
|
||||
self.html_list = []
|
||||
self.conn = None
|
||||
@@ -42,8 +46,8 @@ class csv_sqlite_dash():
|
||||
self.app = dash.Dash(__name__, external_stylesheets=external_stylesheets)
|
||||
# https://community.plotly.com/t/putting-a-dash-instance-inside-a-class/6097/3
|
||||
#https://dash.plotly.com/dash-html-components/button
|
||||
self.app.callback(dash.dependencies.Output('container-button-basic', 'children'),
|
||||
[dash.dependencies.Input(component_id ='submit-val', component_property ='n_clicks')])(self.show)
|
||||
#self.app.callback(dash.dependencies.Output('container-button-basic', 'children'),
|
||||
# [dash.dependencies.Input(component_id ='submit-val', component_property ='n_clicks')])(self.show)
|
||||
|
||||
# information on sqlite database
|
||||
# https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.to_sql.html
|
||||
@@ -53,7 +57,7 @@ class csv_sqlite_dash():
|
||||
self.kpi_list = list(path.glob('**/kpi.csv')) # Hard code for now
|
||||
|
||||
if not self.kpi_list:
|
||||
print("no new kpi.csv found, check input paths")
|
||||
print("WARNING: used --store , no new kpi.csv found, check input path or remove --store from command line")
|
||||
|
||||
for kpi in self.kpi_list: #TODO note empty kpi.csv failed test
|
||||
df_kpi_tmp = pd.read_csv(kpi, sep='\t')
|
||||
@@ -62,7 +66,11 @@ class csv_sqlite_dash():
|
||||
self.df = self.df.append(df_kpi_tmp, ignore_index=True)
|
||||
|
||||
self.conn = sqlite3.connect(self.database)
|
||||
self.df.to_sql(self.table,self.conn,if_exists='append')
|
||||
try:
|
||||
self.df.to_sql(self.table,self.conn,if_exists='append')
|
||||
except:
|
||||
print("attempt to append to database with different column layout, casused exception, input new name --database <new name>")
|
||||
exit(1)
|
||||
self.conn.close()
|
||||
|
||||
def generate_graph_png(self):
|
||||
@@ -72,7 +80,11 @@ class csv_sqlite_dash():
|
||||
self.conn = sqlite3.connect(self.database)
|
||||
df3 = pd.read_sql_query("SELECT * from {}".format(self.table) ,self.conn) #current connection is sqlite3 /TODO move to SQLAlchemy
|
||||
# sort by date column
|
||||
df3 = df3.sort_values(by='Date')
|
||||
try:
|
||||
df3 = df3.sort_values(by='Date')
|
||||
except:
|
||||
print("Database empty: KeyError(key) when sorting by Date, check Database name, path to kpi, typo in path, exiting")
|
||||
exit(1)
|
||||
self.conn.close()
|
||||
|
||||
# graph group and test-tag are used for detemining the graphs, can use any columns
|
||||
@@ -107,16 +119,22 @@ class csv_sqlite_dash():
|
||||
yaxis_title="{}".format(units_list[-1]),
|
||||
xaxis = {'type' : 'date'}
|
||||
)
|
||||
# save the figure - figures will be over written
|
||||
# save the figure - figures will be over written png
|
||||
if self.png:
|
||||
print("kpi_path:{}".format(df_tmp['kpi_path']))
|
||||
png_path = os.path.join(kpi_path_list[-1],"{}_{}_{}_{}_kpi.png".format(test_id_list[-1], group, test_tag, test_rig))
|
||||
print("png_path {}".format(png_path))
|
||||
kpi_fig.write_image(png_path,scale=1,width=1200,height=350)
|
||||
if self.png_generated:
|
||||
pass
|
||||
else:
|
||||
self.png_generated = True
|
||||
print("generating png files")
|
||||
print("kpi_path:{}".format(df_tmp['kpi_path']))
|
||||
png_path = os.path.join(kpi_path_list[-1],"{}_{}_{}_{}_kpi.png".format(test_id_list[-1], group, test_tag, test_rig))
|
||||
print("png_path {}".format(png_path))
|
||||
kpi_fig.write_image(png_path,scale=1,width=1200,height=350)
|
||||
|
||||
# use image from above to creat html display
|
||||
self.children_div.append(dcc.Graph(figure=kpi_fig))
|
||||
|
||||
|
||||
#TODO the link must be to a server to display html
|
||||
# WARNING: DO NOT USE os.path.join will use the path for where the script is RUN which can be container.
|
||||
# need to construct path to server manually.
|
||||
@@ -132,8 +150,9 @@ class csv_sqlite_dash():
|
||||
|
||||
# access from server
|
||||
# https://stackoverflow.com/questions/61678129/how-to-access-a-plotly-dash-app-server-via-lan
|
||||
def show(self,n_clicks):
|
||||
print("refreshes: {}".format(n_clicks))
|
||||
#def show(self,n_clicks):
|
||||
def show(self):
|
||||
#print("refreshes: {}".format(n_clicks))
|
||||
self.generate_graph_png()
|
||||
if not self.children_div:
|
||||
print("NOTE: test-tag may not be present in the kpi thus no results generated")
|
||||
@@ -141,16 +160,20 @@ class csv_sqlite_dash():
|
||||
html.Div(id='my-output'),
|
||||
html.H1(children= "LANforge Testing",className="lanforge",
|
||||
style={'color':'green','text-align':'center'}),
|
||||
html.Button('Submit Recalculate',id='submit-val', n_clicks=0),
|
||||
html.Div(id='container-button-basic', children='to recalculate hit submit'),
|
||||
#html.Button('Submit Recalculate',id='submit-val', n_clicks=0),
|
||||
#html.Div(id='container-button-basic', children='to recalculate hit submit'),
|
||||
html.H2(children= "Results",className="ts1",
|
||||
style={'color':'#00361c','text-align':'left'}),
|
||||
# images_div is already a list, children = a list of html components
|
||||
html.Div(children= self.children_div, style={"maxHeight": "540px", "overflow": "scroll"} ),
|
||||
# remove scrolling : html.Div(children= self.children_div, style={"maxHeight": "600px", "overflow": "scroll"} ),
|
||||
html.Div(children= self.children_div ),
|
||||
html.A('www.candelatech.com',href='http://www.candelatech.com', target='_blank',
|
||||
style={'color':'#00361c','text-align':'left'}),
|
||||
])
|
||||
|
||||
# save as standalone files
|
||||
#https://plotly.com/python/static-image-export/
|
||||
|
||||
if self.server_started:
|
||||
print("refresh complete")
|
||||
pass
|
||||
@@ -173,16 +196,18 @@ def main():
|
||||
''',
|
||||
description='''\
|
||||
File: read kpi.csv place in sql database, create png of historical kpi and present graph on dashboard
|
||||
Usage: kpi_csv_sq.py --path <path to directories to traverse> --database <name of database> --show --png --store
|
||||
Usage: kpi_csv_sq.py --store --png --show --path <path to directories to traverse> --database <name of database>
|
||||
Example: kpi_csv_sq.py --show (show dashboard generated from database)
|
||||
Example: kpi_csv_sq.py --store --png --show --path <path to read kpi.csv> (read kpi.csv store to database, write png, show dashboard )
|
||||
|
||||
''')
|
||||
parser.add_argument('--path', help='--path top directory path to kpi',default='')
|
||||
parser.add_argument('--file', help='--file kpi.csv',default='kpi.csv') #TODO is this needed
|
||||
parser.add_argument('--database', help='--database qa_test_db',default='qa_test_db')
|
||||
parser.add_argument('--table', help='--table qa_table',default='qa_table')
|
||||
parser.add_argument('--store', help='--store , store kpi to db',action='store_true')
|
||||
parser.add_argument('--png', help='--png, generate png for kpi in path, generate display',action='store_true')
|
||||
parser.add_argument('--show', help='--show generate display and show dashboard',action='store_true')
|
||||
parser.add_argument('--path', help='--path top directory path to kpi if regererating database or png files',default='')
|
||||
parser.add_argument('--file', help='--file kpi.csv default: kpi.csv',default='kpi.csv') #TODO is this needed
|
||||
parser.add_argument('--database', help='--database qa_test_db default: qa_test_db',default='qa_test_db')
|
||||
parser.add_argument('--table', help='--table qa_table default: qa_table',default='qa_table')
|
||||
parser.add_argument('--store', help='--store , store kpi to db, action store_true',action='store_true')
|
||||
parser.add_argument('--png', help='--png, generate png for kpi in path, generate display, action store_true',action='store_true')
|
||||
parser.add_argument('--show', help='--show generate display and show dashboard, action store_true',action='store_true')
|
||||
|
||||
args = parser.parse_args()
|
||||
|
||||
@@ -193,13 +218,21 @@ Usage: kpi_csv_sq.py --path <path to directories to traverse> --database <name o
|
||||
__png = args.png
|
||||
|
||||
# needed for refresh button
|
||||
n_clicks = 0
|
||||
# n_clicks = 0
|
||||
|
||||
print("config: path:{} file:{} database:{} table:{} store:{} png:{} show:{} "
|
||||
.format(__path,__file,__database,__table,args.store, args.png,args.show))
|
||||
|
||||
if(__path == '' and args.store == True):
|
||||
print("--path <path of kpi.csv> must be entered if --store")
|
||||
print("--path <path of kpi.csv> must be entered if --store , exiting")
|
||||
exit(1)
|
||||
|
||||
if(args.png == True and args.store == False):
|
||||
print("if --png set to create png files then --store must also be set, exiting")
|
||||
exit(1)
|
||||
|
||||
if(args.png == True and args.show == True):
|
||||
print("WARNING: generating png files will effect initial display performance")
|
||||
|
||||
csv_dash = csv_sqlite_dash(
|
||||
_path = __path,
|
||||
@@ -212,7 +245,8 @@ Usage: kpi_csv_sq.py --path <path to directories to traverse> --database <name o
|
||||
if args.png:
|
||||
csv_dash.generate_graph_png()
|
||||
if args.show:
|
||||
csv_dash.show(n_clicks)
|
||||
#csv_dash.show(n_clicks)
|
||||
csv_dash.show()
|
||||
|
||||
if args.store == False and args.png == False and args.show == False:
|
||||
print("Need to enter an action of --store --png --show ")
|
||||
|
||||
Reference in New Issue
Block a user