Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add memcache service #468

Merged
merged 4 commits into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions config/settings.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import logging
import os
import sys

import dj_database_url
import sentry_sdk
Expand Down Expand Up @@ -85,6 +86,7 @@

INTERNAL_IPS = [
'127.0.0.1',
'localhost',
]

ROOT_URLCONF = 'config.urls'
Expand Down Expand Up @@ -261,3 +263,24 @@
'all_applications': True,
'group_models': True,
}


""" DEBUG_TOOLBAR_CONFIG = {
'SHOW_TOOLBAR_CALLBACK': lambda request: True,
} """


CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.memcached.PyMemcacheCache',
'LOCATION': 'memcached:11211',
}
}

if 'test' in sys.argv:
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
'LOCATION': 'unique-snowflake',
}
}
38 changes: 10 additions & 28 deletions contributors/models/contributor.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,34 +22,16 @@ def visible(self):
def with_contributions(self):
"""Return a list of contributors annotated with contributions."""
return self.annotate(
commits=models.Count(
'contribution', filter=models.Q(contribution__type='cit'),
),
additions=Coalesce(
models.Sum('contribution__stats__additions'), 0,
),
deletions=Coalesce(
models.Sum('contribution__stats__deletions'), 0,
),
pull_requests=models.Count(
'contribution', filter=models.Q(contribution__type='pr'),
),
issues=models.Count(
'contribution', filter=models.Q(contribution__type='iss'),
),
comments=models.Count(
'contribution', filter=models.Q(contribution__type='cnt'),
),
editions=(models.F('additions') + models.F('deletions')),
contribution_amount=sum(
(
models.F('commits'),
models.F('editions'),
models.F('pull_requests'),
models.F('issues'),
models.F('comments'),
),
),
commits=models.Count('contribution', filter=models.Q(contribution__type='cit')),
additions=Coalesce(models.Sum('contribution__stats__additions'), 0),
deletions=Coalesce(models.Sum('contribution__stats__deletions'), 0),
pull_requests=models.Count('contribution', filter=models.Q(contribution__type='pr')),
issues=models.Count('contribution', filter=models.Q(contribution__type='iss')),
comments=models.Count('contribution', filter=models.Q(contribution__type='cnt')),
).annotate(
editions=models.F('additions') + models.F('deletions'),
contribution_amount=models.F('commits') + models.F('editions')
+ models.F('pull_requests') + models.F('issues') + models.F('comments')
)

def for_month(self):
Expand Down
101 changes: 38 additions & 63 deletions contributors/views/home.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,14 @@
from django.db.models import Prefetch
from django.utils.decorators import method_decorator
from django.views.decorators.cache import cache_page
from django.views.generic.base import TemplateView

from contributors.models import Contribution, Contributor

LATEST_COUNT = 10


def get_top10(dataset, contrib_type):
"""Return top 10 contributors of the type from the dataset."""
return dataset.order_by(f'-{contrib_type}')[:LATEST_COUNT]


def get_latest_contributions(dataset, contrib_type):
"""Return latest contributions of contrib_type."""
return dataset.filter(type=contrib_type).order_by(
'-created_at',
)[:LATEST_COUNT]


@method_decorator(cache_page(60 * 15), name='dispatch') # кэширование на 15 минут
class HomeView(TemplateView):
"""Home page view."""

Expand All @@ -28,64 +20,47 @@ def get_context_data(self, **kwargs):

contributors_for_month = (
Contributor.objects.visible_with_monthly_stats()
.prefetch_related(
Prefetch(
'contribution_set',
queryset=Contribution.objects.select_related('repository'),
to_attr='prefetched_contributions'
)
)
)
contributors_for_week = (
Contributor.objects.visible_with_weekly_stats()
.prefetch_related(
Prefetch(
'contribution_set',
queryset=Contribution.objects.select_related('repository'),
to_attr='prefetched_contributions'
)
)
)

latest_contributions = (
Contribution.objects.all()
)

