source: cows_wps/trunk/cows_wps/controllers/jobviewer.py @ 6903

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/cows_wps/trunk/cows_wps/controllers/jobviewer.py@6903
Revision 6903, 4.7 KB checked in by astephen, 10 years ago (diff)

various updates. Key thing to add next is polling of jobs and update of
response when it has polled correctly. Can re-use jquery stuff in
submit_utils.js which is relatively lightweight.

Line 
1import os
2import re
3import glob
4import logging
5import urllib
6import xml.etree.ElementTree as ET
7
8from pylons import request, response, session, tmpl_context as c
9from pylons.controllers.util import abort, redirect_to
10
11
12from cows.exceptions import *
13from cows.pylons.ows_controller import render_ows_exception
14
15from cows_wps.controllers import *
16from cows_wps import utils
17from cows_wps.renderer.ui_renderer import *
18from cows_wps.model.managers import requestManager
19from cows_wps.process_handler.context.process_context import ProcessContext
20from cows_wps.renderer import xml_renderer
21
22from cows_wps.utils.parse_wps_config import wps_config_dict
23from cows_wps.utils.parse_capabilities_config import caps_config_dict
24
25
26from cows_wps.model.managers import requestManager
27
28log = logging.getLogger(__name__)
29
30
31class JobviewerController(BaseController):
32
33
34    def index(self):
35        status_url = str(request.params.getone("status_url"))
36#        job_id = str(request.params.getone("job_id"))
37        getter = urllib.urlopen(status_url)
38        job_xml = getter.read()
39        getter.close()
40
41        job_id = re.search("<JobID>(.*)</JobID>", job_xml).groups()[0]
42
43        job_as_table = self._xmlToTable(job_xml)
44        job_xml_encoded = self._htmlifyXML(job_xml)
45        job_as_plots = "There are no plots available in the outputs of this job."
46
47        resp = """<div id="view_options" style="background: #EEF4FF; border: 2px outset blue; padding: 5px;">
48                <a href="javascript:switchViewTo('table_view');">View as table</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
49                <a href="javascript:switchViewTo('xml_view');">View as XML</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
50                <a href="javascript:switchViewTo('plots_view');">View Plots (if available)</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
51</div>
52<div id="view_container" style="background: white; border: 2px outset blue; padding: 5px;">
53        <div id="view_contents">
54                <div id="table_view" style="visibility: visible; height: auto;">%s</div>
55                <div id="xml_view" style="visibility: hidden; height: 0px;">%s</div>
56                <div id="plots_view" style="visibility: hidden; height: 0px;">%s</div>
57</div>""" % (job_as_table, job_xml_encoded, job_as_plots)
58
59        renderer = UIPageRenderer()
60        resp = renderer.render("WPS Job Information",
61                              [("Job: %s" % job_id, resp)])
62        return resp
63
64
65    def _xmlToTable(self, xml):
66        "Chops up xml to get table of outputs."
67        node = ET.fromstring(xml)
68
69        proc_id = node.find("{http://www.opengeospatial.net/ows}Identifier").text
70        job_details = node.find("{http://www.opengeospatial.net/wps}ProcessOutputs").find("{http://www.opengeospatial.net/wps}Output").find("{http://www.opengeospatial.net/wps}ComplexValue").find("{http://www.opengeospatial.net/wps}WPSResponseDetails").find("{http://www.opengeospatial.net/wps}JobDetails")
71
72        file_set_list = job_details.find("{http://www.opengeospatial.net/wps}FileSet").getchildren()
73
74        items = ["JobID", "JobCompletionTimeDate", "JobDuration", "JobVolume", "RequestDescription",
75                 "RequestType"]
76        mapped_names = ["Job ID", "Completion Time", "Job Duration", "Output Size (Bytes)", 
77                        "Request Description", "Request Type"]
78
79        resp = "<h2>DETAILS</h2>"
80        resp += "<p>Need to realise that in costonly response we have JobCompletionTimeDate empty because it hasn't run yet!</p>"
81
82        for (i, item) in enumerate(items):
83            x = job_details.find("{http://www.opengeospatial.net/wps}" + item).text
84            if type(x) != str: x = "Undefined"
85            resp += "<b>%s</b> = %s<br>" % (mapped_names[i], x.strip())
86
87        file_items = ["FileSize"]
88        mapped_file_items = ["File Size (Bytes)"]
89
90        resp += "<h2>OUTPUTS</h2>The following file outputs are available from your job.<br>"
91        for fnode in file_set_list:
92
93            furl = fnode.find("{http://www.opengeospatial.net/wps}FileURL").text
94            fname = furl.split("/")[-1]
95            len_fname = len(fname)
96
97            p = 70
98            padding = p - len_fname
99            if padding < 0: padding = 0
100
101#            fname = fname[:p] + (padding * "&nbsp")
102            resp += ('<br><kbd>%s [<a href="%s">Download</a>]</kbd>' + (padding * "&nbsp;")) % (fname, furl)
103#            resp += '<br><span style="width: 500px;">%s [<a href="%s">Download</a>]</span>' % (fname, furl)
104
105            for (i, file_item) in enumerate(file_items):
106                x = fnode.find("{http://www.opengeospatial.net/wps}" + file_item).text
107                resp += '%s Bytes' % x
108
109        return resp
110
111
112    def _htmlifyXML(self, xml):
113        "Returns html string that will make XML look ok on HTML page."
114        xml = xml.replace("<", "&lt;").replace(">", "&gt;").replace("\n", "<br>")
115        return xml
116
117
Note: See TracBrowser for help on using the repository browser.