diff --git a/OpenBench/pipeline_views.py b/OpenBench/pipeline_views.py index 9f3dce6..65ecec5 100644 --- a/OpenBench/pipeline_views.py +++ b/OpenBench/pipeline_views.py @@ -35,6 +35,10 @@ import OpenBench.pipeline_layout as drawing import OpenBench.pipelines import OpenBench.views +import copy +import json + +from django.http import JsonResponse from django.views.decorators.http import require_POST from OpenBench.models import LogEvent, Pipeline, PipelineRun, Profile, StepRun, Test @@ -381,3 +385,206 @@ def pipeline_run_cancel(request, run_id): return OpenBench.views.redirect(request, '/pipelines/run/%d/' % (run.id), error='This run is already over') return OpenBench.views.redirect(request, '/pipelines/run/%d/' % (run.id), status='Run cancelled') + + +# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # +# EDITING # +# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # + +# Anybody with an enabled account may make a Pipeline, just like they may make a +# Test. Changing one later is the same can_manage() rule as everything else. +# The editor page builds the graph in the browser. Whatever it sends is never +# trusted: it is validated here by exactly the code that validates a run. + +MAX_GRAPH_BYTES = 262144 + +_GENERAL_DEFAULTS = { + 'upload_pgns' : 'FALSE', 'priority' : '0', 'throughput' : '1000', + 'syzygy_wdl' : 'OPTIONAL', 'syzygy_adj' : 'OPTIONAL', + 'win_adj' : 'movecount=3 score=400', + 'draw_adj' : 'movenumber=40 movecount=8 score=10', +} + +PAYLOAD_DEFAULTS = { + 'TEST' : dict(_GENERAL_DEFAULTS, workload_size='32', test_mode='SPRT', test_bounds='[0.00, 3.00]', test_confidence='[0.05, 0.05]'), + 'TUNE' : dict(_GENERAL_DEFAULTS), + 'DATAGEN' : dict(_GENERAL_DEFAULTS, workload_size='32'), +} + +def _blank_graph(): + return { + 'version' : graphs.SCHEMA_VERSION, + 'start' : 'step1', + 'max_visits' : graphs.DEFAULT_MAX_VISITS, + 'max_steps' : graphs.DEFAULT_MAX_STEPS, + 'steps' : { 'step1' : { 'name' : 'Step 1', 'kind' : 'TEST', 'payload' : dict(PAYLOAD_DEFAULTS['TEST']) } }, + 'edges' : [], + } + +def _parse_graph(raw): + + if not isinstance(raw, str) or len(raw) > MAX_GRAPH_BYTES: + return None + + try: graph = json.loads(raw) + except ValueError: return None + + return graph if isinstance(graph, dict) else None + +def _tidy(graph): + keys = ('version', 'start', 'max_visits', 'max_steps', 'steps', 'edges') + return { key : graph[key] for key in keys if key in graph } + +def _check(graph): + + # validate_graph() is built for garbage, but a page that talks to the browser + # should never answer with a 500 because of what the browser sent + try: return graphs.validate_graph(graph) + except Exception: return [graphs.issue('error', 'graph.invalid', 'The pipeline is not valid')] + +def _named(issues, graph): + + steps = graph.get('steps') if isinstance(graph, dict) else None + steps = steps if isinstance(steps, dict) else {} + + named = [] + for entry in issues: + step = steps.get(entry['step']) if isinstance(entry['step'], str) else None + name = step.get('name') if isinstance(step, dict) else None + named.append(dict(entry, name=name or entry['step'])) + + return named + +def _editor(request, pipeline, name, graph, issues=None, error=None): + + data = { + 'pipeline' : pipeline, + 'name' : name, + 'graph' : graph, + 'issues' : _named(issues or [], graph), + 'meta' : { + 'kinds' : list(graphs.KINDS), + 'outcomes' : list(graphs.EDGE_CONDITIONS), + 'required' : graphs.REQUIRED_FIELDS, + 'conditional' : graphs.CONDITIONAL_FIELDS, + 'allowed' : sorted(graphs.ALLOWED_FIELDS), + 'defaults' : PAYLOAD_DEFAULTS, + 'limits' : { 'steps' : graphs.LIMIT_STEPS, 'visits' : graphs.LIMIT_MAX_VISITS, 'total' : graphs.LIMIT_MAX_STEPS }, + }, + } + + return OpenBench.views.render(request, 'pipeline_edit.html', data, error=error) + +def _submit(request, profile, found): + + """ Shared by creating and editing. Saves, or shows the editor again with what was wrong """ + + name = (request.POST.get('name') or '').strip() + graph = _parse_graph(request.POST.get('graph')) + errors = [] + + if not (1 <= len(name) <= 128): + errors.append('Give the pipeline a name of up to 128 characters') + + if graph is None: + errors.append('The pipeline could not be read. Reload the page and try again') + return _editor(request, found, name, _blank_graph(), error='\n'.join(errors)) + + issues = _check(graph) + + if graphs.has_errors(issues): + errors.append('The pipeline has problems, see the checks below') + + if errors: + return _editor(request, found, name, graph, issues, error='\n'.join(errors)) + + graph = _tidy(graph) + + if found is None: + found = Pipeline.objects.create(name=name, author=profile.user.username, graph=graph) + action = 'CREATED' + else: + found.name = name + found.graph = graph + found.save() + action = 'EDITED' + + _audit(profile.user.username, 'PIPELINE #%d %s' % (found.id, action)) + return OpenBench.views.redirect(request, '/pipelines/%d/' % (found.id), status='Pipeline saved') + +def pipeline_new(request): + + profile = viewer(request) + if profile is None: + return _turn_away(request, profile) + + if request.method != 'POST': + return _editor(request, None, '', _blank_graph()) + + return _submit(request, profile, None) + +def pipeline_edit(request, id): + + profile = viewer(request) + if profile is None: + return _turn_away(request, profile) + + found = Pipeline.objects.filter(id=id, deleted=False).first() + if found is None: + return _missing(request) + + if not can_manage(profile, found): + return _turn_away(request, profile, id, 'edit') + + # Runs that are going work from a snapshot of the graph, so this is safe at any time + if request.method != 'POST': + return _editor(request, found, found.name, copy.deepcopy(found.graph)) + + return _submit(request, profile, found) + +@require_POST +def pipeline_check(request): + + """ Validates a graph without saving it, for the Check button of the editor """ + + if viewer(request) is None: + return JsonResponse({ 'error' : 'Not allowed' }, status=403) + + graph = _parse_graph(request.POST.get('graph')) + if graph is None: + return JsonResponse({ 'issues' : [graphs.issue('error', 'graph.invalid', 'The pipeline could not be read')] }) + + return JsonResponse({ 'issues' : _check(graph) }) + +def pipeline_test_fields(request, test_id): + + """ The settings of an existing SPRT or Games test, as the settings of a step """ + + if viewer(request) is None: + return JsonResponse({ 'error' : 'Not allowed' }, status=403) + + test = Test.objects.select_related('dev', 'base').filter(id=test_id).first() + if test is None or test.test_mode not in ('SPRT', 'GAMES'): + return JsonResponse({ 'error' : 'There is no SPRT or Games test with that number' }, status=404) + + fields = { + 'dev_engine' : test.dev_engine, 'base_engine' : test.base_engine, + 'dev_repo' : test.dev_repo, 'base_repo' : test.base_repo, + 'dev_branch' : test.dev.name, 'base_branch' : test.base.name, + 'dev_network' : test.dev_network, 'base_network' : test.base_network, + 'dev_options' : test.dev_options, 'base_options' : test.base_options, + 'dev_time_control' : test.dev_time_control, 'base_time_control' : test.base_time_control, + 'book_name' : test.book_name, 'upload_pgns' : test.upload_pgns, + 'priority' : test.priority, 'throughput' : test.throughput, + 'workload_size' : test.workload_size, 'test_mode' : test.test_mode, + 'syzygy_wdl' : test.syzygy_wdl, 'syzygy_adj' : test.syzygy_adj, + 'win_adj' : test.win_adj, 'draw_adj' : test.draw_adj, + } + + if test.test_mode == 'SPRT': + fields['test_bounds'] = '[%.2f, %.2f]' % (test.elolower, test.eloupper) + fields['test_confidence'] = '[%.2f, %.2f]' % (test.beta, test.alpha) + else: + fields['test_max_games'] = test.max_games + + return JsonResponse({ 'payload' : { key : str(value) for key, value in fields.items() } }) diff --git a/OpenBench/urls.py b/OpenBench/urls.py index eb86e8d..9478fb7 100644 --- a/OpenBench/urls.py +++ b/OpenBench/urls.py @@ -48,6 +48,10 @@ urlpatterns = [ django.urls.path(r'pipelines//start/', OpenBench.pipeline_views.pipeline_start), django.urls.path(r'pipelines//toggle/', OpenBench.pipeline_views.pipeline_toggle), django.urls.path(r'pipelines//delete/', OpenBench.pipeline_views.pipeline_delete), + django.urls.path(r'pipelines//edit/', OpenBench.pipeline_views.pipeline_edit), + django.urls.path(r'pipelines/new/', OpenBench.pipeline_views.pipeline_new), + django.urls.path(r'pipelines/check/', OpenBench.pipeline_views.pipeline_check), + django.urls.path(r'pipelines/test-fields//', OpenBench.pipeline_views.pipeline_test_fields), django.urls.path(r'pipelines/run//', OpenBench.pipeline_views.pipeline_run), django.urls.path(r'pipelines/run//cancel/', OpenBench.pipeline_views.pipeline_run_cancel), diff --git a/Templates/OpenBench/pipeline.html b/Templates/OpenBench/pipeline.html index 771cf79..280d611 100644 --- a/Templates/OpenBench/pipeline.html +++ b/Templates/OpenBench/pipeline.html @@ -25,6 +25,8 @@ title="{% if usable %}Resume the pipeline to start it{% else %}Fix the pipeline settings first{% endif %}">Start run {% endif %} + Edit +
{% csrf_token %} @@ -54,7 +56,7 @@ {% endif %} diff --git a/Templates/OpenBench/pipeline_edit.html b/Templates/OpenBench/pipeline_edit.html new file mode 100644 index 0000000..b741075 --- /dev/null +++ b/Templates/OpenBench/pipeline_edit.html @@ -0,0 +1,453 @@ +{% extends "OpenBench/base.html" %} + +{% block page_head %} +
+ {% if pipeline %}{{ pipeline.name }}{% else %}Pipelines{% endif %} +

