Commit a430296b authored by Jerome Mariette's avatar Jerome Mariette

handle JSONP and JSON request

parent c6516243
......@@ -20,6 +20,7 @@ import json
import sys
from functools import wraps
import time
import os
try:
import _preamble
......@@ -31,45 +32,6 @@ import jflow.utils as utils
from cctools.util import time_format
def json_workflow_status(workflow):
if workflow.start_time: start_time = time.asctime(time.localtime(workflow.start_time))
else: start_time = "-"
if workflow.start_time and workflow.end_time: elapsed_time = str(workflow.end_time-workflow.start_time)
elif workflow.start_time: elapsed_time = str(time.time()-workflow.start_time)
else: elapsed_time = "-"
if workflow.end_time: end_time = time.asctime(time.localtime(workflow.end_time))
else: end_time = "-"
components = []
for i, component in enumerate(workflow.get_components_nameid()):
status_info = workflow.get_component_status(component)
try: perc_waiting = (status_info["waiting"]*100.0)/status_info["tasks"]
except: perc_waiting = 0
try: perc_running = (status_info["running"]*100.0)/status_info["tasks"]
except: perc_running = 0
try: perc_failed = (status_info["failed"]*100.0)/status_info["tasks"]
except: perc_failed = 0
try: perc_aborted = (status_info["aborted"]*100.0)/status_info["tasks"]
except: perc_aborted = 0
try: perc_completed = (status_info["completed"]*100.0)/status_info["tasks"]
except: perc_completed = 0
components.append({"name": component,
"elapsed_time": time_format(status_info["time"]),
"total": status_info["tasks"],
"waiting": status_info["waiting"],
"failed": status_info["failed"],
"running": status_info["running"],
"aborted": status_info["aborted"],
"completed": status_info["completed"]})
status = {"id":utils.get_nb_string(workflow.id),
"name": workflow.name,
"status": workflow.get_status(),
"elapsed_time": str(elapsed_time),
"start_time": start_time,
"end_time": end_time,
"components": components}
return status
class JFlowServer (object):
def __init__(self):
......@@ -77,15 +39,56 @@ class JFlowServer (object):
self.wfmanager = WorkflowsManager()
def jsonify(func):
'''JSON decorator for CherryPy'''
'''JSON and JSONP decorator for CherryPy'''
@wraps(func)
def wrapper(*args, **kw):
value = func(*args, **kw)
cherrypy.response.headers["Content-Type"] = "application/json"
return json.dumps(value)
# if JSONP request
if kw.has_key("callback"): return kw["callback"] + "(" + json.dumps(value) + ")"
# else return the JSON
else: return json.dumps(value)
return wrapper
def jsonify_workflow_status(self, workflow):
if workflow.start_time: start_time = time.asctime(time.localtime(workflow.start_time))
else: start_time = "-"
if workflow.start_time and workflow.end_time: elapsed_time = str(workflow.end_time-workflow.start_time)
elif workflow.start_time: elapsed_time = str(time.time()-workflow.start_time)
else: elapsed_time = "-"
if workflow.end_time: end_time = time.asctime(time.localtime(workflow.end_time))
else: end_time = "-"
components = []
for i, component in enumerate(workflow.get_components_nameid()):
status_info = workflow.get_component_status(component)
try: perc_waiting = (status_info["waiting"]*100.0)/status_info["tasks"]
except: perc_waiting = 0
try: perc_running = (status_info["running"]*100.0)/status_info["tasks"]
except: perc_running = 0
try: perc_failed = (status_info["failed"]*100.0)/status_info["tasks"]
except: perc_failed = 0
try: perc_aborted = (status_info["aborted"]*100.0)/status_info["tasks"]
except: perc_aborted = 0
try: perc_completed = (status_info["completed"]*100.0)/status_info["tasks"]
except: perc_completed = 0
components.append({"name": component,
"elapsed_time": time_format(status_info["time"]),
"total": status_info["tasks"],
"waiting": status_info["waiting"],
"failed": status_info["failed"],
"running": status_info["running"],
"aborted": status_info["aborted"],
"completed": status_info["completed"]})
status = {"id":utils.get_nb_string(workflow.id),
"name": workflow.name,
"status": workflow.get_status(),
"elapsed_time": str(elapsed_time),
"start_time": start_time,
"end_time": end_time,
"components": components}
return status
@cherrypy.expose
@jsonify
def get_available_workflows(self, **kwargs):
......@@ -109,19 +112,22 @@ class JFlowServer (object):
def rerun_workflow(self, **kwargs):
self.wfmanager.rerun(kwargs["workflow_id"])
workflow = self.wfmanager.get_workflow(kwargs["workflow_id"])
return json_workflow_status(workflow)
return self.jsonify_workflow_status(workflow)
@cherrypy.expose
@jsonify
def get_workflows_status(self, **kwargs):
if kwargs.has_key("workflow_id"):
workflow = self.wfmanager.get_workflow(kwargs["workflow_id"])
return json_workflow_status(workflow)
return self.jsonify_workflow_status(workflow)
else:
status = []
workflows = self.wfmanager.get_workflows()
for workflow in workflows:
status.append(json_workflow_status(workflow))
status.append(self.jsonify_workflow_status(workflow))
return status
cherrypy.quickstart(JFlowServer())
\ No newline at end of file
if __name__ == '__main__':
# start the server
cherrypy.quickstart(JFlowServer())
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment