Create Top5DowntimeReasons RESTful Provider2

Fetching OEE Downtime Data

The RESTful Provider for fetching OEE Downtime reasons called Top5DowntimeReasons will be created in the Sepasoft Web Services module's RESTful Provider area. It will be called by a pre-built RESTful Consumer (in our case, just script in the Script Console) and return a dataset containing the top five reasons for the line provided (Packaging Line 1).


Create Top5DowntimeReasons RESTful Provider

This RESTful Provider has already been partially completed. Open and complete it as follows:

Copy the script below and insert after the #execute analysis function.

Snippet

Python
def do_GET(request):
"""
Responds to an incoming HTTP request with a response in dict format.

Puts a HTTP status code in integer format at the key 'code' in the response
dict (required).

Puts HTTP response headers in dict format at the key 'headers' in the
response dict (optional).

Puts a HTTP response content in dict format at the key 'content' in the
response dict (required).

Arguments:
request: A dict with the incoming HTTP request information. The HTTP
path parameter is contained within a string at request['path']. The HTTP
query parameters are contained within a dict at request['query']. The
HTTP request header parameters are contained within a dict at
request['headers']. The request data is contained within a dict at
input['remote']. The HTTP request body parameter is contained within a
dict at request['body'].
"""
req_path = request['path']
req_query = request['query']
req_headers = request['headers']
req_metadata = request['remote']
req_body = request['body']


res_headers = {}

#get values from request
eqPath = req_query['eqPath']

# Wed Mar 17 20:01:01 UTC 2021
startDate = system.date.parse(req_query['startDate'], 'E MMM d H:m:s z yyyy')
endDate = system.date.parse(req_query['endDate'], 'E MMM d H:m:s z yyyy')


#Instructor Provided Code
#check if analyis settings exist, if not, create
try:
#attemp to load the analysis setting
system.mes.loadMESObject('Top 5 Downtime Reasons', 'AnalysisSettings')
analysis_setting = 'Top 5 Downtime Reasons'
except:
#If load fails, create analysis dynamically
analysis_setting = system.mes.analysis.createMESAnalysisSettings("Top 5 Downtime Reasons")
datapoints = [
"Line Downtime Reason",
"Line State Duration"
]
analysis_setting.setDataPoints(datapoints)
analysis_setting.addParameter('eqPath')
analysis_setting.setFilterExpression("Equipment Path = @eqPath AND Equipment Mode Type = Production AND Line Downtime Reason != ''")
analysis_setting.setGroupBy("Line Downtime Reason")
analysis_setting.setOrderBy("Line State Duration")
analysis_setting.setSettingValues("Descending Order = True,Row Limit = 5, Unknown State as Unplanned Downtime = True")
#End Instructor Provided Code


#execute analysis
#https://help.sepasoft.com/docs/display/SEPA/system.mes.analysis.executeAnalysis
params = {'eqPath':eqPath}
ds = system.mes.analysis.executeAnalysis(startDate, endDate, analysis_setting, params).getDataset()

results = []
for row in range(ds.getRowCount()):
data = {'LineDowntimeReason':ds.getValueAt(row,'Line Downtime Reason'),
'LineStateDuration':round(ds.getValueAt(row,'Line State Duration'),2)}
results.append(data)

res_code = 200

return {'code': res_code, 'headers': res_headers, 'content': {'data':results}}

The completed Response Message script should look like this:

Python
def do_GET(request):
"""
Responds to an incoming HTTP request with a response in dict format.

Puts a HTTP status code in integer format at the key 'code' in the response
dict (required).

Puts HTTP response headers in dict format at the key 'headers' in the
response dict (optional).

Puts a HTTP response content in dict format at the key 'content' in the
response dict (required).

Arguments:
request: A dict with the incoming HTTP request information. The HTTP
path parameter is contained within a string at request['path']. The HTTP
query parameters are contained within a dict at request['query']. The
HTTP request header parameters are contained within a dict at
request['headers']. The request data is contained within a dict at
input['remote']. The HTTP request body parameter is contained within a
dict at request['body'].
"""
req_path = request['path']
req_query = request['query']
req_headers = request['headers']
req_metadata = request['remote']
req_body = request['body']


res_headers = {}

#get values from request
eqPath = req_query['eqPath']

# Wed Mar 17 20:01:01 UTC 2021
startDate = system.date.parse(req_query['startDate'], 'E MMM d H:m:s z yyyy')
endDate = system.date.parse(req_query['endDate'], 'E MMM d H:m:s z yyyy')


#Instructor Provided Code
#check if analyis settings exist, if not, create
try:
#attemp to load the analysis setting
system.mes.loadMESObject('Top 5 Downtime Reasons', 'AnalysisSettings')
analysis_setting = 'Top 5 Downtime Reasons'
except:
#If load fails, create analysis dynamically
analysis_setting = system.mes.analysis.createMESAnalysisSettings("Top 5 Downtime Reasons")
datapoints = [
"Line Downtime Reason",
"Line State Duration"
]
analysis_setting.setDataPoints(datapoints)
analysis_setting.addParameter('eqPath')
analysis_setting.setFilterExpression("Equipment Path = @eqPath AND Equipment Mode Type = Production AND Line Downtime Reason != ''")
analysis_setting.setGroupBy("Line Downtime Reason")
analysis_setting.setOrderBy("Line State Duration")
analysis_setting.setSettingValues("Descending Order = True,Row Limit = 5, Unknown State as Unplanned Downtime = True")
#End Instructor Provided Code


#execute analysis
#https://help.sepasoft.com/docs/display/SEPA/system.mes.analysis.executeAnalysis
params = {'eqPath':eqPath}
ds = system.mes.analysis.executeAnalysis(startDate, endDate, analysis_setting, params).getDataset()

results = []
for row in range(ds.getRowCount()):
data = {'LineDowntimeReason':ds.getValueAt(row,'Line Downtime Reason'),
'LineStateDuration':round(ds.getValueAt(row,'Line State Duration'),2)}
results.append(data)

res_code = 200

return {'code': res_code, 'headers': res_headers, 'content': {'data':results}}