{% if pipeline %}Edit pipeline{% else %}New pipeline{% endif %}

+

Steps run one after another. How a step ends decides which step comes next

+
+{% endblock %} + +{% block content %} + + + + + {% csrf_token %} + + +
+

General

+
+
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+
+
+ +
+
+

Steps

+ +
+
+
+ +
+
+

What happens next

+ +
+
+
+ +
+
+

Checks

+ +
+
    + {% for issue in issues %} +
  • + + {% if issue.name %}{{ issue.name }} {% endif %}{{ issue.message }} +
  • + {% endfor %} +
+
+ +
+ + Cancel +
+
+ + + + {{ graph|json_script:"graph-data" }} + {{ meta|json_script:"meta-data" }} + + + +{% endblock %} diff --git a/Templates/OpenBench/pipelines.html b/Templates/OpenBench/pipelines.html index 9b724fd..ddecb16 100644 --- a/Templates/OpenBench/pipelines.html +++ b/Templates/OpenBench/pipelines.html @@ -5,6 +5,10 @@

Pipelines

Run a test, then let its result decide what runs next

+ + {% endblock %} {% block content %} @@ -63,7 +67,7 @@

No pipelines yet.

-

A pipeline starts a test and picks the next one from the result. An administrator adds them on the admin site for now.

+

A pipeline starts a test and picks the next one from the result. Use New pipeline to create the first one.