[LNT] r255961 - Refector: extract regression model code to a regression module

Chris Matthews via llvm-commits llvm-commits at lists.llvm.org
Thu Dec 17 17:44:49 PST 2015


Author: cmatthews
Date: Thu Dec 17 19:44:49 2015
New Revision: 255961

URL: http://llvm.org/viewvc/llvm-project?rev=255961&view=rev
Log:
Refector: extract regression model code to a regression module

Added:
    lnt/trunk/lnt/server/db/regression.py
Modified:
    lnt/trunk/lnt/server/db/fieldchange.py
    lnt/trunk/lnt/server/db/rules/rule_testhook.py
    lnt/trunk/lnt/server/db/rules/rule_update_fixed_regressions.py
    lnt/trunk/lnt/server/ui/regression_views.py
    lnt/trunk/tests/server/db/rules.py
    lnt/trunk/tests/server/ui/change_processing.py

Modified: lnt/trunk/lnt/server/db/fieldchange.py
URL: http://llvm.org/viewvc/llvm-project/lnt/trunk/lnt/server/db/fieldchange.py?rev=255961&r1=255960&r2=255961&view=diff
==============================================================================
--- lnt/trunk/lnt/server/db/fieldchange.py (original)
+++ lnt/trunk/lnt/server/db/fieldchange.py Thu Dec 17 19:44:49 2015
@@ -3,9 +3,11 @@ import sqlalchemy.sql
 import lnt.server.reporting.analysis
 from lnt.testing.util.commands import warning
 from lnt.testing.util.commands import note
-from lnt.server.ui.regression_views import new_regression, RegressionState
-from lnt.server.ui.regression_views import get_ris
-from lnt.server.db import rules
+from lnt.server.db.regression import new_regression, RegressionState
+from lnt.server.db.regression import get_ris
+from lnt.server.db.regression import rebuild_title
+
+from lnt.server.db import rules_manager as rules
 # How many runs backwards to use in the previous run set.
 # More runs are slower (more DB access), but may provide
 # more accurate results.
