Skip to content

Commit

Permalink
Merge pull request #9 from moevm/import-export
Browse files Browse the repository at this point in the history
Import export: fix
  • Loading branch information
YariKar authored Dec 12, 2024
2 parents 502f0c3 + 9b5124a commit 15d80e5
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 2 deletions.
44 changes: 42 additions & 2 deletions gighunt/src/gighunt/modules/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,43 @@ def _get_dump(self) -> dict:

return graph_data

def _count_imported_records(self, graph_data) -> int:
static = graph_data.get("static", [])
vertices = graph_data.get("vertices", [])
edges = graph_data.get("edges", [])

# Vertices
users = vertices["users"]
groups = vertices["groups"]
announcements = vertices["announcements"]
places = vertices["places"]

# Edges
user_groups = edges["users"]["groups"]
stars_to_users = edges["stars"]["users"]
stars_to_groups = edges["stars"]["groups"]
stars_to_announcements = edges["stars"]["announcements"]
comments_to_announcements = edges["comments"]["announcements"]
producer_announcements_from_users = edges["producer_announcements"]["users"]
producer_announcements_from_groups = edges["producer_announcements"][
"groups"
]

amount = (
len(static)
+ len(users)
+ len(groups)
+ len(announcements)
+ len(places)
+ len(user_groups)
+ len(stars_to_users)
+ len(stars_to_groups)
+ len(stars_to_announcements)
+ len(comments_to_announcements)
+ len(producer_announcements_from_users)
+ len(producer_announcements_from_groups)
)
return amount

async def _import_data(self, file: UploadFile) -> Response:
"""
Expand All @@ -243,8 +280,11 @@ async def _import_data(self, file: UploadFile) -> Response:
file_content = await file.read()
data = json.loads(file_content)
self._set_dump(data)

return {"message": "Graph imported successful!"}
number_of_records = self._count_imported_records(data)
return {
"message": "Данные успешно импортированы!",
"number_of_records": number_of_records
}

except json.JSONDecodeError:
raise HTTPException(status_code=400, detail="Invalid JSON format")
Expand Down
11 changes: 11 additions & 0 deletions gighunt/src/gighunt/modules/use_cases/base_edge_use_cases.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,17 @@ def create_new_entity(self, edge_data:Json, name:str)-> bool | Json:
def get_entity(self, entity_id: str, name) -> Json | None:
return self._graph.edge_collection(name).get(entity_id)

def insert_json_edge(self, name: str, json_edge: Json) -> None:
try:
self._graph.edge_collection(name).insert(json_edge)
except DocumentInsertError as err:
self._logger.error(f"Error inserting edge {json_edge}: {err}")

def clear(self) -> None:
if self.edge_collection_names:
for name in self.edge_collection_names:
self._graph.edge_collection(name.value).truncate()

def delete_entity(self, entity_id: str, name)->Json|None:
return self._graph.edge_collection(name).delete(entity_id)

Expand Down

0 comments on commit 15d80e5

Please sign in to comment.