Skip to content

Added Project Page Sorting #552

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

Merged
merged 12 commits into from
Jan 23, 2025
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
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())})

for replica_name, ranking in replicas.items():
client.set_settings(replica_name, {"ranking": ranking})
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
"""Test cases for the algolia_update_replicas command."""

from io import StringIO
from unittest.mock import patch

import pytest
from algoliasearch.http.exceptions import AlgoliaException
from django.core.management import call_command


class TestUpdateReplicasCommand:
"""Test cases for the algolia_update_replicas command."""

@pytest.fixture(autouse=True)
def _setup(self):
"""Set up test environment."""
self.stdout = StringIO()
with patch("apps.owasp.index.project.ProjectIndex.configure_replicas") as replica_patch:
self.mock_replica_update = replica_patch
yield

def test_successful_replica_configuration(self):
"""Test successful replica configuration."""
with patch("sys.stdout", new=StringIO()) as fake_out:
call_command("algolia_update_replicas")

expected_output = (
"\n Starting replica configuration...\n"
"\n Replica have been Successfully created.\n"
)
assert fake_out.getvalue() == expected_output
self.mock_replica_update.assert_called_once()

def test_handle_exception(self):
"""Test handling of exceptions during replica configuration."""
error_message = "Failed to configure replicas"
self.mock_replica_update.side_effect = AlgoliaException(error_message)

with pytest.raises(AlgoliaException) as exc_info:
call_command("algolia_update_replicas")

assert str(exc_info.value) == error_message
self.mock_replica_update.assert_called_once()
Loading
Loading