Skip to content

Commit

Permalink
feat(storage): improve question insertion with previous question
Browse files Browse the repository at this point in the history
  • Loading branch information
Mala1180 committed Apr 26, 2024
1 parent 858804c commit 34255b3
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 32 deletions.
2 changes: 0 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
version: '3.8'

services:

aequitas-reasoner-db:
Expand Down
101 changes: 71 additions & 30 deletions storage/repositories/graph_question_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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"
Expand All @@ -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

Expand All @@ -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,
),
)

0 comments on commit 34255b3

Please sign in to comment.