fix: 平均值只算本次启动后的记录 + 速度 dm/s→m/s

- 平均值按 automation 开始时间过滤,不再包含历史数据
- 速度单位从 dm/s 转为 m/s(÷10),前端/后端同步转换
This commit is contained in:
wangfq
2026-05-28 14:54:21 +08:00
parent 322563dab0
commit dee27eb3be
8 changed files with 44 additions and 18 deletions

View File

@@ -211,15 +211,22 @@ def get_all_test_data_for_export(serial: str = "", date_from: str = "",
conn.close() conn.close()
def get_automation_averages(dnt_id: int) -> dict: def get_automation_averages(dnt_id: int, since: str = None) -> dict:
"""获取自动化测试的平均值(排除失败记录 state=3""" """获取本次自动化测试的平均值
since: ISO 时间字符串,只统计此时间之后的测试记录
速度从 dm/s 转换为 m/s÷10
"""
conn = get_conn() conn = get_conn()
try: try:
with conn.cursor() as cur: with conn.cursor() as cur:
# 只计算最近一批自动化测试的平均值 where = "dnt_id=%s"
# 取该设备 tb_state_tst 中与 tb_serialnet state=2 对应的记录 params = [dnt_id]
if since:
where += " AND create_time >= %s"
params.append(since)
cur.execute( cur.execute(
"SELECT AVG(ppvalue) as avg_ppvalue, " f"SELECT AVG(ppvalue) as avg_ppvalue, "
"AVG(idle_freq) as avg_idle_freq, " "AVG(idle_freq) as avg_idle_freq, "
"AVG(enter_freq) as avg_enter_freq, " "AVG(enter_freq) as avg_enter_freq, "
"AVG(exit_freq) as avg_exit_freq, " "AVG(exit_freq) as avg_exit_freq, "
@@ -227,14 +234,20 @@ def get_automation_averages(dnt_id: int) -> dict:
"AVG(exit_dist) as avg_exit_dist, " "AVG(exit_dist) as avg_exit_dist, "
"AVG(enter_speed) as avg_enter_speed, " "AVG(enter_speed) as avg_enter_speed, "
"AVG(exit_speed) as avg_exit_speed " "AVG(exit_speed) as avg_exit_speed "
"FROM tb_state_tst WHERE dnt_id=%s", f"FROM tb_state_tst WHERE {where}",
(dnt_id,), params,
) )
row = cur.fetchone() row = cur.fetchone()
finally: finally:
conn.close() conn.close()
if row: if row:
return {k: round(v, 2) if v else 0 for k, v in row.items()} result = {k: round(v, 2) if v else 0 for k, v in row.items()}
# 速度 dm/s → m/s
if result.get("avg_enter_speed"):
result["avg_enter_speed"] = round(result["avg_enter_speed"] / 10, 2)
if result.get("avg_exit_speed"):
result["avg_exit_speed"] = round(result["avg_exit_speed"] / 10, 2)
return result
return {} return {}

View File

@@ -112,9 +112,10 @@ def api_automation_start():
@login_required @login_required
def api_automation_progress(dnt_id): def api_automation_progress(dnt_id):
"""获取自动化进度""" """获取自动化进度"""
since = request.args.get("since", "", type=str)
stats = get_serialnet_stats(dnt_id) stats = get_serialnet_stats(dnt_id)
latest = get_latest_test_state(dnt_id) latest = get_latest_test_state(dnt_id)
averages = get_automation_averages(dnt_id) averages = get_automation_averages(dnt_id, since if since else None)
return jsonify({ return jsonify({
"stats": stats, "stats": stats,
"latest": latest, "latest": latest,

View File

@@ -1,5 +1,10 @@
// 测试信息页 // 测试信息页
function toSpeed(v) {
if (v === null || v === undefined || v === '') return '-';
return (parseFloat(v) / 10).toFixed(1);
}
let currentPage = 1; let currentPage = 1;
let totalPages = 1; let totalPages = 1;
@@ -47,8 +52,8 @@ function renderTable(records) {
<td>${r.exit_freq || '-'}</td> <td>${r.exit_freq || '-'}</td>
<td>${r.enter_dist || '-'}</td> <td>${r.enter_dist || '-'}</td>
<td>${r.exit_dist || '-'}</td> <td>${r.exit_dist || '-'}</td>
<td>${r.enter_speed || '-'}</td> <td>${toSpeed(r.enter_speed)}</td>
<td>${r.exit_speed || '-'}</td> <td>${toSpeed(r.exit_speed)}</td>
<td>${r.create_time || '-'}</td> <td>${r.create_time || '-'}</td>
</tr> </tr>
`).join(""); `).join("");

View File

@@ -5,6 +5,7 @@ let autoTotal = 0;
let autoDone = 0; let autoDone = 0;
let autoFailed = 0; let autoFailed = 0;
let autoRemaining = 0; let autoRemaining = 0;
let autoStartTime = "";
let pollInterval = null; let pollInterval = null;
let timeoutTimers = {}; // record_id → timer let timeoutTimers = {}; // record_id → timer
const TIMEOUT_MS = 10000; const TIMEOUT_MS = 10000;
@@ -47,6 +48,7 @@ async function startAuto() {
autoDone = 0; autoDone = 0;
autoFailed = 0; autoFailed = 0;
autoRemaining = count; autoRemaining = count;
autoStartTime = new Date().toISOString(); // 记录开始时间
// 清空显示 // 清空显示
resetAverages(); resetAverages();
@@ -101,7 +103,7 @@ async function pollProgress() {
if (!autoRunning) return; if (!autoRunning) return;
try { try {
const resp = await fetch(`/api/automation/${DNT_ID}/progress`); const resp = await fetch(`/api/automation/${DNT_ID}/progress?since=${encodeURIComponent(autoStartTime)}`);
const data = await resp.json(); const data = await resp.json();
const stats = data.stats; const stats = data.stats;
@@ -171,6 +173,11 @@ function updateUI() {
`${autoDone}/${autoTotal} (${autoFailed} 失败)`; `${autoDone}/${autoTotal} (${autoFailed} 失败)`;
} }
function toSpeed(v) {
if (v === null || v === undefined || v === '') return '-';
return (parseFloat(v) / 10).toFixed(1);
}
// ─── 显示最新结果 ────────────────────────────── // ─── 显示最新结果 ──────────────────────────────
function renderLatest(data) { function renderLatest(data) {
@@ -183,8 +190,8 @@ function renderLatest(data) {
<p>离开工作频率:${data.exit_freq || '-'} Hz</p> <p>离开工作频率:${data.exit_freq || '-'} Hz</p>
<p>进入距离:${data.enter_dist || '-'} mm</p> <p>进入距离:${data.enter_dist || '-'} mm</p>
<p>离开距离:${data.exit_dist || '-'} mm</p> <p>离开距离:${data.exit_dist || '-'} mm</p>
<p>进入速度:${data.enter_speed || '-'} dm/s</p> <p>进入速度:${toSpeed(data.enter_speed)} m/s</p>
<p>离开速度:${data.exit_speed || '-'} dm/s</p> <p>离开速度:${toSpeed(data.exit_speed)} m/s</p>
<p>是否完成:${data.iffinish === '1' ? '是' : '否'}</p> <p>是否完成:${data.iffinish === '1' ? '是' : '否'}</p>
<p>故障信息:${data.fault_info || '无'}</p> <p>故障信息:${data.fault_info || '无'}</p>
<p>时间:${data.create_time || '-'}</p> <p>时间:${data.create_time || '-'}</p>

View File

@@ -36,8 +36,8 @@
<th>离开频率(Hz)</th> <th>离开频率(Hz)</th>
<th>进入距离(mm)</th> <th>进入距离(mm)</th>
<th>离开距离(mm)</th> <th>离开距离(mm)</th>
<th>进入速度(dm/s)</th> <th>进入速度(m/s)</th>
<th>离开速度(dm/s)</th> <th>离开速度(m/s)</th>
<th>时间</th> <th>时间</th>
</tr> </tr>
</thead> </thead>

View File

@@ -53,8 +53,8 @@
<tr><td>平均进入工作频率</td><td id="avg-enter-freq">-</td><td>Hz</td></tr> <tr><td>平均进入工作频率</td><td id="avg-enter-freq">-</td><td>Hz</td></tr>
<tr><td>平均进入距离</td><td id="avg-enter-dist">-</td><td>mm</td></tr> <tr><td>平均进入距离</td><td id="avg-enter-dist">-</td><td>mm</td></tr>
<tr><td>平均离开距离</td><td id="avg-exit-dist">-</td><td>mm</td></tr> <tr><td>平均离开距离</td><td id="avg-exit-dist">-</td><td>mm</td></tr>
<tr><td>平均进入速度</td><td id="avg-enter-speed">-</td><td>dm/s</td></tr> <tr><td>平均进入速度</td><td id="avg-enter-speed">-</td><td>m/s</td></tr>
<tr><td>平均离开速度</td><td id="avg-exit-speed">-</td><td>dm/s</td></tr> <tr><td>平均离开速度</td><td id="avg-exit-speed">-</td><td>m/s</td></tr>
</table> </table>
</div> </div>
</div> </div>