Skip to content

Commit

Permalink
Fixed issue 652 (#654)
Browse files Browse the repository at this point in the history
* Fixed issue 652

* Update RELEASE_NOTES.md

* Fixed issue 652

* Fixed issue 652

* updated fix for#652
  • Loading branch information
Ravisaketi authored Aug 23, 2022
1 parent 2ceb3c9 commit 6ecedfe
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 13 deletions.
1 change: 1 addition & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
- Removed comments on line no.462 and 467 in sql_translator.py (#659)
- Added logs in src/wq/ql/notify.py (#656)
- Added logs in src/wq/core/task.py (#662)
- Replaced entity with getter (#652)

### Bug fixes

Expand Down
24 changes: 11 additions & 13 deletions src/translators/sql_translator.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,6 @@ def current_timex() -> str:
return datetime.utcnow().isoformat(timespec='milliseconds')


# TODO: use below getters everywhere rather than entity id and type strings!

def entity_id(entity: dict) -> Optional[str]:
"""
Safely get the NGSI ID of the given entity.
Expand Down Expand Up @@ -216,7 +214,7 @@ def insert(self, entities, fiware_service=None, fiware_servicepath='/'):
if len(service_paths) == 1:
entities_by_type = {}
for e in entities:
entities_by_type.setdefault(e['type'], []).append(e)
entities_by_type.setdefault(entity_type(e), []).append(e)

res = None
for et in entities_by_type.keys():
Expand All @@ -233,7 +231,7 @@ def insert(self, entities, fiware_service=None, fiware_servicepath='/'):
for path in entities_by_service_path.keys():
entities_by_type = {}
for e in entities_by_service_path[path]:
entities_by_type.setdefault(e['type'], []).append(e)
entities_by_type.setdefault(entity_type(e), []).append(e)

res = None
for et in entities_by_type.keys():
Expand All @@ -250,17 +248,17 @@ def insert(self, entities, fiware_service=None, fiware_servicepath='/'):
return res

def _insert_entities_of_type(self,
entity_type,
entityType,
entities,
fiware_service=None,
fiware_servicepath='/'):
# All entities must be of the same type and have a time index
# Also, an entity can't have an attribute with the same name
# as that specified by ORIGINAL_ENTITY_COL_NAME.
for e in entities:
if e[NGSI_TYPE] != entity_type:
if entity_type(e) != entityType:
msg = "Entity {} is not of type {}."
raise ValueError(msg.format(e[NGSI_ID], entity_type))
raise ValueError(msg.format(entity_id(e), entity_type))

if self.TIME_INDEX_NAME not in e:
msg = "Translating entity without TIME_INDEX. " \
Expand All @@ -270,7 +268,7 @@ def _insert_entities_of_type(self,

if ORIGINAL_ENTITY_COL in e:
raise ValueError(
f"Entity {e[NGSI_ID]} has a reserved attribute name: " +
f"Entity {entity_id(e)} has a reserved attribute name: " +
"'{ORIGINAL_ENTITY_COL_NAME}'")

# Define column types
Expand All @@ -293,7 +291,7 @@ def _insert_entities_of_type(self,
}

for e in entities:
entity_id = e.get('id')
entityId = entity_id(e)
for attr in iter_entity_attrs(e):
if attr == self.TIME_INDEX_NAME:
continue
Expand Down Expand Up @@ -330,10 +328,10 @@ def _insert_entities_of_type(self,
col = self._ea2cn(attr)
original_attrs[col] = (attr, attr_t)

table[col] = self._compute_type(entity_id, attr_t, e[attr])
table[col] = self._compute_type(entityId, attr_t, e[attr])

# Create/Update metadata table for this type
table_name = self._et2tn(entity_type, fiware_service)
table_name = self._et2tn(entityType, fiware_service)
modified = self._update_metadata_table(table_name, original_attrs)
# Sort out data table.
if modified and modified == original_attrs.keys():
Expand Down Expand Up @@ -468,9 +466,9 @@ def _preprocess_values(self, e, original_attrs, col_names,
values = []
for cn in col_names:
if cn == ENTITY_TYPE_COL:
values.append(e['type'])
values.append(entity_type(e))
elif cn == ENTITY_ID_COL:
values.append(e['id'])
values.append(entity_id(e))
elif cn == self.TIME_INDEX_NAME:
values.append(e[self.TIME_INDEX_NAME])
elif cn == FIWARE_SERVICEPATH:
Expand Down

0 comments on commit 6ecedfe

Please sign in to comment.