1258 lines
20 KiB
Python
1258 lines
20 KiB
Python
#!/usr/bin/env python3
|
||
# -*- coding: utf-8 -*-
|
||
|
||
"""
|
||
Dashboard HTML report.
|
||
|
||
Главная страница отчета.
|
||
|
||
Содержит только обзорную информацию.
|
||
|
||
Без карты.
|
||
|
||
Без полного списка точек доступа.
|
||
|
||
Вся детальная информация находится
|
||
на страницах отдельных поездок.
|
||
"""
|
||
|
||
from pathlib import Path
|
||
|
||
from .config import (
|
||
RECENT_SESSIONS_LIMIT,
|
||
SESSIONS_PAGE_LIMIT,
|
||
)
|
||
|
||
from .utils import (
|
||
esc,
|
||
format_datetime,
|
||
)
|
||
|
||
from .template import (
|
||
|
||
page_begin,
|
||
|
||
page_end,
|
||
|
||
card,
|
||
|
||
stat,
|
||
|
||
stats_grid,
|
||
|
||
)
|
||
|
||
from .statistics import (
|
||
|
||
database_statistics,
|
||
|
||
top_vendors,
|
||
|
||
top_essid,
|
||
|
||
channel_statistics,
|
||
|
||
encryption_statistics,
|
||
|
||
)
|
||
|
||
from .sessions import (
|
||
|
||
recent_sessions,
|
||
|
||
all_sessions,
|
||
|
||
)
|
||
|
||
# ============================================================
|
||
# Helpers
|
||
# ============================================================
|
||
|
||
def format_session_folder(capture_file):
|
||
if not capture_file or not str(capture_file).startswith("captures/"):
|
||
return "-"
|
||
|
||
folder = Path(capture_file).parent.name
|
||
|
||
try:
|
||
from datetime import datetime, timezone
|
||
|
||
dt = datetime.strptime(
|
||
folder,
|
||
"%Y%m%d_%H%M%S"
|
||
).replace(
|
||
tzinfo=timezone.utc
|
||
)
|
||
|
||
return format_datetime(
|
||
dt.isoformat()
|
||
)
|
||
|
||
except ValueError:
|
||
return folder
|
||
|
||
|
||
# ============================================================
|
||
# Database statistics
|
||
# ============================================================
|
||
|
||
def statistics_block(conn):
|
||
|
||
data = database_statistics(conn)
|
||
|
||
items = [
|
||
|
||
stat(
|
||
data["access_points"],
|
||
"Access Points"
|
||
),
|
||
|
||
stat(
|
||
data["access_points_without_coordinates"],
|
||
"AP without Coordinates"
|
||
),
|
||
|
||
stat(
|
||
data["sessions"],
|
||
"Capture Sessions"
|
||
),
|
||
|
||
stat(
|
||
data["vendors"],
|
||
"Vendors"
|
||
),
|
||
|
||
stat(
|
||
data["handshakes"],
|
||
"Handshakes"
|
||
),
|
||
|
||
stat(
|
||
data["credentials"],
|
||
"Handshake Cracked"
|
||
),
|
||
|
||
stat(
|
||
data["pmkid"],
|
||
"PMKID"
|
||
),
|
||
|
||
stat(
|
||
data["cracked"],
|
||
"Cracked AP"
|
||
),
|
||
|
||
stat(
|
||
data["database_size"],
|
||
"Database"
|
||
),
|
||
|
||
]
|
||
|
||
return card(
|
||
|
||
"📊 Database Overview",
|
||
|
||
stats_grid(items)
|
||
|
||
)
|
||
|
||
|
||
# ============================================================
|
||
# Information
|
||
# ============================================================
|
||
|
||
def information_block(conn):
|
||
|
||
data = database_statistics(conn)
|
||
|
||
html = f"""
|
||
|
||
<table>
|
||
|
||
<tr>
|
||
|
||
<td><b>Database size</b></td>
|
||
|
||
<td>{data["database_size"]}</td>
|
||
|
||
</tr>
|
||
|
||
<tr>
|
||
|
||
<td><b>Captures size</b></td>
|
||
|
||
<td>{data["captures_size"]}</td>
|
||
|
||
</tr>
|
||
|
||
<tr>
|
||
|
||
<td><b>Capture files</b></td>
|
||
|
||
<td>{data["capture_files"]}</td>
|
||
|
||
</tr>
|
||
|
||
<tr>
|
||
|
||
<td><b>First access point</b></td>
|
||
|
||
<td>{format_datetime(data["first_seen"])}</td>
|
||
|
||
</tr>
|
||
|
||
<tr>
|
||
|
||
<td><b>Last access point</b></td>
|
||
|
||
<td>{format_datetime(data["last_seen"])}</td>
|
||
|
||
</tr>
|
||
|
||
</table>
|
||
|
||
"""
|
||
|
||
return card(
|
||
|
||
"ℹ️ Database Information",
|
||
|
||
html,
|
||
|
||
)
|
||
# ============================================================
|
||
# Top Vendors
|
||
# ============================================================
|
||
|
||
def vendors_block(conn):
|
||
|
||
rows = top_vendors(
|
||
conn,
|
||
limit=15
|
||
)
|
||
|
||
html = """
|
||
|
||
<table>
|
||
|
||
<tr>
|
||
|
||
<th>Vendor</th>
|
||
|
||
<th>Devices</th>
|
||
|
||
</tr>
|
||
|
||
"""
|
||
|
||
for vendor, total in rows:
|
||
|
||
if not vendor:
|
||
|
||
vendor = "-"
|
||
|
||
html += f"""
|
||
|
||
<tr>
|
||
|
||
<td>{vendor}</td>
|
||
|
||
<td>{total}</td>
|
||
|
||
</tr>
|
||
|
||
"""
|
||
|
||
html += """
|
||
|
||
</table>
|
||
|
||
"""
|
||
|
||
return card(
|
||
|
||
"🏭 Top Vendors",
|
||
|
||
html,
|
||
|
||
)
|
||
|
||
|
||
# ============================================================
|
||
# Top ESSID
|
||
# ============================================================
|
||
|
||
def essid_block(conn):
|
||
|
||
rows = top_essid(
|
||
conn,
|
||
limit=15
|
||
)
|
||
|
||
html = """
|
||
|
||
<table>
|
||
|
||
<tr>
|
||
|
||
<th>ESSID</th>
|
||
|
||
<th>Count</th>
|
||
|
||
</tr>
|
||
|
||
"""
|
||
|
||
for essid, total in rows:
|
||
|
||
if not essid:
|
||
|
||
essid = "<hidden>"
|
||
|
||
html += f"""
|
||
|
||
<tr>
|
||
|
||
<td>{essid}</td>
|
||
|
||
<td>{total}</td>
|
||
|
||
</tr>
|
||
|
||
"""
|
||
|
||
html += """
|
||
|
||
</table>
|
||
|
||
"""
|
||
|
||
return card(
|
||
|
||
"📶 Most Common ESSID",
|
||
|
||
html,
|
||
|
||
)
|
||
|
||
|
||
# ============================================================
|
||
# Channels
|
||
# ============================================================
|
||
|
||
def channels_block(conn):
|
||
|
||
rows = channel_statistics(
|
||
conn
|
||
)
|
||
|
||
html = """
|
||
|
||
<table>
|
||
|
||
<tr>
|
||
|
||
<th>Channel</th>
|
||
|
||
<th>Access Points</th>
|
||
|
||
</tr>
|
||
|
||
"""
|
||
|
||
for channel, total in rows:
|
||
|
||
html += f"""
|
||
|
||
<tr>
|
||
|
||
<td>{channel}</td>
|
||
|
||
<td>{total}</td>
|
||
|
||
</tr>
|
||
|
||
"""
|
||
|
||
html += """
|
||
|
||
</table>
|
||
|
||
"""
|
||
|
||
return card(
|
||
|
||
"📡 Channel Statistics",
|
||
|
||
html,
|
||
|
||
)
|
||
|
||
|
||
# ============================================================
|
||
# Encryption
|
||
# ============================================================
|
||
|
||
def encryption_block(conn):
|
||
|
||
rows = encryption_statistics(
|
||
conn
|
||
)
|
||
|
||
html = """
|
||
|
||
<table>
|
||
|
||
<tr>
|
||
|
||
<th>Encryption</th>
|
||
|
||
<th>Networks</th>
|
||
|
||
</tr>
|
||
|
||
"""
|
||
|
||
for encryption, total in rows:
|
||
|
||
if not encryption:
|
||
|
||
encryption = "-"
|
||
|
||
html += f"""
|
||
|
||
<tr>
|
||
|
||
<td>{encryption}</td>
|
||
|
||
<td>{total}</td>
|
||
|
||
</tr>
|
||
|
||
"""
|
||
|
||
html += """
|
||
|
||
</table>
|
||
|
||
"""
|
||
|
||
return card(
|
||
|
||
"🔐 Encryption",
|
||
|
||
html,
|
||
|
||
)
|
||
# ============================================================
|
||
# Session Card
|
||
# ============================================================
|
||
|
||
def session_card(
|
||
session,
|
||
context=None,
|
||
):
|
||
|
||
session_url = context.session_url(
|
||
session["id"]
|
||
)
|
||
|
||
return f"""
|
||
|
||
<div class="card">
|
||
|
||
<h3>
|
||
|
||
🚗 Session {format_datetime(session.get("trip_start"))}
|
||
|
||
</h3>
|
||
|
||
<table>
|
||
|
||
<tr>
|
||
|
||
<td><b>Trip start</b></td>
|
||
|
||
<td>{format_datetime(session.get("trip_start"))}</td>
|
||
|
||
</tr>
|
||
|
||
<tr>
|
||
|
||
<td><b>Trip end</b></td>
|
||
|
||
<td>{format_datetime(session.get("trip_end"))}</td>
|
||
|
||
</tr>
|
||
|
||
<tr>
|
||
|
||
<td><b>Duration</b></td>
|
||
|
||
<td>{session.get("duration","-")}</td>
|
||
|
||
</tr>
|
||
|
||
<tr>
|
||
|
||
<tr>
|
||
|
||
<td><b>Session origin</b></td>
|
||
|
||
<td>{session.get("origin", "Unknown")}</td>
|
||
|
||
</tr>
|
||
|
||
<tr>
|
||
|
||
<td><b>Capture started</b></td>
|
||
|
||
<td>{format_datetime(session.get("start_time"))}</td>
|
||
|
||
</tr>
|
||
|
||
<tr>
|
||
|
||
<td><b>Session folder</b></td>
|
||
|
||
<td>{format_session_folder(session.get("capture_file"))}</td>
|
||
|
||
</tr>
|
||
|
||
<tr>
|
||
|
||
<td><b>Access Points</b></td>
|
||
|
||
<td>{session.get("access_points","-")}</td>
|
||
|
||
</tr>
|
||
|
||
<tr>
|
||
|
||
<td><b>Coordinates Restored</b></td>
|
||
|
||
<td>{session.get("coordinates_restored","-")}</td>
|
||
|
||
</tr>
|
||
|
||
<tr>
|
||
|
||
<td><b>AP without Coordinates</b></td>
|
||
|
||
<td>{session.get("ap_without_coordinates","-")}</td>
|
||
|
||
</tr>
|
||
|
||
<tr>
|
||
|
||
<td><b>Handshake AP</b></td>
|
||
|
||
<td>{session.get("handshakes","-")}</td>
|
||
|
||
</tr>
|
||
|
||
<tr>
|
||
|
||
<td><b>Handshake Records</b></td>
|
||
|
||
<td>{session.get("handshake_records","-")}</td>
|
||
|
||
</tr>
|
||
|
||
<tr>
|
||
|
||
<td><b>PMKID AP</b></td>
|
||
|
||
<td>{session.get("pmkid","-")}</td>
|
||
|
||
</tr>
|
||
|
||
<tr>
|
||
|
||
<td><b>Cracked AP</b></td>
|
||
|
||
<td>{session.get("cracked","-")}</td>
|
||
|
||
</tr>
|
||
|
||
<tr>
|
||
|
||
<td><b>Directory size</b></td>
|
||
|
||
<td>{session.get("directory_size","-")}</td>
|
||
|
||
</tr>
|
||
|
||
</table>
|
||
|
||
<p>
|
||
|
||
<a
|
||
|
||
class="button"
|
||
draggable="false"
|
||
|
||
href="{session_url}">
|
||
|
||
📄 Open Session Report
|
||
|
||
</a>
|
||
|
||
</p>
|
||
|
||
</div>
|
||
|
||
"""
|
||
|
||
|
||
# ============================================================
|
||
# Recent Sessions
|
||
# ============================================================
|
||
|
||
def sessions_block(
|
||
conn,
|
||
context=None,
|
||
):
|
||
|
||
sessions = recent_sessions(
|
||
|
||
conn,
|
||
|
||
limit=RECENT_SESSIONS_LIMIT,
|
||
|
||
)
|
||
|
||
if not sessions:
|
||
|
||
return card(
|
||
|
||
f"🚗 Recent {RECENT_SESSIONS_LIMIT} Sessions",
|
||
|
||
"""
|
||
|
||
<p>
|
||
|
||
Capture sessions not found.
|
||
|
||
</p>
|
||
|
||
"""
|
||
|
||
)
|
||
|
||
cards = []
|
||
|
||
for session in sessions:
|
||
|
||
cards.append(
|
||
|
||
session_card(
|
||
|
||
session,
|
||
context,
|
||
|
||
)
|
||
|
||
)
|
||
|
||
capture_sessions_url = (
|
||
context.capture_sessions_url()
|
||
)
|
||
|
||
html = """
|
||
|
||
<div class="report-grid">
|
||
|
||
"""
|
||
|
||
html += "\n".join(cards)
|
||
|
||
html += f"""
|
||
|
||
</div>
|
||
|
||
<p class="report-action">
|
||
|
||
<a
|
||
class="button"
|
||
draggable="false"
|
||
href="{capture_sessions_url}">
|
||
|
||
Show all sessions
|
||
|
||
</a>
|
||
|
||
</p>
|
||
|
||
"""
|
||
|
||
return card(
|
||
|
||
f"🚗 Recent {RECENT_SESSIONS_LIMIT} Sessions",
|
||
|
||
html,
|
||
|
||
)
|
||
# ============================================================
|
||
# Dashboard
|
||
# ============================================================
|
||
|
||
def generate_dashboard(
|
||
conn,
|
||
context=None,
|
||
):
|
||
|
||
html = page_begin(
|
||
|
||
title="Dashboard",
|
||
|
||
description=(
|
||
"Overview of collected Wi-Fi data, access points, "
|
||
"capture sessions, and vendors."
|
||
),
|
||
|
||
active="dashboard",
|
||
|
||
context=context,
|
||
|
||
section_title="Reports",
|
||
|
||
)
|
||
|
||
# --------------------------------------------------------
|
||
# Database statistics
|
||
# --------------------------------------------------------
|
||
|
||
html += statistics_block(
|
||
conn
|
||
)
|
||
|
||
# --------------------------------------------------------
|
||
# Information
|
||
# --------------------------------------------------------
|
||
|
||
html += information_block(
|
||
conn
|
||
)
|
||
|
||
# --------------------------------------------------------
|
||
# Vendors / ESSID
|
||
# --------------------------------------------------------
|
||
|
||
html += """
|
||
|
||
<div class="report-grid">
|
||
|
||
"""
|
||
|
||
html += vendors_block(
|
||
conn
|
||
)
|
||
|
||
html += essid_block(
|
||
conn
|
||
)
|
||
|
||
html += """
|
||
|
||
</div>
|
||
|
||
"""
|
||
|
||
# --------------------------------------------------------
|
||
# Channels / Encryption
|
||
# --------------------------------------------------------
|
||
|
||
html += """
|
||
|
||
<div class="report-grid">
|
||
|
||
"""
|
||
|
||
html += channels_block(
|
||
conn
|
||
)
|
||
|
||
html += encryption_block(
|
||
conn
|
||
)
|
||
|
||
html += """
|
||
|
||
</div>
|
||
|
||
"""
|
||
|
||
# --------------------------------------------------------
|
||
# Sessions
|
||
# --------------------------------------------------------
|
||
|
||
html += sessions_block(
|
||
conn,
|
||
context,
|
||
)
|
||
|
||
# --------------------------------------------------------
|
||
# Footer
|
||
# --------------------------------------------------------
|
||
|
||
html += page_end()
|
||
|
||
return html
|
||
|
||
# ============================================================
|
||
# All Capture Sessions
|
||
# ============================================================
|
||
|
||
def generate_capture_sessions(
|
||
conn,
|
||
context=None,
|
||
sessions_page_limit=None,
|
||
):
|
||
|
||
if sessions_page_limit is None:
|
||
display_limit = SESSIONS_PAGE_LIMIT
|
||
elif str(sessions_page_limit).lower() == "all":
|
||
display_limit = "all"
|
||
else:
|
||
try:
|
||
display_limit = int(sessions_page_limit)
|
||
if display_limit <= 0:
|
||
raise ValueError
|
||
except (TypeError, ValueError):
|
||
display_limit = SESSIONS_PAGE_LIMIT
|
||
|
||
sessions = all_sessions(
|
||
conn
|
||
)
|
||
|
||
html = page_begin(
|
||
|
||
title="🚗 Capture Sessions",
|
||
|
||
description=f"""
|
||
All capture sessions
|
||
<br>
|
||
Total sessions: <b>{len(sessions)}</b>
|
||
""",
|
||
|
||
active="capture_sessions",
|
||
|
||
context=context,
|
||
|
||
section_title="Reports",
|
||
|
||
)
|
||
|
||
cards = []
|
||
|
||
for session in sessions:
|
||
|
||
session_id = session["id"]
|
||
|
||
trip_start = session.get("trip_start")
|
||
trip_end = session.get("trip_end")
|
||
|
||
duration_seconds = ""
|
||
|
||
if trip_start and trip_end:
|
||
|
||
from .utils import parse_datetime
|
||
|
||
start = parse_datetime(trip_start)
|
||
end = parse_datetime(trip_end)
|
||
|
||
if start is not None and end is not None:
|
||
|
||
duration_seconds = max(
|
||
0,
|
||
int((end - start).total_seconds())
|
||
)
|
||
|
||
distance_m = session.get("distance_m")
|
||
|
||
distance_html = ""
|
||
|
||
distance_sort = ""
|
||
|
||
if distance_m is not None:
|
||
|
||
distance_sort = str(distance_m)
|
||
|
||
if distance_m >= 1000:
|
||
|
||
distance_label = (
|
||
f"{distance_m / 1000:.2f} km"
|
||
)
|
||
|
||
else:
|
||
|
||
distance_label = (
|
||
f"{distance_m:.0f} m"
|
||
)
|
||
|
||
distance_html = f"""
|
||
<span class="session-compact-stat">
|
||
🛣️ <b>Distance:</b> {distance_label}
|
||
</span>
|
||
"""
|
||
|
||
trip_start_html = (
|
||
format_datetime(trip_start)
|
||
if trip_start
|
||
else "-"
|
||
)
|
||
|
||
trip_end_html = (
|
||
format_datetime(trip_end)
|
||
if trip_end
|
||
else "-"
|
||
)
|
||
|
||
duration_html = (
|
||
esc(session.get("duration"))
|
||
if session.get("duration")
|
||
else "-"
|
||
)
|
||
|
||
access_points = session.get("access_points", 0)
|
||
|
||
cards.append(f"""
|
||
<article
|
||
class="session-compact-card"
|
||
data-session-id="{esc(session_id)}"
|
||
data-date="{esc(trip_start or "")}"
|
||
data-duration="{duration_seconds}"
|
||
data-distance="{distance_sort}"
|
||
data-access-points="{esc(access_points)}"
|
||
>
|
||
<div class="session-compact-main">
|
||
|
||
<div class="session-compact-title">
|
||
🚗 Session #{esc(session_id)}
|
||
</div>
|
||
|
||
<div class="session-compact-details">
|
||
|
||
<span>
|
||
<b>Trip start:</b> {trip_start_html}
|
||
</span>
|
||
|
||
<span>
|
||
<b>Trip end:</b> {trip_end_html}
|
||
</span>
|
||
|
||
<span>
|
||
<b>Duration:</b> {duration_html}
|
||
</span>
|
||
|
||
</div>
|
||
|
||
<div class="session-compact-stats">
|
||
|
||
{distance_html}
|
||
|
||
<span class="session-compact-stat">
|
||
📶 <b>Access Points:</b> {esc(access_points)}
|
||
</span>
|
||
|
||
</div>
|
||
|
||
</div>
|
||
|
||
<a
|
||
class="session-compact-link"
|
||
href="{esc(context.session_url(session_id))}"
|
||
>
|
||
View session →
|
||
</a>
|
||
|
||
</article>
|
||
""")
|
||
|
||
html += f"""
|
||
|
||
<div class="session-list-toolbar">
|
||
|
||
<label for="session-sort">
|
||
Sort by:
|
||
</label>
|
||
|
||
<select id="session-sort">
|
||
<option value="date">Date</option>
|
||
<option value="duration">Duration</option>
|
||
<option value="distance">Distance</option>
|
||
<option value="access-points">Access Points</option>
|
||
</select>
|
||
|
||
<button
|
||
type="button"
|
||
id="session-sort-direction"
|
||
aria-label="Reverse sort direction"
|
||
title="Reverse sort direction"
|
||
>
|
||
↓
|
||
</button>
|
||
|
||
<label for="session-limit">
|
||
Show:
|
||
</label>
|
||
|
||
<select id="session-limit">
|
||
<option value="10">10</option>
|
||
<option value="20">20</option>
|
||
<option value="50">50</option>
|
||
<option value="100">100</option>
|
||
<option value="500">500</option>
|
||
<option value="all">All</option>
|
||
<option value="custom">Custom</option>
|
||
</select>
|
||
|
||
<input
|
||
type="number"
|
||
id="session-custom-limit"
|
||
min="1"
|
||
step="1"
|
||
placeholder="Number"
|
||
aria-label="Custom number of sessions"
|
||
style="width: 100px;"
|
||
>
|
||
|
||
<span id="session-count"></span>
|
||
|
||
</div>
|
||
|
||
<div
|
||
class="session-list"
|
||
id="session-list"
|
||
data-sort-direction="desc"
|
||
data-display-limit="{display_limit}"
|
||
data-default-limit="{SESSIONS_PAGE_LIMIT}"
|
||
>
|
||
"""
|
||
|
||
html += "\n".join(cards)
|
||
|
||
html += """
|
||
|
||
</div>
|
||
|
||
<script>
|
||
(function () {
|
||
|
||
const list = document.getElementById("session-list");
|
||
const sortSelect = document.getElementById("session-sort");
|
||
const directionButton = document.getElementById("session-sort-direction");
|
||
const limitSelect = document.getElementById("session-limit");
|
||
const customLimitInput = document.getElementById("session-custom-limit");
|
||
const sessionCount = document.getElementById("session-count");
|
||
|
||
if (
|
||
!list ||
|
||
!sortSelect ||
|
||
!directionButton ||
|
||
!limitSelect ||
|
||
!customLimitInput ||
|
||
!sessionCount
|
||
) {
|
||
return;
|
||
}
|
||
|
||
let descending = true;
|
||
|
||
const defaultLimit = Number(list.dataset.defaultLimit) || 20;
|
||
const initialLimit = list.dataset.displayLimit;
|
||
const predefinedLimits = ["10", "20", "50", "100", "500", "all"];
|
||
|
||
function getDisplayLimit() {
|
||
|
||
if (limitSelect.value === "all") {
|
||
return "all";
|
||
}
|
||
|
||
if (limitSelect.value === "custom") {
|
||
const value = Number(customLimitInput.value);
|
||
|
||
if (
|
||
!Number.isInteger(value) ||
|
||
value <= 0
|
||
) {
|
||
return defaultLimit;
|
||
}
|
||
|
||
return value;
|
||
}
|
||
|
||
return Number(limitSelect.value) || defaultLimit;
|
||
}
|
||
|
||
function updateUrl() {
|
||
|
||
const params = new URLSearchParams(window.location.search);
|
||
const limit = getDisplayLimit();
|
||
|
||
params.set("limit", String(limit));
|
||
|
||
const query = params.toString();
|
||
const newUrl = window.location.pathname +
|
||
(query ? "?" + query : "") +
|
||
window.location.hash;
|
||
|
||
window.history.replaceState({}, "", newUrl);
|
||
}
|
||
|
||
function applyDisplayLimit() {
|
||
|
||
const cards = Array.from(
|
||
list.querySelectorAll(".session-compact-card")
|
||
);
|
||
|
||
const limit = getDisplayLimit();
|
||
const visibleCount = limit === "all"
|
||
? cards.length
|
||
: Math.min(cards.length, limit);
|
||
|
||
cards.forEach(function (card, index) {
|
||
card.style.display = index >= visibleCount ? "none" : "";
|
||
});
|
||
|
||
sessionCount.textContent =
|
||
"Showing " + visibleCount + " of " + cards.length;
|
||
}
|
||
|
||
function sortSessions() {
|
||
|
||
const sortBy = sortSelect.value;
|
||
|
||
const cards = Array.from(
|
||
list.querySelectorAll(".session-compact-card")
|
||
);
|
||
|
||
cards.sort(function (a, b) {
|
||
|
||
if (sortBy === "distance") {
|
||
|
||
const missingA = a.dataset.distance === "";
|
||
const missingB = b.dataset.distance === "";
|
||
|
||
if (missingA !== missingB) {
|
||
return missingA ? 1 : -1;
|
||
}
|
||
}
|
||
|
||
let valueA;
|
||
let valueB;
|
||
|
||
if (sortBy === "date") {
|
||
|
||
valueA = Date.parse(a.dataset.date) || 0;
|
||
valueB = Date.parse(b.dataset.date) || 0;
|
||
|
||
} else if (sortBy === "duration") {
|
||
|
||
valueA = Number(a.dataset.duration) || 0;
|
||
valueB = Number(b.dataset.duration) || 0;
|
||
|
||
} else if (sortBy === "distance") {
|
||
|
||
valueA = Number(a.dataset.distance) || 0;
|
||
valueB = Number(b.dataset.distance) || 0;
|
||
|
||
} else {
|
||
|
||
valueA = Number(a.dataset.accessPoints) || 0;
|
||
valueB = Number(b.dataset.accessPoints) || 0;
|
||
}
|
||
|
||
if (valueA === valueB) {
|
||
|
||
return Number(a.dataset.sessionId)
|
||
- Number(b.dataset.sessionId);
|
||
}
|
||
|
||
const result = valueA - valueB;
|
||
|
||
return descending ? -result : result;
|
||
});
|
||
|
||
cards.forEach(function (card) {
|
||
list.appendChild(card);
|
||
});
|
||
|
||
directionButton.textContent = descending ? "↓" : "↑";
|
||
|
||
list.dataset.sortDirection = descending ? "desc" : "asc";
|
||
|
||
applyDisplayLimit();
|
||
}
|
||
|
||
sortSelect.addEventListener("change", function () {
|
||
|
||
descending = true;
|
||
|
||
sortSessions();
|
||
});
|
||
|
||
directionButton.addEventListener("click", function () {
|
||
|
||
descending = !descending;
|
||
|
||
sortSessions();
|
||
});
|
||
|
||
limitSelect.addEventListener("change", function () {
|
||
|
||
if (limitSelect.value !== "custom") {
|
||
customLimitInput.value = "";
|
||
}
|
||
|
||
if (limitSelect.value === "custom") {
|
||
customLimitInput.focus();
|
||
}
|
||
|
||
applyDisplayLimit();
|
||
updateUrl();
|
||
});
|
||
|
||
customLimitInput.addEventListener("input", function () {
|
||
|
||
limitSelect.value = "custom";
|
||
|
||
const value = Number(customLimitInput.value);
|
||
|
||
if (
|
||
Number.isInteger(value) &&
|
||
value > 0
|
||
) {
|
||
applyDisplayLimit();
|
||
updateUrl();
|
||
} else {
|
||
applyDisplayLimit();
|
||
}
|
||
});
|
||
|
||
if (initialLimit === "all") {
|
||
|
||
limitSelect.value = "all";
|
||
|
||
} else if (predefinedLimits.includes(initialLimit)) {
|
||
|
||
limitSelect.value = initialLimit;
|
||
|
||
} else {
|
||
|
||
limitSelect.value = "custom";
|
||
customLimitInput.value = initialLimit;
|
||
}
|
||
|
||
sortSessions();
|
||
|
||
})();
|
||
</script>
|
||
|
||
"""
|
||
|
||
html += page_end()
|
||
|
||
return html |