From 2a5d31969c86a07e0a6180c5c75829c268253e8d Mon Sep 17 00:00:00 2001 From: Alexander Metzger Date: Thu, 1 Feb 2024 22:50:30 -0800 Subject: [PATCH] sean tweaks and reordering of graphs --- recipes/VideoBotsStats.py | 134 ++++++++++++++++++++++++++++---------- 1 file changed, 99 insertions(+), 35 deletions(-) diff --git a/recipes/VideoBotsStats.py b/recipes/VideoBotsStats.py index 08787f1b3..718b580f1 100644 --- a/recipes/VideoBotsStats.py +++ b/recipes/VideoBotsStats.py @@ -119,6 +119,7 @@ def render(self): if int(bid) not in allowed_bids: bid = allowed_bids[0] bi = BotIntegration.objects.get(id=bid) + has_analysis_run = bi.analysis_run is not None run_title, run_url = self.parse_run_info(bi) self.show_title_breadcrumb_share(run_title, run_url, bi) @@ -153,14 +154,22 @@ def render(self): st.session_state.setdefault("details", self.request.query_params.get("details")) details = st.horizontal_radio( "### Details", - options=[ - "Conversations", - "Messages", - "Feedback Positive", - "Feedback Negative", - "Answered Successfully", - "Answered Unsuccessfully", - ], + options=( + [ + "Conversations", + "Messages", + "Feedback Positive", + "Feedback Negative", + ] + + ( + [ + "Answered Successfully", + "Answered Unsuccessfully", + ] + if has_analysis_run + else [] + ) + ), key="details", ) @@ -430,7 +439,9 @@ def calculate_stats_binned_by_time( distinct=True, ) ) + .annotate(Average_runtime=Avg("saved_run__run_time")) .annotate(Average_response_time=Avg("response_time")) + .annotate(Average_analysis_time=Avg("analysis_run__run_time")) .annotate(Unique_feedback_givers=Count("feedbacks", distinct=True)) .values( "date", @@ -439,6 +450,8 @@ def calculate_stats_binned_by_time( "Senders", "Unique_feedback_givers", "Average_response_time", + "Average_runtime", + "Average_analysis_time", ) ) @@ -470,6 +483,20 @@ def calculate_stats_binned_by_time( .values("date", "Neg_feedback") ) + successfully_answered = ( + Message.objects.filter( + conversation__bot_integration=bi, + analysis_result__contains={"Answered": True}, + created_at__date__gte=start_date, + created_at__date__lte=end_date, + ) + .order_by() + .annotate(date=trunc_fn("created_at")) + .values("date") + .annotate(Successfully_Answered=Count("id")) + .values("date", "Successfully_Answered") + ) + df = pd.DataFrame( messages_received, columns=[ @@ -479,6 +506,8 @@ def calculate_stats_binned_by_time( "Senders", "Unique_feedback_givers", "Average_response_time", + "Average_runtime", + "Average_analysis_time", ], ) df = df.merge( @@ -493,6 +522,14 @@ def calculate_stats_binned_by_time( left_on="date", right_on="date", ) + df = df.merge( + pd.DataFrame( + successfully_answered, columns=["date", "Successfully_Answered"] + ), + how="outer", + left_on="date", + right_on="date", + ) df["Messages_Sent"] = df["Messages_Sent"] * factor df["Convos"] = df["Convos"] * factor df["Senders"] = df["Senders"] * factor @@ -500,10 +537,13 @@ def calculate_stats_binned_by_time( df["Pos_feedback"] = df["Pos_feedback"] * factor df["Neg_feedback"] = df["Neg_feedback"] * factor df["Percentage_positive_feedback"] = ( - df["Pos_feedback"] / (df["Pos_feedback"] + df["Neg_feedback"]) + df["Pos_feedback"] / df["Messages_Sent"] ) * 100 df["Percentage_negative_feedback"] = ( - df["Neg_feedback"] / (df["Pos_feedback"] + df["Neg_feedback"]) + df["Neg_feedback"] / df["Messages_Sent"] + ) * 100 + df["Percentage_successfully_answered"] = ( + df["Successfully_Answered"] / df["Messages_Sent"] ) * 100 df["Msgs_per_convo"] = df["Messages_Sent"] / df["Convos"] df["Msgs_per_user"] = df["Messages_Sent"] / df["Senders"] @@ -653,41 +693,41 @@ def plot_graphs(self, view, df): ], ) st.plotly_chart(fig) - st.markdown("
", unsafe_allow_html=True) + st.write("---") fig = go.Figure( data=[ go.Scatter( - name="Positive Feedback", + name="Average Response Time", mode="lines+markers", x=list(df["date"]), - y=list(df["Percentage_positive_feedback"]), - text=list(df["Percentage_positive_feedback"]), - hovertemplate="Positive Feedback: %{y:.0f}\\%", + y=list(df["Average_response_time"]), + text=list(df["Average_response_time"]), + hovertemplate="Average Response Time: %{y:.0f}", ), go.Scatter( - name="Negative Feedback", + name="Average Run Time", mode="lines+markers", x=list(df["date"]), - y=list(df["Percentage_negative_feedback"]), - text=list(df["Percentage_negative_feedback"]), - hovertemplate="Negative Feedback: %{y:.0f}\\%", + y=list(df["Average_runtime"]), + text=list(df["Average_runtime"]), + hovertemplate="Average Runtime: %{y:.0f}", + ), + go.Scatter( + name="Average Analysis Time", + mode="lines+markers", + x=list(df["date"]), + y=list(df["Average_analysis_time"]), + text=list(df["Average_analysis_time"]), + hovertemplate="Average Analysis Time: %{y:.0f}", ), ], layout=dict( margin=dict(l=0, r=0, t=28, b=0), yaxis=dict( - title="Percentage", - range=[0, 100], - tickvals=[ - *range( - 0, - 101, - 10, - ) - ], + title="Seconds", ), title=dict( - text=f"{view} Feedback Distribution", + text=f"{view} Performance Metrics", ), height=300, template="plotly_white", @@ -698,21 +738,45 @@ def plot_graphs(self, view, df): fig = go.Figure( data=[ go.Scatter( - name="Average Response Time", + name="Positive Feedback", mode="lines+markers", x=list(df["date"]), - y=list(df["Average_response_time"]), - text=list(df["Average_response_time"]), - hovertemplate="Average Response Time: %{y:.0f}", + y=list(df["Percentage_positive_feedback"]), + text=list(df["Percentage_positive_feedback"]), + hovertemplate="Positive Feedback: %{y:.0f}%", + ), + go.Scatter( + name="Negative Feedback", + mode="lines+markers", + x=list(df["date"]), + y=list(df["Percentage_negative_feedback"]), + text=list(df["Percentage_negative_feedback"]), + hovertemplate="Negative Feedback: %{y:.0f}%", + ), + go.Scatter( + name="Successfully Answered", + mode="lines+markers", + x=list(df["date"]), + y=list(df["Percentage_successfully_answered"]), + text=list(df["Percentage_successfully_answered"]), + hovertemplate="Successfully Answered: %{y:.0f}%", ), ], layout=dict( margin=dict(l=0, r=0, t=28, b=0), yaxis=dict( - title="Seconds", + title="Percentage", + range=[0, 100], + tickvals=[ + *range( + 0, + 101, + 10, + ) + ], ), title=dict( - text=f"{view} Performance Metrics", + text=f"{view} Feedback Distribution", ), height=300, template="plotly_white",