Skip to content

Commit

Permalink
Execution filtering in Granta API
Browse files Browse the repository at this point in the history
  • Loading branch information
Stanislav committed Oct 20, 2024
1 parent c6d8b82 commit 5f17b91
Showing 1 changed file with 47 additions and 40 deletions.
87 changes: 47 additions & 40 deletions mupifDB/restApiControl.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

#

def Request(*, method, url, headers=None, auth=None, data=None, timeout=None, files={}, params={}, allow_redirects=False):
def Request(*, method, url, headers=None, auth=None, data=None, timeout=None, files={}, params={}, allow_redirects=True):
if method == 'get':
response = requests.get(url=url, timeout=timeout, headers=headers, auth=auth, data=data, params=params, allow_redirects=allow_redirects)
elif method == 'post':
Expand All @@ -36,10 +36,10 @@ def Request(*, method, url, headers=None, auth=None, data=None, timeout=None, fi
if response.status_code >= 200 and response.status_code <= 299:
return response
else:
raise Exception('API returned code ' + str(response.status_code))
raise Exception(f'API returned code {response.status_code}, message {response.reason} ({url=}, {method=}, {data=}, {auth=}, {timeout=}, {files=}, {params=}, {allow_redirects=}')
return None

def rGet(*, url, headers=None, auth=None, timeout=100, params={}, allow_redirects=False):
def rGet(*, url, headers=None, auth=None, timeout=100, params={}, allow_redirects=True):
return Request(method='get', url=url, headers=headers, auth=auth, timeout=timeout, params=params, allow_redirects=allow_redirects)

def rPost(*, url, headers=None, auth=None, data=None, timeout=100, files={}):
Expand All @@ -65,6 +65,12 @@ def rDelete(*, url, headers=None, auth=None, timeout=100):

RESTserverMuPIF = RESTserver

def setRESTserver(r):
'Used in tests to set RESTserver after import'
global RESTserver
global RESTserverMuPIF
RESTserver=RESTserverMuPIF=r+'/'

granta_credentials = {'username': '', 'password': ''}

bearer_token = None
Expand Down Expand Up @@ -103,11 +109,11 @@ def getAuthToken():
# Users
# --------------------------------------------------

def getUserByIP(ip):
if api_type == 'granta':
return None
response = rGet(url=RESTserver + "users/" + str(ip))
return response.json()
# def getUserByIP(ip):
# if api_type == 'granta':
# return None
# response = rGet(url=RESTserver + "users/" + str(ip))
# return response.json()


# --------------------------------------------------
Expand Down Expand Up @@ -185,8 +191,8 @@ def updateWorkflow(data):

def fix_json(val):
import re
val = re.sub(",[ \t\r\n]+}", "}", val)
val = re.sub(",[ \t\r\n]+\]", "]", val)
val = re.sub(r",[ \t\r\n]+}", "}", val)
val = re.sub(r",[ \t\r\n]+\]", "]", val)
val = val.replace("False", "false").replace("True", "true")
val
return val
Expand Down Expand Up @@ -431,9 +437,25 @@ def insertWorkflowHistory(data):
# Executions
# --------------------------------------------------

grantaExecutionStatusTranscript = {
'Ready':'Pending',
'On-going':'Running',
'Completed':'Finished',
'Completed, to be reviewed':'Finished',
'Completed & reviewed':'Finished',
'Cancelled':'Failed'
}
grantaExecutionStatusTranscript = {'Ready':'Created','Scheduled':'Pending','Running':'Running','Running':'Scheduled','Completed':'Finished','Completed, to be reviewed':'Finished','Completed & reviewed':'Finished','Failed':'Failed'}

def getExecutionRecords(workflow_id=None, workflow_version=None, label=None, num_limit=None, status=None):
if api_type == 'granta':
granta_status_filter = None
if status is not None:
granta_status_filter = list(grantaExecutionStatusTranscript.keys())[list(grantaExecutionStatusTranscript.values()).index(status)]

url = RESTserver + 'executions/'
if granta_status_filter is not None:
url = url + '?status=' + granta_status_filter
token = getAuthToken()
headers = {'content-type': 'application/json', 'Accept-Charset': 'UTF-8', 'Authorization': f'Bearer {token["access_token"]}'}
r = rGet(url=url, headers=headers)
Expand All @@ -443,20 +465,7 @@ def getExecutionRecords(workflow_id=None, workflow_version=None, label=None, num
execution['_id'] = ex['guid']
execution['WorkflowID'] = ex['template_guid']
execution['WorkflowVersion'] = -1
st = ex['status']
if st == 'Ready':
st = 'Pending'
if st == 'On-going':
st = 'Running'
if st == 'Completed':
st = 'Finished'
if st == 'Completed, to be reviewed':
st = 'Finished'
if st == 'Completed & reviewed':
st = 'Finished'
if st == 'Canceled':
st = 'Failed'
execution['Status'] = st
execution['Status'] = grantaExecutionStatusTranscript.get(ex['status'],ex['status'])
execution['Task_ID'] = ''
res.append(execution)
return res
Expand Down Expand Up @@ -491,20 +500,7 @@ def getExecutionRecord(weid):
execution['WorkflowVersion'] = -1
execution['Status'] = 'unknown'

st = r_json['status']
if st == 'Ready':
st = 'Pending'
if st == 'On-going':
st = 'Running'
if st == 'Completed':
st = 'Finished'
if st == 'Completed, to be reviewed':
st = 'Finished'
if st == 'Completed & reviewed':
st = 'Finished'
if st == 'Canceled':
st = 'Failed'
execution['Status'] = st
execution['Status'] = grantaExecutionStatusTranscript.get(r_json['status'], r_json['status'])

execution['Task_ID'] = ''
return execution
Expand Down Expand Up @@ -589,7 +585,7 @@ def setExecutionOntoBaseObjectIDs(execution_id, name, value):
def setExecutionAttemptsCount(execution_id, val):
if api_type == 'granta':
return None
return setExecutionParameter(execution_id, "Attempts", val, "int")
return setExecutionParameter(execution_id, "Attempts", str(val), "int")


def _setGrantaExecutionResults(eid, val_list):
Expand Down Expand Up @@ -888,7 +884,18 @@ def updateStatScheduler(runningTasks=None, scheduledTasks=None, load=None, proce


# --------------------------------------------------
# Ontology
# Settings
# --------------------------------------------------

def getSettings():
if api_type == 'granta':
return {}
response = rGet(url=RESTserver + "settings")
return response.json()


# --------------------------------------------------
# EDM
# --------------------------------------------------

def getEDMDataArray(DBName, Type):
Expand Down

0 comments on commit 5f17b91

Please sign in to comment.