Skip to content

Instantly share code, notes, and snippets.

@humitos
Last active January 31, 2022 16:40
Show Gist options
  • Save humitos/6635314f8effe1761d877ce735eabb45 to your computer and use it in GitHub Desktop.
Save humitos/6635314f8effe1761d877ce735eabb45 to your computer and use it in GitHub Desktop.
Minimal implementation to check if a project can be considered "active" under a window of certain past months
import datetime
from django.utils import timezone
from readthedocs.analytics.models import PageView
from readthedocsext.spamfighting.utils import spam_score
def is_active_project(
project,
spam_threshold=150,
monthly_pageviews=100,
window_months=3,
):
if spam_score(project) > spam_threshold:
print('Spam project detected')
return False
now = timezone.now()
if project.pub_date > now - datetime.timedelta(days=window_months * 30):
print('Project is too new to perform these "active" checks. Marking as active.')
return True
delta = datetime.timedelta(days=window_months * 30)
if not project.builds.filter(date__gte=now - delta).exists():
print('Do not have a build in days')
return False
current_month = now.month
for month in range(1, window_months + 1):
month = list(range(1, 13))[current_month - month - 1] # get a number between 1 and 12
pageviews = project.page_views.filter(date__month=month).aggregate(pageviews=Sum('view_count'))['pageviews']
if not pageviews or pageviews < monthly_pageviews:
print(f'Do not have enough pageviews ({pageviews}) in month ({month})')
return False
return True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment