Commit 19cfc8fe authored by Manuel's avatar Manuel

traceRetrieval: cleanup

parent 3414bdc1
...@@ -23,7 +23,7 @@ paths: ...@@ -23,7 +23,7 @@ paths:
responses: responses:
'200': '200':
description: "Successful Request" description: "Successful Request"
/transactions-failed/use_case/{use_case}: /use_cases/{use_case}/transactions-failed:
delete: delete:
operationId: "routes.transactions.delete_all_failed_for_use_case" operationId: "routes.transactions.delete_all_failed_for_use_case"
tags: tags:
...@@ -52,7 +52,7 @@ paths: ...@@ -52,7 +52,7 @@ paths:
responses: responses:
'200': '200':
description: "Successful Request" description: "Successful Request"
/transactions/use_case/{use_case}: /use_cases/{use_case}/transactions:
get: get:
operationId: "routes.transactions.all_for_use_case" operationId: "routes.transactions.all_for_use_case"
tags: tags:
...@@ -83,66 +83,4 @@ paths: ...@@ -83,66 +83,4 @@ paths:
type: object type: object
responses: responses:
'200': '200':
description: "Successful echo of request data" description: "Successful echo of request data"
\ No newline at end of file
/trace:
post:
operationId: "routes.blockchain_trace.post"
tags:
- "Blockchain Trace"
summary: "Add a new blockchain trace to SMART"
description: "Receives a new blockchain trace to store in SMART."
parameters:
- in: body
name: "BlockchainTrace"
description: "The trace to be added"
required: true
schema:
$ref: "#/definitions/BlockchainTrace"
responses:
'201':
description: "Successful operation"
'400':
description: "Invalid input"
get:
operationId: "routes.blockchain_trace.get"
tags:
- "Blockchain Trace"
summary: "Get blockchain traces"
description: "Returns all blockchain traces in the database"
parameters: []
responses:
'200':
description: "Successful operation"
schema:
$ref: "#/definitions/BlockchainTrace"
definitions:
BlockchainTrace:
type: "object"
properties:
TransactionId:
type: string
format: uuid
Timestamp:
type: "string"
format: "date-time"
ApplicationType:
type: "string"
TransactionFrom:
type: "string"
format: "uuid"
TransactionTo:
type: "string"
format: "uuid"
TransferredAsset:
type: "string"
ResourceIds:
type: "string"
ResourceMd5:
type: "string"
ResourceState:
type: "string"
Metadata:
type: "string"
\ No newline at end of file
import pymongo
import network_constants as netconst
from database.MongoRepositoryBase import MongoRepositoryBase
class MongoRepository(MongoRepositoryBase):
def __init__(self):
super().__init__(netconst.TRACE_RETRIEVAL_DB_HOSTNAME,
netconst.TRACE_RETRIEVAL_DB_PORT, 'traceRetrievalDB')
self._collection_name = 'traces'
def insert_trace(self, content: dict):
super().insert_entry(self._collection_name, content)
def get_traces(self, selection: dict = {}, projection: dict = {'_': 0}) -> pymongo.cursor.Cursor:
return super().get_entries(self._collection_name, selection, projection)
...@@ -85,9 +85,12 @@ class MessageHandler: ...@@ -85,9 +85,12 @@ class MessageHandler:
def _flatten_transaction(self, transaction: Dict, mappings: Dict): def _flatten_transaction(self, transaction: Dict, mappings: Dict):
''' '''
takes the (possibly nested) dictionary and resolves the given paths Resolves all paths in [mappings] with the data from [transaction] and creates
returns a map which is no longer nested with the keys of the a non-nested flattened element.
mappings parameter
@params
transaction - Required: dictionary, that is arbitrarily deep nested
mappings - Required: contains string->path mappings, describing how the flattened object is built
''' '''
flattened = {} flattened = {}
...@@ -115,13 +118,17 @@ class MessageHandler: ...@@ -115,13 +118,17 @@ class MessageHandler:
return flattened return flattened
def _fetch_schema_information(self, use_case: str) -> Dict: def _fetch_schema_information(self, use_case: str) -> Dict:
'''
Fetches the schema of the use-case from the business-logic microservice per REST.
@params
use_case - Required: string identifier for the use-case
'''
jwt_token = TokenManager.getInstance().getToken() jwt_token = TokenManager.getInstance().getToken()
# query schema information # query schema information
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}/schema'
print(f"CALLING: {url}")
response = requests.get( response = requests.get(
url, url,
verify = False, verify = False,
...@@ -129,27 +136,31 @@ class MessageHandler: ...@@ -129,27 +136,31 @@ class MessageHandler:
headers = { "Authorization": f"Bearer {jwt_token}"} headers = { "Authorization": f"Bearer {jwt_token}"}
) )
print(f"RESPONSE: {response.text}")
if response.status_code != 200: if response.status_code != 200:
raise ValueError("no schema information available") raise ValueError("no schema information available")
return json.loads(response.text) return json.loads(response.text)
def handle_blockchain_transaction(self, transaction: Dict): def handle_blockchain_transaction(self, transaction_message: Dict):
transaction_data = transaction '''
Proccesses a blockchain transaction. The schema gets fetched from the business-logic microservice and a flattened
version then is created out of it.
@params
transaction_message - Required: data of the transaction. Has to contain the key 'ApplicationType' which stores the use-case.
'''
use_case = transaction_data["ApplicationType"] use_case = transaction_message["ApplicationType"]
try: try:
data = self._fetch_schema_information(use_case) data = self._fetch_schema_information(use_case)
except ValueError as e: except ValueError as e:
print(f"{e}") print(f"{e}")
MessageHandler._repository.add_failed_transaction(transaction) MessageHandler._repository.add_failed_transaction(transaction_message)
return return
mappings = data["mappings"] mappings = data["mappings"]
flattened = self._flatten_transaction(transaction_data, mappings) flattened = self._flatten_transaction(transaction_message, mappings)
transaction = Transaction(use_case, flattened) transaction = Transaction(use_case, flattened)
MessageHandler._repository.add_transaction(transaction) MessageHandler._repository.add_transaction(transaction)
...@@ -160,8 +171,6 @@ class MessageHandler: ...@@ -160,8 +171,6 @@ class MessageHandler:
} }
msg_string = json.dumps(msg) msg_string = json.dumps(msg)
print("OUT: "+msg_string)
# inform semantic linking microservice # inform semantic linking microservice
self._message_sender.send_message('datahub', 'semantic-linking', msg_string) self._message_sender.send_message('datahub', 'semantic-linking', msg_string)
\ No newline at end of file
from flask import request, Response
from database.MongoRepository import MongoRepository
mongo_repo = MongoRepository()
def post():
return Response(response='Use the RESTful Gateway instead', status=405)
def get():
return list(mongo_repo.get_traces(projection={'_id': 0}))
\ No newline at end of file
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