init
This commit is contained in:
76
CSV.py
76
CSV.py
@@ -1,4 +1,4 @@
|
|||||||
import json, csv, os, time, stomp
|
import json, csv, os, time, stomp, logging
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from flask_backend.Stomp_client import get_connection
|
from flask_backend.Stomp_client import get_connection
|
||||||
|
|
||||||
@@ -6,31 +6,81 @@ QUEUE_NAME = "/queue/testQueue"
|
|||||||
OUTPUT_DIR = "/app/output"
|
OUTPUT_DIR = "/app/output"
|
||||||
os.makedirs(OUTPUT_DIR, exist_ok=True)
|
os.makedirs(OUTPUT_DIR, exist_ok=True)
|
||||||
|
|
||||||
|
# 🔧 Logger setup
|
||||||
|
logging.basicConfig(
|
||||||
|
level=logging.INFO,
|
||||||
|
format="%(levelname)s:%(name)s:%(message)s"
|
||||||
|
)
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
class MyListener(stomp.ConnectionListener):
|
class MyListener(stomp.ConnectionListener):
|
||||||
def on_message(self, frame):
|
def on_message(self, frame):
|
||||||
|
logger.info(f"Přijatá zpráva z activeMQ: {frame.body}")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
data = json.loads(frame.body)
|
data = json.loads(frame.body)
|
||||||
except json.JSONDecodeError:
|
except Exception as e:
|
||||||
print("Received invalid JSON:", frame.body)
|
logger.error(f"Error when creating JSON: {e}\nInput was: {frame}")
|
||||||
return
|
return
|
||||||
|
|
||||||
|
# Vytvoření souboru
|
||||||
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
||||||
filename = os.path.join(OUTPUT_DIR, f"message_{timestamp}.csv")
|
filename = os.path.join(OUTPUT_DIR, f"actors_{timestamp}.csv")
|
||||||
|
|
||||||
try:
|
"""try:
|
||||||
with open(filename, "w", newline="") as f:
|
with open(filename, "w", newline="", encoding="utf-8") as f:
|
||||||
writer = csv.writer(f)
|
writer = csv.writer(f, delimiter=";")
|
||||||
writer.writerow(data.keys())
|
|
||||||
writer.writerow(data.values())
|
for actor in data:
|
||||||
print(f"Saved CSV: {filename}")
|
row = ["list"]
|
||||||
|
|
||||||
|
for key, value in actor.items():
|
||||||
|
value = actor.get(key, "")
|
||||||
|
|
||||||
|
if isinstance(value, list):
|
||||||
|
for item in value:
|
||||||
|
if isinstance(item, dict):
|
||||||
|
row.extend(item.values())
|
||||||
|
else:
|
||||||
|
row.append(str(item))
|
||||||
|
|
||||||
|
|
||||||
|
elif isinstance(value, bool):
|
||||||
|
value = str(value).lower()
|
||||||
|
|
||||||
|
else:
|
||||||
|
row.append(value)
|
||||||
|
|
||||||
|
writer.writerow(row)
|
||||||
|
|
||||||
|
logger.info(f"CSV uložen: {filename} ({len(data)} řádků)")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print("Error writing CSV:", e)
|
logger.error(f"Chyba při zápisu CSV: {e}")"""
|
||||||
|
|
||||||
|
try:
|
||||||
|
with open(filename, "w", newline="", encoding="utf-8") as f:
|
||||||
|
writer = csv.writer(f, delimiter=";")
|
||||||
|
|
||||||
# Připojení k ActiveMQ s listenerem
|
for actor in data:
|
||||||
|
row = ["list"]
|
||||||
|
for key, value in actor.items():
|
||||||
|
if isinstance(value, dict): # rozbalíme dict (např. address)
|
||||||
|
row.extend(value.values())
|
||||||
|
else:
|
||||||
|
row.append(value)
|
||||||
|
writer.writerow(row)
|
||||||
|
|
||||||
|
logger.info(f"CSV uložen: {filename} ({len(data)} řádků)")
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"Chyba při zápisu CSV: {e}")
|
||||||
|
|
||||||
|
# ActiveMQ client
|
||||||
listener = MyListener()
|
listener = MyListener()
|
||||||
conn = get_connection(listener=listener)
|
conn = get_connection(listener=listener)
|
||||||
conn.subscribe(destination=QUEUE_NAME, id=1, ack="auto")
|
conn.subscribe(destination=QUEUE_NAME, id=1, ack="auto")
|
||||||
|
|
||||||
# keep process alive
|
logger.info(f"ActiveMQ is running\nAddress: {QUEUE_NAME}")
|
||||||
|
|
||||||
|
# Keep the process active
|
||||||
while True:
|
while True:
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
|
|||||||
|
Before Width: | Height: | Size: 46 KiB After Width: | Height: | Size: 46 KiB |
@@ -1,14 +1,32 @@
|
|||||||
import json
|
import json
|
||||||
from Stomp_client import get_connection
|
from Stomp_client import get_connection
|
||||||
|
import logging
|
||||||
|
|
||||||
|
logging.basicConfig(level=logging.INFO)
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
QUEUE_NAME = "/queue/testQueue"
|
QUEUE_NAME = "/queue/testQueue"
|
||||||
|
|
||||||
|
|
||||||
def send_to_queue(payload: dict):
|
def send_to_queue(payload: dict):
|
||||||
"""
|
"""
|
||||||
Posílá JSON payload do ActiveMQ queue, přijíma dic
|
Posílá JSON payload do ActiveMQ queue, přijíma dic
|
||||||
"""
|
"""
|
||||||
|
logger.info("Input payload to send to queue: %s", payload)
|
||||||
|
|
||||||
|
try:
|
||||||
|
actors_list = (
|
||||||
|
payload["soapenv:Envelope"]["soapenv:Body"]["actor"]["list"]
|
||||||
|
)
|
||||||
|
except KeyError as e:
|
||||||
|
logger.error("Missing key in payload: %s", e)
|
||||||
|
return
|
||||||
|
|
||||||
|
actors_list_JSON = json.dumps(actors_list)
|
||||||
conn = get_connection() # připojení s retry loop
|
conn = get_connection() # připojení s retry loop
|
||||||
conn.send(destination=QUEUE_NAME, body=json.dumps(payload))
|
conn.send(destination=QUEUE_NAME, body=actors_list_JSON)
|
||||||
print("Message sent to ActiveMQ:", payload)
|
|
||||||
|
logger.info("Message sent to ActiveMQ: %s", actors_list_JSON)
|
||||||
|
|
||||||
conn.disconnect()
|
conn.disconnect()
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,7 @@
|
|||||||
import stomp, os, time
|
import stomp, os, time, logging
|
||||||
|
|
||||||
|
logging.basicConfig(level=logging.INFO)
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
MQ_HOST = os.getenv("MQ_HOST", "activemq")
|
MQ_HOST = os.getenv("MQ_HOST", "activemq")
|
||||||
MQ_PORT = int(os.getenv("MQ_PORT", 61613))
|
MQ_PORT = int(os.getenv("MQ_PORT", 61613))
|
||||||
|
|||||||
@@ -1,6 +1,9 @@
|
|||||||
from flask import Flask, request, jsonify
|
from flask import Flask, request, jsonify
|
||||||
from Sender import send_to_queue
|
from Sender import send_to_queue
|
||||||
import xmltodict
|
import xmltodict
|
||||||
|
import logging
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
|
|
||||||
@@ -10,10 +13,12 @@ def send():
|
|||||||
try:
|
try:
|
||||||
payload_dict = xmltodict.parse(xml_payload) # převede XML na dict
|
payload_dict = xmltodict.parse(xml_payload) # převede XML na dict
|
||||||
send_to_queue(payload_dict) # pošle se do Sender.py
|
send_to_queue(payload_dict) # pošle se do Sender.py
|
||||||
|
logger.info("Request payload sent to queue: %s", payload_dict)
|
||||||
|
|
||||||
return jsonify({"status": "ok"}), 200
|
return jsonify({"status": "ok"}), 200
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
logger.error("Error processing request: %s", str(e))
|
||||||
return jsonify({"error": str(e)}), 500
|
return jsonify({"error": str(e)}), 500
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
3
output/actors_20251019_205007.csv
Normal file
3
output/actors_20251019_205007.csv
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
list;59088032;Jiri Novak;Vika;StreetName;4312;SANDNES;NO;true;StreetName 1A;NORWAY;+4793488400;+4793488999;testguy@gmail.com;1962-10-01;false;false
|
||||||
|
list;59088033;Tomas Novak;Vika;Postboks 16;4318;BERGEN;NO;true;StreetName 1B;NORWAY;+4793488888;testguy2@gmail.com;1963-10-01;false;false
|
||||||
|
list;59088034;Karel Novak;Vika;StreetName;4319;STAVANGER;NO;true;StreetName 1C;NORWAY;+4793488777;testguy3@gmail.com;1964-10-01;false;false
|
||||||
|
3
output/filler
Normal file
3
output/filler
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
Musí to tu být, protože git neukláda prázdné složky.
|
||||||
|
|
||||||
|
Do složky output se ukladájí CSV soubory.
|
||||||
@@ -1,2 +0,0 @@
|
|||||||
soapenv:Envelope
|
|
||||||
"{'@xmlns:soapenv': 'http://schemas.xmlsoap.org/soap/envelope/', 'soapenv:Body': {'actor': {'list': [{'id': '59088032', 'firstName': 'Jiri Novak', 'lastName': 'Vika', 'address': {'streetName': 'StreetName', 'postCode': '4312', 'cityName': 'SANDNES', 'countryCode': 'NO', 'isPostBox': 'true'}, 'addressLine1': 'StreetName 1A', 'country': 'NORWAY', 'phoneNumber': '+4793488400', 'cellularPhoneNumber': '+4793488999', 'emailAddress': 'testguy@gmail.com', 'dateOfBirth': '1962-10-01', 'hasSecretAddress': 'false', 'isReservedAgainstSalesMaterial': 'false'}, {'id': '59088033', 'firstName': 'Tomas Novak', 'lastName': 'Vika', 'address': {'streetName': 'Postboks 16', 'postCode': '4318', 'cityName': 'BERGEN', 'countryCode': 'NO', 'isPostBox': 'true'}, 'addressLine1': 'StreetName 1B', 'country': 'NORWAY', 'phoneNumber': '+4793488888', 'emailAddress': 'testguy2@gmail.com', 'dateOfBirth': '1963-10-01', 'hasSecretAddress': 'false', 'isReservedAgainstSalesMaterial': 'false'}, {'id': '59088034', 'firstName': 'Karel Novak', 'lastName': 'Vika', 'address': {'streetName': 'StreetName', 'postCode': '4319', 'cityName': 'STAVANGER', 'countryCode': 'NO', 'isPostBox': 'true'}, 'addressLine1': 'StreetName 1C', 'country': 'NORWAY', 'phoneNumber': '+4793488777', 'emailAddress': 'testguy3@gmail.com', 'dateOfBirth': '1964-10-01', 'hasSecretAddress': 'false', 'isReservedAgainstSalesMaterial': 'false'}]}}}"
|
|
||||||
|
Reference in New Issue
Block a user