remotes/origin/stage5
parent
5108562a78
commit
8e87c00e15
1 changed files with 80 additions and 0 deletions
@ -0,0 +1,80 @@ |
|||||||
|
# -*- coding: utf-8 -*- |
||||||
|
from datetime import date, datetime, timedelta |
||||||
|
|
||||||
|
from conference.models import Conference |
||||||
|
from django.core.management.base import BaseCommand |
||||||
|
from expobanner.models import Banner, Log, LogStat, PaidStat |
||||||
|
from exposition.models import Exposition |
||||||
|
from functions.utils import strfdelta |
||||||
|
|
||||||
|
|
||||||
|
today = date.today() |
||||||
|
|
||||||
|
|
||||||
|
class Command(BaseCommand): |
||||||
|
def handle(self, *args, **options): |
||||||
|
day = date(year=2016, month=9, day=1) |
||||||
|
while day <= date.today(): |
||||||
|
print(day) |
||||||
|
start = datetime.now() |
||||||
|
# banners |
||||||
|
banners_ids_with_new_data = Log.objects.filter( |
||||||
|
datetime__startswith=day, |
||||||
|
type__in=[1, 2], |
||||||
|
banner_id__isnull=False, |
||||||
|
).values_list('banner_id', flat=True).distinct() |
||||||
|
banners = Banner.objects\ |
||||||
|
.filter(public=True, pk__in=banners_ids_with_new_data)\ |
||||||
|
.select_related('group') |
||||||
|
|
||||||
|
for banner in banners: |
||||||
|
logstat, created = LogStat.objects.get_or_create(banner=banner, group=banner.group, date=day) |
||||||
|
params = { |
||||||
|
'datetime__startswith': day, |
||||||
|
'banner': banner, |
||||||
|
'group': banner.group, |
||||||
|
} |
||||||
|
if logstat.updated_at is not None: |
||||||
|
params.update({'datetime__gte': logstat.updated_at}) |
||||||
|
|
||||||
|
views = Log.objects.filter(type=1, **params).count() |
||||||
|
clicks = Log.objects.filter(type=2, **params).count() |
||||||
|
unique_views = Log.objects.filter(type=1, **params).values('ip').distinct().count() |
||||||
|
unique_clicks = Log.objects.filter(type=2, **params).values('ip').distinct().count() |
||||||
|
|
||||||
|
logstat.click += clicks |
||||||
|
logstat.view += views |
||||||
|
logstat.unique_click += unique_clicks |
||||||
|
logstat.unique_view += unique_views |
||||||
|
logstat.save() |
||||||
|
|
||||||
|
print(strfdelta(datetime.now() - start, '%H:%M:%S')) |
||||||
|
start = datetime.now() |
||||||
|
|
||||||
|
for model in [Exposition, Conference]: |
||||||
|
self.handle_events(model, day) |
||||||
|
|
||||||
|
print(strfdelta(datetime.now() - start, '%H:%M:%S')) |
||||||
|
day += timedelta(days=1) |
||||||
|
|
||||||
|
def handle_events(self, model, day): |
||||||
|
# paid events |
||||||
|
events = list(model.objects.select_related('paid_new').filter(paid_new__isnull=False)) |
||||||
|
|
||||||
|
for event in events: |
||||||
|
paid = event.paid_new |
||||||
|
|
||||||
|
t_clicks = Log.objects.filter(datetime__startswith=day, banner=paid.tickets, type=2).count() |
||||||
|
p_clicks = Log.objects.filter(datetime__startswith=day, banner=paid.participation, type=2).count() |
||||||
|
o_clicks = Log.objects.filter(datetime__startswith=day, banner=paid.official, type=2).count() |
||||||
|
c_clicks = Log.objects.filter(datetime__startswith=day, banner=paid.catalog, type=2).count() |
||||||
|
|
||||||
|
if any([t_clicks, p_clicks, o_clicks, c_clicks]): |
||||||
|
paidstat, created = PaidStat.objects.get_or_create(paid=paid, date=day) |
||||||
|
|
||||||
|
paidstat.tickets_clicks = t_clicks |
||||||
|
paidstat.participation_clicks = p_clicks |
||||||
|
paidstat.official_clicks = o_clicks |
||||||
|
paidstat.catalog_clicks = c_clicks |
||||||
|
|
||||||
|
paidstat.save() |
||||||
Loading…
Reference in new issue