@@ -86,7 +88,7 @@ def regenerate_fieldchanges_for_run(ts,
                 found, new_reg = identify_related_changes(ts, regressions, f)
                 if found:
                     regressions.append(new_reg)
-                note("Found field change: {}".format(run.machine))
+                    note("Found field change: {}".format(run.machine))
 
             # Always update FCs with new values.
             if f:
@@ -108,33 +110,6 @@ def is_overlaping(fc1, fc2):
            (r1_min < r2_max and r2_min < r1_max)
 
 
-def shortname(benchmark):
-    """Given a benchmarks full name, make a short version"""
-    return benchmark.split("/")[-1]
-
-
-def rebuild_title(ts, regression):
-    """Update the title of a regresson."""
-    if re.match("Regression of \d+ benchmarks.*", regression.title):
-        old_changes = ts.query(ts.RegressionIndicator) \
-            .filter(ts.RegressionIndicator.regression_id == regression.id) \
-            .all()
-        new_size = len(old_changes)
-        benchmarks = set()
-        for ri in old_changes:
-            fc = ri.field_change
-            benchmarks.add(shortname(fc.test.name))
-        print benchmarks
-        FMT = "Regression of {} benchmarks: {}"
-        title = FMT.format(new_size, ', '.join(benchmarks))
-        # Crop long titles.
-        title = (title[:120] + '...') if len(title) > 120 else title
-        regression.title = title
-        print title
-    return regression
-
-
-
 def identify_related_changes(ts, regressions, fc):
     """Can we find a home for this change in some existing regression? """
     for regression in regressions:

Added: lnt/trunk/lnt/server/db/regression.py
URL: http://llvm.org/viewvc/llvm-project/lnt/trunk/lnt/server/db/regression.py?rev=255961&view=auto
==============================================================================
--- lnt/trunk/lnt/server/db/regression.py (added)
+++ lnt/trunk/lnt/server/db/regression.py Thu Dec 17 19:44:49 2015
@@ -0,0 +1,144 @@
+from sqlalchemy import desc, asc
+import datetime
+import re
+from collections import namedtuple
+from lnt.server.reporting.analysis import RunInfo
+
+class RegressionState:
+    # A new regression, not approved by the user yet.
+    DETECTED = 0
+    # Approved, but waiting for cooldown.
+    STAGED = 1
+    # Needs to be investigated.
+    ACTIVE = 10
+
+    # We won't fix this.
+    NTBF = 20
+    # This is not a real regression.
+    IGNORED = 21
+    # Manually marked as fixed.
+    FIXED = 22
+    # System detected it is fixed.
+    DETECTED_FIXED = 23
+    names = {DETECTED: u'Detected',
+             STAGED: u'Staged',
+             ACTIVE: u'Active',
+             NTBF: u'Not to be Fixed',
+             IGNORED: u'Ignored',
+             DETECTED_FIXED: u'Verify',
+             FIXED: u'Fixed'
+             }
+
+ChangeRuns = namedtuple("ChangeRuns", ["before", "after"])
+ChangeData = namedtuple("ChangeData", ["ri", "cr", "run", "latest_cr"])
+
+
+def new_regression(ts, field_changes):
+    """Make a new regression and add to DB."""
+    today = datetime.date.today()
+    MSG = "Regression of 0 benchmarks"
+    title = MSG
+    regression = ts.Regression(title, "", RegressionState.DETECTED)
+    ts.add(regression)
+    for fc_id in field_changes:
+        fc = get_fieldchange(ts, fc_id)
+        ri1 = ts.RegressionIndicator(regression, fc)
+        ts.add(ri1)
+    rebuild_title(ts, regression)
+    ts.commit()
+    return regression
+    
+    
+def shortname(benchmark):
+    """Given a benchmarks full name, make a short version"""
+    return benchmark.split("/")[-1]
+
+
+def rebuild_title(ts, regression):
+    """Update the title of a regresson."""
+    if re.match("Regression of \d+ benchmarks.*", regression.title):
+        old_changes = ts.query(ts.RegressionIndicator) \
+            .filter(ts.RegressionIndicator.regression_id == regression.id) \
+            .all()
+        new_size = len(old_changes)
+        benchmarks = set()
+        for ri in old_changes:
+            fc = ri.field_change
+            benchmarks.add(shortname(fc.test.name))
+        FMT = "Regression of {} benchmarks: {}"
+        title = FMT.format(new_size, ', '.join(benchmarks))
+        # Crop long titles.
+        title = (title[:120] + '...') if len(title) > 120 else title
+        regression.title = title
+    return regression
+    
+
+
+def get_all_orders_for_machine(ts, machine):
+    """Get all the oredrs for this sa machine."""
+    return ts.query(ts.Order) \
+        .join(ts.Run) \
+        .filter(ts.Run.machine_id == machine) \
+        .order_by(asc(ts.Order.llvm_project_revision)) \
+        .all()
+
+
+def get_ris(ts, regression):
+    return ts.query(ts.RegressionIndicator) \
+        .filter(ts.RegressionIndicator.regression_id == regression.id) \
+        .all()
+
+
+def get_runs_for_order_and_machine(ts, order_id, machine_id):
+    """Collect all the runs for a particular order/machine combo."""
+    runs = ts.query(ts.Run) \
+        .filter(ts.Run.machine_id == machine_id) \
+        .filter(ts.Run.order_id == order_id) \
+        .all()
+    return runs
+
+
+def get_runs_of_fieldchange(ts, fc):
+    before_runs = get_runs_for_order_and_machine(ts, fc.start_order_id,
+                                                 fc.machine_id)
+    after_runs = get_runs_for_order_and_machine(ts, fc.end_order_id,
+                                                fc.machine_id)
+    return ChangeRuns(before_runs, after_runs)
+
+
+def get_current_runs_of_fieldchange(ts, fc):
+    before_runs = get_runs_for_order_and_machine(ts, fc.start_order_id,
+                                                 fc.machine_id)
+    newest_order = get_all_orders_for_machine(ts, fc.machine_id)[-1]
+        
+    after_runs = get_runs_for_order_and_machine(ts, newest_order.id,
+                                                fc.machine_id)
+    return ChangeRuns(before_runs, after_runs)
+
+
+def get_first_runs_of_fieldchange(ts, fc):
+    run = ts.query(ts.Run) \
+        .filter(ts.Run.machine_id == fc.machine_id) \
+        .filter(ts.Run.order_id == fc.end_order_id) \
+        .first()
+    return run
+
+
+def get_cr_for_field_change(ts, field_change, current=False):
+    """Given a filed_change, calculate a comparison result for that change. 
+    And the last run."""
+    if current:
+        runs = get_current_runs_of_fieldchange(ts, field_change)
+    else:
+        runs = get_runs_of_fieldchange(ts, field_change)
+    runs_all = list(runs.before)
+    runs_all.extend(runs.after)
+    ri = RunInfo(ts, [r.id for r in runs_all], only_tests=[field_change.test_id])
+    cr = ri.get_comparison_result(runs.after, runs.before,
+                                  field_change.test.id, field_change.field)
+    return cr, runs.after[0]
+
+
+def get_fieldchange(ts, fc_id):
+    """Get a fieldchange given an ID."""
+    return ts.query(ts.FieldChange).filter(ts.FieldChange.id == fc_id).one()

Modified: lnt/trunk/lnt/server/db/rules/rule_testhook.py
URL: http://llvm.org/viewvc/llvm-project/lnt/trunk/lnt/server/db/rules/rule_testhook.py?rev=255961&r1=255960&r2=255961&view=diff
==============================================================================
--- lnt/trunk/lnt/server/db/rules/rule_testhook.py (original)
+++ lnt/trunk/lnt/server/db/rules/rule_testhook.py Thu Dec 17 19:44:49 2015
@@ -1,5 +1,5 @@
 """
-Test that this hook can be run.
+Test rule system. A simple rules, with one hook that can be run.
 """
 
 

Modified: lnt/trunk/lnt/server/db/rules/rule_update_fixed_regressions.py
URL: http://llvm.org/viewvc/llvm-project/lnt/trunk/lnt/server/db/rules/rule_update_fixed_regressions.py?rev=255961&r1=255960&r2=255961&view=diff
==============================================================================
--- lnt/trunk/lnt/server/db/rules/rule_update_fixed_regressions.py (original)
+++ lnt/trunk/lnt/server/db/rules/rule_update_fixed_regressions.py Thu Dec 17 19:44:49 2015
@@ -2,8 +2,8 @@
 Detcted + fixed -> Ignored
 Staged or Active + fixed -> Verify
 """
-from lnt.server.ui.regression_views import RegressionState
-from lnt.server.ui.regression_views import get_cr_for_field_change, get_ris
+from lnt.server.db.regression import RegressionState
+from lnt.server.db.regression import get_cr_for_field_change, get_ris
 from lnt.testing.util.commands import note
 
 def _fixed_rind(ts, rind):

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=255961&r1=255960&r2=255961&view=diff
==============================================================================
--- lnt/trunk/lnt/server/ui/regression_views.py (original)
+++ lnt/trunk/lnt/server/ui/regression_views.py Thu Dec 17 19:44:49 2015
@@ -11,11 +11,10 @@ from flask import redirect
 # from sqlalchemy.orm.exc import NoResultFound
 
 from lnt.server.ui.decorators import v4_route
-from lnt.server.reporting.analysis import RunInfo
 import lnt.server.reporting.analysis
 from lnt.server.ui.globals import db_url_for, v4_url_for
 
-from collections import namedtuple
+
 from random import randint
 from sqlalchemy import desc, asc
 from lnt.server.ui.util import FLASH_DANGER, FLASH_INFO, FLASH_SUCCESS
@@ -26,33 +25,14 @@ from wtforms.validators import DataRequi
 import lnt.server.ui.util as util
 from lnt.testing.util.commands import warning, error, note
 import lnt.server.db.fieldchange
+from lnt.server.db.regression import RegressionState, new_regression
+from lnt.server.db.regression import get_all_orders_for_machine
+from lnt.server.db.regression import ChangeRuns
+from lnt.server.db.regression import get_first_runs_of_fieldchange
+from lnt.server.db.regression import get_cr_for_field_change 
+from lnt.server.db.regression import ChangeData
 from lnt.server.db import rules_manager as rule_hooks
 
-class RegressionState:
-    # A new regression, not approved by the user yet.
-    DETECTED = 0
-    # Approved, but waiting for cooldown.
-    STAGED = 1
-    # Needs to be investigated.
-    ACTIVE = 10
-
-    # We won't fix this.
-    NTBF = 20
-    # This is not a real regression.
-    IGNORED = 21
-    # Manually marked as fixed.
-    FIXED = 22
-    # System detected it is fixed.
-    DETECTED_FIXED = 23
-    names = {DETECTED: u'Detected',
-             STAGED: u'Staged',
-             ACTIVE: u'Active',
-             NTBF: u'Not to be Fixed',
-             IGNORED: u'Ignored',
-             DETECTED_FIXED: u'Verify',
-             FIXED: u'Fixed'
-             }
-
 
 class MultiCheckboxField(SelectMultipleField):
     """
@@ -70,9 +50,6 @@ class TriagePageSelectedForm(Form):
     name = StringField('name', validators=[DataRequired()])
 
 
-ChangeData = namedtuple("ChangeData", ["ri", "cr", "run", "latest_cr"])
-
-
 def get_fieldchange(ts, id):
     return ts.query(ts.FieldChange).filter(ts.FieldChange.id == id).one()
 
@@ -98,22 +75,6 @@ class PrecomputedCR():
         return REGRESSED
 
 
-def new_regression(ts, field_changes):
-    """Make a new regression and add to DB."""
-    today = datetime.date.today()
-    MSG = "Regression of 0 benchmarks"
-    title = MSG
-    regression = ts.Regression(title, "", RegressionState.DETECTED)
-    ts.add(regression)
-    for fc_id in field_changes:
-        fc = get_fieldchange(ts, fc_id)
-        ri1 = ts.RegressionIndicator(regression, fc)
-        ts.add(ri1)
-    lnt.server.db.fieldchange.rebuild_title(ts, regression)
-    ts.commit()
-    return regression
-
-
 @v4_route("/regressions/new", methods=["GET", "POST"])
 def v4_new_regressions():
     form = TriagePageSelectedForm(request.form)
@@ -163,69 +124,6 @@ def v4_new_regressions():
                            form=form)
 
 
