from collections import defaultdict
from contextlib import contextmanager

from extras.events import flush_events
from netbox.context import current_request, events_queue, query_cache
from netbox.utils import register_request_processor


@register_request_processor
@contextmanager
def event_tracking(request):
    """
    Queue interesting events in memory while processing a request, then flush that queue for processing by the
    events pipline before returning the response.

    :param request: WSGIRequest object with a unique `id` set
    """
    current_request.set(request)
    events_queue.set({})
    query_cache.set(defaultdict(dict))

    yield

    # Flush queued webhooks to RQ
    if events := list(events_queue.get().values()):
        flush_events(events)

    # Clear context vars
    current_request.set(None)
    events_queue.set({})
    query_cache.set(None)
