From 5f1cc2795453c61cef899524c1167f487c2f57a7 Mon Sep 17 00:00:00 2001 From: Alexandre Aubin Date: Tue, 22 Jan 2019 16:07:00 +0000 Subject: [PATCH] Updates were registering lot of duplicate tests, causing perf issues --- app/models/appci.py | 34 ++++++++++++++++++++++------------ 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/app/models/appci.py b/app/models/appci.py index bfb8cb2..10c2dfa 100644 --- a/app/models/appci.py +++ b/app/models/appci.py @@ -39,14 +39,15 @@ class AppCIBranch(db.Model): def most_recent_tests_per_app(self): apps = App.query.filter_by(ci_enabled=True).all() + most_recent_tests = AppCIResult.query \ + .filter_by(branch = self) \ + .order_by('date desc') \ + .all() + for app in apps: - most_recent_test = AppCIResult.query \ - .filter_by(branch = self) \ - .filter_by(app = app) \ - .order_by('date desc') \ - .first() + most_recent_test = [ t for t in most_recent_tests if t.app == app ] if most_recent_test: - yield most_recent_test + yield most_recent_test[0] else: yield AppCIResult(app = app, branch = self, @@ -122,11 +123,20 @@ class AppCI(): for test, result in zip(AppCI.tests, test_summary["detailled_success"]): test_results[test] = bool(int(result)) if result in [ "1", "0" ] else None - results = AppCIResult(app = App.query.filter_by(name=test_summary["app"]).first(), - branch = cibranch, - level = test_summary["level"], - date = datetime.datetime.fromtimestamp(test_summary["timestamp"]), - results = test_results) - db.session.add(results) + app = App.query.filter_by(name=test_summary["app"]).first() + date = datetime.datetime.fromtimestamp(test_summary["timestamp"]) + + existing_test = AppCIResult.query \ + .filter_by(branch = cibranch) \ + .filter_by(date = date) \ + .first() + + if not existing_test: + results = AppCIResult(app = app, + branch = cibranch, + level = test_summary["level"], + date = date, + results = test_results) + db.session.add(results) db.session.commit()