-ChangeRuns = namedtuple("ChangeRuns", ["before", "after"])
-
-
-def get_runs_for_order_and_machine(ts, order_id, machine_id):
-    """Collect all the runs for a particular order/machine combo."""
-    runs = ts.query(ts.Run) \
-        .filter(ts.Run.machine_id == machine_id) \
-        .filter(ts.Run.order_id == order_id) \
-        .all()
-    return runs
-
-
-def get_runs_of_fieldchange(ts, fc):
-    before_runs = get_runs_for_order_and_machine(ts, fc.start_order_id,
-                                                 fc.machine_id)
-    after_runs = get_runs_for_order_and_machine(ts, fc.end_order_id,
-                                                fc.machine_id)
-    return ChangeRuns(before_runs, after_runs)
-
-
-def get_current_runs_of_fieldchange(ts, fc):
-    before_runs = get_runs_for_order_and_machine(ts, fc.start_order_id,
-                                                 fc.machine_id)
-    newest_order = get_all_orders_for_machine(ts, fc.machine_id)[-1]
-
-    after_runs = get_runs_for_order_and_machine(ts, newest_order.id,
-                                                fc.machine_id)
-    return ChangeRuns(before_runs, after_runs)
-
-
-def get_first_runs_of_fieldchange(ts, fc):
-    # import ipdb; ipdb.set_trace()
-    run = ts.query(ts.Run) \
-        .filter(ts.Run.machine_id == fc.machine_id) \
-        .filter(ts.Run.order_id == fc.end_order_id) \
-        .first()
-    return run
-
-
-def get_all_orders_for_machine(ts, machine):
-    """Get all the oredrs for this sa machine."""
-    return ts.query(ts.Order) \
-        .join(ts.Run) \
-        .filter(ts.Run.machine_id == machine) \
-        .order_by(asc(ts.Order.llvm_project_revision)) \
-        .all()
-
-
-def get_cr_for_field_change(ts, field_change, current=False):
-    """Given a filed_change, calculate a comparison result for that change. 
-    And the last run."""
-    if current:
-        runs = get_current_runs_of_fieldchange(ts, field_change)
-    else:
-        runs = get_runs_of_fieldchange(ts, field_change)
-    runs_all = list(runs.before)
-    runs_all.extend(runs.after)
-    ri = RunInfo(ts, [r.id for r in runs_all], only_tests=[field_change.test_id])
-    cr = ri.get_comparison_result(runs.after, runs.before,
-                                  field_change.test.id, field_change.field)
-    return cr, runs.after[0]
-
-
 def calc_impact(ts, fcs):
     crs = []
     for fc in fcs:
