Historical Cache Functionality
This article describes how to use the historical cache functionality built into Sepasoft analysis to speed up part of gathering historical data. Note that this only works with sequential historical data, such as a list of downtime events, or a list of runs, or analysis comparing consecutive shifts. If the analysis is grouped by some sequential data point (Operation UUID, State Event Sequence, Line State Event Begin, etc), this may help. It does respect existing setting values, if any are used.
Python Function
Python |
def run_historical_analysis(lookback_hours, cache_key, analysis_name=None, analysis_setting=None): # first we check if the user has passed either an analysis setting object or an analysis setting name if not analysis_setting: analysis_setting = system.mes.analysis.getMESAnalysisSettings(analysis_name) # end date is always now current_end_date = system.date.now() # getting top of current hour current_start_date = system.date.setTime(current_end_date, system.date.getHour24(current_end_date), 0, 0) current_analysis_dataset = system.mes.analysis.executeAnalysis(current_start_date, current_end_date, analysis_setting).getDataset() # historical start date is top of current hour minus lookback_hours historical_start_date = system.date.addHours(current_start_date, -lookback_hours) # checking for existing custom setting values on the analysis setting existing_setting_values = analysis_setting.getSettingValues() # appending the cache settings to the existing setting values analysis_setting.setSettingValues("%s,Cache Expiration=43200,Historical Cache Key=%s" % (existing_setting_values, cache_key)) historical_analysis_dataset = system.mes.analysis.executeAnalysis(historical_start_date, current_start_date, analysis_setting).getDataset() # looping over the current_analysis_dataset and appending it to the historical_analysis_dataset data_to_add = [] for row_num in range(current_analysis_dataset.getRowCount()): row_data = [] for col_num in range(current_analysis_dataset.getColumnCount()): row_data.append(current_analysis_dataset.getValueAt(row_num, col_num)) data_to_add.append(row_data) # returning the combined dataset return system.dataset.addRows(historical_analysis_dataset, data_to_add) |
Usage:
Do the following to execute this function with an existing analysis setting object or one that you have created from scratch:
Usage with a pre-loaded or existing analysis setting
Python |
analysis = system.mes.analysis.getMESAnalysisSettings("Downtime Events") results = run_historical_analysis(400, "blah", analysis_setting=analysis) |
Otherwise, you can simply name the analysis you wish to use, like so:
Usage with just the name of an analysis setting
Python |
results = run_historical_analysis(400, "blah", analysis_name="Downtime Events") |