Commit faa91428 authored by Manuel's avatar Manuel

[BusinessLogic] Additional Endpoint to retrieve all layers for a table

parent f42805b4
......@@ -20,6 +20,7 @@ class Layer:
"properties": self.properties,
"use_case": self.use_case,
"total_properties": self.total_properties,
"table": self.table
}
@staticmethod
......@@ -29,11 +30,13 @@ class Layer:
layer.properties = layer_info["cluster_properties"]
layer.total_properties = layer_info["properties"]
layer.use_case = layer_info["use_case"]
layer.table = layer_info["table"]
return layer
def from_serializable_dict(self, layer_info: Dict, from_db=False):
self.layer_name = layer_info['layer_name']
self.properties = layer_info['properties']
self.table = layer_info["table"]
self.use_case = layer_info["use_case"] if "use_case" in layer_info.keys() else None
self.total_properties = layer_info["total_properties"] if "total_properties"in layer_info.keys() else None
......
......@@ -33,8 +33,8 @@ class Repository(MongoRepositoryBase):
entries = super().get_entries(self._layer_collection)
return [Layer(e) for e in entries]
def get_layers_for_use_case(self, use_case: str) -> List[Layer]:
result = super().get_entries(self._layer_collection, projection={'_id': False}, selection={"use_case": use_case})
def get_layers_for_use_case_and_table(self, use_case: str, table: str) -> List[Layer]:
result = super().get_entries(self._layer_collection, projection={'_id': False}, selection={"use_case": use_case, "table": table})
return [Layer(e) for e in result]
def get_layer(self, layer_name) -> Layer:
......@@ -57,9 +57,9 @@ class Repository(MongoRepositoryBase):
return None
return Layer(entries[0]) if len(entries) == 1 else None
def delete_layer(self, layer_name:str):
def delete_layer(self, layer:Layer):
collection = self._database[self._layer_collection]
collection.delete_one({"layer_name": layer_name})
collection.delete_one({"layer_name": layer.layer_name, "use_case": layer.use_case, "table": layer.table})
def delete_layers_for_use_case(self, use_case: str):
collection = self._database[self._layer_collection]
......
......@@ -36,12 +36,12 @@ class MessageHandler:
else:
LOGGER.info("Message Type could not be processed")
def _fetch_layer_information(self, use_case: str) -> List[Layer]:
def _fetch_layer_information(self, use_case: str, table: str) -> List[Layer]:
# fetch token for authentication
jwt_token = TokenManager.getInstance().getToken()
# query schema information
url = f'https://{netconst.BUSINESS_LOGIC_HOSTNAME}:{netconst.BUSINESS_LOGIC_REST_PORT}/api/use-cases/{use_case}/layers'
url = f'https://{netconst.BUSINESS_LOGIC_HOSTNAME}:{netconst.BUSINESS_LOGIC_REST_PORT}/api/use-cases/{use_case}/tables/{table}/layers'
response = requests.get(
url,
......@@ -51,31 +51,32 @@ class MessageHandler:
)
if response.status_code != 200:
raise ValueError("no schema information available")
raise ValueError(f"no schema information available {response.status_code}")
layers = [Layer.from_business_logic_dict(row) for row in json.loads(response.text)]
# update local DB, insert each layer that does not already exists
for layer in layers:
print(f"Add layer to DB: {layer.to_serializable_dict(for_db=True)}")
self._repository.delete_layer(layer.layer_name)
self._repository.delete_layer(layer)
self._repository.add_layer(layer)
return layers
def handle_new_trace(self, content: Dict):
if "use_case" not in content.keys() or "id" not in content.keys() or "properties" not in content.keys():
LOGGER.error(f"Missing fields in trace, required fields: (use_case, id, properties), given fields: ({content.keys()})")
if "use_case" not in content.keys() or "id" not in content.keys() or "properties" not in content.keys() or "table" not in content.keys():
LOGGER.error(f"Missing fields in trace, required fields: (use_case, id, properties, table), given fields: ({content.keys()})")
return
use_case = content["use_case"]
table = content["table"]
# fetch layer information
layers = self._repository.get_layers_for_use_case(use_case)
layers = self._repository.get_layers_for_use_case_and_table(use_case, table)
# if no local layers are found, fetch information from server
if len(layers) == 0:
layers = self._fetch_layer_information(use_case)
layers = self._fetch_layer_information(use_case, table)
nodes = []
......@@ -86,7 +87,8 @@ class MessageHandler:
node[prop] = content["properties"][prop]
node["layer_name"] = layer.layer_name
node["use_case"] = use_case
node["table"] = layer.table
node["use_case"] = layer.use_case
nodes.append(node)
self._repository.add_layer_nodes(nodes)
......
......@@ -25,15 +25,16 @@ class Test_Pipeline(unittest.TestCase):
return {
"type": "new-trace",
"content": {
"use_case": "debug",
"id": "b38c916a73747abba1d01dbe11ee15714c90d42b23b8328cafc1667e60045e7b",
"properties": {
"name": "Margherita",
"dough": False,
"sauce": "tomato",
"combined_name": "Margheritawheat",
"UniqueID": "b38c916a73747abba1d01dbe11ee15714c90d42b23b8328cafc1667e60045e7b",
"dough_type": "wheat"
'use_case': 'debug',
'table': 'pizza',
'id': '9e1e0983ada4e79de324a80b5f35f681c3e5016b08e1d6612c17892ade17e2c0',
'properties': {
'UniqueID': '9e1e0983ada4e79de324a80b5f35f681c3e5016b08e1d6612c17892ade17e2c0',
'cheese': False,
'dough': 'wheat',
'name': 'Margherita',
'sauce': 'tomato',
'sugar': 0.0
}
}
}
......
......@@ -136,7 +136,7 @@ paths:
description: "Successful Request"
'400':
description: "Table with the name already exists or missing fields in the request."
/use-cases/{use_case}/table/{name}/mapping:
/use-cases/{use_case}/tables/{name}/mapping:
put:
security:
- JwtRegular: []
......@@ -215,7 +215,32 @@ paths:
description: "Successful Request"
'400':
description: "Bad structure in request body or Layer already exists"
/use-cases/{use_case}/table/{table}/layers/{name}/cluster-mapping:
/use-cases/{use_case}/tables/{table}/layers:
get:
security:
- JwtRegular: []
operationId: "routes.layer.get_layers_for_use_case_and_table"
tags:
- "Layers"
summary: "Retrieves all layers belonging to the given Use-Case"
description: "Retrieves all layers belonging to the given Use-Case"
parameters:
- name: "use_case"
in: "path"
description: "Name of the Use-Case the Table belongs to"
required: true
type: "string"
- name: "table"
in: "path"
description: "Name of the Table the Layer belongs to"
required: true
type: "string"
responses:
'200':
description: "Successful Request"
'404':
description: "Use-Case or Table do not exist"
/use-cases/{use_case}/tables/{table}/layers/{name}/cluster-mapping:
put:
security:
- JwtRegular: []
......@@ -318,7 +343,7 @@ paths:
description: "Successful Request"
'404':
description: "Use-Case does not exist"
/use-cases/{use_case}/table/{table}/layers/{name}:
/use-cases/{use_case}/tables/{table}/layers/{name}:
get:
security:
- JwtRegular: []
......
......@@ -24,11 +24,16 @@ class Repository(MongoRepositoryBase):
return [LayerAdapter.from_serializable_dict(row) for row in list(result)]
def all_for_use_case(self, use_case: str) -> List[Dict]:
def all_for_use_case(self, use_case: str) -> List[LayerAdapter]:
result = super().get_entries(self._adapter_collection, projection={'_id': False}, selection={"use_case": use_case})
return [LayerAdapter.from_serializable_dict(row) for row in list(result)]
def all_for_use_case_and_table(self, use_case: str, table: str) -> List[LayerAdapter]:
result = super().get_entries(self._adapter_collection, projection={'_id': False}, selection={"use_case": use_case, "table": table})
return [LayerAdapter.from_serializable_dict(row) for row in list(result)]
def one(self, name : str, use_case: str, table: str) -> LayerAdapter:
result = list(super().get_entries(self._adapter_collection, selection={"name": name, "use_case": use_case, "table": table}))
......
......@@ -23,7 +23,6 @@ def delete_all_layers():
layer_repository.delete_all()
return Response(status=200)
def add_complete():
'''
add a layer already containing attribute mappings to the BD.
......@@ -224,3 +223,6 @@ def delete_one(use_case: str, table: str, name: str):
layer_repository.delete(layer)
return Response(status=200)
def get_layers_for_use_case_and_table(use_case: str, table: str):
return [layer.to_serializable_dict() for layer in layer_repository.all_for_use_case_and_table(use_case, table)]
\ 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