Skip to content

Commit

Permalink
Добавлена загрузка дампа при пустой БД при старте сервера
Browse files Browse the repository at this point in the history
  • Loading branch information
azazzze1 committed Dec 14, 2024
1 parent e123f9f commit 97d95f5
Show file tree
Hide file tree
Showing 6 changed files with 805 additions and 768 deletions.
26 changes: 26 additions & 0 deletions backend/app/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
from flask import Flask
from flask_cors import CORS
import os
import requests
from app.models.neo4jConnection import Neo4jConnection

app = Flask(__name__, static_folder='static')

Expand All @@ -8,4 +11,27 @@
app.secret_key = ' key'
app.config['JSON_AS_ASCII'] = False

uri = os.getenv("NEO4J_URI", "bolt://localhost:7687")
user = os.getenv("NEO4J_USER", "neo4j")
password = os.getenv("NEO4J_PASSWORD", "password")

conn = Neo4jConnection(uri, user, password)

conn.wait_for_neo4j()

from app import routes

initialized = False

@app.before_request
def initialize_app():
global initialized
if not initialized:
initialized = True
query_string = "MATCH (n) RETURN COUNT(n) AS count"
if conn.query(query_string):
file_path = os.path.abspath(os.path.join(os.path.dirname(__file__), 'models/dumps/test.csv'))
with open(file_path, 'rb') as f:
requests.post("http://127.0.0.1:5000/api/import_dump", files={'file': f})


12 changes: 12 additions & 0 deletions backend/app/models/neo4jConnection.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from neo4j import GraphDatabase
import time

class Neo4jConnection:
def __init__(self, uri, user, password):
Expand All @@ -21,3 +22,14 @@ def query(self, query, parameters=None, db=None):
if session is not None:
session.close()
return response

def wait_for_neo4j(self, max_retries=10, delay=5):
for attempt in range(max_retries):
try:
with self.driver.session() as session:
session.run("RETURN 1")
return True
except Exception as e:
print(f"Neo4j is not ready yet: {e}")
time.sleep(delay)
return False
2 changes: 1 addition & 1 deletion backend/app/models/utils/allowedEntity.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
"admin",
"birthday",
"last_update",
"registration_data",
"registration_date",
"disease_name",
"disease_description",
"disease_recommendations",
Expand Down
6 changes: 1 addition & 5 deletions backend/app/routes.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from app import app
from app import app, conn
from app.models.neo4jConnection import Neo4jConnection
from app.models.utils.allowedEntity import allowed_entity_parameters, CSV_columns, allowed_relations
from app.models.utils.modelsForDumpTransform import create_relation_dict
Expand All @@ -9,11 +9,7 @@
import os
import csv

uri = os.getenv("NEO4J_URI", "bolt://localhost:7687")
user = os.getenv("NEO4J_USER", "neo4j")
password = os.getenv("NEO4J_PASSWORD", "password")

conn = Neo4jConnection(uri, user, password)

@app.route('/api/')
@app.route('/index')
Expand Down
1 change: 0 additions & 1 deletion backend/diseases.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
from app import app


Loading

0 comments on commit 97d95f5

Please sign in to comment.