[LNT] r258372 - Make most of the LNT object tree JSON serializable
Chris Matthews via llvm-commits
llvm-commits at lists.llvm.org
Wed Jan 20 16:14:26 PST 2016
Author: cmatthews
Date: Wed Jan 20 18:14:26 2016
New Revision: 258372
URL: http://llvm.org/viewvc/llvm-project?rev=258372&view=rev
Log:
Make most of the LNT object tree JSON serializable
Modified:
lnt/trunk/lnt/server/db/testsuitedb.py
lnt/trunk/lnt/server/reporting/analysis.py
lnt/trunk/lnt/server/ui/regression_views.py
Modified: lnt/trunk/lnt/server/db/testsuitedb.py
URL: http://llvm.org/viewvc/llvm-project/lnt/trunk/lnt/server/db/testsuitedb.py?rev=258372&r1=258371&r2=258372&view=diff
==============================================================================
--- lnt/trunk/lnt/server/db/testsuitedb.py (original)
+++ lnt/trunk/lnt/server/db/testsuitedb.py Wed Jan 20 18:14:26 2016
@@ -14,6 +14,13 @@ from sqlalchemy import *
import testsuite
+def strip(obj):
+ """Give back a dict without sqlalchemy stuff."""
+ new_dict = dict(obj)
+ new_dict.pop('_sa_instance_state', None)
+ return new_dict
+
+
class TestSuiteDB(object):
"""
Wrapper object for an individual test suites database tables.
@@ -140,6 +147,9 @@ class TestSuiteDB(object):
.order_by(ts.Run.start_time.desc()).first()
return closest_run
+
+ def __json__(self):
+ return strip(self.__dict__) # {u'name': self.name, u'MachineID': self.id}
class Order(self.base, ParameterizedMixin):
__tablename__ = db_key_name + '_Order'
@@ -231,7 +241,14 @@ class TestSuiteDB(object):
for item in self.fields),
tuple(convert_field(b.get_field(item))
for item in self.fields))
-
+
+ def __json__(self):
+ order = dict((item.name, self.get_field(item))
+ for item in self.fields)
+ order[u'id'] = self.id
+ return strip(order)
+
+
class Run(self.base, ParameterizedMixin):
__tablename__ = db_key_name + '_Run'
@@ -288,7 +305,12 @@ class TestSuiteDB(object):
@parameters.setter
def parameters(self, data):
self.parameters_data = json.dumps(sorted(data.items()))
-
+
+ def __json__(self):
+ self.machine
+ self.order
+ return strip(self.__dict__)
+
class Test(self.base, ParameterizedMixin):
__tablename__ = db_key_name + '_Test'
@@ -301,6 +323,9 @@ class TestSuiteDB(object):
def __repr__(self):
return '%s_%s%r' % (db_key_name, self.__class__.__name__,
(self.name,))
+
+ def __json__(self):
+ return strip(self.__dict__)
class Sample(self.base, ParameterizedMixin):
__tablename__ = db_key_name + '_Sample'
@@ -434,6 +459,16 @@ class TestSuiteDB(object):
return '%s_%s%r' % (db_key_name, self.__class__.__name__,
(self.start_order, self.end_order,
self.test, self.machine, self.field))
+
+ def __json__(self):
+ self.machine
+ self.test
+ self.field
+ self.run
+ self.start_order
+ self.end_order
+ return strip(self.__dict__)
+
class Regression(self.base, ParameterizedMixin):
"""Regession hold data about a set of RegressionIndicies."""
@@ -452,6 +487,9 @@ class TestSuiteDB(object):
def __repr__(self):
return '%s_%s:"%s"' % (db_key_name, self.__class__.__name__,
self.title)
+
+ def __json__(self):
+ return strip(self.__dict__)
class RegressionIndicator(self.base, ParameterizedMixin):
""""""
@@ -474,6 +512,11 @@ class TestSuiteDB(object):
def __repr__(self):
return '%s_%s%r' % (db_key_name, self.__class__.__name__,(
self.id, self.regression, self.field_change))
+
+ def __json__(self):
+ return {u'RegressionIndicatorID': self.id,
+ u'Regression': self.regression,
+ u'FieldChange': self.field_change}
class ChangeIgnore(self.base, ParameterizedMixin):
"""Changes to ignore in the web interface."""
Modified: lnt/trunk/lnt/server/reporting/analysis.py
URL: http://llvm.org/viewvc/llvm-project/lnt/trunk/lnt/server/reporting/analysis.py?rev=258372&r1=258371&r2=258372&view=diff
==============================================================================
--- lnt/trunk/lnt/server/reporting/analysis.py (original)
+++ lnt/trunk/lnt/server/reporting/analysis.py Wed Jan 20 18:14:26 2016
@@ -122,6 +122,11 @@ class ComparisonResult:
self.prev_samples,
self.confidence_lv,
bool(self.bigger_is_better))
+
+ def __json__(self):
+ simple_dict = self.__dict__
+ simple_dict['aggregation_fn'] = self.aggregation_fn.__name__
+ return simple_dict
def is_result_performance_change(self):
"""Check if we think there was a performance change."""
Modified: lnt/trunk/lnt/server/ui/regression_views.py
URL: http://llvm.org/viewvc/llvm-project/lnt/trunk/lnt/server/ui/regression_views.py?rev=258372&r1=258371&r2=258372&view=diff
==============================================================================
--- lnt/trunk/lnt/server/ui/regression_views.py (original)
+++ lnt/trunk/lnt/server/ui/regression_views.py Wed Jan 20 18:14:26 2016
@@ -6,6 +6,8 @@ from flask import request
from flask import make_response
from flask import flash
from flask import redirect
+import flask
+import json
# import sqlalchemy.sql
# from sqlalchemy.orm.exc import NoResultFound
@@ -75,6 +77,10 @@ class PrecomputedCR():
def get_value_status(self, ignore_small=True):
return REGRESSED
+
+ def __json__(self):
+ return self.__dict__
+
@v4_route("/regressions/new", methods=["GET", "POST"])
@@ -213,6 +219,25 @@ class EditRegressionForm(Form):
state = SelectField(u'State', choices=choices)
+def name(cls):
+ """Get a nice name for this object."""
+ return cls.__class__.__name__
+
+
+class LNTEncoder(flask.json.JSONEncoder):
+ """Encode all the common LNT objects."""
+ def default(self, obj):
+ # Most of our objects have a __json__ defined.
+ if hasattr(obj, "__json__"):
+ return obj.__json__()
+ # From sqlalchemy, when we encounter ignore.
+ if name(obj) == "InstanceState":
+ return
+ if name(obj) == "SampleField":
+ return obj.name
+ return flask.json.JSONEncoder.default(self, obj)
+
+
@v4_route("/regressions/<int:id>", methods=["GET", "POST"])
def v4_regression_detail(id):
ts = request.get_testsuite()
More information about the llvm-commits
mailing list