OpenSlides/openslides/utils/autoupdate.py

172 lines
5.2 KiB
Python
Raw Normal View History

2018-01-20 13:57:25 +01:00
import threading
from typing import Any, Dict, Iterable, List, Optional, Tuple, Union
2015-01-18 15:53:03 +01:00
from asgiref.sync import async_to_sync
from channels.layers import get_channel_layer
2017-08-24 12:26:55 +02:00
from django.db.models import Model
from mypy_extensions import TypedDict
from .cache import element_cache, get_element_id
Element = TypedDict(
'Element',
{
'id': int,
'collection_string': str,
'full_data': Optional[Dict[str, Any]],
}
)
AutoupdateFormat = TypedDict(
'AutoupdateFormat',
{
'changed': Dict[str, List[Dict[str, Any]]],
'deleted': Dict[str, List[int]],
'from_change_id': int,
'to_change_id': int,
'all_data': bool,
},
)
2018-08-22 07:59:22 +02:00
def inform_changed_data(instances: Union[Iterable[Model], Model]) -> None:
"""
Informs the autoupdate system and the caching system about the creation or
update of an element.
2018-01-20 13:57:25 +01:00
The argument instances can be one instance or an iterable over instances.
"""
root_instances = set()
if not isinstance(instances, Iterable):
instances = (instances, )
for instance in instances:
try:
root_instances.add(instance.get_root_rest_element())
except AttributeError:
# Instance has no method get_root_rest_element. Just ignore it.
pass
elements: Dict[str, Element] = {}
for root_instance in root_instances:
2018-08-22 07:59:22 +02:00
key = root_instance.get_collection_string() + str(root_instance.get_rest_pk())
elements[key] = Element(
id=root_instance.get_rest_pk(),
collection_string=root_instance.get_collection_string(),
full_data=root_instance.get_full_data())
2018-01-20 13:57:25 +01:00
bundle = autoupdate_bundle.get(threading.get_ident())
if bundle is not None:
# Put all elements into the autoupdate_bundle.
bundle.update(elements)
else:
# Send autoupdate directly
handle_changed_elements(elements.values())
2016-09-30 20:42:58 +02:00
def inform_deleted_data(deleted_elements: Iterable[Tuple[str, int]]) -> None:
"""
Informs the autoupdate system and the caching system about the deletion of
elements.
"""
elements: Dict[str, Element] = {}
for deleted_element in deleted_elements:
key = deleted_element[0] + str(deleted_element[1])
elements[key] = Element(id=deleted_element[1], collection_string=deleted_element[0], full_data=None)
2018-01-20 13:57:25 +01:00
bundle = autoupdate_bundle.get(threading.get_ident())
if bundle is not None:
# Put all elements into the autoupdate_bundle.
bundle.update(elements)
else:
# Send autoupdate directly
handle_changed_elements(elements.values())
def inform_changed_elements(changed_elements: Iterable[Element]) -> None:
"""
Informs the autoupdate system about some collection elements. This is
used just to send some data to all users.
"""
elements = {}
for changed_element in changed_elements:
key = changed_element['collection_string'] + str(changed_element['id'])
elements[key] = changed_element
2018-01-20 13:57:25 +01:00
bundle = autoupdate_bundle.get(threading.get_ident())
if bundle is not None:
# Put all collection elements into the autoupdate_bundle.
bundle.update(elements)
else:
# Send autoupdate directly
handle_changed_elements(elements.values())
2018-01-20 13:57:25 +01:00
"""
Global container for autoupdate bundles
"""
autoupdate_bundle: Dict[int, Dict[str, Element]] = {}
2018-01-20 13:57:25 +01:00
class AutoupdateBundleMiddleware:
"""
Middleware to handle autoupdate bundling.
"""
2018-02-24 16:38:17 +01:00
def __init__(self, get_response: Any) -> None:
2018-01-20 13:57:25 +01:00
self.get_response = get_response
# One-time configuration and initialization.
2018-02-24 16:38:17 +01:00
def __call__(self, request: Any) -> Any:
2018-01-20 13:57:25 +01:00
thread_id = threading.get_ident()
autoupdate_bundle[thread_id] = {}
response = self.get_response(request)
bundle: Dict[str, Element] = autoupdate_bundle.pop(thread_id)
handle_changed_elements(bundle.values())
2018-01-20 13:57:25 +01:00
return response
def handle_changed_elements(elements: Iterable[Element]) -> None:
"""
Helper function, that sends elements through a channel to the
autoupdate system and updates the cache.
Does nothing if elements is empty.
"""
async def update_cache() -> int:
"""
Async helper function to update the cache.
Returns the change_id
"""
2018-08-22 22:00:08 +02:00
cache_elements: Dict[str, Optional[Dict[str, Any]]] = {}
for element in elements:
element_id = get_element_id(element['collection_string'], element['id'])
cache_elements[element_id] = element['full_data']
return await element_cache.change_elements(cache_elements)
async def async_handle_collection_elements() -> None:
"""
Async helper function to update cache and send autoupdate.
"""
# Update cache
change_id = await update_cache()
# Send autoupdate
channel_layer = get_channel_layer()
await channel_layer.group_send(
"autoupdate",
{
"type": "send_data",
"change_id": change_id,
},
)
if elements:
# TODO: Save histroy here using sync code
# Update cache and send autoupdate
async_to_sync(async_handle_collection_elements)()