Skip to content

Commit 1b9095b

Browse files
authored
[patch] Reduce upserts
[patch] Reduce upserts
2 parents 8f5aa60 + f6d9117 commit 1b9095b

File tree

3 files changed

+363
-117
lines changed

3 files changed

+363
-117
lines changed

iotfunctions/db.py

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,6 @@ class Database(object):
8989
OS_API_CERTIFICATE_FILE = 'API_CERTIFICATE_FILE'
9090
OS_DB_CERTIFICATE_FILE = 'DB_CERTIFICATE_FILE'
9191

92-
KITT_MAX_CONNECTION_RETRY_COUNT = 5
93-
9492
def __init__(self, credentials=None, start_session=False, echo=False, tenant_id=None, entity_metadata=None,
9593
entity_type_id=None, model_store=None):
9694

@@ -571,6 +569,54 @@ def __init__(self, credentials=None, start_session=False, echo=False, tenant_id=
571569
if entity_type_id is not None:
572570
self.init_model_store(self.schema)
573571

572+
# Determine the precision of fractional seconds for timestamps in database
573+
self.precision_fractional_seconds = self.get_precision_fractional_seconds()
574+
575+
def get_precision_fractional_seconds(self):
576+
577+
if self.db_type == 'db2':
578+
sql_statement = f'SELECT CAST(TIMESTAMP(\'1970-01-01-00.00.00.123456789\') AS CHAR(31)) ' \
579+
f'FROM "IOTANALYTICS"."KPI_FUNCTION"'
580+
logger.debug(f"Sql statement to determine the precision of fractional seconds for timestamps in DB2: "
581+
f"{sql_statement}")
582+
583+
# Execute sql statement and get the single result of select statement
584+
timestamp_str = None
585+
try:
586+
result_handle = ibm_db.exec_immediate(self.native_connection, sql_statement)
587+
588+
try:
589+
row = ibm_db.fetch_tuple(result_handle)
590+
if row is not False:
591+
timestamp_str = str(row[0]).strip()
592+
except Exception as ex:
593+
raise RuntimeError(f"The retrieval of the result of sql statement to determine the precision of "
594+
f"fractional seconds in DB2 failed.") from ex
595+
finally:
596+
ibm_db.free_result(result_handle)
597+
598+
except Exception as ex:
599+
raise RuntimeError(f"The sql statement to determine the precision of fractional seconds in DB2 "
600+
f"failed.") from ex
601+
602+
# Timestamp is supposed to look like this: 1970-01-01-00.00.00.123456
603+
if timestamp_str is not None and len(timestamp_str) > 18:
604+
if len(timestamp_str) > 20:
605+
fractional_str = timestamp_str[20:]
606+
precision = len(fractional_str)
607+
else:
608+
precision = 0
609+
else:
610+
precision = 6
611+
logger.warning(f"Unexpected format of timestamp string '{timestamp_str}'. Precision of fractional "
612+
f"seconds for timestamps set to default of 6")
613+
else:
614+
precision = 6
615+
616+
logger.debug(f"Precision of fractional seconds for timestamps in DB2: {precision}")
617+
618+
return precision
619+
574620
def set_entity_type(self, entity_type, entity_type_id):
575621

576622
self.entity_type_id = entity_type_id

iotfunctions/metadata.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1942,14 +1942,14 @@ def create_sample_data(self, drop_existing, generate_days, generate_entities=Non
19421942
g.execute(df=None, start_ts=start, entities=generate_entities)
19431943

19441944
if populate_dm_wiot_entity_list:
1945-
# KITT integration: write dimension data after populating the entity list
1945+
# Write dimension data to the entity list
19461946
if self._generated_dimension_payload:
19471947
logger.debug(self._generated_dimension_payload)
19481948
response = self.db.http_request(object_type='dimensions', object_name=self._entity_type_uuid,
19491949
request='PUT',
19501950
payload=self._generated_dimension_payload, raise_error=True)
19511951

1952-
logger.debug(f'Set dimensions in KITT and {self._dimension_table_name} table')
1952+
logger.debug(f'Set dimensions in {self._dimension_table_name} table')
19531953
logger.debug(response)
19541954

19551955
def populate_entity_list_table(self):

0 commit comments

Comments
 (0)