值守时要一眼看出三件事, 所以这个工具把这三件排在最前面: 1. 有没有真的换到模块(而不是一直"体力不足") 2. 有没有哪次报"狂气读数发生变化" —— 那要立刻人工核查 3. 有没有连着失败 —— 那说明流程坏了, 不是资源不足 用法: python scripts/modules_status.py [--last N] [--json] 顺带用实测数据核对了值守节奏是否合理: 09:58 体力 13 -> 10:04 体力 14 -> 10:44 体力 21 即 47 分钟恢复 8 点 = 每 5.82 分钟 1 点, 与"每 6 分钟 1 点"一致。 4 小时间隔正好攒约 41 点 = 2 个模块, 且不会顶到 126 上限浪费恢复量。 新增 tests/test_modules_status.py 5 个用例(排序/坏文件容错/狂气告警识别/耗时格式化)。 全套 255/255 通过。
64 lines
2.4 KiB
Python
64 lines
2.4 KiB
Python
# -*- coding: utf-8 -*-
|
|
"""modules_status 的汇总逻辑。
|
|
|
|
值守时要一眼看出三件事: 有没有真的换到模块、有没有狂气告警、有没有连着失败。
|
|
"""
|
|
import json
|
|
import os
|
|
import sys
|
|
import tempfile
|
|
import unittest
|
|
from pathlib import Path
|
|
|
|
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / 'scripts'))
|
|
import modules_status as ms # noqa: E402
|
|
|
|
|
|
def rep(started, ok, result, info='', elapsed=100):
|
|
return {'started': started, 'ok': ok, 'result': result, 'elapsed_s': elapsed,
|
|
'steps': [{'step': '合成', 'ok': ok, 'info': info}]}
|
|
|
|
|
|
class SummarizeTest(unittest.TestCase):
|
|
|
|
def setUp(self):
|
|
self.d = tempfile.mkdtemp()
|
|
|
|
def write(self, name, obj):
|
|
with open(os.path.join(self.d, name), 'w', encoding='utf-8') as f:
|
|
json.dump(obj, f, ensure_ascii=False)
|
|
|
|
def test_orders_newest_first(self):
|
|
self.write('convert_20260101_010101.json', rep('2026-01-01 01:01:01', True, 'a'))
|
|
self.write('convert_20260102_010101.json', rep('2026-01-02 01:01:01', True, 'b'))
|
|
rows = ms.summarize(ms.load_reports(self.d))
|
|
self.assertEqual([r['started'] for r in rows],
|
|
['2026-01-02 01:01:01', '2026-01-01 01:01:01'])
|
|
|
|
def test_broken_json_skipped(self):
|
|
self.write('convert_20260101_010101.json', rep('2026-01-01 01:01:01', True, 'a'))
|
|
with open(os.path.join(self.d, 'convert_20260103_010101.json'), 'w', encoding='utf-8') as f:
|
|
f.write('{ not json')
|
|
self.assertEqual(len(ms.load_reports(self.d)), 1)
|
|
|
|
def test_lunacy_alarm_is_flagged(self):
|
|
self.write('convert_20260101_010101.json',
|
|
rep('2026-01-01 01:01:01', False, '⚠️ 狂气读数发生变化 9699 -> 6300, 立即停止并人工核查'))
|
|
rows = ms.summarize(ms.load_reports(self.d))
|
|
self.assertTrue(rows[0]['lunacy_alarm'])
|
|
|
|
def test_normal_run_is_not_an_alarm(self):
|
|
self.write('convert_20260101_010101.json',
|
|
rep('2026-01-01 01:01:01', True, '合成完成: 模组 29 -> 30 (+1)', '模组 29 -> 30 (+1)'))
|
|
rows = ms.summarize(ms.load_reports(self.d))
|
|
self.assertFalse(rows[0]['lunacy_alarm'])
|
|
|
|
def test_time_formatting(self):
|
|
self.assertEqual(ms.fmt_secs(36), '36s')
|
|
self.assertEqual(ms.fmt_secs(180), '3:00')
|
|
self.assertEqual(ms.fmt_secs(None), '-')
|
|
|
|
|
|
if __name__ == '__main__':
|
|
unittest.main()
|