Commit 5aca5b34 authored by Manuel's avatar Manuel

traceRetrieval: added storage of flattened layers

parent 741f1928
...@@ -13,6 +13,32 @@ basePath: "/api" ...@@ -13,6 +13,32 @@ basePath: "/api"
# Paths supported by the server application # Paths supported by the server application
paths: paths:
/transactions:
delete:
operationId: "routes.transactions.delete_all_transactions"
tags:
- "Transactions"
summary: "Delete all Transactions in the DB"
description: "Delete all Transactions in the DB"
responses:
'200':
description: "Successful Request"
/transactions/use_case/{use_case}:
get:
operationId: "routes.transactions.all_for_use_case"
tags:
- "Transactions"
summary: "Retrieves all Transactions in the given Use-Case"
description: "Retrieves all Transactions in the given Use-Case"
parameters:
- in: path
name: "use_case"
required: true
type: "string"
responses:
'200':
description: "Successful Request"
/debug: /debug:
post: post:
operationId: "routes.debug.echo" operationId: "routes.debug.echo"
...@@ -27,7 +53,7 @@ paths: ...@@ -27,7 +53,7 @@ paths:
schema: schema:
type: object type: object
responses: responses:
200: '200':
description: "Successful echo of request data" description: "Successful echo of request data"
/trace: /trace:
...@@ -45,9 +71,9 @@ paths: ...@@ -45,9 +71,9 @@ paths:
schema: schema:
$ref: "#/definitions/BlockchainTrace" $ref: "#/definitions/BlockchainTrace"
responses: responses:
201: '201':
description: "Successful operation" description: "Successful operation"
400: '400':
description: "Invalid input" description: "Invalid input"
get: get:
operationId: "routes.blockchain_trace.get" operationId: "routes.blockchain_trace.get"
...@@ -57,7 +83,7 @@ paths: ...@@ -57,7 +83,7 @@ paths:
description: "Returns all blockchain traces in the database" description: "Returns all blockchain traces in the database"
parameters: [] parameters: []
responses: responses:
200: '200':
description: "Successful operation" description: "Successful operation"
schema: schema:
$ref: "#/definitions/BlockchainTrace" $ref: "#/definitions/BlockchainTrace"
......
from typing import Dict
class Transaction:
def __init__(self, use_case:str, properties:Dict):
self.use_case = use_case
self.properties = properties
def to_serializable_dict(self):
return {
"use_case": self.use_case,
"id": self.properties["UniqueID"],
"properties": self.properties
}
def id(self):
return self.properties["UniqueID"]
@staticmethod
def from_serializable_dict(data: Dict):
return Transaction(data["use_case"], data["properties"])
\ No newline at end of file
# global imports (dont't worry, red is normal)
import network_constants as netconst
from database.MongoRepositoryBase import MongoRepositoryBase
from database.entities.transaction import Transaction
import pymongo
import json
from typing import List, Dict
class Repository(MongoRepositoryBase):
'''This is a repository for MongoDb.'''
def __init__(self):
super().__init__(netconst.BUSINESS_LOGIC_DB_HOSTNAME,
netconst.BUSINESS_LOGIC_DB_PORT,
'rest-gateway-db')
self._transaction_collection = 'transactions'
def delete_all_transactions(self):
collection = self._database[self._transaction_collection]
collection.delete_many({})
def add_transaction(self, transaction: Transaction):
reference = self.get_transaction_with_id(transaction.id())
if reference == None:
super().insert_entry(self._transaction_collection, transaction.to_serializable_dict())
def all_transactions_for_use_case(self, use_case: str) -> List[Transaction]:
result = super().get_entries(self._transaction_collection, projection={'_id': False}, selection={"use_case": use_case})
return [Transaction.from_serializable_dict(row) for row in list(result)]
def get_transaction_with_id(self, unique_id: str) -> Transaction:
result = list(super().get_entries(self._transaction_collection, projection={'_id': False}, selection={"id": unique_id}))
if len(result) == 1:
return Transaction.from_serializable_dict(result[0])
return None
\ No newline at end of file
from authentication.token_manager import TokenManager from authentication.token_manager import TokenManager
import network_constants import network_constants
from database.entities.transaction import Transaction
from database.repository import Repository
import json import json
import hashlib
import logging import logging
import requests import requests
from typing import Dict
LOGGER = logging.getLogger(__name__) LOGGER = logging.getLogger(__name__)
class MessageHandler: class MessageHandler:
...@@ -16,6 +21,8 @@ class MessageHandler: ...@@ -16,6 +21,8 @@ class MessageHandler:
_mongo_repo = None _mongo_repo = None
_message_sender = None _message_sender = None
_repository = Repository()
def __init__(self, mongo_repo, message_sender): def __init__(self, mongo_repo, message_sender):
self._mongo_repo = mongo_repo self._mongo_repo = mongo_repo
self._message_sender = message_sender self._message_sender = message_sender
...@@ -46,7 +53,36 @@ class MessageHandler: ...@@ -46,7 +53,36 @@ class MessageHandler:
LOGGER.info(result) LOGGER.info(result)
return result return result
def _resolve_path(self, data: Dict, path:str) -> Dict:
'''
resolves a path without concatenation in a json dictionary
@params
data - Required: Dictionary that is the decoded json string
path - Required: path of multiple keys seperated by "//" and list indizes "[5]"
'''
path_pieces = path.split("//")
value = data
# resolve all pieces of the path in order
for i in range(0,len(path_pieces)):
piece = path_pieces[i]
# is the current path piece in the form attribute[index]?
if piece[-1] == "]":
start = piece.index("[")
# stem ... attribute name
# index ... list index
stem = piece[:start]
index = int(piece[start+1:-1])
value = value[stem][index]
else:
value = value[piece]
return value
def handle_blockchain_transaction(self, transaction): def handle_blockchain_transaction(self, transaction):
jwt_token = TokenManager.getInstance().getToken() jwt_token = TokenManager.getInstance().getToken()
...@@ -75,41 +111,27 @@ class MessageHandler: ...@@ -75,41 +111,27 @@ class MessageHandler:
# iterate over schema mappings and resolve paths # iterate over schema mappings and resolve paths
for mapping in mappings.keys(): for mapping in mappings.keys():
path_pieces = mappings[mapping].split("//") full_path = mappings[mapping]
value = transaction_data["content"]
# resolve all pieces of the path in order concat_paths = full_path.split("+")
for i in range(0,len(path_pieces)): values = []
piece = path_pieces[i]
# is the current path piece in the form attribute[index]? for path in concat_paths:
if piece[-1] == "]": values.append(
start = piece.index("[") self._resolve_path(transaction_data["content"], path)
)
# stem ... attribute name if len(values) > 1:
# index ... list index final_value = "".join(values)
stem = piece[:start] else:
index = int(piece[start+1:-1]) final_value = values[0]
value = value[stem][index] flattened[mapping] = final_value
else:
value = value[piece]
flattened[mapping] = value
print("Flattened: "+str(flattened)) flattened["UniqueID"] = hashlib.sha256(flattened["UniqueID"].encode("utf-8")).hexdigest()
# TODO forward [flattened] to next MS
# TODO store the data
# TODO think if inserting makes sense (or just caching while processing if ms goes down)
# self._mongo_repo.insert_trace(transaction)
# TODO convert raw input to prepared data based on key 'ApplicationType' transaction = Transaction(use_case, flattened)
MessageHandler._repository.add_transaction(transaction)
# inform semantic linking microservice # inform semantic linking microservice
msg = {'type': 'new-traces-available'} self._message_sender.send_message('datahub', 'semantic-linking', json.dumps(transaction.to_serializable_dict()))
self._message_sender.send_message('datahub', 'semantic-linking', json.dumps(msg)) \ No newline at end of file
\ No newline at end of file
#global imports
from database.entities.transaction import Transaction
from database.repository import Repository
import json
from flask import Response, request
_repository = Repository()
def all_for_use_case(use_case: str):
transactions = _repository.all_transactions_for_use_case(use_case)
return [t.to_serializable_dict() for t in transactions]
def delete_all_transactions():
_repository.delete_all_transactions()
return Response(status=200)
\ 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