[visualisation] ajax form for graph visualisation of test executions
authorArthur Lutz <arthur.lutz@logilab.fr>
Thu, 08 Mar 2012 10:03:44 +0100
changeset 813 7cfec468bed5
parent 812 0153b3ad2308
child 814 543a03522921
[visualisation] ajax form for graph visualisation of test executions
views/tracker.py
--- a/views/tracker.py	Mon Feb 20 12:47:14 2012 +0100
+++ b/views/tracker.py	Thu Mar 08 10:03:44 2012 +0100
@@ -11,6 +11,9 @@
 from cubicweb.selectors import is_instance
 from cubicweb.view import EntityView
 from cubicweb.web import uicfg
+from cubicweb.web.views import forms
+from cubicweb.web.formfields import StringField, DateField
+from cubicweb.web import formwidgets as fwdgs, httpcache
 
 from cubes.apycot.views.plots import ERROR_CODES
 
@@ -20,6 +23,51 @@
 # in graphs ignore branches that have less MIN_NB_RUNS_IN_GRAPH
 MIN_NB_RUNS_IN_GRAPH = 3
 
+def available_graphs(form, **attrs):
+    entity = form.cw_rset.get_entity(0,0)
+    testconfig_rset = form._cw.execute('Any TC,NAME WHERE P has_apycot_environment TENV, '
+                                       'TC use_environment TENV, P eid %(p)s, TC name NAME',  {'p': entity.eid})
+    branches_rset = form._cw.execute('Any B GROUPBY B WHERE TE branch B, '
+                                     'TE is TestExecution, TE using_environment TENV, '
+                                     'P has_apycot_environment TENV, P eid %(p)s HAVING COUNT(TE) > %(limit)s',
+                                     {'p':entity.eid,
+                                      'limit':MIN_NB_RUNS_IN_GRAPH})
+    graph_defs = []
+    for testconfig in testconfig_rset:
+        for branch in branches_rset:
+            label = '%s : %s - %s' % (_(u'Test run time'), testconfig[1], branch[0])
+            rql = 'Any TE,  ET - ST, S ORDERBY ST LIMIT 50 WHERE ' \
+            'TE is TestExecution, TE using_environment TENV, ' \
+            'P has_apycot_environment TENV, TE starttime ST, ' \
+            'TE endtime ET, TE eid E, TE status S, P eid %(p)s, ' \
+            'TE using_config TC, TC eid %(tc)s, TE branch %(branch)s'
+            var_dict = {'p': entity.eid,
+                        'tc': testconfig[0],
+                        'branch':branch[0]}
+            rset = form._cw.execute(rql, var_dict)
+            if rset and len(rset) > MIN_NB_RUNS_IN_GRAPH:
+                graph_defs.append({'label':label,
+                                   'rql':rql,
+                                   'variables': var_dict,
+                                   'vid':'jqplot.testexecution',
+                                   'rset':rset})
+    return [x['label'] for x in graph_defs]
+
+class GraphRefreshForm(forms.FieldsForm):
+    """Form to select what graph is being displayed"""
+    __regid__ = 'select-graph'
+    graphs = StringField(widget=fwdgs.Select(attrs={'onchange':'this.form.submit()'}),
+                         label=_('Graph:'),
+                         choices=available_graphs)
+    form_buttons = [fwdgs.SubmitButton(label=_('Apply'))]
+
+    @property 
+    def onsubmit(self):
+        return u'$("#graph-container").loadxhtml("/json")'
+
+
+
+
 class ProjectTestResultsTab(EntityView):
     """display project's test execution results"""
     __regid__ = title = _('apycottestresults_tab')
@@ -29,28 +77,8 @@
         self.w(u'<h3>%s</h3>' % _('Time taken by Test Executions'))
         self.w(u'''<table class="plotlegend"><tr>%s</tr></table>
         ''' % ''.join(['<td>%s</td><td style="background:%s">&nbsp;&nbsp;&nbsp;</td>' % (x,y) for x,y in  ERROR_CODES.items()]))
-        testconfig_rset = self._cw.execute('Any TC,NAME WHERE P has_apycot_environment TENV, '
-                                           'TC use_environment TENV, P eid %(p)s, TC name NAME',  {'p': entity.eid})
-        branches_rset = self._cw.execute('Any B GROUPBY B WHERE TE branch B, '
-                                         'TE is TestExecution, TE using_environment TENV, '
-                                         'P has_apycot_environment TENV, P eid %(p)s HAVING COUNT(TE) > %(limit)s',
-                                         {'p':entity.eid,
-                                          'limit':MIN_NB_RUNS_IN_GRAPH})
-        for testconfig in testconfig_rset:
-            for branch in branches_rset:
-                label = '%s : %s - %s' % (_(u'Test run time'), testconfig[1], branch[0])
-                rset = self._cw.execute(
-            'Any TE,  ET - ST, S ORDERBY ST LIMIT 50 WHERE '
-            'TE is TestExecution, TE using_environment TENV, '
-            'P has_apycot_environment TENV, TE starttime ST, '
-            'TE endtime ET, TE eid E, TE status S, P eid %(p)s, '
-            'TE using_config TC, TC eid %(tc)s, TE branch %(branch)s',
-                {'p': entity.eid,
-                 'tc': testconfig[0],
-                 'branch':branch[0]})
-                if rset and len(rset) > MIN_NB_RUNS_IN_GRAPH:
-                    self.w(u'<h4>%s</h4>' % label)
-                    self.wview('jqplot.testexecution', rset, 'noresult')
+        form = self._cw.vreg['forms'].select('select-graph', self._cw, rset=self.cw_rset)
+        form.render(w=self.w)
         rset = self._cw.execute(
             'Any T,TC,T,TB,TST,TET,TF, TS ORDERBY TST DESC WHERE '
             'T status TS, T using_config TC, T branch TB, '