You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

31 lines
1.1 KiB

# -*- coding: utf-8 -*-
from optparse import make_option
from datetime import timedelta
from django.core.cache import cache
from django.core.management.base import BaseCommand
from django.utils import timezone
from stats_collector.models import ObjectStats, SectionStats
from stats_collector.utils import unique_items_iterator
class Command(BaseCommand):
def handle(self, *args, **options):
prev_day = timezone.now().date() - timedelta(days=1)
for model in [ObjectStats, SectionStats]:
key = model.make_key(prev_day)
# pop items from redis
l = cache._client.lrange(key, 0, -1)
cache._client.ltrim(key, len(l), -1)
unique_items = {}
for key, val in unique_items_iterator(l):
_val = unique_items.get(key, 0)
unique_items[key] = _val + val
if unique_items:
model_objects = []
for k, v in unique_items.items():
model_objects.append(model.create_instance(*k + (v, ), created_at=prev_day))
model.objects.bulk_create(model_objects)