def get_parts(): Session = sessionmaker(bind=db.engine) session = Session() data = "" try: res = cache.get('all_active_parts') if not res: res = session.execute("SELECT parts.id, supplier.name, locations.name, parts.quantity, parts.name FROM parts, locations, supplier WHERE parts.location = locations.id and parts.supplier = supplier.id and parts.active = 1") res = res.fetchall() cache.set('all_active_parts', res, 0) else: print("Found in cache") for row in res: print(row) data = data + '{"name": "' + str(row[4]) + '","location": "'+ str(row[2]) +'", "quantity": "'+ str(row[3]) +'", "id": ' + str(row[0]) + \ ', "supplier": "'+ str(row[1]) +'" },' except: session.close() return json.dumps({'success': False}), 500, {'ContentType': 'application/json'} result = '{"data": [' + data[:-1] + ']}' print(result) session.close() return json.loads(result)
def supplier_get(): Session = sessionmaker(bind=db.engine) session = Session() data = json.loads(request.form['sendData']) id = data['id'] data = "" key = "part_"+str(id)+"_" try: res = cache.get(key) if not res: res = session.execute("SELECT parts.name, parts.quantity, parts.id from parts where parts.id = :id and parts.active = 1", {'id': id}) res = res.fetchall() cache.set(key, res, 0) else: print("Found in cache") for row in res: data = data + '{"name": "' + str(row[0]) + '","quantity": "'+ str(row[1]) +'", "id": "'+ str(row[2]) +'" },' except: session.close() return json.dumps({'success': False}), 500, {'ContentType': 'application/json'} result = data[:-1] print(result) session.close() return json.loads(result)
def dash_data(): Session = sessionmaker(bind=db.engine) session = Session() data = "" try: res = cache.get('in_service') if not res: res = session.execute( "SELECT allparts.id, DATE_FORMAT(allparts.create, '%M %D %Y'), parts.name, equipment.name, allparts.quantity FROM allparts, parts, equipment WHERE allparts.installed_in = equipment.id and allparts.part = parts.id and allparts.active = 1" ) res = res.fetchall() cache.set('in_service', res, 0) else: print("Found in cache") for row in res: print(row) data = data + '{"name": "' + str(row[2]) + '","installed": "'+ str(row[3]) +'", "create": "'+ str(row[1]) +'", "id": ' + str(row[0]) + \ ', "life": "N/A", "quantity": "'+str(row[4])+'" },' except: session.close() return json.dumps({'success': False}), 500, { 'ContentType': 'application/json' } result = '{"data": [' + data[:-1] + ']}' print(result) session.close() return json.loads(result)
def supplier_get(): Session = sessionmaker(bind=db.engine) session = Session() data = json.loads(request.form['sendData']) id = data['id'] data = "" key = "supplier_" + str(id) try: res = cache.get(key) if not res: res = session.execute( "SELECT supplier.name, supplier.email, supplier.phone, supplier.id from supplier where supplier.active = 1 and supplier.id = :id", {'id': id}) res = res.fetchall() cache.set(key, res, 0) else: print("Found in cache") for row in res: data = data + '{"name": "' + str(row[0]) + '","email": "' + str( row[1]) + '", "phone": "' + str(row[2]) + '", "id": ' + str( row[3]) + ' },' except: session.close() json.dumps({'success': False}), 500, { 'ContentType': 'application/json' } result = data[:-1] print(result) session.close() return json.loads(result)
def supplier_data(): Session = sessionmaker(bind=db.engine) session = Session() data = "" try: res = cache.get('all_suppliers') if not res: res = session.execute( "select supplier.name, supplier.email, supplier.phone, supplier.id from supplier where supplier.active = 1" ) res = res.fetchall() cache.set('all_suppliers', res, 0) else: print("Found in cache") for row in res: data = data + '{"name": "' + str(row[0]) + '","email": "' + str( row[1]) + '", "phone": "' + str(row[2]) + '", "id": ' + str( row[3]) + ' },' except: session.close() json.dumps({'success': False}), 500, { 'ContentType': 'application/json' } result = '{"data": [' + data[:-1] + ']}' print(result) session.close() return json.loads(result)
def initiate_app_layout(): """ Initiates the entire app layout. Returns ------- The app layout to be served. """ # Set the title print('Setting the title') title = make_navbar_title() # Set the layout print('Setting the layout') app_layout_style = { "position": "fixed", "max-width": "100vw", "min-width": "100vw", "max-height": "100vh", "min-height": "100vh", "background-image": "radial-gradient(#697582, #383F49,#383F49)", } # Get the data df = get_covid_19_data() if config.use_cache: print('Setting to cache') cache.set("covid-19-data", df) else: pass # Perform a groupby print('Performing a groupby') df_groupby = dashboard_connector.DashboardGraphs.group_by_data( df=df, group_by_col="location") # Remove the continents print('Removing the continents from the groupby dataframe') df_groupby = df_groupby[~df_groupby["location"].isin(config.continents)] # Make death rate data print('Making death rate data') df_group_by_death_rate = dashboard_connector.DashboardGraphs.create_death_rate_data( group_by_df=df_groupby) # Make a cases by country chart print('Making a cases by country chart') cases_by_country_chart = make_horizontal_bar_chart( data=df_groupby.sort_values(by=["total_cases"], ascending=False).head(10), x_col="total_cases", y_col="location", title="Cases by country", desc="Which countries have recorded the most cases so far", ) # Make a deaths by country chart print('Making a deaths by country chart') deaths_by_country_chart = make_horizontal_bar_chart( data=df_groupby.sort_values(by=["total_deaths"], ascending=False).head(10), x_col="total_deaths", y_col="location", title="Deaths by country", desc="Which countries have recorded the most deaths", ) # Make a death rate by country chart print('Making a death rate by country chart') death_rate_by_country_chart = make_horizontal_bar_chart( data=df_group_by_death_rate.sort_values(by=["death_rate"], ascending=False).head(10), x_col="death_rate", y_col="location", title="Death rate by country", desc="Which countries have the greatest death rates (%)", ) # Create a horizontal charts row print('Creating a horizontal charts row') horizontal_chart_row = html.Div( dbc.Row(children=[ dbc.Col( [ dcc.Graph(id="cases-by-country-graph", figure=cases_by_country_chart) ], width=4, ), dbc.Col( dcc.Graph(id="deaths-by-country-graph", figure=deaths_by_country_chart), width=4, ), dbc.Col( [ dcc.Graph( id="death-rate-by-country-graph", figure=death_rate_by_country_chart, ), html.Div("Death Rate = Number of deaths/number of cases"), ], width=4, ), ])) # Make a time series dataframe print('Making a time series dataframe') df_time_series = dashboard_connector.DashboardGraphs.create_time_series_data( df=df) if config.use_cache: print('Setting the time series dataframe to cache') cache.set("original-time-series-data", df_time_series) else: pass # Make a time series chart print('Making a time series chart') time_series_chart = make_time_series_chart( data=df_time_series, x_col="date", y_cols=["new_cases", "new_deaths"], title="Cases and deaths by country", desc="Time series of Covid-19 cases", ) # Make time series dropdown options print('Making the time series dropdown options') options = dashboard_connector.DashboardGraphs.create_dropdown_options( df=df) # Make the time series dropdown print('Creating the time series dropdown') dropdown = make_time_series_dropdown(options=options) print('Making a time series row') time_series_row = html.Div( dbc.Row(children=[ dbc.Col( html.Div( dcc.Graph(id="time-series-chart", figure=time_series_chart)), width=10, id="time-series-chart-area", ), dbc.Col([make_break(), make_break(), dropdown]), ])) # Make a world map of cases & deaths print('Making a world map of cases') df_groupby_choropleth_cases = dashboard_connector.DashboardGraphs.create_choropleth_data( df=df, col='total_cases') print('Making a world map of deaths') df_groupby_choropleth_deaths = dashboard_connector.DashboardGraphs.create_choropleth_data( df=df, col='total_deaths') cases_world_map = make_choropleth_map(data=df_groupby_choropleth_cases, country_code_col='country_code', country_col='location', display_col='total_cases', title='Map of cases', desc='', colour='Blue') deaths_world_map = make_choropleth_map(data=df_groupby_choropleth_deaths, country_code_col='country_code', country_col='location', display_col='total_deaths', title='Map of deaths', desc='', colour='Red') # Make a world map row print('Making a world map row') world_map_row = html.Div( dbc.Row(children=[ dbc.Col([ html.Div(dcc.Graph(id='map-of-cases', figure=cases_world_map)) ]), dbc.Col([ html.Div(dcc.Graph(id='map-of-deaths', figure=deaths_world_map)) ]) ])) # Set the layout print('Setting the app layout') app_layout = html.Div(children=[ title, horizontal_chart_row, make_break(), time_series_row, world_map_row ]) return app_layout