-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstatus_changed.py
More file actions
41 lines (35 loc) · 1.6 KB
/
Copy pathstatus_changed.py
File metadata and controls
41 lines (35 loc) · 1.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import os
import django
import time
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'DjangoProject.settings')
django.setup()
from trajectory.models import Trajectory
def fix_status_changed():
all_cars = Trajectory.objects.values_list('COMMADDR', flat=True).distinct()
for car in all_cars:
# 用iterator流式遍历,避免内存暴涨
qs = Trajectory.objects.filter(COMMADDR=car).order_by('BEIJING').only('id', '状态')
prev_status = None
to_update = []
is_first_record = True
for obj in qs.iterator():
# 检查是否为第一行数据或车牌第一次出现
if is_first_record:
to_update.append(obj.id)
is_first_record = False
# 检查状态变化
if prev_status is not None and obj.状态 != prev_status:
if (prev_status, obj.状态) in [('空载', '载客'), ('载客', '空载')]:
to_update.append(obj.id)
prev_status = obj.状态
# 分批更新,避免单次SQL过大
batch_size = 1000
for i in range(0, len(to_update), batch_size):
Trajectory.objects.filter(pk__in=to_update[i:i+batch_size]).update(status_changed=1)
print(f"车辆 {car} 状态切换点已标注,共 {len(to_update)} 条", flush=True)
if __name__ == "__main__":
start_time = time.time()
print(f"status_changed 修正开始!", flush=True)
fix_status_changed()
total_time = time.time() - start_time
print(f"status_changed 字段修正完成!总耗时: {total_time:.2f}秒", flush=True)