@@ -315,12 +213,6 @@ class EditRegressionForm(Form):
     state = SelectField(u'State', choices=choices)
 
 
-def get_ris(ts, regression):
-    return ts.query(ts.RegressionIndicator) \
-        .filter(ts.RegressionIndicator.regression_id == regression.id) \
-        .all()
-
-
 @v4_route("/regressions/<int:id>",  methods=["GET", "POST"])
 def v4_regression_detail(id):
     ts = request.get_testsuite()

Modified: lnt/trunk/tests/server/db/rules.py
URL: http://llvm.org/viewvc/llvm-project/lnt/trunk/tests/server/db/rules.py?rev=255961&r1=255960&r2=255961&view=diff
==============================================================================
--- lnt/trunk/tests/server/db/rules.py (original)
+++ lnt/trunk/tests/server/db/rules.py Thu Dec 17 19:44:49 2015
@@ -7,7 +7,7 @@ import sys
 
 logging.basicConfig(level=logging.DEBUG)
 
-import lnt.server.db.rules as rules
+import lnt.server.db.rules_manager as rules
 
 class RuleProcssingTests(unittest.TestCase):
     """Test the Rules facility."""

Modified: lnt/trunk/tests/server/ui/change_processing.py
URL: http://llvm.org/viewvc/llvm-project/lnt/trunk/tests/server/ui/change_processing.py?rev=255961&r1=255960&r2=255961&view=diff
==============================================================================
--- lnt/trunk/tests/server/ui/change_processing.py (original)
+++ lnt/trunk/tests/server/ui/change_processing.py Thu Dec 17 19:44:49 2015
@@ -15,7 +15,7 @@ import datetime
 from lnt.server.config import Config
 from lnt.server.db import v4db
 from lnt.server.db.fieldchange import is_overlaping, identify_related_changes
