Skip to content

Commit

Permalink
refactored sort_nuggets
Browse files Browse the repository at this point in the history
  • Loading branch information
cophilot committed Feb 8, 2024
1 parent a6bb745 commit c8c854f
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 12 deletions.
4 changes: 3 additions & 1 deletion celery_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from celery import Celery

from wannadb_web.worker.tasks import BaseTask, DocumentBaseAddAttributes, DocumentBaseForgetMatches, DocumentBaseForgetMatchesForAttribute, DocumentBaseInteractiveTablePopulation, DocumentBaseLoad, DocumentBaseRemoveAttributes, DocumentBaseUpdateAttributes, TestTask, InitManager, CreateDocumentBase
from wannadb_web.worker.tasks import BaseTask, DocumentBaseAddAttributes, DocumentBaseConfirmNugget, DocumentBaseForgetMatches, DocumentBaseForgetMatchesForAttribute, DocumentBaseGetOrderedNuggets, DocumentBaseInteractiveTablePopulation, DocumentBaseLoad, DocumentBaseRemoveAttributes, DocumentBaseUpdateAttributes, TestTask, InitManager, CreateDocumentBase

logging.basicConfig(level=logging.DEBUG, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s")

Expand All @@ -22,3 +22,5 @@
app.register_task(DocumentBaseForgetMatches)
app.register_task(DocumentBaseForgetMatchesForAttribute)
app.register_task(DocumentBaseInteractiveTablePopulation)
app.register_task(DocumentBaseGetOrderedNuggets)
app.register_task(DocumentBaseConfirmNugget)
20 changes: 20 additions & 0 deletions wannadb_web/postgres/queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,26 @@ def getDocument(document_id: int, user_id: int):
else:
return None

def getDocumentByNameAndContent(doc_name: str, doc_content: str, user_id: int):
select_query = sql.SQL("""SELECT name,content,content_byte
FROM documents
JOIN membership m ON documents.organisationid = m.organisationid
WHERE name = (%s) AND content = (%s) AND m.userid = (%s)
""")

result = execute_query(select_query, (doc_name, doc_content, user_id,))
if len(result) > 0:
for document in result:
name = document[0]
if document[1]:
content = document[1]
return str(name), str(content)
elif document[2]:
content = document[2]
return str(name), bytes(content)
else:
return None


def getDocumentsForOrganization(organisation_id: int):
select_query = sql.SQL("""SELECT id, name,content,content_byte
Expand Down
16 changes: 9 additions & 7 deletions wannadb_web/routing/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,32 +297,34 @@ def sort_nuggets():
Example Header:
{
"Authorization": "your_authorization_token"
}
Example JSON Payload:
Example Form Payload:
{
"authorization": "your_authorization_token"
"organisationId": "your_organisation_id",
"baseName": "your_document_base_name",
"document_id": "1", (important: only one document id)
"attributes": "plane,car,bike"
"documentName": "your_document_name",
"documentContent": "your_document_content",
}
"""
form = request.form
authorization = form.get("authorization")
organisation_id: Optional[int] = form.get("organisationId")
base_name = form.get("baseName")
document_id = form.get("document_ids")
if organisation_id is None or base_name is None or document_id is None or authorization is None:
document_name = form.get("documentName")
document_content = form.get("documentContent")
if organisation_id is None or base_name is None or document_name is None or document_content is None or authorization is None:
return make_response({"error": "missing parameters"}, 400)

_token = tokenDecode(authorization)

if _token is False:
return make_response({"error": "invalid token"}, 401)

user_id = _token.id

task = DocumentBaseGetOrderedNuggets().apply_async(args=(user_id, base_name, organisation_id, document_id))
task = DocumentBaseGetOrderedNuggets().apply_async(args=(user_id, base_name, organisation_id, document_name, document_content))

return make_response({'task_id': task.id}, 202)

17 changes: 16 additions & 1 deletion wannadb_web/worker/Web_API.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from wannadb.statistics import Statistics
from wannadb.status import StatusCallback
from wannadb_web.SQLite.Cache_DB import SQLiteCacheDBWrapper
from wannadb_web.postgres.queries import getDocument_by_name, updateDocumentContent, getDocument
from wannadb_web.postgres.queries import getDocument_by_name, getDocumentByNameAndContent, updateDocumentContent, getDocument
from wannadb_web.postgres.transactions import addDocument
from wannadb_web.worker.data import Signals

Expand Down Expand Up @@ -122,6 +122,21 @@ def get_ordert_nuggets(self, document_id: int):
logger.error(f"Document \"{document_name}\" not found in document base!")
self.signals.error.emit(Exception(f"Document \"{document_name}\" not found in document base!"))

def get_ordered_nuggets_by_doc_name(self, document_name: str, document_content: str):
document = getDocumentByNameAndContent(document_name, document_content, self.user_id)
if document is None:
logger.error(f"Document {document_name} not found!")
self.signals.error.emit(Exception(f"Document {document_name} not found!"))
return
logger.debug("get_ordered_nuggets_by_doc_name")
self.signals.status.emit("get_ordered_nuggets_by_doc_name")
for document in self.document_base.documents:
if document.name == document_name:
self.signals.ordert_nuggets.emit(list(sorted(document.nuggets, key=lambda x: x[CachedDistanceSignal])))
return
logger.error(f"Document \"{document_name}\" not found in document base!")
self.signals.error.emit(Exception(f"Document \"{document_name}\" not found in document base!"))


def create_document_base(self, documents: list[Document], attributes: list[Attribute], statistics: Statistics):
logger.debug("Called slot 'create_document_base'.")
Expand Down
8 changes: 5 additions & 3 deletions wannadb_web/worker/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,21 +305,23 @@ def run(self, user_id: int, base_name: str, organisation_id: int):
class DocumentBaseGetOrderedNuggets(BaseTask):
name = "DocumentBaseGetOrderedNuggets"

def run(self, user_id: int, base_name: str, organisation_id: int, document_id: int):
#def run(self, user_id: int, base_name: str, organisation_id: int, document_id: int):
def run(self, user_id: int, base_name: str, organisation_id: int, document_name: str, document_content: str):
self._signals = Signals(str(self.request.id))
self._redis_client = RedisCache(str(self.request.id))
self.load()

api = WannaDB_WebAPI(user_id, base_name, organisation_id)
api.load_document_base_from_bson()
api.get_ordert_nuggets(document_id)
#api.get_ordert_nuggets(document_id)
api.get_ordered_nuggets_by_doc_name(document_name, document_content)
# no need to update the document base
self.update(State.SUCCESS)
return self


class DocumentBaseConfirmNugget(BaseTask):
name = "DocumentBaseGetOrderedNuggets"
name = "DocumentBaseConfirmNugget"

def run(self, user_id: int, base_name: str, organisation_id: int,
document_id_for_nugget_x: int, nugget: Union[str, InformationNugget],
Expand Down

0 comments on commit c8c854f

Please sign in to comment.