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.
101 lines
3.2 KiB
101 lines
3.2 KiB
# -*- coding: utf-8 -*-
|
|
import json
|
|
import time
|
|
from collections import namedtuple
|
|
from datetime import timedelta
|
|
|
|
from conference.models import Conference
|
|
from django.contrib.contenttypes.generic import GenericForeignKey
|
|
from django.contrib.contenttypes.models import ContentType
|
|
from django.core.cache import cache
|
|
from django.core.serializers.json import DjangoJSONEncoder
|
|
from django.db import models
|
|
from django.utils import timezone
|
|
from exposition.models import Exposition
|
|
|
|
|
|
class StatsSuperBase(models.Model):
|
|
_strftime_format = '%d.%m.%Y'
|
|
_cache_key = 'section_stats_{date}'
|
|
|
|
created_at = models.DateField('created at', blank=True, null=True, db_index=True)
|
|
value = models.PositiveIntegerField('value', blank=True, null=True)
|
|
|
|
class Meta:
|
|
verbose_name = 'Stats Object'
|
|
verbose_name_plural = 'Stats Objects'
|
|
ordering = ('created_at',)
|
|
abstract = True
|
|
|
|
@classmethod
|
|
def make_key(cls, date=None):
|
|
if date is None:
|
|
date = timezone.now().date()
|
|
return cls._cache_key.format(date=date.isoformat())
|
|
|
|
@classmethod
|
|
def dump_item(self, item):
|
|
return json.dumps(item, cls=DjangoJSONEncoder)
|
|
|
|
@classmethod
|
|
def create_item(cls, *args, **kwargs):
|
|
return cls._collector_class(*args, **kwargs)
|
|
|
|
@classmethod
|
|
def create_instance(cls, *args, **kwargs):
|
|
created_at = kwargs.pop('created_at', None)
|
|
item = cls.create_item(*args, **kwargs)
|
|
return cls(created_at=created_at, **item._asdict())
|
|
|
|
@classmethod
|
|
def create_cache_item(cls, *args, **kwargs):
|
|
return cls.dump_item(cls.create_item(*args, **kwargs))
|
|
|
|
@classmethod
|
|
def cache_count_add(cls, *args, **kwargs):
|
|
item = cls.create_cache_item(*args, **kwargs)
|
|
key = cls.make_key()
|
|
cache._client.rpush(key, item)
|
|
return item
|
|
|
|
@property
|
|
def created_at_(self):
|
|
return self.created_at.strftime(self._strftime_format)
|
|
|
|
|
|
# class StatsBase(StatsSuperBase):
|
|
|
|
|
|
class SectionStats(StatsSuperBase):
|
|
_collector_class = namedtuple(
|
|
'SectionCollector',
|
|
['section', 'kind', 'content_type_id', 'object_id', 'value']
|
|
)
|
|
|
|
content_type = models.ForeignKey(ContentType, blank=True, null=True)
|
|
object_id = models.IntegerField(blank=True, null=True)
|
|
content_object = GenericForeignKey('content_type', 'object_id')
|
|
|
|
class Meta(StatsSuperBase.Meta):
|
|
index_together = [
|
|
("content_type", "object_id"),
|
|
]
|
|
|
|
section = models.CharField('section', blank=True, null=True, max_length=32)
|
|
kind = models.CharField('kind', blank=True, null=True, max_length=64)
|
|
|
|
def __unicode__(self):
|
|
return u'{0}:{1} {2}'.format(self.section, self.kind, self.created_at.strftime(self._strftime_format))
|
|
|
|
|
|
class ObjectStats(StatsSuperBase):
|
|
_cache_key = 'obj_stats_{date}'
|
|
_collector_class = namedtuple(
|
|
'ObjectCollector',
|
|
['conference_id', 'exposition_id', 'value']
|
|
)
|
|
exposition = models.ForeignKey(Exposition, null=True, blank=True)
|
|
conference = models.ForeignKey(Conference, null=True, blank=True)
|
|
|
|
def __unicode__(self):
|
|
return u'{0}'.format(self.created_at.strftime(self._strftime_format))
|
|
|