Фикс GTO
This commit is contained in:
parent
4f523c46c4
commit
7b3e55639b
21
apps/gto/migrations/0020_auto_20250605_1323.py
Normal file
21
apps/gto/migrations/0020_auto_20250605_1323.py
Normal file
@ -0,0 +1,21 @@
|
||||
# Generated by Django 3.2.25 on 2025-06-05 13:23
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('gto', '0019_alter_equipmenttracking_options'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.RemoveIndex(
|
||||
model_name='equipmenttracking',
|
||||
name='gto_equipme_equipme_7c0944_idx',
|
||||
),
|
||||
migrations.AddIndex(
|
||||
model_name='equipmenttracking',
|
||||
index=models.Index(fields=['equipment', '-time'], name='gto_equipme_equipme_490e56_idx'),
|
||||
),
|
||||
]
|
||||
@ -36,7 +36,8 @@ class EquipmentTracking(models.Model): # Передвижение оборуд
|
||||
icon_obj = ModelTypeIcons.objects.filter(model=type_g).first()
|
||||
if icon_obj is not None:
|
||||
icon = icon_obj.icon.url
|
||||
connection_condition = self.equipment.get_condition(self.time, time_filter)
|
||||
connection_condition = self.equipment.get_condition(self.time,
|
||||
time_filter)
|
||||
|
||||
return {
|
||||
"id": self.equipment.id,
|
||||
@ -69,7 +70,7 @@ class EquipmentTracking(models.Model): # Передвижение оборуд
|
||||
),
|
||||
models.Index(
|
||||
fields=[
|
||||
"equipment",
|
||||
"equipment", "-time"
|
||||
]
|
||||
),
|
||||
models.Index(
|
||||
|
||||
@ -6,7 +6,9 @@ from datetime import datetime, timedelta
|
||||
import requests
|
||||
from django.contrib.auth.decorators import login_required
|
||||
from django.core.paginator import Paginator
|
||||
from django.db.models import Q, Max, OuterRef, Subquery
|
||||
from django.core.serializers.json import DjangoJSONEncoder
|
||||
from django.db.models import Q, Max, OuterRef, Subquery, Window, F
|
||||
from django.db.models.functions import RowNumber
|
||||
from django.http import HttpResponse, JsonResponse
|
||||
from django.shortcuts import redirect, render
|
||||
from django.utils import timezone
|
||||
@ -29,35 +31,86 @@ from sabron.util import logging
|
||||
def update_object(request):
|
||||
try:
|
||||
now = get_current_time()
|
||||
week_diff = now - timedelta(days=7)
|
||||
week_ago = now - timedelta(days=7)
|
||||
time_filter = request.GET.get("time")
|
||||
|
||||
if time_filter:
|
||||
time_filter = time_filter.rstrip('Z') # удаляет Z в конце
|
||||
time_filter = datetime.fromisoformat(time_filter).replace(tzinfo=utc)
|
||||
icons_map = {
|
||||
icon.model.strip(): icon.icon.url
|
||||
for icon in ModelTypeIcons.objects.all()
|
||||
}
|
||||
|
||||
subquery = EquipmentTracking.objects.filter(
|
||||
result = []
|
||||
|
||||
if time_filter:
|
||||
time_filter = datetime.fromisoformat(
|
||||
time_filter.rstrip('Z')).replace(tzinfo=utc)
|
||||
|
||||
latest_time_subquery = EquipmentTracking.objects.filter(
|
||||
equipment=OuterRef('equipment'),
|
||||
equipment__block=False,
|
||||
time__lt=time_filter
|
||||
).order_by('-time').values('time')[:1]
|
||||
|
||||
object_list = EquipmentTracking.objects.filter(
|
||||
# Берём последние точки трекинга до time_filter
|
||||
tracking_qs = EquipmentTracking.objects.filter(
|
||||
equipment__block=False,
|
||||
time__lt=time_filter,
|
||||
time=Subquery(subquery)
|
||||
).distinct('equipment')
|
||||
time=Subquery(latest_time_subquery),
|
||||
time__lt=time_filter
|
||||
).select_related('equipment')
|
||||
|
||||
for obj in tracking_qs:
|
||||
eq = obj.equipment
|
||||
if eq is None:
|
||||
continue # safety
|
||||
|
||||
type_g = eq.model.strip()
|
||||
icon = icons_map.get(type_g, "")
|
||||
condition = eq.get_condition(obj.time, time_filter)
|
||||
|
||||
result.append({
|
||||
"id": eq.id,
|
||||
"name": eq.name,
|
||||
"lat": obj.lat,
|
||||
"lon": obj.lon,
|
||||
"time": obj.time.strftime("%d.%m.%Y %H:%M:%S"),
|
||||
"iso_time": obj.time.isoformat(),
|
||||
"statenum": eq.statenum,
|
||||
"model": eq.model,
|
||||
"icon": icon,
|
||||
"garagenum": eq.garagenum,
|
||||
"condition": condition
|
||||
})
|
||||
else:
|
||||
object_list = Equipment.objects.filter(
|
||||
# Только последние координаты за неделю
|
||||
equipment_qs = Equipment.objects.filter(
|
||||
block=False,
|
||||
time__gt=week_diff
|
||||
).all()
|
||||
res = []
|
||||
for obj in object_list:
|
||||
res.append(obj.to_dict(time_filter))
|
||||
return JsonResponse(res, safe=False)
|
||||
except Exception as err:
|
||||
time__gt=week_ago
|
||||
)
|
||||
|
||||
for eq in equipment_qs:
|
||||
type_g = eq.model.strip()
|
||||
icon = icons_map.get(type_g, "")
|
||||
condition = eq.get_condition(eq.time, now)
|
||||
|
||||
result.append({
|
||||
"id": eq.id,
|
||||
"name": eq.name,
|
||||
"lat": eq.lat,
|
||||
"lon": eq.lon,
|
||||
"time": eq.time.strftime("%d.%m.%Y %H:%M:%S"),
|
||||
"iso_time": eq.time.isoformat(),
|
||||
"statenum": eq.statenum,
|
||||
"model": eq.model,
|
||||
"icon": icon,
|
||||
"garagenum": eq.garagenum,
|
||||
"condition": condition
|
||||
})
|
||||
|
||||
return JsonResponse(result, safe=False)
|
||||
|
||||
except Exception:
|
||||
logging.error(traceback.format_exc())
|
||||
return JsonResponse({"error": "Internal server error"}, status=500)
|
||||
|
||||
|
||||
@login_required(login_url="/")
|
||||
|
||||
115
apps/main/migrations/0013_auto_20250605_0907.py
Normal file
115
apps/main/migrations/0013_auto_20250605_0907.py
Normal file
@ -0,0 +1,115 @@
|
||||
# Generated by Django 3.2.25 on 2025-06-05 09:07
|
||||
|
||||
from django.db import migrations
|
||||
|
||||
|
||||
def create_schedules(apps, schema_editor):
|
||||
IntervalSchedule = apps.get_model('django_celery_beat', 'IntervalSchedule')
|
||||
PeriodicTask = apps.get_model('django_celery_beat', 'PeriodicTask')
|
||||
|
||||
interval1 = IntervalSchedule.objects.create(
|
||||
id=1,
|
||||
every=30,
|
||||
period='seconds'
|
||||
)
|
||||
|
||||
interval2 = IntervalSchedule.objects.create(
|
||||
id=2,
|
||||
every=30,
|
||||
period='minutes'
|
||||
)
|
||||
|
||||
interval3 = IntervalSchedule.objects.create(
|
||||
id=3,
|
||||
every=60,
|
||||
period='seconds'
|
||||
)
|
||||
|
||||
PeriodicTask.objects.create(
|
||||
id=1,
|
||||
name="Загрузка GTO",
|
||||
task="apps.schedulers.tasks.load_navigation",
|
||||
interval=interval1,
|
||||
crontab=None,
|
||||
solar=None,
|
||||
clocked=None,
|
||||
args="[]",
|
||||
kwargs="{}",
|
||||
queue=None,
|
||||
exchange=None,
|
||||
routing_key=None,
|
||||
headers="{}",
|
||||
priority=None,
|
||||
expires=None,
|
||||
expire_seconds=None,
|
||||
one_off=False,
|
||||
enabled=True,
|
||||
total_run_count=22,
|
||||
description=""
|
||||
)
|
||||
|
||||
PeriodicTask.objects.create(
|
||||
id=3,
|
||||
name="Загрузка SP",
|
||||
task="apps.schedulers.tasks.load_earthquake",
|
||||
interval=interval2,
|
||||
crontab=None,
|
||||
solar=None,
|
||||
clocked=None,
|
||||
args="[]",
|
||||
kwargs="{}",
|
||||
queue=None,
|
||||
exchange=None,
|
||||
routing_key=None,
|
||||
headers="{}",
|
||||
priority=None,
|
||||
expires=None,
|
||||
expire_seconds=None,
|
||||
one_off=False,
|
||||
enabled=True,
|
||||
total_run_count=1,
|
||||
description=""
|
||||
)
|
||||
|
||||
PeriodicTask.objects.create(
|
||||
id=4,
|
||||
name="Проверка доступности вышек PLA",
|
||||
task="apps.schedulers.tasks.ping_plagroups",
|
||||
interval=interval3,
|
||||
crontab=None,
|
||||
solar=None,
|
||||
clocked=None,
|
||||
args="[]",
|
||||
kwargs="{}",
|
||||
queue=None,
|
||||
exchange=None,
|
||||
routing_key=None,
|
||||
headers="{}",
|
||||
priority=None,
|
||||
expires=None,
|
||||
expire_seconds=None,
|
||||
one_off=False,
|
||||
enabled=False,
|
||||
total_run_count=9,
|
||||
description=""
|
||||
)
|
||||
|
||||
|
||||
def delete_schedules(apps, schema_editor):
|
||||
PeriodicTask = apps.get_model('django_celery_beat', 'PeriodicTask')
|
||||
IntervalSchedule = apps.get_model('django_celery_beat', 'IntervalSchedule')
|
||||
CrontabSchedule = apps.get_model('django_celery_beat', 'CrontabSchedule')
|
||||
|
||||
PeriodicTask.objects.filter(id__in=[1, 2, 3, 4]).delete()
|
||||
IntervalSchedule.objects.filter(id__in=[1, 2, 3]).delete()
|
||||
CrontabSchedule.objects.filter(id=1).delete()
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
dependencies = [
|
||||
('main', '0012_alter_staticlayer_layer_type'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.RunPython(create_schedules, delete_schedules),
|
||||
]
|
||||
Loading…
x
Reference in New Issue
Block a user