Commit 5aca5b34 authored by Manuel's avatar Manuel

traceRetrieval: added storage of flattened layers

parent 741f1928
......@@ -13,6 +13,32 @@ basePath: "/api"
# Paths supported by the server application
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:
post:
operationId: "routes.debug.echo"
......@@ -27,7 +53,7 @@ paths:
schema:
type: object
responses:
200:
'200':
description: "Successful echo of request data"
/trace:
......@@ -45,9 +71,9 @@ paths:
schema:
$ref: "#/definitions/BlockchainTrace"
responses:
201:
'201':
description: "Successful operation"
400:
'400':
description: "Invalid input"
get:
operationId: "routes.blockchain_trace.get"
......@@ -57,7 +83,7 @@ paths:
description: "Returns all blockchain traces in the database"
parameters: []
responses:
200:
'200':
description: "Successful operation"
schema:
$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
import network_constants
from database.entities.transaction import Transaction
from database.repository import Repository
import json
import hashlib
import logging
import requests
from typing import Dict
LOGGER = logging.getLogger(__name__)
class MessageHandler:
......@@ -16,6 +21,8 @@ class MessageHandler:
_mongo_repo = None
_message_sender = None
_repository = Repository()
def __init__(self, mongo_repo, message_sender):
self._mongo_repo = mongo_repo
self._message_sender = message_sender
......@@ -47,6 +54,35 @@ class MessageHandler:
LOGGER.info(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):
jwt_token = TokenManager.getInstance().getToken()
......@@ -75,41 +111,27 @@ class MessageHandler:
# iterate over schema mappings and resolve paths
for mapping in mappings.keys():
path_pieces = mappings[mapping].split("//")
value = transaction_data["content"]
# resolve all pieces of the path in order
for i in range(0,len(path_pieces)):
piece = path_pieces[i]
full_path = mappings[mapping]
# is the current path piece in the form attribute[index]?
if piece[-1] == "]":
start = piece.index("[")
concat_paths = full_path.split("+")
values = []
# stem ... attribute name
# index ... list index
stem = piece[:start]
index = int(piece[start+1:-1])
for path in concat_paths:
values.append(
self._resolve_path(transaction_data["content"], path)
)
value = value[stem][index]
if len(values) > 1:
final_value = "".join(values)
else:
value = value[piece]
flattened[mapping] = value
print("Flattened: "+str(flattened))
# 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)
final_value = values[0]
flattened[mapping] = final_value
flattened["UniqueID"] = hashlib.sha256(flattened["UniqueID"].encode("utf-8")).hexdigest()
# 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
msg = {'type': 'new-traces-available'}
self._message_sender.send_message('datahub', 'semantic-linking', json.dumps(msg))
\ No newline at end of file
self._message_sender.send_message('datahub', 'semantic-linking', json.dumps(transaction.to_serializable_dict()))
\ 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