Commit f42805b4 authored by Manuel's avatar Manuel

[TraceRetrieval] changed MessageHandler to work with tables

parent 313cea69
...@@ -15,7 +15,7 @@ ...@@ -15,7 +15,7 @@
# }, # },
# { # {
# "use_case": use_case, # "use_case": use_case,
# "name": "brand_layer", # "name": "brand_layer",a
# "properties": [ # "properties": [
# "UniqueID", # "UniqueID",
# "brand", # "brand",
......
import sys
import os
from pathlib import Path
from typing import Dict, Any
import requests
modules_path = '../../../modules/'
if os.path.exists(modules_path):
sys.path.insert(1, modules_path)
import network_constants as nc
from security.token_manager import TokenManager
import tables.add_pizza_table as pizza
def add_use_case(use_case: str):
jwt = TokenManager.getInstance().getToken()
url = f"https://articonf1.itec.aau.at:30420/api/use-cases"
response = requests.post(
url,
verify=False,
proxies = { "http":None, "https":None },
headers = { "Authorization": f"Bearer {jwt}"},
json = {"name": use_case}
)
print(url+": "+str(response.status_code))
if __name__ == "__main__":
use_case = "debug"
# disable ssl warnings :)
requests.packages.urllib3.disable_warnings()
add_use_case(use_case)
pizza.main(use_case)
\ No newline at end of file
import network_constants as nc
from security.token_manager import TokenManager
import requests
def add_table(use_case: str, table_name: str):
jwt = TokenManager.getInstance().getToken()
columns = {
"name": "name",
"dough": "dough//type",
"cheese": "dough//cheese",
"sauce": "sauces[0]//name",
"sugar": "sauces[0]//sugarcontent",
"UniqueID": "name+dough//type"
}
url = f"https://articonf1.itec.aau.at:30420/api/use-cases/{use_case}/tables"
table = {
"name": table_name,
"mappings": columns
}
response = requests.post(
url,
verify=False,
proxies = { "http":None, "https":None },
headers = { "Authorization": f"Bearer {jwt}"},
json = table
)
print(url+": "+str(response.status_code))
def main(use_case: str):
print("PIZZA")
add_table(use_case, "pizza")
\ No newline at end of file
from typing import Dict from typing import Dict
class Transaction: class Transaction:
def __init__(self, use_case:str, properties:Dict): def __init__(self, use_case:str, table:str, properties:Dict):
self.use_case = use_case self.use_case = use_case
self.properties = properties self.properties = properties
self.table = table
def to_serializable_dict(self): def to_serializable_dict(self):
return { return {
"use_case": self.use_case, "use_case": self.use_case,
"table": self.table,
"id": self.properties["UniqueID"], "id": self.properties["UniqueID"],
"properties": self.properties "properties": self.properties
} }
...@@ -17,4 +19,4 @@ class Transaction: ...@@ -17,4 +19,4 @@ class Transaction:
@staticmethod @staticmethod
def from_serializable_dict(data: Dict): def from_serializable_dict(data: Dict):
return Transaction(data["use_case"], data["properties"]) return Transaction(data["use_case"], data["table"], data["properties"])
\ No newline at end of file \ No newline at end of file
...@@ -124,9 +124,8 @@ class MessageHandler: ...@@ -124,9 +124,8 @@ class MessageHandler:
''' '''
jwt_token = TokenManager.getInstance().getToken() jwt_token = TokenManager.getInstance().getToken()
# query schema information # query tables for use-case
url = f'https://{network_constants.BUSINESS_LOGIC_HOSTNAME}:{network_constants.BUSINESS_LOGIC_REST_PORT}/api/use-cases/{use_case}/schema' url = f'https://{network_constants.BUSINESS_LOGIC_HOSTNAME}:{network_constants.BUSINESS_LOGIC_REST_PORT}/api/use-cases/{use_case}/tables'
response = requests.get( response = requests.get(
url, url,
verify = False, verify = False,
...@@ -135,9 +134,10 @@ class MessageHandler: ...@@ -135,9 +134,10 @@ class MessageHandler:
) )
if response.status_code != 200: if response.status_code != 200:
raise ValueError("no schema information available") raise ValueError("Error while retrieving schema information")
return json.loads(response.text) tables = json.loads(response.text)
return tables
def handle_blockchain_transaction(self, transaction_message: Dict): def handle_blockchain_transaction(self, transaction_message: Dict):
''' '''
...@@ -148,22 +148,41 @@ class MessageHandler: ...@@ -148,22 +148,41 @@ class MessageHandler:
transaction_message - Required: data of the transaction. Has to contain the key 'ApplicationType' which stores the use-case. transaction_message - Required: data of the transaction. Has to contain the key 'ApplicationType' which stores the use-case.
''' '''
# check if there is a use-case in the message
if "ApplicationType" not in transaction_message.keys(): if "ApplicationType" not in transaction_message.keys():
LOGGER.error("Transaction has no ApplicationType, storing it under use-case 'unknown'.") LOGGER.error("Transaction has no ApplicationType, storing it under use-case 'unknown'.")
transaction_message["ApplicationType"] = "unknown" transaction_message["ApplicationType"] = "unknown"
self._mongo_repo.add_failed_transaction(transaction_message) self._mongo_repo.add_failed_transaction(transaction_message)
return return
# check if there is a doctype in the message
if "docType" not in transaction_message.keys():
LOGGER.error("Transaction has no docType, ignoring it.")
return
use_case = transaction_message["ApplicationType"] use_case = transaction_message["ApplicationType"]
docType = transaction_message["docType"]
try: try:
data = self._fetch_schema_information(use_case) tables = self._fetch_schema_information(use_case)
except ValueError as e: except ValueError as e:
print(f"{e}") print(f"{e}")
self._mongo_repo.add_failed_transaction(transaction_message) self._mongo_repo.add_failed_transaction(transaction_message)
return return
mappings = data["mappings"] target_table = None
# find correct table
for table in tables:
if table["name"] == docType:
target_table = table
break
# abort if table does not exist.
if target_table == None:
LOGGER.error(f"There is no table '{docType}', ignoring the message.")
return
mappings = table["mappings"]
try: try:
flattened = self._flatten_transaction(transaction_message, mappings) flattened = self._flatten_transaction(transaction_message, mappings)
except KeyError as e: except KeyError as e:
...@@ -171,7 +190,7 @@ class MessageHandler: ...@@ -171,7 +190,7 @@ class MessageHandler:
self._mongo_repo.add_failed_transaction(transaction_message) self._mongo_repo.add_failed_transaction(transaction_message)
return return
transaction = Transaction(use_case, flattened) transaction = Transaction(use_case, target_table["name"], flattened)
try: try:
self._mongo_repo.add_transaction(transaction) self._mongo_repo.add_transaction(transaction)
except ValueError as e: except ValueError as e:
......
...@@ -3,6 +3,7 @@ import manage_sys_paths ...@@ -3,6 +3,7 @@ import manage_sys_paths
import json import json
from messaging.MessageHandler import MessageHandler from messaging.MessageHandler import MessageHandler
from database.entities.transaction import Transaction
class DummyMongoRepo: class DummyMongoRepo:
'''Dummy class to be used for testing the MessageHandler''' '''Dummy class to be used for testing the MessageHandler'''
...@@ -10,6 +11,10 @@ class DummyMongoRepo: ...@@ -10,6 +11,10 @@ class DummyMongoRepo:
def insert_trace(self, trace): def insert_trace(self, trace):
self.last_trace = trace self.last_trace = trace
def add_transaction(self, transaction: Transaction):
print(str(transaction.to_serializable_dict()))
from messaging.DummyMessageManager import DummyMessageManager as DummyMessageSender from messaging.DummyMessageManager import DummyMessageManager as DummyMessageSender
class Test_MessageHandler(unittest.TestCase): class Test_MessageHandler(unittest.TestCase):
...@@ -50,6 +55,7 @@ class Test_MessageHandler(unittest.TestCase): ...@@ -50,6 +55,7 @@ class Test_MessageHandler(unittest.TestCase):
'content': 'content':
{ {
"ApplicationType": "debug", "ApplicationType": "debug",
"docType": "pizza",
"name": "Margherita", "name": "Margherita",
"dough": { "dough": {
"type": "wheat", "type": "wheat",
...@@ -57,10 +63,12 @@ class Test_MessageHandler(unittest.TestCase): ...@@ -57,10 +63,12 @@ class Test_MessageHandler(unittest.TestCase):
}, },
"sauces": [ "sauces": [
{ {
"name": "tomato" "name": "tomato",
"sugarcontent": 0.0,
}, },
{ {
"name": "caramel" "name": "caramel",
"sugarcontent": 1.0,
} }
] ]
} }
...@@ -76,6 +84,9 @@ class Test_MessageHandler(unittest.TestCase): ...@@ -76,6 +84,9 @@ class Test_MessageHandler(unittest.TestCase):
print("STARTING THE TEST...") print("STARTING THE TEST...")
msg = self._get_pizza_message() msg = self._get_pizza_message()
print(f"msg: {msg}")
_ = self.handler.handle_blockchain_transaction(json.loads(msg)["content"]) _ = self.handler.handle_blockchain_transaction(json.loads(msg)["content"])
if __name__ == '__main__': if __name__ == '__main__':
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment