#!/usr/bin/python # policies.py # # Author: M. Shankar, Jan 31, 2012 # Modification History # Jan 31, 2012, Shankar: Initial version of policies.py with comments. # May 14, 2012, Li, Shankar: Added support for archiving extra fields into policy file. # # This is the policies.py used to enforce policies for archiving PVs # At a very high level, when users request PVs to be archived, the mgmt web app samples the PV to determine event rate and other parameters. # In addition, various fields of the PV like .NAME, .ADEL, .MDEL, .RTYP etc are also obtained # These are passed to this python script as a dictionary argument to a method called determinePolicy # The variable name in the python environment for this information is 'pvInfo' (so use other variable names etc.). # The method is expected to use this information to make decisions on various archiving parameters. # The result is expected to be another dictionary that is placed into the variable called "pvPolicy". # Optionally, fields in addition to the VAL field that are to be archived with the PV are passed in as a property of pvPolicy called 'archiveFields' # If the user overrides the policy, this is communicated in the pvinfo as a property called 'policyName' # # In addition, this script must communicate the list of available policies to the JVM as another method called getPolicyList which takes no arguments. # The results of this method is placed into a variable called called 'pvPolicies'. # The dictionary is a name to description mapping - the description is used in the UI; the name is what is communicated to determinePolicy as a user override # # In addition, this script must communicate the list of fields that are to be archived as part of the stream in a method called getFieldsArchivedAsPartOfStream. # The results of this method is placed into a list variable called called 'pvStandardFields'. import sys import os # Generate a list of policy names. This is used to feed the dropdown in the UI. def getPolicyList(): pvPoliciesDict = {} pvPoliciesDict['Default'] = 'The default policy' pvPoliciesDict['VAC'] = 'MR Vacuum' pvPoliciesDict['test - BPMS'] = 'test - BPMS that generate more than 1GB a year' pvPoliciesDict['test - 2HzPVs'] = 'test - PVs with an event rate more than 2Hz' pvPoliciesDict['test - 3DaysMTSOnly'] = 'test -Store data for 3 days upto the MTS only.' return pvPoliciesDict # Define a list of fields that will be archived as part of every PV. # The data for these fields will included in the stream for the PV. # We also make an assumption that the data type for these fields is the same as that of the .VAL field def getFieldsArchivedAsPartOfStream(): return ['HIHI','HIGH','LOW','LOLO','LOPR','HOPR','DRVH','DRVL']; # We use the environment variables ARCHAPPL_SHORT_TERM_FOLDER and ARCHAPPL_MEDIUM_TERM_FOLDER to determine the location of the STS and the MTS in the appliance # PARTITION_5MIN / PARTITION_15MIN / PARTITION_30MIN / PARTITION_HOUR / PARTITION_DAY / PARTITION_MONTH / PARTITION_YEAR #shorttermstore_plugin_url = 'pb://localhost?name=STS&rootFolder=${ARCHAPPL_SHORT_TERM_FOLDER}&partitionGranularity=PARTITION_5MIN' #mediumtermstore_plugin_url = 'pb://localhost?name=MTS&rootFolder=${ARCHAPPL_MEDIUM_TERM_FOLDER}&partitionGranularity=PARTITION_15MIN' shorttermstore_plugin_url = 'pb://localhost?name=STS&rootFolder=${ARCHAPPL_SHORT_TERM_FOLDER}&partitionGranularity=PARTITION_HOUR' mediumtermstore_plugin_url = 'pb://localhost?name=MTS&rootFolder=${ARCHAPPL_MEDIUM_TERM_FOLDER}&partitionGranularity=PARTITION_DAY&hold=5&gather=1' longtermstore_plugin_url = 'pb://localhost?name=LTS&rootFolder=${ARCHAPPL_LONG_TERM_FOLDER}&partitionGranularity=PARTITION_MONTH' # #shorttermstore_plugin_url = 'pb://localhost?name=STS&rootFolder=${ARCHAPPL_SHORT_TERM_FOLDER}&partitionGranularity=PARTITION_5MIN' #shorttermstore_plugin_url = 'pb://localhost?name=STS&rootFolder=${ARCHAPPL_SHORT_TERM_FOLDER}&partitionGranularity=PARTITION_HOUR&consolidateOnShutdown=true' #longtermstore_plugin_url = 'pb://localhost?name=LTS&rootFolder=${ARCHAPPL_LONG_TERM_FOLDER}&partitionGranularity=PARTITION_YEAR' #longtermstore_plugin_url = 'blackhole://localhost' def determinePolicy(pvInfoDict): pvPolicyDict = {} userPolicyOverride = '' if 'policyName' in pvInfoDict: userPolicyOverride = pvInfoDict['policyName'] # if userPolicyOverride == 'VAC' or (userPolicyOverride == '' and pvInfoDict['pvName'].startswith('MRVAC') and pvInfoDict['pvName'].startswith('PRESS')): # We reduce sampling rate for 'MRVAC:...:PRESS' records to 2sec, while these records are scanned at 1Hz. pvPolicyDict['samplingPeriod'] = 2.0 pvPolicyDict['samplingMethod'] = 'SCAN' pvPolicyDict['dataStores'] = [ shorttermstore_plugin_url, mediumtermstore_plugin_url, longtermstore_plugin_url ] pvPolicyDict['policyName'] = 'VAC'; # elif userPolicyOverride == 'test - 2HzPVs' or (userPolicyOverride == '' and pvInfoDict['eventRate'] > 2.0): pvPolicyDict['samplingPeriod'] = 1.0 pvPolicyDict['samplingMethod'] = 'MONITOR' pvPolicyDict['dataStores'] = [ shorttermstore_plugin_url, mediumtermstore_plugin_url, longtermstore_plugin_url ] pvPolicyDict['policyName'] = 'test - 2HzPVs'; # #elif userPolicyOverride == 'Default' or (userPolicyOverride == '' and pvInfoDict['pvName'] == 'mshankar:arch:sine'): elif userPolicyOverride == 'Default': pvPolicyDict['samplingPeriod'] = 1.0 pvPolicyDict['samplingMethod'] = 'MONITOR' pvPolicyDict['dataStores'] = [ shorttermstore_plugin_url, mediumtermstore_plugin_url, longtermstore_plugin_url ] pvPolicyDict['policyName'] = 'Default'; # elif userPolicyOverride == 'test - BPMS' or (userPolicyOverride == '' and pvInfoDict['pvName'].startswith('BPMS') and pvInfoDict['storageRate'] > 35): # We limit BPM PVs to 1GB/year (34 bytes/sec) # We reduce the sampling rate (and hence increase the sampling period) to cater to this. pvPolicyDict['samplingPeriod'] = pvInfoDict['storageRate']/(35*pvInfoDict['eventRate']) pvPolicyDict['samplingMethod'] = 'MONITOR' pvPolicyDict['dataStores'] = [ shorttermstore_plugin_url, mediumtermstore_plugin_url, longtermstore_plugin_url ] pvPolicyDict['policyName'] = 'test - BPMS'; # elif userPolicyOverride == 'test - 3DaysMTSOnly': pvPolicyDict['samplingPeriod'] = 1.0 pvPolicyDict['samplingMethod'] = 'MONITOR' pvPolicyDict['dataStores'] = [ shorttermstore_plugin_url, # We want to store 3 days worth of data in the MTS. 'pb://localhost?name=MTS&rootFolder=${ARCHAPPL_MEDIUM_TERM_FOLDER}&partitionGranularity=PARTITION_DAY&hold=4&gather=1', 'blackhole://localhost?name=LTS' ] pvPolicyDict['policyName'] = 'test - 2HzPVs'; # else: pvPolicyDict['samplingPeriod'] = 1.0 pvPolicyDict['samplingMethod'] = 'MONITOR' pvPolicyDict['dataStores'] = [ shorttermstore_plugin_url, mediumtermstore_plugin_url, longtermstore_plugin_url ] pvPolicyDict['policyName'] = 'Default'; archiveFields=[] if "RTYP" not in pvInfoDict: pvPolicyDict["archiveFields"]=archiveFields else: pvRTYP=pvInfoDict["RTYP"] if pvRTYP=="ai": #archiveFields=['HIHI','HIGH','LOW','LOLO','LOPR','HOPR'] archiveFields=[] elif pvRTYP=="ao": #archiveFields=['HIHI','HIGH','LOW','LOLO','LOPR','HOPR','DRVH','DRVL'] archiveFields=[] elif pvRTYP=="calc": #archiveFields=['HIHI','HIGH','LOW','LOLO','LOPR','HOPR'] archiveFields=[] elif pvRTYP=="calcout": #archiveFields=['HIHI','HIGH','LOW','LOLO','LOPR','HOPR'] archiveFields=[] elif pvRTYP=="longin": #archiveFields=['HIHI','HIGH','LOW','LOLO','LOPR','HOPR'] archiveFields=[] elif pvRTYP=="longout": #archiveFields=['HIHI','HIGH','LOW','LOLO','LOPR','HOPR','DRVH','DRVL'] archiveFields=[] elif pvRTYP=="dfanout": #archiveFields=['HIHI','HIGH','LOW','LOLO','LOPR','HOPR'] archiveFields=[] elif pvRTYP=="sub": #archiveFields=['HIHI','HIGH','LOW','LOLO','LOPR','HOPR'] archiveFields=[] pvPolicyDict["archiveFields"]=archiveFields return pvPolicyDict