adjusted file with gemini
This commit is contained in:
@ -4,7 +4,7 @@ from datetime import datetime
|
||||
import os
|
||||
import dash_bootstrap_components as dbc
|
||||
|
||||
from ..data.loader import DataSchema
|
||||
from ..data.loader_gz import MTBFSchema
|
||||
from . import ids
|
||||
|
||||
def render(app: Dash, data: pd.DataFrame) -> html.Div:
|
||||
@ -13,33 +13,48 @@ def render(app: Dash, data: pd.DataFrame) -> html.Div:
|
||||
[
|
||||
Input(ids.YEAR_DROPDOWN, "value"),
|
||||
Input(ids.WEEK_DROPDOWN, "value"),
|
||||
Input(ids.CATEGORY_DROPDOWN, "value"),
|
||||
Input(ids.HITS_SELECTOR, "value"),
|
||||
Input(ids.SINGLE_HITTER_FILTER, "value"),
|
||||
],
|
||||
)
|
||||
def update_data_table(
|
||||
years: list[str], weeks: list[str], categories: list[str]
|
||||
years: list[str], weeks: list[str], hits_type: str, single_hitter_filter: list[str]
|
||||
) -> html.Div:
|
||||
filtered_data = data.query(
|
||||
"year in @years and week in @weeks and category in @categories"
|
||||
"year in @years and week in @weeks"
|
||||
)
|
||||
|
||||
if filtered_data.shape[0] == 0:
|
||||
return html.Div("No data selected.")
|
||||
|
||||
pt = filtered_data.pivot_table(
|
||||
values=DataSchema.AMOUNT,
|
||||
index=[DataSchema.CATEGORY],
|
||||
aggfunc="sum",
|
||||
fill_value=0,
|
||||
dropna=False,
|
||||
).reset_index().sort_values(DataSchema.AMOUNT, ascending=False)
|
||||
if hits_type == "p-hits":
|
||||
hits_col = MTBFSchema.P_HITS
|
||||
else:
|
||||
hits_col = MTBFSchema.U_HITS
|
||||
|
||||
columns = [{"name": i.capitalize(), "id": i} for i in pt.columns]
|
||||
# Count 'Y' values
|
||||
hits_data = filtered_data[filtered_data[hits_col] == 'Y']
|
||||
|
||||
table_data = hits_data.groupby(MTBFSchema.AIR).agg(
|
||||
count=(hits_col, 'size'),
|
||||
air_issue_description=(MTBFSchema.AIR_ISSUE_DESCRIPTION, lambda x: ', '.join(x.unique())),
|
||||
close_notes=(MTBFSchema.CLOSE_NOTES, lambda x: ', '.join(x.unique()))
|
||||
).reset_index()
|
||||
|
||||
if 'remove_single' in single_hitter_filter:
|
||||
table_data = table_data[table_data['count'] > 1]
|
||||
else:
|
||||
table_data = table_data[table_data['count'] == 1]
|
||||
|
||||
table_data = table_data.sort_values('count', ascending=False)
|
||||
|
||||
# Reorder columns
|
||||
table_data = table_data[[MTBFSchema.AIR, 'air_issue_description', 'close_notes', 'count']]
|
||||
|
||||
return dash_table.DataTable(
|
||||
id=ids.CATEGORY_TABLE,
|
||||
data=pt.to_dict("records"),
|
||||
columns=columns,
|
||||
id=ids.CATEGORY_TABLE, # Using this ID for feedback callbacks
|
||||
data=table_data.to_dict("records"),
|
||||
columns=[{"name": i, "id": i} for i in table_data.columns],
|
||||
page_size=10,
|
||||
row_selectable='single',
|
||||
selected_rows=[]
|
||||
)
|
||||
@ -54,7 +69,7 @@ def render(app: Dash, data: pd.DataFrame) -> html.Div:
|
||||
State(ids.CATEGORY_TABLE, "data"),
|
||||
State(ids.FEEDBACK_INPUT, "value"),
|
||||
)
|
||||
def handle_feedback_modal(selected_rows, save_clicks, close_clicks, is_open, data, comment):
|
||||
def handle_feedback_modal(selected_rows, save_clicks, close_clicks, is_open, table_data, comment):
|
||||
ctx = callback_context
|
||||
if not ctx.triggered:
|
||||
return False, ""
|
||||
@ -65,12 +80,12 @@ def render(app: Dash, data: pd.DataFrame) -> html.Div:
|
||||
return True, ""
|
||||
|
||||
if triggered_id == ids.SAVE_FEEDBACK_BUTTON_POPUP and selected_rows:
|
||||
selected_row_data = data[selected_rows[0]]
|
||||
category = selected_row_data[DataSchema.CATEGORY]
|
||||
selected_row_data = table_data[selected_rows[0]]
|
||||
air_value = selected_row_data[MTBFSchema.AIR]
|
||||
timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
|
||||
if not comment:
|
||||
comment = ""
|
||||
feedback_data = f'{timestamp},{category},"{comment}"\n'
|
||||
feedback_data = f'{timestamp},dashboard_table,"AIR: {air_value} - {comment}"\n'
|
||||
file_path = "data/feedback.csv"
|
||||
try:
|
||||
is_new_file = not os.path.exists(file_path) or os.path.getsize(file_path) == 0
|
||||
@ -78,7 +93,7 @@ def render(app: Dash, data: pd.DataFrame) -> html.Div:
|
||||
if is_new_file:
|
||||
f.write("timestamp,category,comment\n")
|
||||
f.write(feedback_data)
|
||||
message = f"Feedback for category '{category}' has been saved successfully at {timestamp}."
|
||||
message = f"Feedback for AIR '{air_value}' has been saved successfully at {timestamp}."
|
||||
return False, message
|
||||
except Exception as e:
|
||||
return True, "An error occurred while saving feedback."
|
||||
@ -94,17 +109,17 @@ def render(app: Dash, data: pd.DataFrame) -> html.Div:
|
||||
State(ids.CATEGORY_TABLE, "data"),
|
||||
prevent_initial_call=True
|
||||
)
|
||||
def update_feedback_category_label(selected_rows, data):
|
||||
def update_feedback_air_label(selected_rows, data):
|
||||
if selected_rows:
|
||||
category = data[selected_rows[0]][DataSchema.CATEGORY]
|
||||
return f"Category: {category}"
|
||||
return "Category: "
|
||||
air_value = data[selected_rows[0]][MTBFSchema.AIR]
|
||||
return f"AIR: {air_value}"
|
||||
return "AIR: "
|
||||
|
||||
modal = dbc.Modal(
|
||||
[
|
||||
dbc.ModalHeader(dbc.ModalTitle("Submit Feedback")),
|
||||
dbc.ModalBody([
|
||||
html.H6("Category: ", id="feedback-category-label"),
|
||||
html.H6("AIR: ", id="feedback-category-label"),
|
||||
dcc.Input(id=ids.FEEDBACK_INPUT, type='text', placeholder='Enter feedback...', style={'width': '100%'}),
|
||||
]),
|
||||
dbc.ModalFooter([
|
||||
|
Reference in New Issue
Block a user