Cleaning + fix some date time django magic

This commit is contained in:
AndyGrant committed 2018-06-11 20:45:42 -04:00
1 parent 36d73bacbe
commit 841d634e08
5 files changed
+53 -72

No files matched your search

+2 -2
View File
@@ -1,9 +1,9 @@
from django.contrib import admin from django.contrib import admin
from OpenBench.models import LogEvent, Engine, Profile, Machine, Results, Test from OpenBench.models import LogEvent, Engine, Profile, Machine, Result, Test
admin.site.register(LogEvent) admin.site.register(LogEvent)
admin.site.register(Engine) admin.site.register(Engine)
admin.site.register(Profile) admin.site.register(Profile)
admin.site.register(Machine) admin.site.register(Machine)
admin.site.register(Results) admin.site.register(Result)
admin.site.register(Test) admin.site.register(Test)
+6 -6
View File
@@ -8,7 +8,7 @@ class LogEvent(Model):
data = CharField(max_length=256) data = CharField(max_length=256)
author = CharField(max_length=64) author = CharField(max_length=64)
creation = DateTimeField(auto_now=True) creation = DateTimeField(auto_now_add=True)
def __str__(self): def __str__(self):
return "{0} {1} : {2}".format(self.creation, self.author, self.data) return "{0} {1} : {2}".format(self.creation, self.author, self.data)
@@ -42,12 +42,12 @@ class Machine(Model):
osname = CharField(max_length=128) osname = CharField(max_length=128)
threads = IntegerField(default=0) threads = IntegerField(default=0)
mnps = FloatField(default=0.00) mnps = FloatField(default=0.00)
lastseen = DateTimeField(auto_now=True) updated = DateTimeField(auto_now=True)
def __str__(self): def __str__(self):
return '{0}-{1} ({2})'.format(self.owner, self.osname, self.id) return '{0}-{1} ({2})'.format(self.owner, self.osname, self.id)
class Results(Model): class Result(Model):
test = ForeignKey('Test', PROTECT, related_name='test') test = ForeignKey('Test', PROTECT, related_name='test')
machine = ForeignKey('Machine', PROTECT, related_name='machine') machine = ForeignKey('Machine', PROTECT, related_name='machine')
@@ -57,7 +57,7 @@ class Results(Model):
draws = IntegerField(default=0) draws = IntegerField(default=0)
crashes = IntegerField(default=0) crashes = IntegerField(default=0)
time = IntegerField(default=0) time = IntegerField(default=0)
lastseen = DateTimeField(default=0) updated = DateTimeField(auto_now=True)
def __str__(self): def __str__(self):
return '{0} {1}'.format(self.test.dev.name, self.machine.__str__()) return '{0} {1}'.format(self.test.dev.name, self.machine.__str__())
@@ -100,8 +100,8 @@ class Test(Model):
deleted = BooleanField(default=False) deleted = BooleanField(default=False)
approved = BooleanField(default=False) approved = BooleanField(default=False)
creation = DateTimeField(auto_now=True) creation = DateTimeField(auto_now_add=True)
completion = DateTimeField(auto_now=True) updated = DateTimeField(auto_now=True)
def __str__(self): def __str__(self):
return '{0} vs {1} @ {2}'.format(self.dev.name, self.base.name, self.timecontrol) return '{0} vs {1} @ {2}'.format(self.dev.name, self.base.name, self.timecontrol)
+27 -32
View File
@@ -15,22 +15,6 @@ def getSourceLocation(branch, repo):
except: except:
raise Exception('Unable to find branch ({0})'.format(branch)) 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): def newTest(request):
test = Test() # New Test, saved only after parsing test = Test() # New Test, saved only after parsing
@@ -65,11 +49,11 @@ def newTest(request):
# Build or fetch the Development version # Build or fetch the Development version
devsha, devsource = getSourceLocation(devname, test.source) devsha, devsource = getSourceLocation(devname, test.source)
test.dev = newEngine(devname, devsource, devprotocol, devsha, devbench) test.dev = getEngine(devname, devsource, devprotocol, devsha, devbench)
# Build or fetch the Base version # Build or fetch the Base version
basesha, basesource = getSourceLocation(basename, test.source) basesha, basesource = getSourceLocation(basename, test.source)
test.base = newEngine(basename, basesource, baseprotocol, basesha, basebench) test.base = getEngine(basename, basesource, baseprotocol, basesha, basebench)
# Track # of tests by this user # Track # of tests by this user
profile = Profile.objects.get(user=request.user) profile = Profile.objects.get(user=request.user)
@@ -80,22 +64,29 @@ def newTest(request):
test.save() test.save()
return test return test
def getEngine(name, source, protocol, sha, bench):
# Engine may already exist, which is okay
try: return Engine.objects.get(sha=sha)
except: pass
# Build new Engine
return Engine.objects.create(
name=name, source=source,
protocol=protocol, sha=sha, bench=bench)
def getMachine(machineid, username, osname, threads): def getMachine(machineid, username, osname, threads):
# Machine does not exist, make a new one # Client has no saved machine ID, make a new machine
if machineid == 'None': if machineid == 'None':
machine = Machine() return Machine.objects.create(owner=username, osname=osname, threads=threads)
machine.owner = username
machine.osname = osname
machine.threads = threads
machine.mnps = 0.00
machine.save()
return machine
# Verify the selected machine is the user's # Fetch and verify the claimed machine ID
machine = Machine.objects.get(id=machineid) machine = Machine.objects.get(id=machineid)
assert machine.owner == username assert machine.owner == username
assert machine.osname == osname assert machine.osname == osname
# Update to reflect new worload
machine.threads = threads machine.threads = threads
machine.mnps = 0.00 machine.mnps = 0.00
machine.save() machine.save()
@@ -124,7 +115,7 @@ def getWorkload(machine):
tokens = test.baseoptions.split(' ') tokens = test.baseoptions.split(' ')
basethreads = tokens[0].split('=')[1] basethreads = tokens[0].split('=')[1]
# Machines need at to support Dev & Base # Minimum threads to support Dev & Base
threadcnt = max(devthreads, basethreads) threadcnt = max(devthreads, basethreads)
# Empty list or higher priority found for workable test # Empty list or higher priority found for workable test
@@ -152,22 +143,26 @@ def getWorkload(machine):
options = options[1:] options = options[1:]
def getResults(machine, test): def getResults(machine, test):
pass
def workloadDictionary(profile, machine, result, test): # Can find an existing result by test and machine
results = Result.objects.filter(test=test)
results = list(Result.objects.filter(machine=machine))
def workloadDictionary(machine, result, test):
# Worker will send back the id of each model for ease of # Worker will send back the id of each model for ease of
# updating. Worker needs test information, as well as the # updating. Worker needs test information, as well as the
# specification for both engines. Group the dev and base # specification for both engines. Group the dev and base
# options with the coressponding engine, for easy usage # options with the coressponding engine, for easy usage
return { return {
'profile' : { 'id' : profile.id, },
'machine' : { 'id' : machine.id, }, 'machine' : { 'id' : machine.id, },
'result' : { 'id' : result.id, }, 'result' : { 'id' : result.id, },
'test' : { 'test' : {
'id' : test.id, 'id' : test.id,
'bookname' : test.bookname, 'bookname' : test.bookname,
'booksource' : FRAMEWORK_REPO_URL + '/raw/master/Books/' 'booksource' : FRAMEWORK_REPO_URL + '/raw/master/Books/',
'timecontrol' : test.timecontrol, 'timecontrol' : test.timecontrol,
'dev' : { 'dev' : {
'name' : test.dev.name, 'name' : test.dev.name,
+18 -32
View File
@@ -10,7 +10,7 @@ from django.views.decorators.csrf import csrf_exempt
from OpenBench.config import * from OpenBench.config import *
from OpenBench.models import LogEvent, Engine, Profile from OpenBench.models import LogEvent, Engine, Profile
from OpenBench.models import Machine, Results, Test from OpenBench.models import Machine, Result, Test
import OpenBench.utils import OpenBench.utils
@@ -34,26 +34,20 @@ def register(request):
if not request.POST['username'].isalnum(): if not request.POST['username'].isalnum():
raise Exception('Alpha Numeric Usernames Only') raise Exception('Alpha Numeric Usernames Only')
# Attempt to create and login the new user # Create new User and Profile
user = User.objects.create_user( Profile.objects.create(
request.POST['username'], user=User.objects.create_user(
request.POST['email'], request.POST['username'],
request.POST['password1'] request.POST['email'],
) request.POST['password1']))
# Save and log the user in # Log the User in now
user.save(); loginUser(request, user) loginUser(request, User.objects.get(name=request.POST['username']))
# Wrap the User in a Profile
profile = Profile()
profile.user = user
profile.save()
# Log the registration # Log the registration
event = LogEvent() LogEvent.objects.create(
event.data = 'Created an account' data='Created an account',
event.author = request.POST['username'] author=request.POST['username'])
event.save()
# Kick back to index # Kick back to index
return HttpResponseRedirect('/index/') return HttpResponseRedirect('/index/')
@@ -185,10 +179,9 @@ def newTest(request):
test = OpenBench.utils.newTest(request) test = OpenBench.utils.newTest(request)
# Log the test creation # Log the test creation
event = LogEvent() LogEvent.objects.create(
event.data = 'Created test {0} ({1})'.format(str(test), test.id) data='Created test {0} ({1})'.format(str(test), test.id),
event.author = request.user.username author=request.user.username)
event.save()
return HttpResponseRedirect('/index/') return HttpResponseRedirect('/index/')
@@ -201,7 +194,7 @@ def viewTest(request, id):
try: try:
# Find Test and Results # Find Test and Results
test = Test.objects.get(id=id) test = Test.objects.get(id=id)
results = Results.objects.all().filter(test=test) results = Result.objects.all().filter(test=test)
data = {'test' : test, 'results' : results} data = {'test' : test, 'results' : results}
return render(request, 'viewTest.html', data) return render(request, 'viewTest.html', data)
@@ -377,15 +370,8 @@ def getWorkload(request):
try: results = OpenBench.utils.getResults(machine, test) try: results = OpenBench.utils.getResults(machine, test)
except: return HttpResponse('None') except: return HttpResponse('None')
# Convert everything for the Client # Send ID's and test information as a string dictionary
data = { return HttpResponse(str(workloadDictionary(machine, result, test)))
"machine" : machine.dictionary(),
"test" : test.dictionary(),
"result" : results.dictionary()
}
# Send context as a string0
return HttpResponse(str(data))
@csrf_exempt @csrf_exempt
def submitResults(request): def submitResults(request):
BIN
View File
Binary file not shown.