Jambase Calls reduziert

This commit is contained in:
ecki
2026-09-20 17:21:38 +02:00
parent 3018573b22
commit bbfda72bc0
5 changed files with 267 additions and 72 deletions
+112 -1
View File
@@ -137,6 +137,7 @@ def init_provider_sync_state(provider_name: str) -> dict:
"error_terms": [],
"event_count": 0,
"raw_event_count": 0,
"api_call_count": 0,
"last_message": "",
}
@@ -148,6 +149,7 @@ def record_provider_sync_state(
message: str,
event_count: int = 0,
raw_event_count: int | None = None,
api_call_count: int | None = None,
):
if PROVIDER_STATUS_PRIORITY[status] > PROVIDER_STATUS_PRIORITY[state["status"]]:
state["status"] = status
@@ -157,6 +159,8 @@ def record_provider_sync_state(
state["event_count"] += event_count
if raw_event_count is not None:
state["raw_event_count"] += raw_event_count
if api_call_count is not None:
state["api_call_count"] += api_call_count
elif status == ProviderStatusType.blocked:
if term not in state["blocked_terms"]:
state["blocked_terms"].append(term)
@@ -188,6 +192,11 @@ def build_provider_status_message(state: dict) -> str:
if state["raw_event_count"]
else ""
)
+ (
f" {state['api_call_count']} API-Calls."
if state["api_call_count"]
else ""
)
)
@@ -394,6 +403,12 @@ def run_sync(db: Session, force_providers: bool = False) -> SyncResult:
for provider in providers
if provider.source_name not in skipped_provider_messages
]
bulk_providers = [
provider for provider in providers_to_sync if hasattr(provider, "search_events_bulk")
]
regular_providers = [
provider for provider in providers_to_sync if provider not in bulk_providers
]
provider_states = {
provider.source_name: init_provider_sync_state(provider.source_name)
for provider in providers_to_sync
@@ -481,7 +496,7 @@ def run_sync(db: Session, force_providers: bool = False) -> SyncResult:
prune_stale_source_events(db, watch_item, source_name, seen_source_event_ids)
db.commit()
for provider in providers_to_sync:
for provider in regular_providers:
try:
events = provider.search_events(
term=watch_item.name,
@@ -501,6 +516,7 @@ def run_sync(db: Session, force_providers: bool = False) -> SyncResult:
message=provider_message,
event_count=len(events),
raw_event_count=getattr(provider, "last_raw_count", None),
api_call_count=getattr(provider, "last_api_call_count", None),
)
except Exception as exc:
provider_message = getattr(
@@ -567,6 +583,101 @@ def run_sync(db: Session, force_providers: bool = False) -> SyncResult:
notifications_skipped += 1
db.commit()
watch_items_by_id = {watch_item.id: watch_item for watch_item in active_items}
for provider in bulk_providers:
try:
events_by_watch_item_id = provider.search_events_bulk(active_items)
provider_status_name = getattr(provider, "last_status", "ok")
provider_message = getattr(
provider,
"last_message",
f"{provider.source_name} completed bulk sync.",
)
state = provider_states[provider.source_name]
provider_status = ProviderStatusType(provider_status_name)
if PROVIDER_STATUS_PRIORITY[provider_status] > PROVIDER_STATUS_PRIORITY[state["status"]]:
state["status"] = provider_status
if provider_status == ProviderStatusType.ok:
state["ok_count"] += len(active_items)
state["event_count"] += sum(
len(events) for events in events_by_watch_item_id.values()
)
state["raw_event_count"] += getattr(provider, "last_raw_count", 0) or 0
state["api_call_count"] += getattr(provider, "last_api_call_count", 0) or 0
elif provider_status == ProviderStatusType.blocked:
state["blocked_terms"].append("Bulk-Sync")
elif provider_status == ProviderStatusType.error:
state["error_terms"].append("Bulk-Sync")
state["last_message"] = provider_message
except Exception as exc:
provider_message = getattr(
provider,
"last_message",
f"Provider bulk sync failed for {provider.source_name}.",
)
if not provider_message:
provider_message = (
f"{provider.source_name} bulk sync failed: "
f"{type(exc).__name__}: {exc}"
)
logger.exception(
"Provider bulk sync failed for provider=%s",
provider.source_name,
)
db.rollback()
record_provider_sync_state(
state=provider_states[provider.source_name],
status=ProviderStatusType.error,
term="Bulk-Sync",
message=provider_message,
)
continue
for watch_item_id, events in events_by_watch_item_id.items():
watch_item = watch_items_by_id.get(watch_item_id)
if watch_item is None:
continue
for event_data in events:
tracked_event, is_new = upsert_event(
db=db,
watch_item=watch_item,
provider_name=provider.source_name,
event_data=event_data,
)
if is_new:
new_events += 1
else:
updated_events += 1
should_notify = (
is_new
and tracked_event.discovery_notified_at is None
and not has_equivalent_existing_event(db, tracked_event)
)
if should_notify:
status = send_email_notification(
db=db,
tracked_event=tracked_event,
notification_type=NotificationType.discovery,
subject=f"Neuer Termin fuer {watch_item.name}",
body=(
f"Es wurde ein neuer Termin fuer '{watch_item.name}' gefunden.\n\n"
f"Quelle: {tracked_event.source}\n"
f"Titel: {tracked_event.title}\n"
f"Ort: {tracked_event.venue_name or 'unbekannt'}\n"
f"Stadt: {tracked_event.city or 'unbekannt'}\n"
f"Datum: {tracked_event.event_date or 'unbekannt'}\n"
f"Tickets: {tracked_event.ticket_url or 'keine URL'}\n"
),
)
if status == NotificationStatus.sent:
tracked_event.discovery_notified_at = datetime.utcnow()
notifications_sent += 1
else:
notifications_skipped += 1
db.commit()
for provider_name, state in provider_states.items():
update_provider_status(
db=db,