diff --git a/OpenBench/utils.py b/OpenBench/utils.py index 9f6dd5b..304cc82 100644 --- a/OpenBench/utils.py +++ b/OpenBench/utils.py @@ -43,6 +43,83 @@ from OpenBench.stats import SPRT from OpenBench.views import redirect +class TimeControl(object): + + FIXED_NODES = 'FIXED-NODES' # N= or nodes= + FIXED_DEPTH = 'FIXED-DEPTH' # D= or depth= + FIXED_TIME = 'FIXED-TIME' # MT= or movetime= + CYCLIC = 'CYCLIC' # X/Y or X/Y+Z + FISCHER = 'FISCHER' # Y or Y+Z + + @staticmethod + def parse(time_str): + + # Display Nodes as N=, Depth as D=, MoveTime as MT= + conversion = { + 'N' : 'N', 'nodes' : 'N', + 'D' : 'D', 'depth' : 'D', + 'MT' : 'MT', 'movetime' : 'MT', + } + + # Searching for "nodes=", "depth=", and "movetime=" time controls + pattern = '(?P((N)|(D)|(MT)|(nodes)|(depth)|(movetime)))=(?P(\d+))' + if results := re.search(pattern, time_str.upper()): + mode, value = results.group('mode', 'value') + return '%s=%s' % (conversion[mode], value) + + # Searching for "X/Y+Z" time controls, where "X/" is optional + pattern = '(?P(\d+/)?)(?P\d*(\.\d+)?)(?P\+(\d+\.)?\d+)?' + if results := re.search(pattern, time_str): + moves, base, inc = results.group('moves', 'base', 'inc') + + # Strip the trailing and leading symbols + moves = None if moves == '' else moves.rstrip('/') + inc = 0.0 if inc is None else inc.lstrip('+') + + # Format the time control for cutechess cleanly + if moves is None: return '%.1f+%.2f' % (float(base), float(inc)) + return '%d/%.1f+%.2f' % (int(moves), float(base), float(inc)) + + print ('FAIL') + import sys + sys.stdout.flush() + + raise Exception('Unable to parse Time Control (%s)' % (time_str)) + + @staticmethod + def control_type(time_str): + + # Return one of the types defined at the top of TimeControl + + if time_str.startswith('N='): + return TimeControl.FIXED_NODES + + if time_str.startswith('D='): + return TimeControl.FIXED_DEPTH + + if time_str.startswith('MT='): + return TimeControl.FIXED_TIME + + if '/' in time_str: + return TimeControl.CYCLIC + + return TimeControl.FISCHER + + @staticmethod + def control_base(time_str): + + # Fixed-nodes, Fixed-depth, Fixed-time + if '=' in time_str: + return int(time_str.split('=')[1]) + + # Cyclic + if '/' in time_str: + return float(time_str.split('/')[0]) + + # Fischer or Sudden Death otherwise + return float(time_str.split('+')[0]) + + def path_join(*args): return "/".join([f.lstrip("/").rstrip("/") for f in args]).rstrip('/') @@ -57,36 +134,6 @@ def extract_option(options, option): match = re.search('(?<={0}=)[^ ]*'.format(option), options) if match: return match.group() -def parse_time_control(time_control): - - # Display Nodes as N=, Depth as D=, MoveTime as MT= - conversion = { - 'N' : 'N', 'nodes' : 'N', - 'D' : 'D', 'depth' : 'D', - 'MT' : 'MT', 'movetime' : 'MT', - } - - # Searching for "nodes=", "depth=", and "movetime=" Time Controls - pattern = '(?P((N)|(D)|(MT)|(nodes)|(depth)|(movetime)))=(?P(\d+))' - if results := re.search(pattern, time_control.upper()): - mode, value = results.group('mode', 'value') - return '%s=%s' % (conversion[mode], value) - - # Searching for "X/Y+Z" time controls - pattern = '(?P(\d+/)?)(?P\d*(\.\d+)?)(?P\+(\d+\.)?\d+)?' - if results := re.search(pattern, time_control): - moves, base, inc = results.group('moves', 'base', 'inc') - - # Strip the trailing and leading symbols - moves = None if moves == '' else moves.rstrip('/') - inc = 0.0 if inc is None else inc.lstrip('+') - - # Format the time control for cutechess - if moves is None: return '%.1f+%.2f' % (float(base), float(inc)) - return '%d/%.1f+%.2f' % (int(moves), float(base), float(inc)) - - raise Exception('Unable to parse Time Control (%s)' % (time_control)) - def get_pending_tests(): t = Test.objects.filter(approved=False) @@ -328,8 +375,8 @@ def verify_test_creation(request): except: errors.append('{0} was not found in the configuration'.format(field_name)) def verify_time_control(field, field_name): - try: parse_time_control(request.POST[field]) - except: errors.append('{0} is not a parsable'.format(field_name)) + try: TimeControl.parse(request.POST[field]) + except: errors.append('{0} is not parsable'.format(field_name)) def verify_win_adj(field): try: @@ -451,14 +498,14 @@ def create_new_test(request): test.dev_engine = request.POST['dev_engine'] test.dev_options = request.POST['dev_options'] test.dev_network = request.POST['dev_network'] - test.dev_time_control = parse_time_control(request.POST['dev_time_control']) + test.dev_time_control = TimeControl.parse(request.POST['dev_time_control']) test.base = getEngine(*baseinfo) test.base_repo = request.POST['base_repo'] test.base_engine = request.POST['base_engine'] test.base_options = request.POST['base_options'] test.base_network = request.POST['base_network'] - test.base_time_control = parse_time_control(request.POST['base_time_control']) + test.base_time_control = TimeControl.parse(request.POST['base_time_control']) test.report_rate = int(request.POST['report_rate']) test.workload_size = int(request.POST['workload_size']) diff --git a/OpenBench/views.py b/OpenBench/views.py index 2c0f7d4..277298b 100644 --- a/OpenBench/views.py +++ b/OpenBench/views.py @@ -33,7 +33,7 @@ from OpenBench.models import * from django.contrib.auth.models import User from OpenSite.settings import MEDIA_ROOT -from django.db.models import F +from django.db.models import F, Q from django.http import HttpResponse, JsonResponse from django.views.decorators.csrf import csrf_exempt from django.core.files.storage import FileSystemStorage @@ -46,7 +46,7 @@ from django.core.files.base import ContentFile class UnableToAuthenticate(Exception): pass -def render(request, template, content={}, always_allow=False): +def render(request, template, content={}, always_allow=False, error=None, status=None): data = content.copy() data.update({ 'config' : OPENBENCH_CONFIG }) @@ -66,6 +66,12 @@ def render(request, template, content={}, always_allow=False): elif request.user.is_authenticated and not profile.first(): request.session['error_message'] = data['config']['error']['fakeuser'] + if error: + request.session['error_message'] = error + + if status: + request.session['status_message'] = status + response = django.shortcuts.render(request, 'OpenBench/{0}'.format(template), data) for key in ['status_message', 'error_message']: @@ -276,36 +282,101 @@ def search(request): return render(request, 'search.html', {}) tests = Test.objects.all() - keywords = request.POST['keywords'].upper().split() - if not keywords: keywords = [""] - if request.POST['engine'] != '': - tests = tests.filter(engine=request.POST['engine']) + # Optional Selection box filters - if request.POST['author'] != '': + if request.POST['author']: tests = tests.filter(author=request.POST['author']) - if request.POST['showgreens'] == 'False': - tests = tests.exclude(passed=True) + if request.POST['engine']: + tests = tests.filter(Q(base_engine=request.POST['engine']) | Q(dev_engine=request.POST['engine'])) - if request.POST['showyellows'] == 'False': + if request.POST['opening-book']: + tests = tests.filter(book_name=request.POST['opening-book']) + + if request.POST['test-mode']: + tests = tests.filter(test_mode=request.POST['test-mode']) + + if request.POST['syzygy-wdl']: + tests = tests.filter(syzygy_wdl=request.POST['syzygy-wdl']) + + # Checkboxes for Test statuses + + if 'show-greens' not in request.POST: + tests = tests.annotate(x=F('elolower') + F('eloupper')).exclude(x__gte=0, passed=True) + + if 'show-yellows' not in request.POST: tests = tests.exclude(failed=True, wins__gte=F('losses')) - if request.POST['showreds'] == 'False': + if 'show-reds' not in request.POST: tests = tests.exclude(failed=True, wins__lt=F('losses')) - if request.POST['showunfinished'] == 'False': + if 'show-blues' not in request.POST: + tests = tests.annotate(x=F('elolower') + F('eloupper')).exclude(x__lt=0, passed=True) + + if 'show-stopped' not in request.POST: tests = tests.exclude(passed=False, failed=False) - if request.POST['showdeleted'] == 'False': + if 'show-deleted' not in request.POST: tests = tests.exclude(deleted=True) - filtered = [ - test for test in tests.order_by('-updated') if - any(keyword in test.dev.name.upper() for keyword in keywords) - ] + # Remaining filtering is hard to do with standard Django queries - return render(request, 'search.html', {'tests' : filtered}) + filtered = [] + keywords = request.POST['keywords'].upper().split() + + tc_type = request.POST['tc-type'] + tc_value = request.POST['tc-value-input'] + tc_select = request.POST['tc-value-select'] + + # Attempt to parse the time control + + try: + if tc_value: + tc_value = OpenBench.utils.TimeControl.parse(tc_value) + except: + return redirect(request, '/search/', error='Invalid Time Control') + + # Filter out tests + + for test in tests: + + # None of the keywords appear in the dev branch name + if keywords and not any(x in test.dev.name.upper() for x in keywords): + continue + + # Determine the max number of threads that either engine used + dev_threads = OpenBench.utils.extract_option(test.dev_options, 'Threads') + base_threads = OpenBench.utils.extract_option(test.base_options, 'Threads') + max_threads = max(int(dev_threads), int(base_threads)) + + # Extract requsted configuration + select_value = request.POST['threads-select'] + input_value = int(request.POST['threads-input']) + + # Requested Threads value did not match observed value + if select_value == '=' and max_threads != input_value: continue + if select_value == '>=' and max_threads < input_value: continue + if select_value == '<=' and max_threads > input_value: continue + + # Filter our undesired time control types + if tc_type and tc_type != OpenBench.utils.TimeControl.control_type(test.dev_time_control): + continue + + # Filter tests of the same time control type, but outside our range + if tc_value: + + search_base = OpenBench.utils.TimeControl.control_base(tc_value) + test_base = OpenBench.utils.TimeControl.control_base(test.dev_time_control) + + if tc_select == '=' and search_base != test_base: continue + if tc_select == '>=' and search_base > test_base: continue + if tc_select == '<=' and search_base < test_base: continue + + filtered.append(test) + + error = 'No matching tests found' if not len(filtered) else None + return render(request, 'search.html', { 'tests' : filtered }, error=error) # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # GENERAL DATA TABLE VIEWS # diff --git a/Templates/OpenBench/search.html b/Templates/OpenBench/search.html index 5d2be87..06e7078 100644 --- a/Templates/OpenBench/search.html +++ b/Templates/OpenBench/search.html @@ -2,84 +2,150 @@ {% block scripts %} - function set_default_engine() { - {% if profile.engine %} - document.getElementById("engine").value = "{{profile.engine}}"; - {% endif %} - } + {% endblock %} {% block content %} - {% if not tests %} -
{% csrf_token %} -
-
+ +
-
+ +

+
- - + + {% for name, data in config.books.items %} + + {% endfor %}
- - + + +
- - + + + + + + +
- - + + + + + +
- -
+ + +
+
+
+ + +
+
+
+
+
+
+ + +
+
+ + +
+
+
+
+ + +
+
+ + +
+
+
+
+ + +
+
+ + +
+
- {% endif %} @@ -87,5 +153,4 @@ {% include "OpenBench/Blocks/testsummary.html" %} {% endfor %}
- {% endblock %}