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

feat: add update vul tantivy index receiver #1861

Merged
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
2 changes: 1 addition & 1 deletion dongtai_conf/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -1003,7 +1003,7 @@ def safe_execute(default, exception, function, *args):
try:
TANTIVY_INDEX_PATH = config.get("tantivy", "index_path")
except Exception:
TANTIVY_INDEX_PATH = urljoin(TMP_COMMON_PATH, "tantivy")
TANTIVY_INDEX_PATH = os.path.join(TMP_COMMON_PATH, "tantivy")


ELASTICSEARCH_STATE = config.get("elastic_search", "enable") == "true"
Expand Down
4 changes: 2 additions & 2 deletions dongtai_web/aggr_vul/app_vul_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,12 +220,12 @@ def post(self, request):
es_query["order"] = order_type_desc + order_type
if ELASTICSEARCH_STATE:
vul_data = get_vul_list_from_elastic_search(projects, page=page, page_size=page_size, **es_query)
if TANTIVY_STATE:
if TANTIVY_STATE and keywords:
ids = set()
id_score = []
tantivy_query_str = ""
for key, value in tantivy_query.items():
if isinstance(value, str):
if isinstance(value, (str, int)):
tantivy_query_str += f'+{key}:"{value}" '
elif isinstance(value, list):
tantivy_query_str += f"+{key}: IN [{' '.join(map(str, value))}] "
Expand Down
23 changes: 22 additions & 1 deletion dongtai_web/aggr_vul/tasks.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,31 @@
import logging

import tantivy
from celery import shared_task
from celery_singleton import Singleton
from django.db.models.signals import m2m_changed, post_delete, post_save
from django.dispatch import receiver

from dongtai_common.models.vulnerablity import IastVulnerabilityModel, tantivy_index
from dongtai_conf.settings import TANTIVY_STATE

logger = logging.getLogger("dongtai-core")


@receiver(post_delete, sender=IastVulnerabilityModel, dispatch_uid="update_vul_tantivy_index")
@receiver(post_save, sender=IastVulnerabilityModel, dispatch_uid="update_vul_tantivy_index")
@receiver(m2m_changed, sender=IastVulnerabilityModel, dispatch_uid="update_vul_tantivy_index")
def update_vul_tantivy_index_receiver(sender, instance, **kwargs):
update_vul_tantivy_index.apply_async(countdown=120)

@shared_task(queue="dongtai-periodic-task")

@shared_task(
queue="dongtai-periodic-task",
base=Singleton,
lock_expiry=20,
)
def update_vul_tantivy_index():
logger.info("Start update vul tantivy index")
if not TANTIVY_STATE:
return
index = tantivy_index()
Expand Down Expand Up @@ -46,3 +65,5 @@ def update_vul_tantivy_index():
)

writer.commit()

logger.info("Update vul tantivy index success!")
Loading