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.
77 lines
3.0 KiB
77 lines
3.0 KiB
# -*- 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):
|
|
|
|
start = datetime.now()
|
|
# banners
|
|
banners_ids_with_new_data = Log.objects.filter(
|
|
datetime__startswith=today,
|
|
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=today)
|
|
params = {
|
|
'datetime__startswith': today,
|
|
'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)
|
|
|
|
print(strfdelta(datetime.now() - start, '%H:%M:%S'))
|
|
|
|
def handle_events(self, model):
|
|
# 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=today, banner=paid.tickets, type=2).count()
|
|
p_clicks = Log.objects.filter(datetime__startswith=today, banner=paid.participation, type=2).count()
|
|
o_clicks = Log.objects.filter(datetime__startswith=today, banner=paid.official, type=2).count()
|
|
c_clicks = Log.objects.filter(datetime__startswith=today, 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=today)
|
|
|
|
paidstat.tickets_clicks = t_clicks
|
|
paidstat.participation_clicks = p_clicks
|
|
paidstat.official_clicks = o_clicks
|
|
paidstat.catalog_clicks = c_clicks
|
|
|
|
paidstat.save()
|
|
|