def handle(self, **options): """ Send Report E-mails """ from django.conf import settings translation.activate(settings.LANGUAGE_CODE) backend = get_backend() # This command is a NOOP if using the Mixpanel backend if backend == 'app_metrics.backends.mixpanel': print "Useless use of metrics_send_email when using Mixpanel backend." return # Determine if we should also send any weekly or monthly reports today = datetime.date.today() if today.weekday == 0: send_weekly = True else: send_weekly = False if today.day == 1: send_monthly = True else: send_monthly = False qs = MetricSet.objects.filter(Q(no_email=False), Q(send_daily=True) | Q(send_monthly=send_monthly) | Q(send_weekly=send_weekly)) if "mailer" in settings.INSTALLED_APPS: from mailer import send_html_mail USE_MAILER = True else: from django.core.mail import EmailMultiAlternatives USE_MAILER = False for s in qs: subject = _("%s Report") % s.name recipient_list = s.email_recipients.values_list('email', flat=True) (message, message_html) = generate_report(s, html=True) if message == None: continue if USE_MAILER: send_html_mail(subject=subject, message=message, message_html=message_html, from_email=settings.DEFAULT_FROM_EMAIL, recipient_list=recipient_list) else: msg = EmailMultiAlternatives(subject=subject, body=message, from_email=settings.DEFAULT_FROM_EMAIL, to=recipient_list) msg.attach_alternative(message_html, "text/html") msg.send() translation.deactivate()
def handle_noargs(self, **options): backend = get_backend() # If using Mixpanel this command is a NOOP if backend == 'app_metrics.backends.mixpanel': print "Useless use of run_thresholds when using Mixpanel backend" return [ t.log_is_reached() for t in Threshold.active.all() ]
def handle(self, **options): """ Send Report E-mails """ translation.activate(settings.LANGUAGE_CODE) backend = get_backend() # This command is a NOOP if using the Mixpanel backend if backend == 'app_metrics.backends.mixpanel': print("Useless use of metrics_send_email when using Mixpanel backend.") return # Determine if we should also send any weekly or monthly reports today = datetime.date.today() if today.weekday == 0: send_weekly = True else: send_weekly = False if today.day == 1: send_monthly = True else: send_monthly = False qs = MetricSet.objects.filter(Q(no_email=False), Q(send_daily=True) | Q(send_monthly=send_monthly) | Q(send_weekly=send_weekly)) if "mailer" in settings.INSTALLED_APPS: from mailer import send_html_mail USE_MAILER = True else: from django.core.mail import EmailMultiAlternatives USE_MAILER = False for s in qs: subject = _("%s Report") % s.name recipient_list = s.email_recipients.values_list('email', flat=True) (message, message_html) = generate_report(s, html=True) if message is None: continue if USE_MAILER: send_html_mail(subject=subject, message=message, message_html=message_html, from_email=settings.DEFAULT_FROM_EMAIL, recipient_list=recipient_list) else: msg = EmailMultiAlternatives(subject=subject, body=message, from_email=settings.DEFAULT_FROM_EMAIL, to=recipient_list) msg.attach_alternative(message_html, "text/html") msg.send() translation.deactivate()
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 handle(self, **options): """Move MetricItems from the db backend to statsd""" backend = get_backend() # If not using statsd, this command is a NOOP. if backend != 'app_metrics.backends.statsd_backend': sys.exit("You need to set the backend to 'statsd_backend'") items = MetricItem.objects.all() for i in items: metric(i.metric.slug, num=i.num) # 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.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 handle(self, **options): """ Move MetricItems from the db backend to MixPanel" """ backend = get_backend() # If not using Mixpanel this command is a NOOP if backend != 'app_metrics.backends.mixpanel': print "You need to set the backend to MixPanel" return items = MetricItem.objects.all() for i in items: properties = { 'time': i.created.strftime('%s'), } metric(i.metric.slug, num=i.num, properties=properties) # Kill off our items items.delete()
def handle(self, *args, **options): """ Move MetricItems from the db backend to MixPanel" """ backend = get_backend() # If not using Mixpanel this command is a NOOP if backend != 'app_metrics.backends.mixpanel': print("You need to set the backend to MixPanel") return items = MetricItem.objects.all() for i in items: properties = { 'time': i.created.strftime('%s'), } metric(i.metric.slug, num=i.num, properties=properties) # Kill off our items items.delete()
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()