# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # UI helpers for the OpenBench website. # # # # Kept out of views.py / utils.py so the presentation logic ( time control # # buckets, PGN and log parsing for the event viewer ) is easy to find, # # test, and extend. Nothing in here touches the database schema. # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # import re from functools import lru_cache from django.db.models import Sum from OpenBench.models import Profile, Result # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # TIME CONTROLS # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # Fischer time controls with a base time below this many seconds are STC, and # anything at or above it is LTC. ( 10+0.1 is STC, 60+0.6 is LTC ) STC_MAX_BASE_SECONDS = 30 BUCKETS = ('STC', 'LTC', 'OTHER') @lru_cache(maxsize=512) def classify_time_control(time_control): # Imported here, since utils.py imports views.py which imports this module from OpenBench.utils import TimeControl try: tc = str(time_control or '') # Fixed nodes, depth, movetime, and cyclic controls are not STC or LTC if TimeControl.control_type(tc) != TimeControl.FISCHER: return 'OTHER' base = TimeControl.control_base(tc) return 'STC' if base < STC_MAX_BASE_SECONDS else 'LTC' except Exception: return 'OTHER' # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # USERS PAGE # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # SORT_KEYS = { 'total' : lambda row: (-row['total'], -row['tests']), 'stc' : lambda row: (-row['stc' ], -row['total']), 'ltc' : lambda row: (-row['ltc' ], -row['total']), 'tests' : lambda row: (-row['tests'], -row['total']), } def _share(part, whole): # Returned as a string, so templates never localise it into "63,20" return '%.2f' % (100.0 * part / whole) if whole else '0.00' def users_context(request): sort = request.GET.get('sort', 'total') if sort not in SORT_KEYS: sort = 'total' # Games are credited to whoever's machines played them, which is not # necessarily the author of the test. Results hold one row per (test, machine) # so grouping by user and time control in the database stays cheap. per_user = {} grouped = Result.objects.values('machine__user__username', 'test__dev_time_control') grouped = grouped.annotate(total=Sum('games')) for entry in grouped: buckets = per_user.setdefault(entry['machine__user__username'], dict.fromkeys(BUCKETS, 0)) buckets[classify_time_control(entry['test__dev_time_control'])] += entry['total'] or 0 rows = [] for profile in Profile.objects.select_related('user'): if not (profile.games or profile.tests or profile.approver): continue buckets = per_user.get(profile.user.username, dict.fromkeys(BUCKETS, 0)) split = sum(buckets.values()) rows.append({ 'username' : profile.user.username, 'approver' : profile.approver, 'engine' : profile.engine, 'joined' : profile.user.date_joined, 'total' : profile.games, 'tests' : profile.tests, 'stc' : buckets['STC' ], 'ltc' : buckets['LTC' ], 'other' : buckets['OTHER'], 'stc_share' : _share(buckets['STC' ], split), 'ltc_share' : _share(buckets['LTC' ], split), 'oth_share' : _share(buckets['OTHER'], split), }) rows.sort(key=lambda row: SORT_KEYS[sort](row) + (row['username'].lower(),)) stc = sum(row['stc' ] for row in rows) ltc = sum(row['ltc' ] for row in rows) other = sum(row['other'] for row in rows) whole = stc + ltc + other summary = { 'users' : len(rows), 'total' : sum(row['total'] for row in rows), 'stc' : stc, 'ltc' : ltc, 'other' : other, 'stc_share' : _share(stc, whole), 'ltc_share' : _share(ltc, whole), 'oth_share' : _share(other, whole), } return { 'rows' : rows, 'summary' : summary, 'sort' : sort, 'show_other' : other > 0, 'stc_limit' : STC_MAX_BASE_SECONDS, } # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # EVENT VIEWER # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # def event_tone(summary): # Colour used for badges: bad ( red ), warn ( amber ), info ( blue ) text = (summary or '').lower() bad = ('illegal', 'disconnect', 'crash', 'fail', 'bad bench', 'missing', 'error') if any(word in text for word in bad): return 'bad' if any(word in text for word in ('stall', 'timeout', 'timeloss', 'warn')): return 'warn' return 'info' # When a worker reports an Illegal Move, Disconnect, or Stalled game, the client # uploads the PGN of that game ( see PGNHelper.pretty_format in Client/worker.py ). # Other events ( build failures, bad benches, ... ) carry plain text logs. PGN_PROBE_RE = re.compile(r'^\s*\[Event\s+"', re.MULTILINE) HEADER_RE = re.compile(r'^\[(\w+)\s+"(.*)"\]\s*$') TOKEN_RE = re.compile(r'\{[^}]*\}|\([^)]*\)|\$\d+|\d+\.(?:\.\.)?|1-0|0-1|1/2-1/2|\*|[^\s{}()]+') NUMBER_RE = re.compile(r'^\d+\.(?:\.\.)?$') EVAL_RE = re.compile(r'^(?P[+-]?(?:M\d+|\d+(?:\.\d+)?))/(?P\d+)(?:\s+(?P