diff --git a/frogpilot/system/frogpilot_stats.py b/frogpilot/system/frogpilot_stats.py index b488113ca..28090f16d 100644 --- a/frogpilot/system/frogpilot_stats.py +++ b/frogpilot/system/frogpilot_stats.py @@ -20,6 +20,30 @@ from openpilot.frogpilot.common.frogpilot_variables import get_frogpilot_toggles BASE_URL = "https://nominatim.openstreetmap.org" MINIMUM_POPULATION = 100_000 +SEARCH_RADIUS_DEGREES = 1.45 + +def get_population_value(population_str): + if population_str is None: + return None + try: + return int(str(population_str).replace(",", "").split(";")[0].strip()) + except Exception: + return None + +def search_nearby_major_cities(lat, lon, session, state_name, country_name): + viewbox = f"{lon - SEARCH_RADIUS_DEGREES},{lat + SEARCH_RADIUS_DEGREES},{lon + SEARCH_RADIUS_DEGREES},{lat - SEARCH_RADIUS_DEGREES}" + cities = (session.get(f"{BASE_URL}/search", params={ + "addressdetails": 1, "bounded": 1, "extratags": 1, "format": "jsonv2", "limit": 20, "q": "city", "viewbox": viewbox + }, timeout=10).json() or []) + + qualifying = [c for c in cities if (get_population_value((c.get("extratags") or {}).get("population")) or 0) >= MINIMUM_POPULATION] + if not qualifying: + return None + + nearest = min(qualifying, key=lambda c: (float(c["lat"]) - lat) ** 2 + (float(c["lon"]) - lon) ** 2) + addr = nearest.get("address") or {} + return float(nearest["lat"]), float(nearest["lon"]), addr.get("city") or addr.get("town") or nearest.get("display_name", "").split(",")[0], state_name, country_name + def get_city_center(latitude, longitude): try: @@ -44,14 +68,7 @@ def get_city_center(latitude, longitude): if data: tags = data[0] - population = (tags.get("extratags") or {}).get("population") - - population_value = None - if population is not None: - try: - population_value = int(str(population).replace(",", "").split(";")[0].strip()) - except Exception: - population_value = None + population_value = get_population_value((tags.get("extratags") or {}).get("population")) if population_value is not None and population_value >= MINIMUM_POPULATION: latitude_value = float(tags["lat"]) @@ -62,6 +79,10 @@ def get_city_center(latitude, longitude): return latitude_value, longitude_value, city_label, state_name, country_name + nearby_result = search_nearby_major_cities(latitude, longitude, session, state_name, country_name) + if nearby_result: + return nearby_result + query = f"{state_name} state capital" if country_code == "us" else f"capital of {state_name}, {country_name}" response = session.get(f"{BASE_URL}/search", params={"addressdetails": 1, "extratags": 1, "format": "jsonv2", "limit": 5, "q": query}, timeout=10) response.raise_for_status() @@ -129,11 +150,9 @@ def send_stats(): if frogpilot_toggles.car_make == "mock": return - bucket = os.environ.get("STATS_BUCKET", "") - org_ID = os.environ.get("STATS_ORG_ID", "") - token = os.environ.get("STATS_TOKEN", "") - url = os.environ.get("STATS_URL", "") - + bucket = "StarPilot" + org_ID = "StarPilot" + url = "https://stats.firestar.link" frogpilot_stats = json.loads(params.get("FrogPilotStats") or "{}") location = json.loads(params.get("LastGPSPosition") or "{}") @@ -161,14 +180,19 @@ def send_stats(): user_point = ( Point("user_stats") + .tag("car_make", "GM" if frogpilot_toggles.car_make == "gm" else frogpilot_toggles.car_make.title()) + .tag("car_model", frogpilot_toggles.car_model) + .tag("city", city) + .tag("country", country) + .tag("device", HARDWARE.get_device_type()) + .tag("driving_model", clean_model_name(frogpilot_toggles.model_name)) + .tag("state", state) + .tag("theme", selected_theme.title()) + .tag("branch", build_metadata.channel) + .tag("dongle_id", params.get("FrogPilotDongleId", encoding="utf-8")) + .field("blocked_user", frogpilot_toggles.block_user) - .field("car_make", "GM" if frogpilot_toggles.car_make == "gm" else frogpilot_toggles.car_make.title()) - .field("car_model", frogpilot_toggles.car_model) - .field("city", city) - .field("country", country) .field("current_months_kilometers", int(frogpilot_stats.get("CurrentMonthsKilometers", 0))) - .field("device", HARDWARE.get_device_type()) - .field("driving_model", clean_model_name(frogpilot_toggles.model_name)) .field("event", 1) .field("frogpilot_drives", int(frogpilot_stats.get("FrogPilotDrives", 0))) .field("frogpilot_hours", float(frogpilot_stats.get("FrogPilotSeconds", 0)) / (60 * 60)) @@ -184,8 +208,6 @@ def send_stats(): .field("longitude", longitude) .field("rainbow_path", frogpilot_toggles.rainbow_path) .field("random_events", frogpilot_toggles.random_events) - .field("state", state) - .field("theme", selected_theme.title()) .field("total_aol_seconds", float(frogpilot_stats.get("AOLTime", 0))) .field("total_lateral_seconds", float(frogpilot_stats.get("LateralTime", 0))) .field("total_longitudinal_seconds", float(frogpilot_stats.get("LongitudinalTime", 0))) @@ -194,15 +216,12 @@ def send_stats(): .field("up_to_date", is_up_to_date(build_metadata)) .field("using_stock_acc", not (frogpilot_toggles.has_cc_long or frogpilot_toggles.openpilot_longitudinal)) - .tag("branch", build_metadata.channel) - .tag("dongle_id", params.get("FrogPilotDongleId", encoding="utf-8")) - .time(now) ) all_points = [user_point] + update_branch_commits(now) - client = InfluxDBClient(org=org_ID, token=token, url=url) + client = InfluxDBClient(org=org_ID, token=org_ID, url=url) client.write_api(write_options=SYNCHRONOUS).write(bucket=bucket, org=org_ID, record=all_points) print("Successfully sent FrogPilot stats!") except Exception as exception: diff --git a/system/sentry.py b/system/sentry.py index ae7b37a00..62240d0da 100644 --- a/system/sentry.py +++ b/system/sentry.py @@ -1,5 +1,6 @@ -"""Install exception handler for process crash.""" +import glob import os +import re import sentry_sdk import traceback from datetime import datetime @@ -9,15 +10,16 @@ from sentry_sdk.integrations.threading import ThreadingIntegration from openpilot.common.params import Params from openpilot.system.hardware import HARDWARE, PC from openpilot.common.swaglog import cloudlog +from openpilot.system.hardware.hw import Paths from openpilot.system.version import get_build_metadata, get_version from openpilot.frogpilot.common.frogpilot_variables import ERROR_LOGS_PATH, params class SentryProject(Enum): # python project - SELFDRIVE = os.environ.get("SENTRY_DSN", "") + SELFDRIVE = "https://7305139359a548fcb348ec09497dc389@bugsink.firestar.link/1" # native project - SELFDRIVE_NATIVE = os.environ.get("SENTRY_DSN", "") + SELFDRIVE_NATIVE = "https://7305139359a548fcb348ec09497dc389@bugsink.firestar.link/1" def report_tombstone(fn: str, message: str, contents: str) -> None: @@ -26,6 +28,12 @@ def report_tombstone(fn: str, message: str, contents: str) -> None: with sentry_sdk.configure_scope() as scope: scope.set_extra("tombstone_fn", fn) scope.set_extra("tombstone", contents) + + # Attach qlog for debugging context + qlogs = glob.glob(f"{Paths.log_root()}/*/qlog") + if qlogs: + scope.add_attachment(path=max(qlogs, key=os.path.getmtime), filename="qlog") + sentry_sdk.capture_message(message=message) sentry_sdk.flush() @@ -51,8 +59,14 @@ def capture_exception(*args, crash_log=True, **kwargs) -> None: cloudlog.error("crash", exc_info=kwargs.get('exc_info', 1)) try: - sentry_sdk.capture_exception(*args, **kwargs) - sentry_sdk.flush() # https://github.com/getsentry/sentry-python/issues/291 + with sentry_sdk.push_scope() as scope: + # Attach qlog for debugging context + qlogs = glob.glob(f"{Paths.log_root()}/*/qlog") + if qlogs: + scope.add_attachment(path=max(qlogs, key=os.path.getmtime), filename="qlog") + + sentry_sdk.capture_exception(*args, **kwargs) + sentry_sdk.flush() # https://github.com/getsentry/sentry-python/issues/291 except Exception: cloudlog.exception("sentry exception") @@ -90,25 +104,15 @@ def save_exception(exc_text: str, crash_log) -> None: def init(project: SentryProject) -> bool: - build_metadata = get_build_metadata() - FrogPilot = "frogai" in build_metadata.openpilot.git_origin.lower() - if not FrogPilot or PC: + if PC: return False + build_metadata = get_build_metadata() short_branch = build_metadata.channel - if short_branch in ["COMMA", "HEAD"]: - return - elif short_branch == "FrogPilot-Development": - env = "Development" - elif build_metadata.release_channel: - env = "Release" - elif short_branch == "FrogPilot-Testing": + env = short_branch + if re.search("test", short_branch, re.IGNORECASE): env = "Testing" - elif build_metadata.tested_channel: - env = "Staging" - else: - env = short_branch dongle_id = params.get("DongleId", encoding="utf-8") installed = params.get("InstallDate", encoding="utf-8")