Skip to content
Merged
Show file tree
Hide file tree
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
13 changes: 7 additions & 6 deletions backend/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,11 +325,11 @@ def api_root():
def read_tests(request: Request, db: Session = Depends(get_db)):
"""Return aggregated test records for the last ten minutes.

Multiple tests from the same ``client_ip`` within the last ten minutes are
collapsed into a single entry with average ``ping_ms``,
``download_mbps`` and ``upload_mbps`` values computed directly in the
database. The most recent ``timestamp`` for each IP is retained so that
results remain chronologically ordered.
Multiple tests from the same ``client_ip`` and ``speedtest_type`` within
the last ten minutes are collapsed into a single entry with average
``ping_ms``, ``download_mbps`` and ``upload_mbps`` values computed directly
in the database. The most recent ``timestamp`` for each group is retained
so that results remain chronologically ordered.
"""

ten_min_ago = datetime.utcnow() - timedelta(minutes=10)
Expand All @@ -348,9 +348,10 @@ def read_tests(request: Request, db: Session = Depends(get_db)):
func.avg(models.TestRecord.upload_mbps).label("upload_mbps"),
func.max(models.TestRecord.timestamp).label("timestamp"),
func.max(models.TestRecord.test_target).label("test_target"),
func.max(models.TestRecord.speedtest_type).label("speedtest_type"),
)
.filter(models.TestRecord.timestamp >= ten_min_ago)
.group_by(models.TestRecord.client_ip)
.group_by(models.TestRecord.client_ip, models.TestRecord.speedtest_type)
.order_by(func.max(models.TestRecord.timestamp).desc())
)

Expand Down
37 changes: 31 additions & 6 deletions backend/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def test_create_speedtest_record():
assert data["download_mbps"] == 10.0


def test_recent_tests_are_aggregated_by_ip():
def test_recent_tests_are_aggregated_by_ip_and_type():
from datetime import datetime, timedelta
from backend.database import SessionLocal
from backend.models import TestRecord
Expand All @@ -116,20 +116,31 @@ def test_recent_tests_are_aggregated_by_ip():
records = [
TestRecord(
client_ip="1.1.1.1",
speedtest_type="single",
ping_ms=10,
download_mbps=20,
upload_mbps=5,
timestamp=now - timedelta(minutes=5),
),
TestRecord(
client_ip="1.1.1.1",
speedtest_type="single",
ping_ms=20,
download_mbps=30,
upload_mbps=15,
timestamp=now - timedelta(minutes=4),
),
TestRecord(
client_ip="1.1.1.1",
speedtest_type="multi",
ping_ms=25,
download_mbps=50,
upload_mbps=25,
timestamp=now - timedelta(minutes=3),
),
TestRecord(
client_ip="2.2.2.2",
speedtest_type="single",
ping_ms=50,
download_mbps=60,
upload_mbps=70,
Expand All @@ -138,6 +149,7 @@ def test_recent_tests_are_aggregated_by_ip():
# Outside the 10 minute window and should be ignored
TestRecord(
client_ip="1.1.1.1",
speedtest_type="single",
ping_ms=30,
download_mbps=40,
upload_mbps=25,
Expand All @@ -152,12 +164,25 @@ def test_recent_tests_are_aggregated_by_ip():
res = client.get("/tests")
assert res.status_code == 200
data = res.json()
assert len(data["records"]) == 2
assert len(data["records"]) == 3

rec = next(r for r in data["records"] if r["client_ip"] == "1.1.1.1")
assert abs(rec["ping_ms"] - 15) < 0.01
assert abs(rec["download_mbps"] - 25) < 0.01
assert abs(rec["upload_mbps"] - 10) < 0.01
rec_single = next(
r
for r in data["records"]
if r["client_ip"] == "1.1.1.1" and r["speedtest_type"] == "single"
)
assert abs(rec_single["ping_ms"] - 15) < 0.01
assert abs(rec_single["download_mbps"] - 25) < 0.01
assert abs(rec_single["upload_mbps"] - 10) < 0.01

rec_multi = next(
r
for r in data["records"]
if r["client_ip"] == "1.1.1.1" and r["speedtest_type"] == "multi"
)
assert abs(rec_multi["ping_ms"] - 25) < 0.01
assert abs(rec_multi["download_mbps"] - 50) < 0.01
assert abs(rec_multi["upload_mbps"] - 25) < 0.01


def test_create_test_merges_recent_records():
Expand Down
Loading