Skip to content

Commit

Permalink
sean tweaks and reordering of graphs
Browse files Browse the repository at this point in the history
  • Loading branch information
SanderGi committed Feb 2, 2024
1 parent 819cf1f commit 2a5d319
Showing 1 changed file with 99 additions and 35 deletions.
134 changes: 99 additions & 35 deletions recipes/VideoBotsStats.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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",
)

Expand Down Expand Up @@ -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",
Expand All @@ -439,6 +450,8 @@ def calculate_stats_binned_by_time(
"Senders",
"Unique_feedback_givers",
"Average_response_time",
"Average_runtime",
"Average_analysis_time",
)
)

Expand Down Expand Up @@ -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=[
Expand All @@ -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(
Expand All @@ -493,17 +522,28 @@ 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
df["Unique_feedback_givers"] = df["Unique_feedback_givers"] * factor
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"]
Expand Down Expand Up @@ -653,41 +693,41 @@ def plot_graphs(self, view, df):
],
)
st.plotly_chart(fig)
st.markdown("<br>", 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}\\%<extra></extra>",
y=list(df["Average_response_time"]),
text=list(df["Average_response_time"]),
hovertemplate="Average Response Time: %{y:.0f}<extra></extra>",
),
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}\\%<extra></extra>",
y=list(df["Average_runtime"]),
text=list(df["Average_runtime"]),
hovertemplate="Average Runtime: %{y:.0f}<extra></extra>",
),
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}<extra></extra>",
),
],
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",
Expand All @@ -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}<extra></extra>",
y=list(df["Percentage_positive_feedback"]),
text=list(df["Percentage_positive_feedback"]),
hovertemplate="Positive Feedback: %{y:.0f}%<extra></extra>",
),
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}%<extra></extra>",
),
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}%<extra></extra>",
),
],
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",
Expand Down

0 comments on commit 2a5d319

Please sign in to comment.