top10_committers_of_month = get_top10(
contributors_for_month, 'commits',
)
top10_requesters_of_month = get_top10(
contributors_for_month, 'pull_requests',
)
top10_reporters_of_month = get_top10(
contributors_for_month, 'issues',
)
top10_commentators_of_month = get_top10(
contributors_for_month, 'comments',
)

top10_committers_of_week = get_top10(
contributors_for_week, 'commits',
)
top10_requesters_of_week = get_top10(
contributors_for_week, 'pull_requests',
)
top10_reporters_of_week = get_top10(
contributors_for_week, 'issues',
)
top10_commentators_of_week = get_top10(
contributors_for_week, 'comments',
)

latest_issues = get_latest_contributions(
latest_contributions, 'iss',
Contribution.objects.select_related('contributor', 'repository')
.order_by('-created_at')[:LATEST_COUNT * 2]
)

latest_pr = get_latest_contributions(
latest_contributions, 'pr',
)

context.update(
{
'contributors_for_month': contributors_for_month,
'top10_committers_of_month': top10_committers_of_month,
'top10_requesters_of_month': top10_requesters_of_month,
'top10_reporters_of_month': top10_reporters_of_month,
'top10_commentators_of_month': top10_commentators_of_month,
'top10_committers_of_week': top10_committers_of_week,
'top10_requesters_of_week': top10_requesters_of_week,
'top10_reporters_of_week': top10_reporters_of_week,
'top10_commentators_of_week': top10_commentators_of_week,
'contributions_for_year': Contribution.objects.for_year(),
'latest_time_issues': latest_issues,
'latest_time_pr': latest_pr,
},
)
context.update({
'top10_committers_of_month': self.get_top10(contributors_for_month, 'commits'),
'top10_requesters_of_month': self.get_top10(contributors_for_month, 'pull_requests'),
'top10_reporters_of_month': self.get_top10(contributors_for_month, 'issues'),
'top10_commentators_of_month': self.get_top10(contributors_for_month, 'comments'),
'top10_committers_of_week': self.get_top10(contributors_for_week, 'commits'),
'top10_requesters_of_week': self.get_top10(contributors_for_week, 'pull_requests'),
'top10_reporters_of_week': self.get_top10(contributors_for_week, 'issues'),
'top10_commentators_of_week': self.get_top10(contributors_for_week, 'comments'),
'contributions_for_year': Contribution.objects.for_year(),
'latest_time_issues': [c for c in latest_contributions if c.type == 'iss'][:LATEST_COUNT],
'latest_time_pr': [c for c in latest_contributions if c.type == 'pr'][:LATEST_COUNT],
})

return context

@staticmethod
def get_top10(dataset, contrib_type):
"""Return top 10 contributors of the type from the dataset."""
return sorted(dataset, key=lambda x: getattr(x, contrib_type), reverse=True)[:LATEST_COUNT]
12 changes: 10 additions & 2 deletions contributors/views/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,15 @@ class PaginationMixin(MultipleObjectMixin):

def get_context_data(self, **kwargs):
"""Add context."""
paginator = Paginator(self.get_queryset(), self.paginate_by)
queryset = kwargs.pop('object_list', self.object_list)
if queryset is None:
queryset = self.get_queryset()

# Убедимся, что queryset отсортирован
if not queryset.query.order_by:
queryset = queryset.order_by('id')

paginator = Paginator(queryset, self.paginate_by)
page = paginator.get_page(self.request.GET.get('page'))

page_slice = get_page_slice(
Expand All @@ -71,7 +79,7 @@ def get_context_data(self, **kwargs):
self.inner_visible_pages,
)

