Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
S
SMART
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
3
Issues
3
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
UNI-KLU
SMART
Commits
5aca5b34
Commit
5aca5b34
authored
Aug 27, 2020
by
Manuel
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
traceRetrieval: added storage of flattened layers
parent
741f1928
Changes
5
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
158 additions
and
35 deletions
+158
-35
swagger.yml
...b-in/trace-retrieval-microservice/app/configs/swagger.yml
+30
-4
transaction.py
...trieval-microservice/app/database/entities/transaction.py
+20
-0
repository.py
...n/trace-retrieval-microservice/app/database/repository.py
+39
-0
MessageHandler.py
...ce-retrieval-microservice/app/messaging/MessageHandler.py
+53
-31
transactions.py
...n/trace-retrieval-microservice/app/routes/transactions.py
+16
-0
No files found.
src/transaction-hub-in/trace-retrieval-microservice/app/configs/swagger.yml
View file @
5aca5b34
...
@@ -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"
...
...
src/transaction-hub-in/trace-retrieval-microservice/app/database/entities/transaction.py
0 → 100644
View file @
5aca5b34
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
src/transaction-hub-in/trace-retrieval-microservice/app/database/repository.py
0 → 100644
View file @
5aca5b34
# 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
src/transaction-hub-in/trace-retrieval-microservice/app/messaging/MessageHandler.py
View file @
5aca5b34
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
...
@@ -47,6 +54,35 @@ class MessageHandler:
...
@@ -47,6 +54,35 @@ 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
for
i
in
range
(
0
,
len
(
path_pieces
)):
piece
=
path_pieces
[
i
]
# is the current path piece in the form attribute[index]?
concat_paths
=
full_path
.
split
(
"+"
)
if
piece
[
-
1
]
==
"]"
:
values
=
[]
start
=
piece
.
index
(
"["
)
# stem ... attribute name
for
path
in
concat_paths
:
# index ... list index
values
.
append
(
s
tem
=
piece
[:
start
]
s
elf
.
_resolve_path
(
transaction_data
[
"content"
],
path
)
index
=
int
(
piece
[
start
+
1
:
-
1
]
)
)
value
=
value
[
stem
][
index
]
if
len
(
values
)
>
1
:
final_value
=
""
.
join
(
values
)
else
:
else
:
value
=
value
[
piece
]
final_value
=
values
[
0
]
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)
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
# 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
src/transaction-hub-in/trace-retrieval-microservice/app/routes/transactions.py
0 → 100644
View file @
5aca5b34
#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
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment