Skip to content

Commit

Permalink
Merge branch 'speedup-outstandings'
Browse files Browse the repository at this point in the history
  • Loading branch information
FreekBes committed Feb 2, 2023
2 parents 97361f4 + 13bef48 commit 0cd5c10
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 39 deletions.
8 changes: 0 additions & 8 deletions src/v1/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,11 +153,3 @@ def set_v1_settings(form:OldSettings):
db.session.rollback()
return False
return True


def get_projects_users(user:User):
db_teams:list[Team] = db.session.query(Team.projects_user_id).filter_by(user_id = user.intra_id).group_by(Team.projects_user_id).all()
projects_user_ids:list[int] = list()
for db_team in db_teams:
projects_user_ids.append(db_team.projects_user_id)
return projects_user_ids
57 changes: 26 additions & 31 deletions src/v1/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
import time

from flask import session, jsonify, request, redirect, url_for, render_template, Response
from src.v1.helpers import get_v1_settings, set_v1_settings, get_projects_users
from src.v1.helpers import get_v1_settings, set_v1_settings
from src.models.models import OAuth2Token, User, Team, Evaluation, Runner
from werkzeug.datastructures import CombinedMultiDict
from src.lib.auth.oauth import authstart
from src.v1.forms import OldSettings
from sqlalchemy import func, case
from src import app, db


Expand Down Expand Up @@ -99,46 +100,40 @@ def oldOutstandings():
if not 'username' in request.args:
return { 'type': 'error', 'message': 'GET key \'username\' is not set, but is required' }, 400

# Retrieve user
db_user:User = User.query.filter_by(login = request.args['username']).first()
if not db_user:
return { 'type': 'success', 'message': 'No data exists for this user', 'data': [] }, 200
db_runner:Runner = db.session.query(Runner.outstandings).filter(Runner.user_id == db_user.intra_id).one()
if not db_runner or not db_runner.outstandings:
# Retrieve user and check if outstanding runner already fetched outstandings for this user
db_user = db.session.query(User.intra_id, Runner.outstandings).join(Runner).filter(User.login == request.args['username']).first()
if not db_user or not db_user.outstandings:
return { 'type': 'success', 'message': 'No data exists for this user', 'data': [] }, 200

# Set up outstandings per projects_user dict
outstandings = dict()
projects_user_ids = get_projects_users(db_user)
for projects_user_id in projects_user_ids:
pu_outstandings = dict()
pu_outstandings['current'] = 0
pu_outstandings['best'] = 0
pu_outstandings['all'] = list()
outstandings[str(projects_user_id)] = pu_outstandings

# Loop over all teams in the database
db_teams:list[Team] = db.session.query(Team).filter(Team.user_id == db_user.intra_id).order_by(Team.projects_user_id.desc(), Team.intra_id.desc()).all()
for db_team in db_teams:
team_outstandings = 0
db_evals:list[Evaluation] = db.session.query(Evaluation.outstanding).filter(Evaluation.intra_team_id == db_team.intra_id).all()
for db_eval in db_evals:
if db_eval.outstanding:
team_outstandings += 1
pu_outstandings = outstandings[str(db_team.projects_user_id)]
pu_outstandings['all'].append(team_outstandings)
if db_team.best:
pu_outstandings['best'] = team_outstandings
if db_team.current:
pu_outstandings['current'] = team_outstandings

# Retrieve outstandings for each project user and fetch the amount of outstanding evaluations for each team
# Outer join, to make sure even teams without evaluations are returned
db_q = db.session.query(Team, func.sum(case([(Evaluation.outstanding == True, 1)], else_ = 0)).label('outstandings')).outerjoin(Evaluation, Evaluation.intra_team_id == Team.intra_id).filter(Team.user_id == db_user.intra_id).order_by(Team.projects_user_id.desc(), Team.intra_id.desc()).group_by(Team.id)
db_res:list = db_q.all()

for db_row in db_res:
if not str(db_row.Team.projects_user_id) in outstandings:
pu_outstandings = dict()
pu_outstandings['current'] = 0
pu_outstandings['best'] = 0
pu_outstandings['all'] = list()
outstandings[str(db_row.Team.projects_user_id)] = pu_outstandings
else:
pu_outstandings = outstandings[str(db_row.Team.projects_user_id)]
if db_row.Team.current:
pu_outstandings['current'] = db_row.outstandings
if db_row.Team.best:
pu_outstandings['best'] = db_row.outstandings
pu_outstandings['all'].append(db_row.outstandings)

# Create response with additional headers
resp = Response(
response = { 'type': 'success', 'message': 'Outstandings for user, per projectsUser', 'data': outstandings },
status = 200,
mimetype = 'application/json'
)
resp.headers['Last-Modified'] = db_runner.outstandings.strftime('%a, %d %b %Y %H:%M:%S GMT')
resp.headers['Last-Modified'] = db_user.outstandings.strftime('%a, %d %b %Y %H:%M:%S GMT')
return (resp.response, resp.status_code, resp.headers.items())


Expand Down

0 comments on commit 0cd5c10

Please sign in to comment.