Skip to content
This repository has been archived by the owner on Oct 10, 2024. It is now read-only.

Improve /rate and /history color selection #122

Merged
merged 4 commits into from
Dec 19, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 39 additions & 2 deletions buttercup/cogs/history.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,39 @@ def add_zero_rates(
return data.reindex(new_index, fill_value=0).sort_index()


def get_user_colors(users: Optional[List[BlossomUser]]) -> List[str]:
"""Assign a color to each user.

This will prefer to assign a user their rank color.
A single user will get white for better readability.
"""
if not users or len(users) == 1:
# If we don't need to distinguish, take white (best contrast)
return ["#eeeeee"]

color_mapping = {}
available_ranks = [r for r in ranks]
left_over_users = []

for user in users:
user_rank = get_rank(user["gamma"])

# Give the user their rank color if possible
if user_rank in available_ranks:
color_mapping[user["username"]] = user_rank["color"]
available_ranks = [
r for r in available_ranks if r["name"] != user_rank["name"]
]
else:
left_over_users.append(user)

# Give the left over users another rank's color
for i, user in enumerate(left_over_users):
color_mapping[user["username"]] = available_ranks[i]["color"]

return [color_mapping[user["username"]] for user in users]


def add_milestone_lines(
ax: plt.Axes,
milestones: List[Dict[str, Union[str, int]]],
Expand Down Expand Up @@ -454,6 +487,8 @@ async def _history(
)

users = get_user_list(usernames, ctx, self.blossom_api)
users.sort(key=lambda u: u["gamma"], reverse=True)
colors = get_user_colors(users)

min_gammas = []
max_gammas = []
Expand Down Expand Up @@ -488,7 +523,7 @@ async def _history(

history_data = self.get_user_history(user, after_time, before_time)

color = ranks[index]["color"]
color = colors[index]
first_point = history_data.iloc[0]
last_point = history_data.iloc[-1]

Expand Down Expand Up @@ -586,6 +621,8 @@ async def _rate(
)

users = get_user_list(usernames, ctx, self.blossom_api)
users.sort(key=lambda u: u["gamma"], reverse=True)
colors = get_user_colors(users)

max_rates = []

Expand Down Expand Up @@ -623,7 +660,7 @@ async def _rate(
max_rates.append(max_rate)
max_rate_point = user_data[user_data["count"] == max_rate].iloc[0]

color = ranks[index]["color"]
color = colors[index]

# Plot the graph
ax.plot(
Expand Down