Skip to content

Commit

Permalink
div by zero fix
Browse files Browse the repository at this point in the history
  • Loading branch information
mitya52 committed Dec 14, 2023
1 parent b661e0a commit efa4690
Showing 1 changed file with 18 additions and 19 deletions.
37 changes: 18 additions & 19 deletions self_hosting_machinery/dashboard_service/dashboards/dash_prime.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,18 @@
from self_hosting_machinery.dashboard_service.utils import StatsDataTables, complete_date_axis


def robot_human_ratio(robot: int, human: int) -> float:
if human == 0:
return 1
if robot == 0:
return 0
return round(robot / robot + human, 2)


def barplot_rh(
rh_df: pd.DataFrame,
extra: Dict
) -> Dict:
def robot_human_ratio(group) -> float:
robot = group["robot_characters"].sum()
human = group["human_characters"].sum()
if human == 0:
return 1
if robot == 0:
return 0
return round(robot / (robot + human), 2)

def create_chart_data(data_dict, x_values, title, date_kind):
return {
"data": {key: [v[key] for v in data_dict.values()] for key in ["robot", "human", "ratio", "completions"]},
Expand All @@ -35,25 +34,25 @@ def create_chart_data(data_dict, x_values, title, date_kind):
res = {}
day_to_rh = {
datetime.strftime(group["dt_end"].iloc[0], "%b %d"): {
"robot": int(group["robot_characters"].sum()),
"human": int(group["human_characters"].sum()),
"ratio": robot_human_ratio(group),
"robot": (robot := int(group["robot_characters"].sum())),
"human": (human := int(group["human_characters"].sum())),
"ratio": robot_human_ratio(robot, human),
"completions": int(group["completions_cnt"].sum()),
} for date, group in rh_df.groupby(rh_df['dt_end'].dt.date)
}
week_to_rh = {
group["dt_end"].iloc[0].week: {
"robot": int(group["robot_characters"].sum()),
"human": int(group["human_characters"].sum()),
"ratio": robot_human_ratio(group),
"robot": (robot := int(group["robot_characters"].sum())),
"human": (human := int(group["human_characters"].sum())),
"ratio": robot_human_ratio(robot, human),
"completions": int(group["completions_cnt"].sum()),
} for date, group in rh_df.groupby(rh_df['dt_end'].dt.isocalendar().week)
}
month_to_rh = {
group["dt_end"].iloc[0].month: {
"robot": int(group["robot_characters"].sum()),
"human": int(group["human_characters"].sum()),
"ratio": robot_human_ratio(group),
"robot": (robot := int(group["robot_characters"].sum())),
"human": (human := int(group["human_characters"].sum())),
"ratio": robot_human_ratio(robot, human),
"completions": int(group["completions_cnt"].sum()),
} for date, group in rh_df.groupby(rh_df['dt_end'].dt.month)
}
Expand Down Expand Up @@ -185,7 +184,7 @@ def extract_stats(df: pd.DataFrame, date_kind: str) -> Dict:
"Assistant": (robot := int(group["robot_characters"].sum())),
"Human": (human := int(group["human_characters"].sum())),
"Total (characters)": robot + human,
"A/(A+H)": round(robot / (robot + human), 2),
"A/(A+H)": robot_human_ratio(robot, human),
"Completions": int(group["completions_cnt"].sum()),
"Users": int(group["tenant_name"].nunique()),
}
Expand Down

0 comments on commit efa4690

Please sign in to comment.