Spaces:
Running
Running
gauravlochab
commited on
Commit
·
9428dec
1
Parent(s):
7d091ea
feat: update APR data filtering to exclude zero and -100 values, enhance graph handling for negative APR
Browse files
app.py
CHANGED
@@ -251,15 +251,15 @@ def fetch_apr_data_from_db():
|
|
251 |
# Add is_dummy flag (all real data)
|
252 |
apr_data["is_dummy"] = False
|
253 |
|
254 |
-
#
|
255 |
-
if apr_data["apr"]
|
256 |
apr_data["metric_type"] = "APR"
|
257 |
logger.debug(f"Agent {agent_name} ({attr['agent_id']}): APR value: {apr_data['apr']}")
|
258 |
-
# Add to the data list
|
259 |
apr_data_list.append(apr_data)
|
260 |
else:
|
261 |
-
# Log that we're skipping
|
262 |
-
logger.debug(f"Skipping
|
263 |
|
264 |
# Convert list of dictionaries to DataFrame
|
265 |
if not apr_data_list:
|
@@ -273,8 +273,8 @@ def fetch_apr_data_from_db():
|
|
273 |
logger.info(f"Created DataFrame with {len(global_df)} rows")
|
274 |
logger.info(f"DataFrame columns: {global_df.columns.tolist()}")
|
275 |
logger.info(f"APR statistics: min={global_df['apr'].min()}, max={global_df['apr'].max()}, mean={global_df['apr'].mean()}")
|
276 |
-
#
|
277 |
-
logger.info("All values are APR type (
|
278 |
logger.info(f"Agents count: {global_df['agent_name'].value_counts().to_dict()}")
|
279 |
|
280 |
# Log the entire dataframe for debugging
|
@@ -534,16 +534,15 @@ def create_combined_time_series_graph(df):
|
|
534 |
unique_agents = df['agent_id'].unique()
|
535 |
colors = px.colors.qualitative.Plotly[:len(unique_agents)]
|
536 |
|
537 |
-
#
|
538 |
-
|
539 |
-
min_apr = 0 # Start at 0
|
540 |
max_apr = max(df['apr'].max() * 1.1, 10) # Add 10% padding, minimum of 10
|
541 |
|
542 |
# Add background shapes for APR and Performance regions
|
543 |
min_time = df['timestamp'].min()
|
544 |
max_time = df['timestamp'].max()
|
545 |
|
546 |
-
# Add shape for APR region (above zero)
|
547 |
fig.add_shape(
|
548 |
type="rect",
|
549 |
fillcolor="rgba(230, 243, 255, 0.3)",
|
@@ -553,6 +552,16 @@ def create_combined_time_series_graph(df):
|
|
553 |
layer="below"
|
554 |
)
|
555 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
556 |
# Add zero line
|
557 |
fig.add_shape(
|
558 |
type="line",
|
@@ -621,7 +630,7 @@ def create_combined_time_series_graph(df):
|
|
621 |
|
622 |
# Update layout - use simple boolean values everywhere
|
623 |
fig.update_layout(
|
624 |
-
title="APR Values for All Agents
|
625 |
xaxis_title="Time",
|
626 |
yaxis_title="Value",
|
627 |
template="plotly_white",
|
@@ -644,10 +653,10 @@ def create_combined_time_series_graph(df):
|
|
644 |
showgrid=True,
|
645 |
gridwidth=1,
|
646 |
gridcolor='rgba(0,0,0,0.1)',
|
647 |
-
range=[min_apr, max_apr], #
|
648 |
tickmode='linear',
|
649 |
tick0=0,
|
650 |
-
dtick=10
|
651 |
)
|
652 |
|
653 |
# Update x-axis
|
@@ -714,10 +723,10 @@ def create_combined_time_series_graph(df):
|
|
714 |
|
715 |
# Simplified layout
|
716 |
simple_fig.update_layout(
|
717 |
-
title="APR Values
|
718 |
xaxis_title="Time",
|
719 |
yaxis_title="Value",
|
720 |
-
yaxis=dict(range=[
|
721 |
height=600,
|
722 |
width=1000
|
723 |
)
|
@@ -1168,23 +1177,13 @@ def dashboard():
|
|
1168 |
|
1169 |
# Set up the button click event with error handling
|
1170 |
try:
|
1171 |
-
#
|
1172 |
-
refresh_btn.click(
|
1173 |
-
|
1174 |
-
|
1175 |
-
|
1176 |
-
)
|
1177 |
-
except TypeError:
|
1178 |
-
# Fall back to original method
|
1179 |
-
refresh_btn.click(
|
1180 |
-
fn=update_apr_graph,
|
1181 |
-
inputs=[],
|
1182 |
-
outputs=[combined_graph]
|
1183 |
-
)
|
1184 |
-
|
1185 |
# Initialize the graph on load
|
1186 |
# We'll use placeholder figure initially
|
1187 |
-
import plotly.graph_objects as go
|
1188 |
placeholder_fig = go.Figure()
|
1189 |
placeholder_fig.add_annotation(
|
1190 |
text="Click 'Refresh APR Data' to load APR graph",
|
|
|
251 |
# Add is_dummy flag (all real data)
|
252 |
apr_data["is_dummy"] = False
|
253 |
|
254 |
+
# Include all APR values (including negative ones) EXCEPT zero and -100
|
255 |
+
if apr_data["apr"] != 0 and apr_data["apr"] != -100:
|
256 |
apr_data["metric_type"] = "APR"
|
257 |
logger.debug(f"Agent {agent_name} ({attr['agent_id']}): APR value: {apr_data['apr']}")
|
258 |
+
# Add to the data list
|
259 |
apr_data_list.append(apr_data)
|
260 |
else:
|
261 |
+
# Log that we're skipping zero or -100 values
|
262 |
+
logger.debug(f"Skipping value for agent {agent_name} ({attr['agent_id']}): {apr_data['apr']} (zero or -100)")
|
263 |
|
264 |
# Convert list of dictionaries to DataFrame
|
265 |
if not apr_data_list:
|
|
|
273 |
logger.info(f"Created DataFrame with {len(global_df)} rows")
|
274 |
logger.info(f"DataFrame columns: {global_df.columns.tolist()}")
|
275 |
logger.info(f"APR statistics: min={global_df['apr'].min()}, max={global_df['apr'].max()}, mean={global_df['apr'].mean()}")
|
276 |
+
# All values are APR type (excluding zero and -100 values)
|
277 |
+
logger.info("All values are APR type (excluding zero and -100 values)")
|
278 |
logger.info(f"Agents count: {global_df['agent_name'].value_counts().to_dict()}")
|
279 |
|
280 |
# Log the entire dataframe for debugging
|
|
|
534 |
unique_agents = df['agent_id'].unique()
|
535 |
colors = px.colors.qualitative.Plotly[:len(unique_agents)]
|
536 |
|
537 |
+
# Update y-axis range to include negative values
|
538 |
+
min_apr = min(df['apr'].min() * 1.1, -10) # Add 10% padding, minimum of -10
|
|
|
539 |
max_apr = max(df['apr'].max() * 1.1, 10) # Add 10% padding, minimum of 10
|
540 |
|
541 |
# Add background shapes for APR and Performance regions
|
542 |
min_time = df['timestamp'].min()
|
543 |
max_time = df['timestamp'].max()
|
544 |
|
545 |
+
# Add shape for positive APR region (above zero)
|
546 |
fig.add_shape(
|
547 |
type="rect",
|
548 |
fillcolor="rgba(230, 243, 255, 0.3)",
|
|
|
552 |
layer="below"
|
553 |
)
|
554 |
|
555 |
+
# Add shape for negative APR region (below zero)
|
556 |
+
fig.add_shape(
|
557 |
+
type="rect",
|
558 |
+
fillcolor="rgba(255, 230, 230, 0.3)",
|
559 |
+
line=dict(width=0),
|
560 |
+
y0=min_apr, y1=0,
|
561 |
+
x0=min_time, x1=max_time,
|
562 |
+
layer="below"
|
563 |
+
)
|
564 |
+
|
565 |
# Add zero line
|
566 |
fig.add_shape(
|
567 |
type="line",
|
|
|
630 |
|
631 |
# Update layout - use simple boolean values everywhere
|
632 |
fig.update_layout(
|
633 |
+
title="APR Values for All Agents",
|
634 |
xaxis_title="Time",
|
635 |
yaxis_title="Value",
|
636 |
template="plotly_white",
|
|
|
653 |
showgrid=True,
|
654 |
gridwidth=1,
|
655 |
gridcolor='rgba(0,0,0,0.1)',
|
656 |
+
range=[min_apr, max_apr], # Updated range including negative values
|
657 |
tickmode='linear',
|
658 |
tick0=0,
|
659 |
+
dtick=10
|
660 |
)
|
661 |
|
662 |
# Update x-axis
|
|
|
723 |
|
724 |
# Simplified layout
|
725 |
simple_fig.update_layout(
|
726 |
+
title="APR Values for All Agents",
|
727 |
xaxis_title="Time",
|
728 |
yaxis_title="Value",
|
729 |
+
yaxis=dict(range=[min_apr, max_apr]),
|
730 |
height=600,
|
731 |
width=1000
|
732 |
)
|
|
|
1177 |
|
1178 |
# Set up the button click event with error handling
|
1179 |
try:
|
1180 |
+
# Use Gradio's button click properly
|
1181 |
+
refresh_btn.click(fn=update_apr_graph, outputs=combined_graph)
|
1182 |
+
except Exception as e:
|
1183 |
+
logger.error(f"Error setting up button handler: {e}")
|
1184 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1185 |
# Initialize the graph on load
|
1186 |
# We'll use placeholder figure initially
|
|
|
1187 |
placeholder_fig = go.Figure()
|
1188 |
placeholder_fig.add_annotation(
|
1189 |
text="Click 'Refresh APR Data' to load APR graph",
|