You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

create_document_index.py 1.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import contextlib
  2. import logging
  3. import time
  4. import click
  5. from werkzeug.exceptions import NotFound
  6. from core.indexing_runner import DocumentIsPausedError, IndexingRunner
  7. from events.document_index_event import document_index_created
  8. from extensions.ext_database import db
  9. from libs.datetime_utils import naive_utc_now
  10. from models.dataset import Document
  11. logger = logging.getLogger(__name__)
  12. @document_index_created.connect
  13. def handle(sender, **kwargs):
  14. dataset_id = sender
  15. document_ids = kwargs.get("document_ids", [])
  16. documents = []
  17. start_at = time.perf_counter()
  18. for document_id in document_ids:
  19. logger.info(click.style(f"Start process document: {document_id}", fg="green"))
  20. document = (
  21. db.session.query(Document)
  22. .where(
  23. Document.id == document_id,
  24. Document.dataset_id == dataset_id,
  25. )
  26. .first()
  27. )
  28. if not document:
  29. raise NotFound("Document not found")
  30. document.indexing_status = "parsing"
  31. document.processing_started_at = naive_utc_now()
  32. documents.append(document)
  33. db.session.add(document)
  34. db.session.commit()
  35. with contextlib.suppress(Exception):
  36. try:
  37. indexing_runner = IndexingRunner()
  38. indexing_runner.run(documents)
  39. end_at = time.perf_counter()
  40. logger.info(click.style(f"Processed dataset: {dataset_id} latency: {end_at - start_at}", fg="green"))
  41. except DocumentIsPausedError as ex:
  42. logger.info(click.style(str(ex), fg="yellow"))