import json
from datetime import timedelta
from ciso8601 import parse_datetime
from tqdm import tqdm

from django.conf import settings
from django.core.management.base import BaseCommand
from django.utils import timezone

from vehicles.utils import redis_client
from vehicles.models import VehicleJourney


one_day = timedelta(days=1)


class Command(BaseCommand):
    def handle(self, **options):
        directory = settings.DATA_DIR / 'journeys'

        if not directory.exists():
            directory.mkdir()

        today = timezone.localdate()
        while_ago = today - timedelta(days=10)

        while while_ago < today:
            journeys = VehicleJourney.objects.filter(datetime__date=while_ago).only('id')

            pipe = redis_client.pipeline(transaction=False)
            for journey in journeys:
                pipe.exists(f'journey{journey.id}')
            exists = pipe.execute()
            #exists = redis_client.exists(*[f'journey{journey.id}' for journey in journeys])
            #print(exists)

            for i, journey in enumerate(tqdm(journeys)):
                if not exists[i]:
                    continue
                redis_key = f'journey{journey.id}'
                locations = redis_client.lrange(redis_key, 0, -1)
                assert locations
                last = json.loads(locations[-1])
                age = timezone.now() - parse_datetime(last[0])
                if age > one_day:
                            locations = b'\n'.join(locations)
                            path = journey.get_path()
                            path.write_bytes(locations)
                            redis_client.delete(redis_key)

            while_ago += timedelta(days=1)

@@ -906,128 +906,8 @@
 te()
-%0A            #exists = redis_client.exists(*%5Bf'journey%7Bjourney.id%7D' for journey in journeys%5D)%0A            #print(exists)
 %0A%0A  
@@ -957,16 +957,30 @@
 journeys
+, disable=None
 )):%0A    
@@ -1350,32 +1350,24 @@
             
-        
 locations = 
@@ -1384,32 +1384,24 @@
 (locations)%0A
-        
             
@@ -1450,32 +1450,24 @@
             
-        
 path.write_b
@@ -1478,32 +1478,24 @@
 (locations)%0A
-        
             

disable tqdm if no tty?