Files
test_dashboard/server/scanner.py
T

217 lines
6.6 KiB
Python
Raw Normal View History

2026-05-26 14:36:34 -04:00
import os
import re
2026-05-27 11:29:02 -04:00
from db_py import clear_tests, get_all_tests, mark_completed, set_coe_pair, upsert_test
2026-05-26 14:36:34 -04:00
from parser import (
parse_elapsed_time,
parse_result_filename,
parse_target_filename,
parse_timestamp,
parse_tput_rssi,
)
def full_scan(target_dir, results_dir):
clear_tests()
if not target_dir or not results_dir:
return
print(f"[scanner] target dir : {target_dir}")
print(f"[scanner] results dir: {results_dir}")
scan_targets(target_dir)
scan_results(results_dir)
2026-05-27 11:29:02 -04:00
update_p2p_coe_pairs()
def update_p2p_coe_pairs():
pair_fields = [
"device",
"rotation",
"test_point",
"rssi",
"station",
"band",
"channel",
"bandwidth",
"direction",
]
tests = get_all_tests()
coe_by_key = {}
for test in tests:
if test.get("interference") != "COE":
continue
key = tuple(test.get(field) for field in pair_fields)
device = test.get("device")
test_id = test.get("test_id")
if not device or not test_id:
continue
coe_by_key.setdefault(key, []).append(f"{device}_{test_id}")
updated = 0
for test in tests:
if test.get("interference") != "P2P":
continue
key = tuple(test.get(field) for field in pair_fields)
pairs = sorted(set(coe_by_key.get(key, [])))
set_coe_pair(test.get("id"), pairs)
updated += 1
print(f"[scanner] coe_pair updated for {updated} P2P test(s)")
2026-05-26 14:36:34 -04:00
def scan_targets(target_dir):
try:
parent_entries = [
name
for name in os.listdir(target_dir)
if os.path.isdir(os.path.join(target_dir, name))
]
except OSError as exc:
print(f"[scanner] Cannot read target dir: {exc}")
return
print(f"[scanner] subdirectories found: {len(parent_entries)}")
for parent_name in parent_entries:
parent_path = os.path.join(target_dir, parent_name)
try:
files = [
name
for name in os.listdir(parent_path)
if os.path.isfile(os.path.join(parent_path, name))
and not name.startswith("GLOBAL")
and name.endswith(".ini")
]
except OSError as exc:
print(f"[scanner] Cannot read parent dir {parent_name}: {exc}")
continue
print(f"[scanner] {parent_name} -> {len(files)} target test file(s)")
for filename in files:
parsed = parse_target_filename(filename, parent_name)
if not parsed["test_id"]:
print(f"[scanner] skip (no test_id): {parent_name}/{filename}")
continue
print(
f"[scanner] found: {parent_name}/{filename} -> test_id={parsed['test_id']}"
)
upsert_test(
{
"id": parsed["id"],
"test_id": parsed["test_id"],
"parent_dir": parent_name,
"filename": filename,
"interference": parsed["interference"],
"device": parsed["device"],
"rotation": parsed["rotation"],
"test_point": parsed["test_point"],
"station": parsed["station"],
"band": parsed["band"],
"channel": parsed["channel"],
"bandwidth": parsed["bandwidth"],
"rssi": parsed["rssi"],
"direction": parsed["direction"],
2026-05-27 10:29:26 -04:00
"throttled": parsed["throttled"],
2026-05-26 14:36:34 -04:00
}
)
def scan_results(results_dir):
try:
entries = [
name
for name in os.listdir(results_dir)
if os.path.isdir(os.path.join(results_dir, name))
]
except OSError as exc:
print(f"[scanner] Cannot read results dir: {exc}")
return
print(f"[scanner] results: {len(entries)} result dir(s) found")
for dir_name in entries:
process_result_dir(results_dir, dir_name)
def process_result_dir(results_dir, result_dir_name):
parsed = parse_result_filename(result_dir_name)
test_id = parsed["test_id"]
device = parsed["device"]
if not test_id or not device:
return
result_dir_path = os.path.join(results_dir, result_dir_name)
try:
log_files = [
name
for name in os.listdir(result_dir_path)
if os.path.isfile(os.path.join(result_dir_path, name))
and name.endswith(".txt")
and test_id in name
]
except OSError as exc:
print(f"[scanner] Cannot read result dir {result_dir_name}: {exc}")
return
if not log_files:
print(f"[scanner] completed (no log yet): {test_id}")
mark_completed(test_id, device, None, None)
return
latest_log = sorted(log_files)[-1]
log_path = os.path.join(result_dir_path, latest_log)
completed_at = parse_timestamp(latest_log)
data = extract_log_data(log_path)
print(
f"[scanner] completed: {test_id} device={device} "
f"duration={data['duration_seconds']}s stations={len(data['tputResults'])} at={completed_at}"
)
mark_completed(
test_id,
device,
completed_at,
data["duration_seconds"],
data["tputResults"],
)
def parse_deleted_result_dir_name(dir_name):
segments = re.split(r"[_\-]", dir_name)
test_id = next((s for s in segments if re.match(r"^R\d+[A-Z0-9]+$", s, re.IGNORECASE)), None)
device = next((s for s in segments if re.match(r"^CGW\d+$", s, re.IGNORECASE)), None)
return test_id, device
def extract_log_data(log_file_path):
elapsed_regex = re.compile(r"\[.*?INFO\]\s+Elapsed time\s*:\s*([\d]+:[\d]{2}:[\d]{2}\.[\d]+)")
tput_regex = re.compile(
r"\[.*?INFO\]\s+STA(\d+) over angles >> AVG IxChariot TPUT: (\d+) Mbps, AVG DL RSSI: (-?\d+) dBm, AVG UL RSSI: (-?\d+) dBm"
)
result = {"duration_seconds": None, "tputResults": []}
try:
with open(log_file_path, "r", encoding="utf-8", errors="ignore") as fh:
for line in fh:
time_match = elapsed_regex.search(line)
if time_match:
result["duration_seconds"] = parse_elapsed_time(time_match.group(1))
continue
if tput_regex.search(line):
parsed = parse_tput_rssi(line)
if parsed:
result["tputResults"].append(parsed)
except OSError as exc:
print(f"[scanner] Cannot read log file {log_file_path}: {exc}")
return result