def build_week_stats(): """ A temporary helper to populate our weekly stats """ from perma.models import Link, LinkUser, Organization, Registrar, WeekStats from datetime import datetime, timedelta from django.utils import timezone oldest_link = Link.objects.earliest('creation_timestamp') # this is always the end date in our range, usually a saturday date_of_stats = oldest_link.creation_timestamp # this is the start date in our range, always a sunday start_date = date_of_stats links_this_week = 0 users_this_week = 0 orgs_this_week = 0 registrars_this_week = 0 while date_of_stats < timezone.now(): links_this_week += Link.objects.filter( creation_timestamp__year=date_of_stats.year, creation_timestamp__month=date_of_stats.month, creation_timestamp__day=date_of_stats.day).count() users_this_week += LinkUser.objects.filter( date_joined__year=date_of_stats.year, date_joined__month=date_of_stats.month, date_joined__day=date_of_stats.day).count() orgs_this_week += Organization.objects.filter( date_created__year=date_of_stats.year, date_created__month=date_of_stats.month, date_created__day=date_of_stats.day).count() registrars_this_week += Registrar.objects.filter( date_created__year=date_of_stats.year, date_created__month=date_of_stats.month, date_created__day=date_of_stats.day).count() # if this is a saturday, write our sums and reset our counts if date_of_stats.weekday() == 5: week_of_stats = WeekStats(start_date=start_date, end_date=date_of_stats, links_sum=links_this_week, users_sum=users_this_week, organizations_sum=orgs_this_week, registrars_sum=registrars_this_week) week_of_stats.save() links_this_week = 0 users_this_week = 0 orgs_this_week = 0 registrars_this_week = 0 start_date = date_of_stats + timedelta(days=1) date_of_stats += timedelta(days=1)
def build_week_stats(): """ A temporary helper to populate our weekly stats """ from perma.models import Link, LinkUser, Organization, Registrar, WeekStats from datetime import timedelta from django.utils import timezone # regenerate all weekly stats WeekStats.objects.all().delete() oldest_link = Link.objects.earliest('creation_timestamp') # this is always the end date in our range, usually a saturday date_of_stats = oldest_link.creation_timestamp # this is the start date in our range, always a sunday start_date = date_of_stats links_this_week = 0 users_this_week = 0 orgs_this_week = 0 registrars_this_week = 0 while date_of_stats < timezone.now(): links_this_week += Link.objects.filter(creation_timestamp__year=date_of_stats.year, creation_timestamp__month=date_of_stats.month, creation_timestamp__day=date_of_stats.day).count() users_this_week += LinkUser.objects.filter(date_joined__year=date_of_stats.year, date_joined__month=date_of_stats.month, date_joined__day=date_of_stats.day).count() orgs_this_week += Organization.objects.filter(date_created__year=date_of_stats.year, date_created__month=date_of_stats.month, date_created__day=date_of_stats.day).count() registrars_this_week += Registrar.objects.approved().filter(date_created__year=date_of_stats.year, date_created__month=date_of_stats.month, date_created__day=date_of_stats.day).count() # if this is a saturday, write our sums and reset our counts if date_of_stats.weekday() == 5: week_of_stats = WeekStats(start_date=start_date, end_date=date_of_stats, links_sum=links_this_week, users_sum=users_this_week, organizations_sum=orgs_this_week, registrars_sum=registrars_this_week) week_of_stats.save() links_this_week = 0 users_this_week = 0 orgs_this_week = 0 registrars_this_week = 0 start_date = date_of_stats + timedelta(days=1) date_of_stats += timedelta(days=1)
def update_stats(): """ run once per minute by celerybeat. logs our minute-by-minute activity, and also rolls our weekly stats (perma.models.WeekStats) """ # On the first minute of the new week, roll our weekly stats entry now = timezone.now() if now.weekday() == 6 and now.hour == 0 and now.minute == 0: week_to_close = WeekStats.objects.latest('start_date') week_to_close.end_date = now week_to_close.save() new_week = WeekStats(start_date=now) new_week.save() # We only need to keep a day of data for our visualization. # TODO: this is 1560 minutes is 26 hours, that likely doesn't # cover everyone outside of the east coast. Our vis should # be timezone aware. Fix this. if MinuteStats.objects.all().count() == 1560: MinuteStats.objects.all()[0].delete() # Add our new minute measurements a_minute_ago = now - timedelta(seconds=60) links_sum = Link.objects.filter( creation_timestamp__gt=a_minute_ago).count() users_sum = LinkUser.objects.filter(date_joined__gt=a_minute_ago).count() organizations_sum = Organization.objects.filter( date_created__gt=a_minute_ago).count() registrars_sum = Registrar.objects.approved().filter( date_created__gt=a_minute_ago).count() new_minute_stat = MinuteStats(links_sum=links_sum, users_sum=users_sum, organizations_sum=organizations_sum, registrars_sum=registrars_sum) new_minute_stat.save() # Add our minute activity to our current weekly sum if links_sum or users_sum or organizations_sum or registrars_sum: current_week = WeekStats.objects.latest('start_date') current_week.end_date = now current_week.links_sum += links_sum current_week.users_sum += users_sum current_week.organizations_sum += organizations_sum current_week.registrars_sum += registrars_sum current_week.save()
def update_stats(): """ run once per minute by celerybeat. logs our minute-by-minute activity, and also rolls our weekly stats (perma.models.WeekStats) """ # On the first minute of the new week, roll our weekly stats entry now = timezone.now() if now.weekday() == 6 and now.hour == 0 and now.minute == 0: week_to_close = WeekStats.objects.latest('start_date') week_to_close.end_date = now week_to_close.save() new_week = WeekStats(start_date=now) new_week.save() # We only need to keep a day of data for our visualization. # TODO: this is 1560 minutes is 26 hours, that likely doesn't # cover everyone outside of the east coast. Our vis should # be timezone aware. Fix this. if MinuteStats.objects.all().count() == 1560: MinuteStats.objects.all()[0].delete() # Add our new minute measurements a_minute_ago = now - timedelta(seconds=60) links_sum = Link.objects.filter(creation_timestamp__gt=a_minute_ago).count() users_sum = LinkUser.objects.filter(date_joined__gt=a_minute_ago).count() organizations_sum = Organization.objects.filter(date_created__gt=a_minute_ago).count() registrars_sum = Registrar.objects.approved().filter(date_created__gt=a_minute_ago).count() new_minute_stat = MinuteStats(links_sum=links_sum, users_sum=users_sum, organizations_sum=organizations_sum, registrars_sum=registrars_sum) new_minute_stat.save() # Add our minute activity to our current weekly sum if links_sum or users_sum or organizations_sum or registrars_sum: current_week = WeekStats.objects.latest('start_date') current_week.end_date = now current_week.links_sum += links_sum current_week.users_sum += users_sum current_week.organizations_sum += organizations_sum current_week.registrars_sum += registrars_sum current_week.save()