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

Added Project Page Sorting #552

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions backend/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ github-update-project-related-repositories:
index-data:
@echo "Indexing Nest data"
@CMD="poetry run python manage.py algolia_reindex" $(MAKE) exec-backend-command
@CMD="poetry run python manage.py algolia_update_replicas" $(MAKE) exec-backend-command
@CMD="poetry run python manage.py algolia_update_synonyms" $(MAKE) exec-backend-command
@CMD="poetry run python manage.py algolia_update_suggestions" $(MAKE) exec-backend-command

Expand Down
6 changes: 3 additions & 3 deletions backend/apps/common/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class IndexBase:
"""Nest index synonyms mixin and record count."""

@staticmethod
def _get_client():
def get_client():
"""Get the Algolia client."""
return SearchClientSync(
settings.ALGOLIA_APPLICATION_ID,
Expand Down Expand Up @@ -73,7 +73,7 @@ def reindex_synonyms(app_name, index_name):
if not (synonyms := IndexBase._parse_synonyms_file(file_path)):
return None

client = IndexBase._get_client()
client = IndexBase.get_client()
index_name = f"{settings.ENVIRONMENT.lower()}_{index_name}"

try:
Expand All @@ -91,7 +91,7 @@ def reindex_synonyms(app_name, index_name):
@lru_cache(maxsize=1024)
def get_total_count(index_name):
"""Get total count of records in index."""
client = IndexBase._get_client()
client = IndexBase.get_client()
try:
return client.search_single_index(
index_name=f"{settings.ENVIRONMENT.lower()}_{index_name}",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
"""A command to update OWASP Nest index replicas."""

from django.core.management.base import BaseCommand

from apps.owasp.index.project import ProjectIndex


class Command(BaseCommand):
help = "Update OWASP Nest index replicas."

def handle(self, *_args, **_options):
print("\n Starting replica configuration...")
ProjectIndex.configure_replicas()
print("\n Replica have been Successfully created.")
22 changes: 22 additions & 0 deletions backend/apps/owasp/index/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from algoliasearch_django import AlgoliaIndex
from algoliasearch_django.decorators import register
from django.conf import settings

from apps.common.index import IS_LOCAL_BUILD, LOCAL_INDEX_LIMIT, IndexBase
from apps.owasp.models.project import Project
Expand Down Expand Up @@ -92,3 +93,24 @@ def get_queryset(self):
def update_synonyms():
"""Update synonyms."""
return ProjectIndex.reindex_synonyms("owasp", "projects")

@staticmethod
def configure_replicas():
"""Configure the settings for project replicas."""
env = settings.ENVIRONMENT.lower()
client = IndexBase.get_client()
replicas = {
f"{env}_projects_name_asc": ["asc(idx_name)"],
f"{env}_projects_name_desc": ["desc(idx_name)"],
f"{env}_projects_stars_count_asc": ["asc(idx_stars_count)"],
f"{env}_projects_stars_count_desc": ["desc(idx_stars_count)"],
f"{env}_projects_contributors_count_asc": ["asc(idx_contributors_count)"],
f"{env}_projects_contributors_count_desc": ["desc(idx_contributors_count)"],
f"{env}_projects_forks_count_asc": ["asc(idx_forks_count)"],
f"{env}_projects_forks_count_desc": ["desc(idx_forks_count)"],
}

client.set_settings(f"{env}_projects", {"replicas": list(replicas.keys())})
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems replicas can be created in index/projects.py


for replica_name, ranking in replicas.items():
client.set_settings(replica_name, {"ranking": ranking})
Loading
Loading