# Copyright (C) 2005-2025 Splunk Inc. All Rights Reserved. import time import math import SA_ITOA_app_common.splunklib.results as results from itsi.itsi_utils import ITOAInterfaceUtils from ITOA.setup_logging import logger start_timestamp = None def render_ui_message_required(session_key, app_name): """ We use ui_logger to surface migration messages on the UI However, some migration operations take a long time without logging out any message, which causes UI 'hangs' In this case, we detect if no logs being generated in the last 30 Returning True means we should surface some messages to the UI to let the user know the migration is till going on. @type session_key: basestring @param session_key: session key @type app_name: basestring @param app_name: app name, like 'SA-ITOA' @rtype: bool @return: whether a migration UI message should be rendered """ global start_timestamp if start_timestamp is None: # The collection itsi_migration_status is designed to have only one entry , hence the method get_migration_status_from_kv will return only one entry entry = ITOAInterfaceUtils.get_migration_status_from_kv(session_key) start_timestamp = entry.get('start_timestamp') if entry.get('is_running', False) else None migration_elapsed_time = '-1d' if start_timestamp is None else '-{}h'.format( int(math.ceil((time.time() - start_timestamp) / 3600))) logger.info("Migration start time difference in days/hours from current time: %s", migration_elapsed_time) service = ITOAInterfaceUtils.service_connection(session_key, app_name) query = 'search index=_internal sourcetype=itsi_internal_log "[itsi.migration]" "UI" OR "Local failure skipped" ' \ '| eval time=_time | head 1| table time' kwargs = {'earliest_time': migration_elapsed_time, 'latest_time': 'now'} job = service.jobs.create(query, **kwargs) while not job.is_done(): time.sleep(.2) rr = results.ResultsReader(job.results()) for result in rr: last_updated_time = result.get('time', None) if last_updated_time: if time.time() - float(last_updated_time) > 30: return True return False