值守: 新增 modules_status.py 汇总合成历史
值守时要一眼看出三件事, 所以这个工具把这三件排在最前面: 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 通过。
This commit is contained in:
@@ -0,0 +1,111 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
"""汇总模块合成的历史, 供值守时快速核对。
|
||||||
|
|
||||||
|
用法: python modules_status.py [--last N] [--json]
|
||||||
|
|
||||||
|
读 ~/lc_mcp/modules/convert_*.json(每轮一份报告)与 convert.log, 输出:
|
||||||
|
时间 / 结果 / 耗时 / 模组变化 / 体力 / 狂气读数
|
||||||
|
|
||||||
|
值守时关心三件事, 所以这三件排在最前面:
|
||||||
|
1. 有没有真的换到模块(而不是一直"体力不足")
|
||||||
|
2. 有没有哪次报"狂气读数发生变化"(那要立刻人工核查)
|
||||||
|
3. 有没有连着失败(说明流程坏了, 不是资源问题)
|
||||||
|
"""
|
||||||
|
import argparse
|
||||||
|
import glob
|
||||||
|
import json
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
|
||||||
|
DIR = os.path.expanduser(os.path.join('~', 'lc_mcp', 'modules'))
|
||||||
|
|
||||||
|
|
||||||
|
def load_reports(d):
|
||||||
|
rows = []
|
||||||
|
for p in sorted(glob.glob(os.path.join(d, 'convert_*.json'))):
|
||||||
|
try:
|
||||||
|
with open(p, encoding='utf-8') as f:
|
||||||
|
r = json.load(f)
|
||||||
|
except (OSError, ValueError):
|
||||||
|
continue # 写了一半的报告不该拖垮汇总
|
||||||
|
r['_file'] = os.path.basename(p)
|
||||||
|
rows.append(r)
|
||||||
|
rows.sort(key=lambda r: r.get('started', ''), reverse=True)
|
||||||
|
return rows
|
||||||
|
|
||||||
|
|
||||||
|
def summarize(rows):
|
||||||
|
out = []
|
||||||
|
for r in rows:
|
||||||
|
steps = r.get('steps') or []
|
||||||
|
conv = steps[0] if steps else {}
|
||||||
|
out.append({
|
||||||
|
'started': r.get('started', '?'),
|
||||||
|
'ok': bool(r.get('ok')),
|
||||||
|
'result': r.get('result', ''),
|
||||||
|
'elapsed_s': r.get('elapsed_s'),
|
||||||
|
'info': conv.get('info', ''),
|
||||||
|
'lunacy_alarm': '狂气读数发生变化' in (r.get('result') or ''),
|
||||||
|
})
|
||||||
|
return out
|
||||||
|
|
||||||
|
|
||||||
|
def fmt_secs(s):
|
||||||
|
if s is None:
|
||||||
|
return '-'
|
||||||
|
if s < 60:
|
||||||
|
return '%ds' % s
|
||||||
|
return '%d:%02d' % (s // 60, s % 60)
|
||||||
|
|
||||||
|
|
||||||
|
def main(argv=None):
|
||||||
|
ap = argparse.ArgumentParser(description='模块合成历史汇总')
|
||||||
|
ap.add_argument('--last', type=int, default=0, help='只看最近 N 条(0=全部)')
|
||||||
|
ap.add_argument('--json', action='store_true')
|
||||||
|
ap.add_argument('--dir', default=DIR)
|
||||||
|
args = ap.parse_args(argv)
|
||||||
|
|
||||||
|
rows = summarize(load_reports(args.dir))
|
||||||
|
if args.last:
|
||||||
|
rows = rows[:args.last]
|
||||||
|
|
||||||
|
if args.json:
|
||||||
|
json.dump(rows, sys.stdout, ensure_ascii=False, indent=2)
|
||||||
|
print()
|
||||||
|
return 0
|
||||||
|
|
||||||
|
if not rows:
|
||||||
|
print('还没有合成记录(%s)' % args.dir)
|
||||||
|
return 0
|
||||||
|
|
||||||
|
got = sum(1 for r in rows if '+1' in r['info'] or '模组' in r['info'] and '->' in r['info'])
|
||||||
|
fails = [r for r in rows if not r['ok']]
|
||||||
|
alarms = [r for r in rows if r['lunacy_alarm']]
|
||||||
|
|
||||||
|
print('共 %d 轮: 换到模块 %d 轮, 失败 %d 轮, 狂气告警 %d 轮'
|
||||||
|
% (len(rows), got, len(fails), len(alarms)))
|
||||||
|
if alarms:
|
||||||
|
print('⚠️ 有狂气告警, 需要人工核查:')
|
||||||
|
for r in alarms:
|
||||||
|
print(' %s %s' % (r['started'], r['result']))
|
||||||
|
# 连着失败(最新几条都不是好结果)要醒目 —— 那说明流程坏了
|
||||||
|
streak = 0
|
||||||
|
for r in rows:
|
||||||
|
if r['ok']:
|
||||||
|
break
|
||||||
|
streak += 1
|
||||||
|
if streak >= 3:
|
||||||
|
print('⚠️ 连续 %d 轮不成功, 可能是流程问题而不是资源不足' % streak)
|
||||||
|
|
||||||
|
print()
|
||||||
|
print('%-19s %-4s %-7s %s' % ('时间', 'OK', '耗时', '结果'))
|
||||||
|
print('-' * 100)
|
||||||
|
for r in rows:
|
||||||
|
print('%-19s %-4s %-7s %s'
|
||||||
|
% (r['started'], '✅' if r['ok'] else '❌', fmt_secs(r['elapsed_s']),
|
||||||
|
(r['result'] or r['info'])[:70]))
|
||||||
|
return 0
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
raise SystemExit(main())
|
||||||
@@ -0,0 +1,63 @@
|
|||||||
|
# -*- 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()
|
||||||
Reference in New Issue
Block a user