Попытка оптимизации
This commit is contained in:
parent
47e75b4e13
commit
798e80e0a0
@ -219,30 +219,45 @@ async def handle_channel_transmission(channel, voice_call, websocket,
|
||||
|
||||
@sync_to_async
|
||||
def create_or_update_worker_batch(data_list):
|
||||
with transaction.atomic():
|
||||
# 1. Подготовка данных для WorkersLocation (основная модель)
|
||||
locations = [
|
||||
WorkersLocation(
|
||||
uid=data["abonent_id"],
|
||||
latitude=data["latitude"],
|
||||
longitude=data["longitude"],
|
||||
velocity=data["velocity"],
|
||||
radius=data["radius"],
|
||||
update_time=parser.isoparse(data["update_time"])
|
||||
)
|
||||
for data in data_list
|
||||
]
|
||||
from django.db import transaction
|
||||
|
||||
WorkersLocation.objects.bulk_create(
|
||||
locations,
|
||||
update_conflicts=True,
|
||||
update_fields=['latitude', 'longitude', 'velocity', 'radius',
|
||||
'update_time'],
|
||||
unique_field='uid'
|
||||
)
|
||||
with transaction.atomic():
|
||||
worker_ids = [data["abonent_id"] for data in data_list]
|
||||
existing_workers = WorkersLocation.objects.filter(
|
||||
uid__in=worker_ids).in_bulk(field_name='uid')
|
||||
|
||||
to_update = []
|
||||
to_create = []
|
||||
|
||||
for data in data_list:
|
||||
worker_data = {
|
||||
'latitude': data["latitude"],
|
||||
'longitude': data["longitude"],
|
||||
'velocity': data["velocity"],
|
||||
'radius': data["radius"],
|
||||
'update_time': parser.isoparse(data["update_time"])
|
||||
}
|
||||
|
||||
if data["abonent_id"] in existing_workers:
|
||||
worker = existing_workers[data["abonent_id"]]
|
||||
for field, value in worker_data.items():
|
||||
setattr(worker, field, value)
|
||||
to_update.append(worker)
|
||||
else:
|
||||
to_create.append(
|
||||
WorkersLocation(uid=data["abonent_id"], **worker_data))
|
||||
|
||||
if to_create:
|
||||
WorkersLocation.objects.bulk_create(to_create)
|
||||
|
||||
if to_update:
|
||||
fields = ['latitude', 'longitude', 'velocity', 'radius',
|
||||
'update_time']
|
||||
WorkersLocation.objects.bulk_update(to_update, fields)
|
||||
|
||||
worker_ids = {data["abonent_id"] for data in data_list}
|
||||
workers = WorkersLocation.objects.in_bulk(worker_ids, field_name='uid')
|
||||
workers = WorkersLocation.objects.filter(uid__in=worker_ids).in_bulk(
|
||||
field_name='uid')
|
||||
|
||||
tracking_objects = [
|
||||
WorkersTracking(
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user