From 34255b35c2a97b2c210c9d2f288c8fc173f1c4bb Mon Sep 17 00:00:00 2001 From: Mattia Matteini Date: Wed, 24 Apr 2024 18:57:16 +0200 Subject: [PATCH] feat(storage): improve question insertion with previous question --- docker-compose.yml | 2 - .../repositories/graph_question_repository.py | 101 ++++++++++++------ 2 files changed, 71 insertions(+), 32 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index e9fa685..cab4b85 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,5 +1,3 @@ -version: '3.8' - services: aequitas-reasoner-db: diff --git a/storage/repositories/graph_question_repository.py b/storage/repositories/graph_question_repository.py index 1a43928..2e1c62e 100644 --- a/storage/repositories/graph_question_repository.py +++ b/storage/repositories/graph_question_repository.py @@ -5,7 +5,9 @@ from dotenv import load_dotenv from neo4j import GraphDatabase -from domain.graph.core import ProjectId, QuestionId, Question +from domain.graph.core import ProjectId, QuestionId, Question, AnswerId, Answer +from domain.graph.core.enum import QuestionType, Action +from domain.graph.factories import AnswerFactory, QuestionFactory from domain.graph.repositories import QuestionRepository from presentation.presentation import serialize, deserialize @@ -32,12 +34,11 @@ def get_all_questions(self, project_id: ProjectId) -> List[Question]: q: dict = question["q"] q["id"] = {"code": q["id"]} q["available_answers"] = [a for a in question["answers"]] - q["selected_answers"] = [] questions.append(deserialize(q, Question)) return questions def get_question_by_id( - self, project_id: ProjectId, question_id: QuestionId + self, project_id: ProjectId, question_id: QuestionId ) -> Question: query: LiteralString = ( "MATCH (q:Question {id: $question_id})-[:HAS_ANSWER]->(a:Answer) RETURN q, COLLECT(a) AS answers" @@ -47,28 +48,36 @@ def get_question_by_id( question: dict = res[0]["q"] question["id"] = {"code": question["id"]} question["available_answers"] = [a for a in res[0]["answers"]] - question["selected_answers"] = [] return deserialize(question, Question) def insert_question(self, project_id: ProjectId, question: Question) -> None: with self._driver.session() as session: q: dict = self.__convert_question_in_node(question) + prev_question_id: str = question.previous_question_id.code if question.previous_question_id else None + print(q) session.run("CREATE (:Question $question)", question=q).data() for answer in question.available_answers: + a: dict = self.__convert_answer_in_node(answer) session.run( "CREATE (:Answer $answer)", - answer=serialize(answer), + answer=a, ).data() session.run( - "MATCH (q:Question {id: $question_id}) MATCH (a:Answer {text: $text, value: $value}) CREATE (q)-[:HAS_ANSWER]->(a)", + "MATCH (q:Question {id: $question_id}) MATCH (a:Answer {id: $answer_id}) CREATE (q)-[:HAS_ANSWER]->(a)", question_id=question.id.code, - text=answer.text, - value=answer.value, + answer_id=answer.id.code, + ).data() + + if prev_question_id: + session.run( + "MATCH (q1:Question {id: $question_id}) MATCH (q2:Question {id: $prev_question_id}) CREATE (q1)-[:PREVIOUS]->(q2)", + question_id=question.id.code, + prev_question_id=prev_question_id, ).data() def update_question( - self, project_id: ProjectId, question_id: str, question + self, project_id: ProjectId, question_id: str, question ) -> None: pass @@ -78,30 +87,62 @@ def delete_question(self, project_id: ProjectId, question_id: str) -> None: def __convert_question_in_node(self, question: Question) -> dict: q: dict = serialize(question) q["id"] = question.id.code + del q["previous_question_id"] del q["available_answers"] - del q["selected_answers"] return q + def __convert_answer_in_node(self, answer: Answer) -> dict: + a: dict = serialize(answer) + a["id"] = answer.id.code + print(a) + return a + + def delete_all_questions(self) -> None: + with self._driver.session() as session: + session.run("MATCH (n:Question) DETACH DELETE n").data() + session.run("MATCH (n:Answer) DETACH DELETE n").data() + if __name__ == "__main__": - print( - GraphQuestionRepository().get_question_by_id( - ProjectId(code="project1"), QuestionId(code="ci-question") - ) - ) - # GraphQuestionRepository().insert_question( - # ProjectId(code="project1"), - # QuestionFactory().create_question( - # QuestionId(code="ci-question"), - # "Do you use CI?", - # QuestionType.SINGLE_CHOICE, - # frozenset( - # { - # AnswerFactory().create_answer("Yes", "yes"), - # AnswerFactory().create_answer("A little bit", "little-bit"), - # AnswerFactory().create_answer("No", "no"), - # } - # ), - # action_needed=Action.METRICS_CHECK, - # ), + GraphQuestionRepository().delete_all_questions() + + # print( + # GraphQuestionRepository().get_question_by_id( + # ProjectId(code="project1"), QuestionId(code="ci-question") + # ) # ) + GraphQuestionRepository().insert_question( + ProjectId(code="project1"), + QuestionFactory().create_question( + QuestionId(code="ci-question"), + "Do you use CI?", + QuestionType.SINGLE_CHOICE, + frozenset( + { + AnswerFactory().create_answer(AnswerId(code="answer-yes"), "Yes", "yes"), + AnswerFactory().create_answer(AnswerId(code="answer-little-bit"), "A little bit", "little-bit"), + AnswerFactory().create_answer(AnswerId(code="answer-no"), "No", "no"), + } + ), + None, + action_needed=Action.METRICS_CHECK, + ), + ) + + GraphQuestionRepository().insert_question( + ProjectId(code="project2"), + QuestionFactory().create_question( + QuestionId(code="cd-question"), + "Do you use CD?", + QuestionType.SINGLE_CHOICE, + frozenset( + { + AnswerFactory().create_answer(AnswerId(code="yes"), "Yes", "yes"), + AnswerFactory().create_answer(AnswerId(code="little-bit"), "A little bit", "little-bit"), + AnswerFactory().create_answer(AnswerId(code="no"), "No", "no"), + } + ), + QuestionId(code="ci-question"), + action_needed=Action.METRICS_CHECK, + ), + )