diff --git a/OpenBench/urls.py b/OpenBench/urls.py index a41fe76..5511e58 100644 --- a/OpenBench/urls.py +++ b/OpenBench/urls.py @@ -34,8 +34,8 @@ urlpatterns = [ django.urls.path(r'greens/', OpenBench.views.greens), django.urls.path(r'greens//', OpenBench.views.greens), django.urls.path(r'search/', OpenBench.views.search), - django.urls.path(r'viewUser//', OpenBench.views.viewUser), - django.urls.path(r'viewUser///', OpenBench.views.viewUser), + django.urls.path(r'user//', OpenBench.views.user), + django.urls.path(r'user///', OpenBench.views.user), # Links for viewing general information tables django.urls.path(r'users/', OpenBench.views.users), diff --git a/OpenBench/utils.py b/OpenBench/utils.py index f8e098b..d2183a8 100644 --- a/OpenBench/utils.py +++ b/OpenBench/utils.py @@ -18,15 +18,80 @@ # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # -import math, requests, random +import math, requests, random, datetime + +import OpenBench.models from django.utils import timezone from django.db.models import F from django.contrib.auth import authenticate +from django.utils.timezone import utc from OpenBench.config import * from OpenBench.models import Engine, Profile, Machine, Result, Test + +def getPendingTests(): + pending = OpenBench.models.Test.objects.filter(approved=False) + pending = pending.exclude(finished=True) + pending = pending.exclude(deleted=True) + return pending.order_by('-creation') + +def getActiveTests(): + active = OpenBench.models.Test.objects.filter(approved=True) + active = active.exclude(finished=True) + active = active.exclude(deleted=True) + return active.order_by('-priority', '-currentllr') + +def getCompletedTests(): + completed = OpenBench.models.Test.objects.filter(finished=True) + completed = completed.exclude(deleted=True) + return completed.order_by('-updated') + +def getMachineStatus(username=None): + + # Filter for machines used in the last 10 minutes + target = datetime.datetime.utcnow().replace(tzinfo=utc) \ + - datetime.timedelta(minutes=10) + machines = Machine.objects.filter(updated__gte=target) + + # Filter for user specific views + if username != None: machines = machines.filter(owner=username) + + # Extract stat information from workers + return "{0} Machines ".format(len(machines)) + \ + "{0} Threads ".format(sum([f.threads for f in machines])) + \ + "{0} MNPS ".format(sum([f.threads * f.mnps for f in machines])) + +def getPagedContent(content, page, pagelen, url): + + start = max(0, pagelen * (page - 1)) + end = min(len(content), pagelen * page) + count = 1 + math.ceil(len(content) / pagelen) + + part1 = list(range(1, min(4, count))) + part2 = list(range(page - 2, page + 1)) + part3 = list(range(page + 1, page + 3)) + part4 = list(range(count - 3, count + 1)) + + pages = part1 + part2 + part3 + part4 + pages = [f for f in pages if f >= 1 and f <= count] + pages = list(set(pages)) + pages.sort() + + final = [] + for f in range(len(pages) - 1): + final.append(pages[f]) + if pages[f] != pages[f+1] - 1: + final.append('...') + + context = { + "url" : url, "page" : page, "pages" : final, + "prev" : max(1, page - 1), "next" : max(1, min(page + 1, count - 1)), + } + + return content[start:end], context + def pagingContext(page, pageLength, items, url): # Tests within the given page diff --git a/OpenBench/views.py b/OpenBench/views.py index 33a846c..afd4f91 100644 --- a/OpenBench/views.py +++ b/OpenBench/views.py @@ -181,136 +181,127 @@ def profile(request): # Test List Views # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # -def index(request, page=1, pageLength=24, greens=False, username=None, error=''): +def index(request, page=1, error=''): - # Get tests pending approval - pending = Test.objects.filter(approved=False) - pending = pending.exclude(finished=True) - pending = pending.exclude(deleted=True) - pending = pending.order_by('-creation') + # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # + # # + # GET : Return all pending, active, and completed tests. Limit the # + # display of tests by the requested page number. Also display the # + # status for connected machines. # + # # + # POST : Return all pending, active, and completed tests. Limit the # + # display of tests by the requested page number. Also display the # + # status for connected machines. # + # # + # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # - # Get tests currently running - active = Test.objects.filter(approved=True) - active = active.exclude(finished=True) - active = active.exclude(deleted=True) - active = active.order_by('-priority', '-currentllr') + pending = OpenBench.utils.getPendingTests() + active = OpenBench.utils.getActiveTests() + completed = OpenBench.utils.getCompletedTests() - # Get the completed tests (sliced later) - completed = Test.objects.filter(finished=True) - completed = completed.exclude(deleted=True) - completed = completed.order_by('-updated') + completed, paging = OpenBench.utils.getPagedContent( + completed, page, 25, 'index'.format()) - # Pull data from active machines - target = datetime.datetime.utcnow().replace(tzinfo=utc) - datetime.timedelta(minutes=10) - machines = Machine.objects.filter(updated__gte=target) - if username != None: machines = machines.filter(owner=username) - - # Extract stat information from workers - machineCount = len(machines) - threadCount = sum(machine.threads for machine in machines) - npsTotal = sum(machine.threads * machine.mnps for machine in machines) - workerData = "{0} Machines {1} Threads {2} MNPS".format( - machineCount, threadCount, round(npsTotal, 2) - ) - - # Index is wrapped for just viewing passed tests - if greens == True: - pending = active = [] - completed = completed.filter(passed=True) - source = 'greens' - - # Index is wrapped just to view one user - elif username != None: - pending = pending.filter(author=username) - active = active.filter(author=username) - completed = completed.filter(author=username) - source = "viewUser/{0}".format(username) - - # Index is normal, not wrapped for any views - else: - source = "index" - - # Choose tests within the given page, if any - items = len(completed) - start = (page - 1) * pageLength - end = page * pageLength - start = max(0, min(start, items)) - end = max(0, min(end, items)) - paging = pagingContext(page, pageLength, items, source) - - # Build context dictionary for index template data = { - 'error' : error, - 'pending' : pending, - 'active' : active, - 'completed' : completed[start:end], - 'status' : workerData, - 'greens' : greens, - 'paging' : paging, + 'error' : error, 'pending' : pending, + 'active' : active, 'completed' : completed, + 'paging' : paging, 'status' : OpenBench.utils.getMachineStatus(), } return render(request, 'index.html', data) def greens(request, page=1): - # Index but with only passed tests - return index(request, page, greens=True) + # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # + # # + # GET : Return all tests both passed and completed. Limit the display # + # of tests by the requested page number. # + # # + # POST : Return all tests both passed and completed. Limit the display # + # of tests by the requested page number. # + # # + # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # + + completed = OpenBench.utils.getCompletedTests().filter(passed=True) + completed, paging = OpenBench.utils.getPagedContent( + completed, page, 25, 'greens'.format()) + + data = {'completed' : completed, 'paging' : paging} + return render(request, 'index.html', data) def search(request): - # First time viewing the page - if request.method == 'GET': - return render(request, 'search.html', {'tests' : []}) + # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # + # # + # GET : Return the HTML template for searching tests on the framework. # + # # + # POST : Filter the tests by the provided criteria, and return a display # + # with the filtered results. Keywords are case insensitive # + # # + # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # + + if request.method == 'GET': + return render(request, 'search.html', {}) - # Base starting point with all tests tests = Test.objects.all() - # Only show tests for selected engine (default is all engines) if request.POST['engine'] != '': tests = tests.filter(engine=request.POST['engine']) - # Only show tests from a selected author (default is all authors) if request.POST['author'] != '': tests = tests.filter(author=request.POST['author']) - # Don't show tests that have passed if request.POST['showgreens'] == 'False': tests = tests.exclude(passed=True) - # Don't show tests that have failed yellow if request.POST['showyellows'] == 'False': - tests = tests.exclude(failed=True,wins__gte=F('losses')) + tests = tests.exclude(failed=True, wins__gte=F('losses')) - # Don't show tests that have failed red - if request.POST['showreds'] == 'False': - tests = tests.exclude(failed=True,wins__lt=F('losses')) + if request.POST['showreds'] == 'False': + tests = tests.exclude(failed=True, wins__lt=F('losses')) - # Don't show tests that are unfinished if request.POST['showunfinished'] == 'False': - tests = tests.exclude(passed=False,failed=False) + tests = tests.exclude(passed=False, failed=False) - # Don't show tests that have been deleted if request.POST['showdeleted'] == 'False': tests = tests.exclude(deleted=True) - # If there are no keywords, we are done searching - keywords = request.POST['keywords'].split() - if keywords == []: - return render(request, 'search.html', {'tests' : tests.order_by('-updated')}) + keywords = ['(?i)' + f.upper() for f in request.POST['keywords'].split()] + tests = tests.filter(dev__name__regex=r'{0}'.format('|'.join(keywords))) + return render(request, 'search.html', {'tests' : tests.order_by('-updated')}) - # Only grab tests which contain at least one keyword - filtered = [] - for test in tests.order_by('-updated'): - for keyword in keywords: - if keyword.upper() in test.dev.name.upper(): - filtered.append(test) - break - return render(request, 'search.html', {'tests' : filtered}) +def user(request, username, page=1): -def viewUser(request, username, page=0): + # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # + # # + # GET : Return all pending, active, and completed tests for the User # + # that has been requested. Limit the display of completed tests # + # by the requested page number. Also display the User's machines # + # # + # POST : Return all pending, active, and completed tests for the User # + # that has been requested. Limit the display of completed tests # + # by the requested page number. Also display the User's machines # + # # + # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # - # Index but with only username's tests - return index(request, page, username=username) + pending = OpenBench.utils.getPendingTests().filter(author=username) + active = OpenBench.utils.getActiveTests().filter(author=username) + completed = OpenBench.utils.getCompletedTests().filter(author=username) + + completed, paging = OpenBench.utils.getPagedContent( + completed, page, 25, 'user/{0}'.format(username)) + + data = { + 'pending' : pending, 'active' : active, + 'completed' : completed, 'paging' : paging, + 'status' : OpenBench.utils.getMachineStatus(username), + } + + return render(request, 'index.html', data) + +# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # +# # +# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # def users(request): diff --git a/Templates/OpenBench/Blocks/testsummary.html b/Templates/OpenBench/Blocks/testsummary.html index bbf236d..06a5580 100644 --- a/Templates/OpenBench/Blocks/testsummary.html +++ b/Templates/OpenBench/Blocks/testsummary.html @@ -1,6 +1,6 @@ {% load mytags %} - {{test.author|slice:":6"}} + {{test.author|slice:":6"}} {{test.engine}} {{test.dev.name}} diff diff --git a/Templates/OpenBench/index.html b/Templates/OpenBench/index.html index 1adb2a4..1235ef3 100644 --- a/Templates/OpenBench/index.html +++ b/Templates/OpenBench/index.html @@ -5,7 +5,7 @@ - {% if paging.page == 1 and greens == False %} + {% if paging.page == 1 %} {% if pending %} diff --git a/Templates/OpenBench/search.html b/Templates/OpenBench/search.html index 91a04f8..8c670cf 100644 --- a/Templates/OpenBench/search.html +++ b/Templates/OpenBench/search.html @@ -14,23 +14,23 @@
- +
- +
- + - +
{{profile.user.username}}{{profile.user.username}} {{profile.games|insertCommas}} {{profile.tests|insertCommas}} {{profile.engine}}