|
1 | 1 | """Logic to find all tests, their progress and details of individual test.""" |
2 | 2 |
|
3 | 3 | import os |
4 | | -from datetime import datetime |
5 | 4 | from typing import Any, Dict, List |
6 | 5 |
|
7 | 6 | from flask import (Blueprint, Response, abort, g, jsonify, redirect, request, |
8 | 7 | url_for) |
9 | | -from sqlalchemy import and_, func |
10 | | -from sqlalchemy.sql import label |
| 8 | +from sqlalchemy import and_ |
11 | 9 |
|
12 | 10 | from decorators import template_renderer |
13 | 11 | from exceptions import TestNotFoundException |
14 | 12 | from mod_auth.controllers import check_access_rights, login_required |
15 | 13 | from mod_auth.models import Role |
16 | | -from mod_ci.models import GcpInstance |
17 | 14 | from mod_customized.models import TestFork |
18 | 15 | from mod_home.models import CCExtractorVersion, GeneralData |
19 | 16 | from mod_regression.models import (Category, RegressionTestOutput, |
@@ -133,58 +130,44 @@ def get_data_for_test(test, title=None) -> Dict[str, Any]: |
133 | 130 | if title is None: |
134 | 131 | title = f"test {test.id}" |
135 | 132 |
|
136 | | - hours = 0.00 |
137 | | - minutes = 0.00 |
138 | | - queued_tests = 0 |
139 | | - |
140 | | - """ |
141 | | - evaluating estimated time if the test is still in queue |
142 | | - estimated time = (number of tests already in queue + 1) * (average time of that platform) |
143 | | - - (time already spend by those tests) |
144 | | - calculates time in minutes and hours |
145 | | - """ |
| 133 | + # Calculate average runtime for this platform (used when test hasn't started yet) |
| 134 | + avg_minutes = 0 |
146 | 135 | if len(test.progress) == 0: |
147 | | - var_average = 'average_time_' + test.platform.value |
148 | | - |
149 | | - # get average build and prep time. |
150 | | - prep_average_key = 'avg_prep_time_' + test.platform.value |
151 | | - average_prep_time = int(float(GeneralData.query.filter(GeneralData.key == prep_average_key).first().value)) |
152 | | - |
153 | | - test_progress_last_entry = g.db.query(func.max(TestProgress.test_id)).first() |
154 | | - last_test_id = test_progress_last_entry[0] if test_progress_last_entry is not None else 0 |
155 | | - queued_gcp_instance = g.db.query(GcpInstance.test_id).filter(GcpInstance.test_id < test.id).subquery() |
156 | | - queued_gcp_instance_entries = g.db.query(Test.id).filter( |
157 | | - and_(Test.id.in_(queued_gcp_instance), Test.platform == test.platform) |
158 | | - ).subquery() |
159 | | - gcp_instance_test = g.db.query(TestProgress.test_id, label('time', func.group_concat( |
160 | | - TestProgress.timestamp))).filter(TestProgress.test_id.in_(queued_gcp_instance_entries)).group_by( |
161 | | - TestProgress.test_id).all() |
162 | | - number_gcp_instance_test = g.db.query(Test.id).filter( |
163 | | - and_(Test.id > last_test_id, Test.id < test.id, Test.platform == test.platform) |
164 | | - ).count() |
165 | | - average_duration = float(GeneralData.query.filter(GeneralData.key == var_average).first().value) |
166 | | - queued_tests = number_gcp_instance_test |
167 | | - time_run = 0.00 |
168 | | - for pr_test in gcp_instance_test: |
169 | | - timestamps = pr_test.time.split(',') |
170 | | - start = datetime.strptime(timestamps[0], '%Y-%m-%d %H:%M:%S') |
171 | | - end = datetime.strptime(timestamps[-1], '%Y-%m-%d %H:%M:%S') |
172 | | - time_run += (end - start).total_seconds() |
173 | | - # subtracting current running tests |
174 | | - total = average_prep_time + average_duration - time_run |
175 | | - minutes = (total % 3600) // 60 |
176 | | - hours = total // 3600 |
| 136 | + try: |
| 137 | + avg_time_key = 'average_time_' + test.platform.value |
| 138 | + prep_time_key = 'avg_prep_time_' + test.platform.value |
| 139 | + |
| 140 | + avg_time_record = GeneralData.query.filter(GeneralData.key == avg_time_key).first() |
| 141 | + prep_time_record = GeneralData.query.filter(GeneralData.key == prep_time_key).first() |
| 142 | + |
| 143 | + avg_duration = float(avg_time_record.value) if avg_time_record else 0 |
| 144 | + avg_prep = float(prep_time_record.value) if prep_time_record else 0 |
| 145 | + |
| 146 | + # Total average time in minutes |
| 147 | + avg_minutes = int((avg_duration + avg_prep) / 60) |
| 148 | + except (ValueError, AttributeError): |
| 149 | + avg_minutes = 0 |
177 | 150 |
|
178 | 151 | results = get_test_results(test) |
179 | 152 |
|
| 153 | + # Calculate sample progress for initial page load |
| 154 | + completed_samples = len(test.results) |
| 155 | + total_samples = len(test.get_customized_regressiontests()) |
| 156 | + progress_percentage = 0 |
| 157 | + if total_samples > 0: |
| 158 | + progress_percentage = int((completed_samples / total_samples) * 100) |
| 159 | + |
180 | 160 | return { |
181 | 161 | 'test': test, |
182 | 162 | 'TestType': TestType, |
183 | 163 | 'results': results, |
184 | 164 | 'title': title, |
185 | | - 'next': queued_tests, |
186 | | - 'min': minutes, |
187 | | - 'hr': hours |
| 165 | + 'avg_minutes': avg_minutes, |
| 166 | + 'sample_progress': { |
| 167 | + 'current': completed_samples, |
| 168 | + 'total': total_samples, |
| 169 | + 'percentage': progress_percentage |
| 170 | + } |
188 | 171 | } |
189 | 172 |
|
190 | 173 |
|
@@ -212,11 +195,23 @@ def get_json_data(test_id): |
212 | 195 | 'message': entry.message |
213 | 196 | }) |
214 | 197 |
|
| 198 | + # Calculate sample progress from existing TestResult data |
| 199 | + completed_samples = len(test.results) |
| 200 | + total_samples = len(test.get_customized_regressiontests()) |
| 201 | + progress_percentage = 0 |
| 202 | + if total_samples > 0: |
| 203 | + progress_percentage = int((completed_samples / total_samples) * 100) |
| 204 | + |
215 | 205 | return jsonify({ |
216 | 206 | 'status': 'success', |
217 | 207 | 'details': pr_data["progress"], |
218 | 208 | 'complete': test.finished, |
219 | | - 'progress_array': progress_array |
| 209 | + 'progress_array': progress_array, |
| 210 | + 'sample_progress': { |
| 211 | + 'current': completed_samples, |
| 212 | + 'total': total_samples, |
| 213 | + 'percentage': progress_percentage |
| 214 | + } |
220 | 215 | }) |
221 | 216 |
|
222 | 217 |
|
|
0 commit comments