Skip to content

Commit 8cce2a4

Browse files
authored
[patch] Sample Device Generation
[patch] Sample Device Generation
2 parents 1b9095b + b903139 commit 8cce2a4

File tree

2 files changed

+10
-22
lines changed

2 files changed

+10
-22
lines changed

iotfunctions/db.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1171,6 +1171,8 @@ def http_request(self, object_type, object_name, request, payload=None, object_n
11711171
self.url['dimensions', 'PUT'] = '/'.join(
11721172
[core_url, 'v2', 'core', 'deviceTypes', object_name, 'devices', object_type])
11731173

1174+
self.url['devices', 'POST'] = '/'.join([core_url, 'v2', 'deviceTypes', object_name, object_type])
1175+
11741176
encoded_payload = json.dumps(payload).encode('utf-8')
11751177
headers = {'Content-Type': "application/json", 'X-api-key': self.credentials['as']['api_key'],
11761178
'X-api-token': self.credentials['as']['api_token'], 'Cache-Control': "no-cache",

iotfunctions/metadata.py

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1954,29 +1954,15 @@ def create_sample_data(self, drop_existing, generate_days, generate_entities=Non
19541954

19551955
def populate_entity_list_table(self):
19561956

1957-
try:
1958-
insert_tuples = tuple(
1959-
(self._entity_type_id, str(self._start_entity_id + x)) for x in range(self._auto_entity_count)
1960-
)
1961-
1962-
if self.db.db_type == 'db2':
1963-
1964-
sql_statement = f'INSERT INTO "IOTANALYTICS"."DEVICE_LIST" ' \
1965-
f'("ID", "ENTITY_TYPE_ID", "ENTITY_ID") ' \
1966-
f'VALUES (NEXT VALUE FOR "IOTANALYTICS"."DEVICE_LIST_SEQ", ?, ?)'
1957+
payload_new_devices = []
1958+
for i in range(self._auto_entity_count):
1959+
payload_new_devices.append({"name": self._start_entity_id + i})
19671960

1968-
stmt = ibm_db.prepare(self.db.native_connection, sql_statement)
1969-
1970-
try:
1971-
ibm_db.execute_many(stmt, insert_tuples)
1972-
finally:
1973-
ibm_db.free_result(stmt)
1974-
1975-
else:
1976-
raise Exception("Databases other than DB2 are not supported.")
1977-
1978-
except Exception as e:
1979-
logger.error('Error populating device_list table.', e)
1961+
try:
1962+
response = self.db.http_request(object_type='devices', object_name=self._entity_type_uuid, request='POST',
1963+
payload=payload_new_devices, raise_error=True)
1964+
except Exception as ex:
1965+
raise RuntimeError("Devices could not be created in configuration.") from ex
19801966

19811967
def register(self, publish_kpis=False, raise_error=False, sample_entity_type=False):
19821968
"""

0 commit comments

Comments
 (0)