Permalink
Browse files

VersionCount model and associated changes to make version foo indepen…

…dent of code change
  • Loading branch information...
1 parent ff774c1 commit 6213ced6b89cce56bd9febc8fbe0000c81dfcd7c @nigelbabu nigelbabu committed Oct 20, 2011
Showing with 70 additions and 1 deletion.
  1. +26 −1 apps/feedback/cron.py
  2. +27 −0 apps/feedback/models.py
  3. +13 −0 migrations/16-versions.sql
  4. +4 −0 settings.py
View
@@ -1,13 +1,15 @@
import datetime
import random
+import logging
from django.conf import settings
from django.db import transaction, models
import cronjobs
+from product_details.version_compare import Version
import input
-from feedback.models import Opinion, extract_terms
+from feedback.models import Opinion, VersionCount, extract_terms
DEFAULT_NUM_OPINIONS = 100
@@ -51,6 +53,7 @@
DEVICES = dict(Samsung='Epic Vibrant Transform'.split(),
HTC='Evo Hero'.split(),
Motorola='DroidX Droid2'.split())
+logger = logging.getLogger(__name__)
@cronjobs.register
@@ -91,3 +94,25 @@ def populate(num_opinions=None, product='mobile', type=None, locale=None):
models.signals.post_save.connect(extract_terms, sender=Opinion,
dispatch_uid='extract_terms')
+
+
+@cronjobs.register
+def version_counter():
+ """Cron to activate and deactivate product versions."""
+ thirtydaysago = datetime.datetime.now() - datetime.timedelta(30)
+ versions = (Opinion.objects.filter(created__gte=(thirtydaysago))
+ .values('product', 'version')
+ .annotate(count=models.Count('id')))
+ logger.debug("Found %d versions" % (len(versions)))
+ for version in versions:
+ vc, created = VersionCount.objects.get_or_create(
+ product=version['product'], version=version['version'],
+ defaults={'num_opinions': version['count']})
+ if not created:
+ vc.num_opinions = version['count']
+ if vc.product == input.FIREFOX.id:
+ vc.active = (vc.num_opinions >= settings.DASHBOARD_THRESHOLD)
+ else:
+ vc.active = (
+ vc.num_opinions >= settings.DASHBOARD_THRESHOLD_MOBILE)
+ vc.save()
View
@@ -9,6 +9,7 @@
from elasticutils import es_required
from pyes import djangoutils
from pyes.exceptions import NotFoundException as PyesNotFoundException
+from product_details.version_compare import Version
from feedback import query, utils
from feedback.utils import ua_parse, extract_terms, smart_truncate
@@ -212,3 +213,29 @@ def __unicode__(self):
class Meta:
ordering = ('term',)
+
+
+class VersionCount(ModelBase):
+ """Denormalized model built from product info and opinion counts.
+
+ Built from products, versions, number of opinions, and active status.
+ """
+
+ product = models.PositiveSmallIntegerField(db_index=True)
+ version = models.CharField(max_length=30, db_index=True)
+ version_int = models.BigIntegerField(db_index=True)
+ num_opinions = models.IntegerField()
+ active = models.BooleanField(db_index=True)
+
+ class Meta:
+ unique_together = (('product', 'version'))
+ db_table = 'version_count'
+
+
+def update_version_int(sender, instance, **kwargs):
+ if not instance.pk:
+ v = Version(instance.version)
+ instance.version_int = v._version_int
+
+
+signals.pre_save.connect(update_version_int, sender=VersionCount)
View
@@ -0,0 +1,13 @@
+CREATE TABLE `version_count` (
+ `id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY,
+ `product` smallint UNSIGNED NOT NULL,
+ `version` varchar(30) NOT NULL,
+ `version_int` bigint NOT NULL,
+ `num_opinions` integer NOT NULL,
+ `active` bool NOT NULL,
+ UNIQUE (`product`, `version`)
+) ENGINE=InnoDB DEFAULT CHARSET=utf8;
+CREATE INDEX `version_count_7e1f2157` ON `version_count` (`product`);
+CREATE INDEX `version_count_659105e4` ON `version_count` (`version`);
+CREATE INDEX `version_count_6fd24c02` ON `version_count` (`version_int`);
+CREATE INDEX `version_count_34d728db` ON `version_count` (`active`);
View
@@ -347,3 +347,7 @@ def JINJA_CONFIG():
# (good for testing)
ENFORCE_USER_AGENT = True
DISABLE_TERMS = False
+
+# Minnum of opinions in the last 30 days for version to be shown in dashboard
+DASHBOARD_THRESHOLD = 800
+DASHBOARD_THRESHOLD_MOBILE = 120

0 comments on commit 6213ced

Please sign in to comment.