def handle_noargs(self, **options): """ Aggregate Application Metrics """ # Aggregate Items items = MetricItem.objects.all() for i in items: # Daily Aggregation day, create = MetricDay.objects.get_or_create(metric=i.metric, created=i.created) day.num = day.num + i.num day.save() # Weekly Aggregation week_date = week_for_date(i.created) week, create = MetricWeek.objects.get_or_create(metric=i.metric, created=week_date) week.num = week.num + i.num week.save() # Monthly Aggregation month_date = month_for_date(i.created) month, create = MetricMonth.objects.get_or_create(metric=i.metric, created=month_date) month.num = month.num + i.num month.save() # Yearly Aggregation year_date = year_for_date(i.created) year, create = MetricYear.objects.get_or_create(metric=i.metric, created=year_date) year.num = year.num + i.num year.save() # Kill off our items items.delete()
def handle(self, *args, **options): """ Aggregate Application Metrics """ backend = get_backend() # If using Mixpanel this command is a NOOP if backend == 'app_metrics.backends.mixpanel': print( "Useless use of metrics_aggregate when using Mixpanel backend") return # Aggregate Items items = MetricItem.objects.all().order_by('id') while items.exists(): with transaction.atomic(): batch = items[0:BATCH_SIZE] for i in batch: # Daily Aggregation day, create = MetricDay.objects.get_or_create( metric=i.metric, created=i.created) day.num = day.num + i.num day.save() # Weekly Aggregation week_date = week_for_date(i.created) week, create = MetricWeek.objects.get_or_create( metric=i.metric, created=week_date) week.num = week.num + i.num week.save() # Monthly Aggregation month_date = month_for_date(i.created) month, create = MetricMonth.objects.get_or_create( metric=i.metric, created=month_date) month.num = month.num + i.num month.save() # Yearly Aggregation year_date = year_for_date(i.created) year, create = MetricYear.objects.get_or_create( metric=i.metric, created=year_date) year.num = year.num + i.num year.save() # Kill off our items items.filter(id__in=[i.id for i in batch]).delete()
def test_missing_trending(self): this_week_date = week_for_date(datetime.date.today()) previous_week_date = this_week_date - datetime.timedelta(weeks=1) previous_month_date = get_previous_month(this_week_date) previous_year_date = get_previous_year(this_week_date) MetricWeek.objects.create(metric=self.metric1, num=5, created=this_week_date) MetricWeek.objects.create(metric=self.metric1, num=4, created=previous_week_date) MetricWeek.objects.create(metric=self.metric1, num=3, created=previous_month_date) data = _trending_for_week(self.metric1) self.assertEqual(data['week'], 5) self.assertEqual(data['previous_week'], 4) self.assertEqual(data['previous_month_week'], 3) self.assertEqual(data['previous_year_week'], 0)
def handle_noargs(self, **options): """ Aggregate Application Metrics """ backend = get_backend() # If using Mixpanel this command is a NOOP if backend == 'app_metrics.backends.mixpanel': print("Useless use of metrics_aggregate when using Mixpanel backend") return # Aggregate Items items = MetricItem.objects.all().order_by('id') while items.exists(): with transaction.atomic(): batch = items[0:BATCH_SIZE] for i in batch: # Daily Aggregation day,create = MetricDay.objects.get_or_create(metric=i.metric, created=i.created) day.num = day.num + i.num day.save() # Weekly Aggregation week_date = week_for_date(i.created) week, create = MetricWeek.objects.get_or_create(metric=i.metric, created=week_date) week.num = week.num + i.num week.save() # Monthly Aggregation month_date = month_for_date(i.created) month, create = MetricMonth.objects.get_or_create(metric=i.metric, created=month_date) month.num = month.num + i.num month.save() # Yearly Aggregation year_date = year_for_date(i.created) year, create = MetricYear.objects.get_or_create(metric=i.metric, created=year_date) year.num = year.num + i.num year.save() # Kill off our items items.filter(id__in=[i.id for i in batch]).delete()
def handle_noargs(self, **options): """ Aggregate Application Metrics """ backend = get_backend() # If using Mixpanel this command is a NOOP if backend == 'app_metrics.backends.mixpanel': print "Useless use of metrics_aggregate when using Mixpanel backend" return # Aggregate Items items = MetricItem.objects.all() for i in items: # Daily Aggregation day, create = MetricDay.objects.get_or_create(metric=i.metric, created=i.created) day.num = day.num + i.num day.save() # Weekly Aggregation week_date = week_for_date(i.created) week, create = MetricWeek.objects.get_or_create(metric=i.metric, created=week_date) week.num = week.num + i.num week.save() # Monthly Aggregation month_date = month_for_date(i.created) month, create = MetricMonth.objects.get_or_create( metric=i.metric, created=month_date) month.num = month.num + i.num month.save() # Yearly Aggregation year_date = year_for_date(i.created) year, create = MetricYear.objects.get_or_create(metric=i.metric, created=year_date) year.num = year.num + i.num year.save() # Kill off our items items.delete()
def handle_noargs(self, **options): """ Aggregate Application Metrics """ backend = get_backend() # If using Mixpanel this command is a NOOP if backend == 'app_metrics.backends.mixpanel': print "Useless use of metrics_aggregate when using Mixpanel backend" return # Aggregate Items items = MetricItem.objects.filter(site_id=settings.SITE_ID) for i in items: # Daily Aggregation day,create = MetricDay.objects.get_or_create(metric=i.metric, created=i.created) day.num = day.num + i.num day.save() # Weekly Aggregation week_date = week_for_date(i.created) week, create = MetricWeek.objects.get_or_create(metric=i.metric, created=week_date) week.num = week.num + i.num week.save() # Monthly Aggregation month_date = month_for_date(i.created) month, create = MetricMonth.objects.get_or_create(metric=i.metric, created=month_date) month.num = month.num + i.num month.save() # Yearly Aggregation year_date = year_for_date(i.created) year, create = MetricYear.objects.get_or_create(metric=i.metric, created=year_date) year.num = year.num + i.num year.save() # Kill off our items items.delete()
def _trending_for_week(metric=None): this_week_date = week_for_date(datetime.date.today()) previous_week_date = this_week_date - datetime.timedelta(weeks=1) previous_month_week_date = get_previous_month(this_week_date) previous_year_week_date = get_previous_year(this_week_date) data = { 'week': 0, 'previous_week': 0, 'previous_month_week': 0, 'previous_year_week': 0, } try: week = MetricWeek.objects.get(metric=metric, created=this_week_date) data['week'] = week.num except ObjectDoesNotExist: pass try: previous_week = MetricWeek.objects.get(metric=metric, created=previous_week_date) data['previous_week'] = previous_week.num except ObjectDoesNotExist: pass try: previous_month_week = MetricWeek.objects.get( metric=metric, created=previous_month_week_date) data['previous_month_week'] = previous_month_week.num except ObjectDoesNotExist: pass try: previous_year_week = MetricWeek.objects.get( metric=metric, created=previous_year_week_date) data['previous_year_week'] = previous_year_week.num except ObjectDoesNotExist: pass return data
def _trending_for_week(metric=None): this_week_date = week_for_date(datetime.date.today()) previous_week_date = this_week_date - datetime.timedelta(weeks=1) previous_month_week_date = get_previous_month(this_week_date) previous_year_week_date = get_previous_year(this_week_date) data = { 'week': 0, 'previous_week': 0, 'previous_month_week': 0, 'previous_year_week': 0, } try: week = MetricWeek.objects.get(metric=metric, created=this_week_date) data['week'] = week.num except ObjectDoesNotExist: pass try: previous_week = MetricWeek.objects.get(metric=metric, created=previous_week_date) data['previous_week'] = previous_week.num except ObjectDoesNotExist: pass try: previous_month_week = MetricWeek.objects.get(metric=metric, created=previous_month_week_date) data['previous_month_week'] = previous_month_week.num except ObjectDoesNotExist: pass try: previous_year_week = MetricWeek.objects.get(metric=metric, created=previous_year_week_date) data['previous_year_week'] = previous_year_week.num except ObjectDoesNotExist: pass return data
def handle(self, **options): """ Aggregate Application Metrics """ backend = get_backend() # If using Mixpanel this command is a NOOP if backend == 'app_metrics.backends.mixpanel': print "Useless use of metrics_aggregate when using Mixpanel backend" return # Aggregate Items items = MetricItem.objects.all() for i in items: # Daily Aggregation try: days = MetricDay.objects.filter(metric=i.metric, created=i.created) except ObjectDoesNotExist: day,create = MetricDay.objects.get_or_create(metric=i.metric, created=i.created) else: if days.count() > 1: days = days.exclude(id=days[0].id) days.delete() try: day = days[0] except IndexError: day,create = MetricDay.objects.get_or_create(metric=i.metric, created=i.created) day.num = day.num + i.num day.save() # Weekly Aggregation week_date = week_for_date(i.created) try: weeks = MetricWeek.objects.filter(metric=i.metric, created=week_date) except ObjectDoesNotExist: week,create = MetricWeek.objects.get_or_create(metric=i.metric, created=week_date) else: if weeks.count() > 1: weeks = weeks.exclude(id=weeks[0].id) weeks.delete() try: week = weeks[0] except IndexError: week,create = MetricWeek.objects.get_or_create(metric=i.metric, created=week_date) week.num = week.num + i.num week.save() # Monthly Aggregation month_date = month_for_date(i.created) try: months = MetricMonth.objects.filter(metric=i.metric, created=month_date) except ObjectDoesNotExist: month,create = MetricMonth.objects.get_or_create(metric=i.metric, created=month_date) else: if months.count() > 1: months = months.exclude(id=months[0].id) months.delete() try: month = months[0] except IndexError: month,create = MetricMonth.objects.get_or_create(metric=i.metric, created=month_date) # Yearly Aggregation year_date = year_for_date(i.created) try: years = MetricYear.objects.filter(metric=i.metric, created=year_date) except ObjectDoesNotExist: years,create = MetricYear.objects.get_or_create(metric=i.metric, created=year_date) else: if years.count() > 1: years = years.exclude(id=years[0].id) years.delete() try: year = years[0] except IndexError: year,create = MetricYear.objects.get_or_create(metric=i.metric, created=year_date) year.num = year.num + i.num year.save() # Kill off our items items.delete()