Skip to content

Commit 9b7c51b

Browse files
committed
Refactor code
1 parent 136dc6c commit 9b7c51b

12 files changed

+1270
-338
lines changed

cache_handler/cache_handler.py

+18-3
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,15 @@
44
import redis
55
import logging
66
import data_translator
7+
from time import sleep
78
from typing import Dict, Any
8-
from database import Memgraph
9+
from gqlalchemy import Memgraph
910

1011

1112
DB_FETCH_TIME = float(os.getenv('DB_FETCH_TIME', '0.5'))
12-
REDIS_HOST = os.getenv('REDIS_HOST', '172.18.0.2')
13+
MEMGRAPH_IP = os.getenv('MEMGRAPH_IP', 'memgraph')
14+
MEMGRAPH_PORT = os.getenv('MEMGRAPH_PORT', '7687')
15+
REDIS_HOST = os.getenv('REDIS_HOST', 'redis')
1316
REDIS_PORT = int(os.getenv('REDIS_PORT', '6379'))
1417

1518
logging.basicConfig(format='%(asctime)-15s [%(levelname)s]: %(message)s')
@@ -21,9 +24,21 @@
2124

2225
r = redis.StrictRedis(host=REDIS_HOST, port=REDIS_PORT,
2326
charset="utf-8", decode_responses=True)
24-
db = Memgraph()
27+
28+
def connect_to_memgraph(memgraph_ip, memgraph_port):
29+
memgraph = Memgraph(host=memgraph_ip, port=int(memgraph_port))
30+
while(True):
31+
try:
32+
if (memgraph._get_cached_connection().is_active()):
33+
return memgraph
34+
except:
35+
logger.info("Memgraph probably isn't running.")
36+
sleep(1)
37+
38+
db = connect_to_memgraph(MEMGRAPH_IP, MEMGRAPH_PORT)
2539
results = {}
2640

41+
2742
while(True):
2843
start_time = time.time()
2944

cache_handler/data_translator.py

+21-17
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from typing import Any, Dict, Iterator
55

66

7-
logger = logging.getLogger('cache')
7+
logger = logging.getLogger("cache")
88

99

1010
def json_relationships_satellites(relationships: Iterator[Dict[str, Any]]) -> "JSON":
@@ -14,22 +14,26 @@ def json_relationships_satellites(relationships: Iterator[Dict[str, Any]]) -> "J
1414
json_satellites = {}
1515
sat_ids = set()
1616
for rel in relationships:
17-
r = rel['r']
18-
s1 = rel['s1']
19-
s2 = rel['s2']
20-
21-
json_relationships.append([s1.properties['x'], s1.properties['y'],
22-
s2.properties['x'], s2.properties['y'], r.properties['transmission_time']])
23-
24-
if(not s1.properties['id'] in sat_ids):
25-
json_satellites[s1.properties['id']] = [
26-
s1.properties['x'], s1.properties['y']]
27-
sat_ids.add(s1.properties['id'])
28-
if(not s2.properties['id'] in sat_ids):
29-
json_satellites[s2.properties['id']] = [
30-
s2.properties['x'], s2.properties['y']]
31-
sat_ids.add(s2.properties['id'])
17+
r = rel["r"]
18+
s1 = rel["s1"]
19+
s2 = rel["s2"]
20+
print(rel)
21+
json_relationships.append([s1.x, s1.y, s2.x, s2.y, r.transmission_time])
22+
23+
if not s1.id in sat_ids:
24+
json_satellites[s1.id] = [
25+
s1.x,
26+
s1.y,
27+
]
28+
sat_ids.add(s1.id)
29+
if not s2.id in sat_ids:
30+
json_satellites[s2.id] = [
31+
s2.x,
32+
s2.y,
33+
]
34+
sat_ids.add(s2.id)
3235

3336
logger.info(
34-
f'Relationship and Satellite JSON created in {time.time() - start_time} seconds.')
37+
f"Relationship and Satellite JSON created in {time.time() - start_time} seconds."
38+
)
3539
return json.dumps(json_relationships), json.dumps(json_satellites)

cache_handler/database/__init__.py

-2
This file was deleted.

cache_handler/database/connection.py

-119
This file was deleted.

cache_handler/database/memgraph.py

-82
This file was deleted.

cache_handler/database/models.py

-82
This file was deleted.

0 commit comments

Comments
 (0)