-from lnt.server.db.fieldchange import rebuild_title, RegressionState
+from lnt.server.db.regression import rebuild_title, RegressionState
 from lnt.server.db.rules import rule_update_fixed_regressions
 
 logging.basicConfig(level=logging.DEBUG)
@@ -40,14 +40,29 @@ class ChangeProcessingTests(unittest.Tes
 
         start_time = end_time = datetime.datetime.utcnow()
         machine = self.machine = ts_db.Machine("test-machine")
+        ts_db.add(machine)
+        
         test = self.test = ts_db.Test("test-a")
+        ts_db.add(test)
+        
         machine2 = self.machine2 = ts_db.Machine("test-machine2")
+        ts_db.add(machine2)
+        
         test2 = self.test2 = ts_db.Test("test-b")
-
+        ts_db.add(test2)
+        
         run = self.run = ts_db.Run(machine, order1235,  start_time,
                         end_time)
+        ts_db.add(run)
+        
+        run2 = self.run2 = ts_db.Run(machine2, order1235,  start_time,
+                        end_time)
+        ts_db.add(run2)
+        
         sample = ts_db.Sample(run, test, compile_time=1.0,
                               score=4.2)
+        ts_db.add(sample)
+        
         a_field = self.a_field = list(sample.get_primary_fields())[0]
         a_field2 = self.a_field2 = list(sample.get_primary_fields())[1]
 
@@ -58,6 +73,14 @@ class ChangeProcessingTests(unittest.Tes
                                          a_field)
         ts_db.add(field_change)
 
+        fc_mach2 = ts_db.FieldChange(order1234,
+                                         order1236,
+                                         machine2,
+                                         test,
+                                         a_field)
+        ts_db.add(fc_mach2)
+
+
         field_change2 = self.field_change2 = ts_db.FieldChange(order1235, order1236, machine,
                                           test,
                                           a_field)
@@ -82,7 +105,8 @@ class ChangeProcessingTests(unittest.Tes
 
         # All the regressions we detected.
         self.regressions = [regression]
-        
+        ts_db.commit()
+
     def tearDown(self):
         self.db.close_engine()
 




More information about the llvm-commits mailing list