fix graph order for same date sessions
This commit is contained in:
1 file changed
+55
-27
@@ -212,6 +212,38 @@ def cents_to_dollars(cents: int) -> str:
|
|||||||
return f"${value:,.2f}"
|
return f"${value:,.2f}"
|
||||||
|
|
||||||
|
|
||||||
|
def session_sort_key(session: SessionSummary) -> tuple[str, int, str]:
|
||||||
|
session_id = session.session_id.strip()
|
||||||
|
|
||||||
|
if session_id == session.session_date:
|
||||||
|
return (session.session_date, 1, session_id)
|
||||||
|
|
||||||
|
suffix = session_id.replace(f"{session.session_date}-", "")
|
||||||
|
if suffix.isdigit():
|
||||||
|
return (session.session_date, int(suffix), session_id)
|
||||||
|
|
||||||
|
if suffix.lower().startswith("s") and suffix[1:].isdigit():
|
||||||
|
return (session.session_date, int(suffix[1:]), session_id)
|
||||||
|
|
||||||
|
return (session.session_date, 9999, session_id)
|
||||||
|
|
||||||
|
|
||||||
|
def session_chart_label(session: SessionSummary) -> str:
|
||||||
|
session_id = session.session_id.strip()
|
||||||
|
|
||||||
|
if session_id == session.session_date:
|
||||||
|
return safe_date_label(session.session_date)
|
||||||
|
|
||||||
|
suffix = session_id.replace(f"{session.session_date}-", "")
|
||||||
|
if suffix.isdigit():
|
||||||
|
return f"{safe_date_label(session.session_date)} · S{int(suffix)}"
|
||||||
|
|
||||||
|
if suffix.lower().startswith("s") and suffix[1:].isdigit():
|
||||||
|
return f"{safe_date_label(session.session_date)} · S{int(suffix[1:])}"
|
||||||
|
|
||||||
|
return f"{safe_date_label(session.session_date)} · {suffix}"
|
||||||
|
|
||||||
|
|
||||||
def safe_date_label(raw_date: str) -> str:
|
def safe_date_label(raw_date: str) -> str:
|
||||||
try:
|
try:
|
||||||
return datetime.strptime(raw_date, "%Y-%m-%d").strftime("%b %d, %Y")
|
return datetime.strptime(raw_date, "%Y-%m-%d").strftime("%b %d, %Y")
|
||||||
@@ -468,9 +500,10 @@ def build_leaderboard(sessions: list[SessionSummary]) -> list[PlayerStats]:
|
|||||||
return leaderboard
|
return leaderboard
|
||||||
|
|
||||||
|
|
||||||
def cumulative_profit_series(sessions: list[SessionSummary]) -> dict[str, Any]:
|
def cumulative_profit_series(sessions: list[SessionSummary]) -> dict[str, object]:
|
||||||
ordered_sessions = sorted(sessions, key=lambda session: session.session_date)
|
ordered_sessions = sorted(sessions, key=session_sort_key)
|
||||||
player_names = sorted(
|
|
||||||
|
all_players = sorted(
|
||||||
{
|
{
|
||||||
entry.player_name
|
entry.player_name
|
||||||
for session in ordered_sessions
|
for session in ordered_sessions
|
||||||
@@ -479,13 +512,13 @@ def cumulative_profit_series(sessions: list[SessionSummary]) -> dict[str, Any]:
|
|||||||
key=str.casefold,
|
key=str.casefold,
|
||||||
)
|
)
|
||||||
|
|
||||||
labels = [safe_date_label(session.session_date) for session in ordered_sessions]
|
labels = [session_chart_label(session) for session in ordered_sessions]
|
||||||
datasets = []
|
datasets = []
|
||||||
|
|
||||||
for player_name in player_names:
|
for player_name in all_players:
|
||||||
series: list[float | None] = []
|
|
||||||
running_total = 0
|
running_total = 0
|
||||||
has_started = False
|
seen_player = False
|
||||||
|
data: list[float | None] = []
|
||||||
|
|
||||||
for session in ordered_sessions:
|
for session in ordered_sessions:
|
||||||
matching_entry = next(
|
matching_entry = next(
|
||||||
@@ -496,37 +529,32 @@ def cumulative_profit_series(sessions: list[SessionSummary]) -> dict[str, Any]:
|
|||||||
),
|
),
|
||||||
None,
|
None,
|
||||||
)
|
)
|
||||||
if matching_entry is not None:
|
|
||||||
has_started = True
|
if matching_entry is None:
|
||||||
running_total += matching_entry.net_cents
|
data.append(running_total / 100 if seen_player else None)
|
||||||
series.append(round(running_total / 100, 2))
|
continue
|
||||||
elif has_started:
|
|
||||||
series.append(round(running_total / 100, 2))
|
seen_player = True
|
||||||
else:
|
running_total += matching_entry.net_cents
|
||||||
series.append(None)
|
data.append(round(running_total / 100, 2))
|
||||||
|
|
||||||
datasets.append(
|
datasets.append(
|
||||||
{
|
{
|
||||||
"label": player_name,
|
"label": player_name,
|
||||||
"data": series,
|
"data": data,
|
||||||
"borderColor": color_for_name(player_name, player_names),
|
|
||||||
"backgroundColor": color_for_name(player_name, player_names),
|
|
||||||
"pointRadius": 3,
|
|
||||||
"pointHoverRadius": 5,
|
|
||||||
"pointHitRadius": 10,
|
|
||||||
"borderWidth": 2.5,
|
|
||||||
"tension": 0.22,
|
|
||||||
"spanGaps": False,
|
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
return {"labels": labels, "datasets": datasets}
|
return {
|
||||||
|
"labels": labels,
|
||||||
|
"datasets": datasets,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
def player_session_series(
|
def player_session_series(
|
||||||
sessions: list[SessionSummary], player_name: str
|
sessions: list[SessionSummary], player_name: str
|
||||||
) -> dict[str, Any]:
|
) -> dict[str, Any]:
|
||||||
ordered_sessions = sorted(sessions, key=lambda session: session.session_date)
|
ordered_sessions = sorted(sessions, key=session_sort_key)
|
||||||
labels: list[str] = []
|
labels: list[str] = []
|
||||||
net_values: list[float] = []
|
net_values: list[float] = []
|
||||||
cumulative_values: list[float] = []
|
cumulative_values: list[float] = []
|
||||||
@@ -549,7 +577,7 @@ def player_session_series(
|
|||||||
if matching_entry is None:
|
if matching_entry is None:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
labels.append(safe_date_label(session.session_date))
|
labels.append(session_chart_label(session))
|
||||||
net_values.append(round(matching_entry.net_cents / 100, 2))
|
net_values.append(round(matching_entry.net_cents / 100, 2))
|
||||||
running_total += matching_entry.net_cents
|
running_total += matching_entry.net_cents
|
||||||
cumulative_values.append(round(running_total / 100, 2))
|
cumulative_values.append(round(running_total / 100, 2))
|
||||||
|
|||||||
Reference in new issue
Block a user