context = super().get_context_data(**kwargs)
context = super().get_context_data(object_list=queryset, **kwargs)
context.update({
'page': page,
'page_range': paginator.page_range[page_slice],
Expand Down
22 changes: 15 additions & 7 deletions contributors/views/organizations_views/organization.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,40 @@
from django.db.models import Count, Prefetch, Q
from django.shortcuts import get_object_or_404
from django.utils.translation import gettext_lazy as _

from contributors.models import Organization
from contributors.models import Organization, Repository
from contributors.views.repositories_views import repositories


class OrgRepositoryList(repositories.ListView):
"""An organization's details."""

template_name = (
'contributors_sections/organizations/organization_details.html'
)
template_name = 'contributors_sections/organizations/organization_details.html'
sortable_fields = (
'name',
'project',
'pull_requests',
'issues',
('contributors_count', _("Contributors")),
)
context_object_name = 'repositories'

def get_queryset(self):
"""Get a dataset."""
self.organization = get_object_or_404(
Organization,
Organization.objects.prefetch_related(
Prefetch(
'repository_set',
queryset=Repository.objects.filter(is_visible=True).annotate(
pull_requests=Count('contribution', filter=Q(contribution__type='pr')), # noqa: E501
issues=Count('contribution', filter=Q(contribution__type='iss')),
contributors_count=Count('contribution__contributor', distinct=True) # noqa: E501
)
)
),
name=self.kwargs['slug'],
)
self.queryset = self.queryset.filter(organization=self.organization)
return super().get_queryset()
return self.organization.repository_set.all()

def get_context_data(self, **kwargs):
"""Add context."""
Expand Down
59 changes: 36 additions & 23 deletions contributors/views/repositories_views/repositories.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from django.db.models import Count, Q
from django.db.models import Count, Prefetch, Q
from django.utils.decorators import method_decorator
from django.utils.translation import gettext_lazy as _
from django.views import generic
from django.views.decorators.cache import cache_page

from contributors.models import Label, Repository
from contributors.views.mixins import (
Expand All @@ -17,25 +19,35 @@ class ListView(
"""A list of repositories."""

for_visible_contributor = Q(contribution__contributor__is_visible=True)
queryset = (
Repository.objects.select_related('organization').filter(
is_visible=True,
).distinct().annotate(
pull_requests=Count(
'contribution',
filter=Q(contribution__type='pr') & for_visible_contributor,
),
issues=Count(
'contribution',
filter=Q(contribution__type='iss') & for_visible_contributor,
),
contributors_count=Count(
'contribution__contributor',
filter=for_visible_contributor,
distinct=True,
),

@method_decorator(cache_page(60 * 15)) # кэширование на 15 минут
def dispatch(self, *args, **kwargs):
return super().dispatch(*args, **kwargs)

def get_queryset(self):
queryset = (
Repository.objects.select_related('organization', 'project').filter(
is_visible=True,
).distinct().annotate(
pull_requests=Count(
'contribution',
filter=Q(contribution__type='pr') & self.for_visible_contributor,
),
issues=Count(
'contribution',
filter=Q(contribution__type='iss') & self.for_visible_contributor,
),
contributors_count=Count(
'contribution__contributor',
filter=self.for_visible_contributor,
distinct=True,
),
).prefetch_related(
Prefetch('labels', queryset=Label.objects.only('name'))
)
)
)
return queryset

template_name = 'contributors_sections/repositories/repositories_list.html'
sortable_fields = (
'name',
Expand All @@ -50,12 +62,13 @@ class ListView(

def get_context_data(self, **kwargs):
"""Add context."""
all_labels = Label.objects.all()
context = super().get_context_data(**kwargs)

all_labels = Label.objects.only('name')
labels = Label.objects.filter(
repository__id__in=self.get_queryset(),
).distinct()
repository__in=self.get_queryset(),
).distinct().only('name')

context = super().get_context_data(**kwargs)
context['all_labels'] = all_labels
context['labels'] = labels

Expand Down
6 changes: 6 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ services:

ports:
- 5433:5432

memcached:
image: memcached:alpine
ports:
- "11211:11211"

django:
build:
Expand All @@ -26,6 +31,7 @@ services:
- 8000:8000
depends_on:
- db
- memcached

volumes:
postgres_volume:
Loading
Loading