Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes v2 dev website dashboard not loading #1256

Merged
merged 19 commits into from
Jul 15, 2022
Merged
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 44 additions & 45 deletions server/dash/dashboards/neighborhood_recent.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
import dash_core_components as dcc
import dash_html_components as html
import dash_table
from dash.dependencies import Input, Output
import pandas as pd
import plotly.express as px
from flask import request
joshuayhwu marked this conversation as resolved.
Show resolved Hide resolved

from app import app, batch_get_data
from config import API_HOST
from dash.dependencies import Input, Output
from design import CONFIG_OPTIONS, DISCRETE_COLORS, DISCRETE_COLORS_MAP, LABELS, apply_figure_style
from flask import request
from design import CONFIG_OPTIONS, DISCRETE_COLORS, LABELS, apply_figure_style, DISCRETE_COLORS_MAP


pretty_columns = {
'srnumber': "SR Number",
Expand All @@ -22,8 +24,10 @@
'address': "Address"
}

start_date = datetime.date.today() - datetime.timedelta(days=7)
end_date = datetime.date.today() - datetime.timedelta(days=1)
START_DATE_DELTA = 7
END_DATE_DELTA = 1
start_date = datetime.date.today() - datetime.timedelta(days=START_DATE_DELTA)
end_date = datetime.date.today() - datetime.timedelta(days=END_DATE_DELTA)

# TITLE
title = "NEIGHBORHOOD WEEKLY REPORT"
Expand All @@ -44,10 +48,16 @@
selected_council = 'Arleta'

table_df = df.query(f"councilName == '{selected_council}'")[['srnumber', 'createdDate', 'closedDate', 'typeName', 'agencyName', 'sourceName', 'address']] # noqa
figure_df = df.query(f"councilName == '{selected_council}' and createdDate >= '{start_date}'").groupby(['createdDate', 'typeName'])['srnumber'].count().reset_index() # noqa

req_type_line_base_graph = px.line()
apply_figure_style(req_type_line_base_graph)

req_type_pie_base_graph = px.pie()
apply_figure_style(req_type_pie_base_graph)

# Populate the neighborhood dropdown


def populate_options():
council_df_path = '/councils'
council_df = pd.read_json(API_HOST + council_df_path)
Expand All @@ -60,39 +70,6 @@ def populate_options():
return values


fig = px.line(
figure_df,
x="createdDate",
y="srnumber",
color="typeName",
color_discrete_sequence=DISCRETE_COLORS,
color_discrete_map=DISCRETE_COLORS_MAP,
labels=LABELS,
)

fig.update_xaxes(
tickformat="%a\n%m/%d",
)

fig.update_traces(
mode='markers+lines'
) # add markers to lines

apply_figure_style(fig)

pie_fig = px.pie(
figure_df,
names="typeName",
values="srnumber",
color="typeName",
color_discrete_sequence=DISCRETE_COLORS,
color_discrete_map=DISCRETE_COLORS_MAP,
labels=LABELS,
hole=.3,
)
apply_figure_style(pie_fig)


# Layout
layout = html.Div([
html.H1(title),
Expand Down Expand Up @@ -120,11 +97,11 @@ def populate_options():
], className="graph-row"),
html.Div([
html.Div(
dcc.Graph(id='graph', figure=fig, config=CONFIG_OPTIONS),
dcc.Graph(id='graph', figure=req_type_line_base_graph, config=CONFIG_OPTIONS),
className="half-graph"
),
html.Div(
dcc.Graph(id='pie_graph', figure=pie_fig, config=CONFIG_OPTIONS),
dcc.Graph(id='pie_graph', figure=req_type_pie_base_graph, config=CONFIG_OPTIONS),
className="half-graph"
)
]),
Expand Down Expand Up @@ -162,6 +139,13 @@ def populate_options():
)
def update_table(selected_council):
table_df = df.query(f"councilName == '{selected_council}'")[['srnumber', 'createdDate', 'closedDate', 'typeName', 'agencyName', 'sourceName', 'address']] # noqa
# The following check is to ensure Dash graphs are populate with dummy data when query returns empty dataframe
joshuayhwu marked this conversation as resolved.
Show resolved Hide resolved
if table_df.shape[0] == 0:
joshuayhwu marked this conversation as resolved.
Show resolved Hide resolved
table_df = pd.DataFrame(columns=["Request Type"])
i = 0
for request_type in DISCRETE_COLORS_MAP:
joshuayhwu marked this conversation as resolved.
Show resolved Hide resolved
table_df.loc[i] = [request_type]
i += 1
return table_df.to_dict('records')


Expand All @@ -176,7 +160,11 @@ def update_table(selected_council):
def update_text(selected_council):
create_count = df.query(f"councilName == '{selected_council}' and createdDate >= '{start_date}'")['srnumber'].count() # noqa
close_count = df.query(f"councilName == '{selected_council}' and closedDate >= '{start_date}'")['srnumber'].count() # noqa
return create_count, close_count, create_count - close_count
# This check is to ensure data quality issues doesn't flow downstream to the dashboard (closed request exist without new request)
joshuayhwu marked this conversation as resolved.
Show resolved Hide resolved
if create_count == 0 and close_count > 0:
return 0, 0, 0
else:
return create_count, close_count, create_count - close_count


@app.callback(
Expand All @@ -185,8 +173,14 @@ def update_text(selected_council):
)
def update_figure(selected_council):
figure_df = df.query(f"councilName == '{selected_council}' and createdDate >= '{start_date}'").groupby(['createdDate', 'typeName'])['srnumber'].count().reset_index() # noqa
# The following check is to ensure Dash graphs are populate with dummy data when query returns empty dataframe
joshuayhwu marked this conversation as resolved.
Show resolved Hide resolved
if figure_df.shape[0] == 0:
joshuayhwu marked this conversation as resolved.
Show resolved Hide resolved
figure_df = pd.DataFrame(columns=["createdDate", "srnumber", "typeName"])
for j in range(START_DATE_DELTA):
for request_type in DISCRETE_COLORS_MAP:
figure_df.loc[figure_df.shape[0]] = [
joshuayhwu marked this conversation as resolved.
Show resolved Hide resolved
start_date + datetime.timedelta(days=j), 0, request_type]
figure_df.typeName = figure_df.typeName.map(lambda x: '<br>'.join(textwrap.wrap(x, width=16))) # noqa

fig = px.line(
figure_df,
x="createdDate",
Expand All @@ -209,12 +203,17 @@ def update_figure(selected_council):


@app.callback(
Output("pie_graph", "pie_fig"),
Output("pie_graph", "figure"),
Input("council_list", "value")
)
def update_council_figure(selected_council):
pie_df = df.query(f"councilName == '{selected_council}' and createdDate >= '{start_date}'").groupby(['typeName']).agg('count').reset_index() # noqa

if pie_df.shape[0] == 0:
pie_df = pd.DataFrame(columns=["srnumber", "typeName"])
i = 0
for request_type in DISCRETE_COLORS_MAP:
joshuayhwu marked this conversation as resolved.
Show resolved Hide resolved
pie_df.loc[i] = [0, request_type]
i += 1
pie_fig = px.pie(
pie_df,
names="typeName",
Expand Down