From e8025ecb98d83e68e1d649c1fc77bbfd23e81d64 Mon Sep 17 00:00:00 2001 From: AndyGrant Date: Sun, 10 Jun 2018 02:15:05 -0400 Subject: [PATCH] Add Create Test --- OpenBench/config.py | 33 ++++++----- OpenBench/models.py | 18 +++--- OpenBench/static/style.css | 4 +- OpenBench/templatetags/mytags.py | 2 +- OpenBench/utils.py | 74 +++++++++++++++++++++++++ OpenBench/views.py | 91 ++++++++++++++++++++----------- Templates/OpenBench/base.html | 5 ++ Templates/OpenBench/index.html | 1 - Templates/OpenBench/newTest.html | 65 ++++++++++++++++++++++ db.sqlite3 | Bin 56320 -> 56320 bytes 10 files changed, 228 insertions(+), 65 deletions(-) create mode 100644 OpenBench/utils.py create mode 100644 Templates/OpenBench/newTest.html diff --git a/OpenBench/config.py b/OpenBench/config.py index 0b458bc..8e3fc6f 100644 --- a/OpenBench/config.py +++ b/OpenBench/config.py @@ -1,21 +1,20 @@ -FRAMEWORK_REPO_URL = "http://github.com/AndyGrant/OpenBench/" +FRAMEWORK_REPO_URL = 'http://github.com/AndyGrant/OpenBench/' FRAMEWORK_DEFAULTS = { - "config" : { - - "source" : "http://github.com/AndyGrant/EtherealDev/", - "homepage" : "http://github.com/AndyGrant/Ethereal", - "framework" : "http://github.com/AndyGrant/OpenBench/", - - "protocol" : "uci", - "engine" : "Ethereal", - - "alpha" : .10, - "beta" : .10, - "elower" : 0, - "eupper" : 5, - "hashsize" : 8, - "threads" : 1, - "timecontrol" : "5.0+0.05" + 'config' : { + 'source' : 'http://github.com/AndyGrant/EtherealDev/', + 'homepage' : 'http://github.com/AndyGrant/Ethereal', + 'framework' : 'http://github.com/AndyGrant/OpenBench/', + 'engine' : 'Ethereal', + 'timecontrol' : '5.0+0.05', + 'hash' : 8, + 'threads' : 1, + 'protocol' : 'uci', + 'elolower' : 0.0, + 'eloupper' : 5.0, + 'alpha' : 0.10, + 'beta' : 0.10, + 'threads' : 1, + 'bookname' : '2moves_v1.pgn', } } diff --git a/OpenBench/models.py b/OpenBench/models.py index 4622f67..1a1a7c9 100644 --- a/OpenBench/models.py +++ b/OpenBench/models.py @@ -14,11 +14,11 @@ class LogEvent(Model): class Engine(Model): - name = CharField(max_length=128) - source = CharField(max_length=1024) - proto = CharField(max_length=16) - sha = CharField(max_length=64) - bench = IntegerField(default=0) + name = CharField(max_length=128) + source = CharField(max_length=1024) + protocol = CharField(max_length=16) + sha = CharField(max_length=64) + bench = IntegerField(default=0) def __str__(self): return '{0} ({1})'.format(self.name, self.bench) @@ -37,7 +37,6 @@ class Machine(Model): owner = CharField(max_length=64) osname = CharField(max_length=128) - cores = IntegerField(default=0) mnps = FloatField(default=0.00) lastseen = DateTimeField(auto_now=True) @@ -49,7 +48,6 @@ class Results(Model): test = ForeignKey('Test', PROTECT, related_name='test') machine = ForeignKey('Machine', PROTECT, related_name='machine') - games = IntegerField(default=0) wins = IntegerField(default=0) losses = IntegerField(default=0) @@ -71,16 +69,14 @@ class Test(Model): bookname = CharField(max_length=32) timecontrol = CharField(max_length=16) - hashsize = IntegerField(default=1) - threads = IntegerField(default=1) priority = IntegerField(default=0) throughput = IntegerField(default=0) - alpha = FloatField(default=0.0) - beta = FloatField(default=0.0) elolower = FloatField(default=0.0) eloupper = FloatField(default=0.0) + alpha = FloatField(default=0.0) + beta = FloatField(default=0.0) lowerllr = FloatField(default=0.0) currentllr = FloatField(default=0.0) diff --git a/OpenBench/static/style.css b/OpenBench/static/style.css index b36fc26..d5417b3 100644 --- a/OpenBench/static/style.css +++ b/OpenBench/static/style.css @@ -17,7 +17,7 @@ body { width: auto; } -#errormessagebar { +#error { background-color: RED; padding: 8px; text-align: center; @@ -130,7 +130,7 @@ input[type=submit]:hover { label, option { display: inline-block; - width: 140px; + width: 180px; font-weight:bold; vertical-align: middle; } diff --git a/OpenBench/templatetags/mytags.py b/OpenBench/templatetags/mytags.py index b577b95..55ed3f7 100644 --- a/OpenBench/templatetags/mytags.py +++ b/OpenBench/templatetags/mytags.py @@ -40,7 +40,7 @@ def gitDiffLink(test): repo = "/".join(dev.split("/")[:-2]) - return "{0}/compare/{1}...{2}".format(repo, basesha[:8], testsha[:8]) + return "{0}/compare/{1}...{2}".format(repo, basesha[:8], devsha[:8]) register = template.Library() register.filter("oneDigitPrecision", oneDigitPrecision) diff --git a/OpenBench/utils.py b/OpenBench/utils.py new file mode 100644 index 0000000..f2efa29 --- /dev/null +++ b/OpenBench/utils.py @@ -0,0 +1,74 @@ +from OpenBench.models import Engine, Test + +import math, requests + +def getSourceLocation(branch, repo): + + try: + target = repo.replace('github.com', 'api.github.com/repos') + target = target + 'branches/' + branch + data = requests.get(target).json() + source = repo + 'archive/' + data['commit']['commit']['tree']['sha'] + '.zip' + sha = data['commit']['sha'] + return (sha, source) + + except: + raise Exception('Unable to find branch ({0})'.format(branch)) + +def newEngine(name, source, protocol, sha, bench): + + # Engine may already exist, which is okay + try: return Engine.objects.get(sha=sha) + except: pass + + # Build new Engine + engine = Engine() + engine.name = name + engine.source = source + engine.protocol = protocol + engine.sha = sha + engine.bench = bench + engine.save() + return engine + +def newTest(request): + + test = Test() # New Test, saved only after parsing + + # Extract Development Fields + devname = request.POST['devbranch'] + devbench = int(request.POST['devbench']) + devprotocol = request.POST['devprotocol'] + + # Extract Base Fields + basename = request.POST['basebranch'] + basebench = int(request.POST['basebench']) + baseprotocol = request.POST['baseprotocol'] + + # Extract test configuration + test.devoptions = request.POST['devoptions'] + test.baseoptions = request.POST['baseoptions'] + test.bookname = request.POST['bookname'] + test.timecontrol = request.POST['timecontrol'] + test.priority = int(request.POST['priority']) + test.throughput = int(request.POST['throughput']) + test.elolower = float(request.POST['elolower']) + test.eloupper = float(request.POST['eloupper']) + test.alpha = float(request.POST['alpha']) + test.beta = float(request.POST['beta']) + + # Compute LLR cutoffs + test.lowerllr = math.log(test.beta / (1.0 - test.alpha)) + test.upperllr = math.log((1.0 - test.beta) / test.alpha) + + # Build or fetch the Development version + devsha, devsource = getSourceLocation(devname, request.POST['source']) + test.dev = newEngine(devname, devsource, devprotocol, devsha, devbench) + + # Build or fetch the Base version + basesha, basesource = getSourceLocation(basename, request.POST['source']) + test.base = newEngine(basename, basesource, baseprotocol, basesha, basebench) + + # Nothing seems to have gone wrong + test.save() + return test \ No newline at end of file diff --git a/OpenBench/views.py b/OpenBench/views.py index 0f12a10..779a2e5 100644 --- a/OpenBench/views.py +++ b/OpenBench/views.py @@ -1,17 +1,18 @@ from django.shortcuts import render as djangoRender - from django.http import HttpResponse, HttpResponseRedirect - from django.contrib.auth import authenticate from django.contrib.auth import login as loginUser from django.contrib.auth import logout as logoutUser from django.contrib.auth.models import User +from django.contrib.auth.decorators import login_required +from django.contrib.admin.views.decorators import staff_member_required from OpenBench.config import * - from OpenBench.models import LogEvent, Engine, Profile from OpenBench.models import Machine, Results, Test +import OpenBench.utils + # Wrap django.shortcuts.render to add framework settings def render(request, template, data): data.update(FRAMEWORK_DEFAULTS) @@ -23,28 +24,34 @@ def register(request): if request.method == 'GET': return render(request, 'register.html', {}) - # Attempt to create and login the new user - user = User.objects.create_user( - request.POST['username'], - request.POST['email'], - request.POST['password'] - ) + try: + # Attempt to create and login the new user + user = User.objects.create_user( + request.POST['username'], + request.POST['email'], + request.POST['password'] + ) - # Login the user and return to index - user.save() - loginUser(request, user) + # Login the user and return to index + user.save() + loginUser(request, user) - # Wrap the User in a Profile - profile = Profile() - profile.user = user - profile.save() + # Wrap the User in a Profile + profile = Profile() + profile.user = user + profile.save() - # Log the registration - event = LogEvent() - event.data = 'Created user {0}'.format(request.POST['username']) - event.save() + # Log the registration + event = LogEvent() + event.data = 'Created user {0}'.format(request.POST['username']) + event.save() - return HttpResponseRedirect('/index/') + # Kick back to index + return HttpResponseRedirect('/index/') + + # Bad data, kick back to index with error + except Exception as error: + return index(request, error=str(error)) def login(request): @@ -52,10 +59,15 @@ def login(request): if request.method == 'GET': return render(request, 'login.html', {}) - # Attempt to login the user, and return to index - user = authenticate(username=request.POST['username'], password=request.POST['password']) - loginUser(request, user) - return HttpResponseRedirect('/index/') + try: + # Attempt to login the user, and return to index + user = authenticate(username=request.POST['username'], password=request.POST['password']) + loginUser(request, user) + return HttpResponseRedirect('/index/') + + # Bad data, kick back to index with error + except Exception as error: + return index(request, error=str(error)) def logout(request): @@ -63,7 +75,7 @@ def logout(request): logoutUser(request) return HttpResponseRedirect('/index/') -def index(request, page=0): +def index(request, page=0, error=''): # Get tests pending approval pending = Test.objects.filter(approved=False) @@ -82,20 +94,19 @@ def index(request, page=0): completed = completed.exclude(deleted=True) completed = completed.order_by('completion') - # Get machines currently active workloads - # machines = Machine.objects.get() - # machines = Machines played within last - # Save my @nitrocan @defenchess - + # Compile context dictionary data = { 'pending' : pending, 'active' : active, 'completed' : completed, + 'error' : error, } return render(request, 'index.html', data) def users(request): + + # Build context dictionary for template profiles = Profile.objects.all() data = {'profiles' : Profile.objects.all()} return render(request, 'users.html', data) @@ -111,18 +122,32 @@ def eventLog(request): data['events'].append({ 'data' : event.data, 'creation' : event.creation}) - return render(request, 'eventLog.html', data) +@login_required def newTest(request): - pass + # User trying to view the new test page + if request.method == 'GET': + return render(request, 'newTest.html', {"user" : request.user}) + + try: + # Create test and verify fields + OpenBench.utils.newTest(request) + return HttpResponseRedirect('/index/') + + # Bad data, kick back to index with error + except Exception as error: + return index(request, error=str(error)) + +@staff_member_required def editTest(request, id): pass def viewTest(request, id): pass +@staff_member_required def approveTest(request, id): pass diff --git a/Templates/OpenBench/base.html b/Templates/OpenBench/base.html index da7d857..e3aa80c 100644 --- a/Templates/OpenBench/base.html +++ b/Templates/OpenBench/base.html @@ -12,6 +12,11 @@ + {% if error %} +
+ {{error}} +
+ {% endif %}