Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 21 additions & 46 deletions command_api.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from http import HTTPStatus

from flask import request
from flask import Response, request

from app_setup import app
from task_database import get_task_progress, get_taskCurrent
from task_database import get_user
import logging
from task_api import token_required_v2
from user_dao import UserDatabaseObject
Expand All @@ -23,63 +23,38 @@ def store_rsn_by_username(user: UserDatabaseObject):
body = request.json
rsn = body['rsn']

logger.debug("Storing RSN mapping: username=%s, rsn=%s", user.username, rsn)
username_by_rsn_cache[rsn] = user.username

logger.debug(
"Storing RSN mapping: username=%s, rsn=%s, body=%s",
user.username,
rsn,
body
)
return Response(status=HTTPStatus.NO_CONTENT)

return '', HTTPStatus.NO_CONTENT


@app.route('/command/<rsn>', methods=['GET'])
@app.route('/api/v2/command/<rsn>', methods=['GET'])
def command_get_task_progress(rsn: str):
logger.debug("Looking up RSN in cache: rsn=%s, cache=%s",rsn, username_by_rsn_cache)
logger.debug('Looking up RSN in cache: rsn=%s, cache=%s',rsn, username_by_rsn_cache)

try:
username = username_by_rsn_cache.get(rsn)

if username is None:
logger.debug("No username found in cache for rsn=%s", rsn)
return {
"error": "Unknown RSN"
}, HTTPStatus.NOT_FOUND
logger.debug('No username found in cache for rsn=%s', rsn)
return { 'error': 'Unknown RSN' }, HTTPStatus.NOT_FOUND

user = get_user(username)
if user is None:
return { 'error': 'Unknown user' }, HTTPStatus.NOT_FOUND

logger.debug("Found username for RSN: rsn=%s, username=%s", rsn, username)
task = get_taskCurrent(username)
progress = get_task_progress(username)
curr_tier = _get_current_tier(progress)
logger.debug('Found username for RSN: rsn=%s, username=%s', rsn, username)
task_id = user.current_task_id()
curr_tier = user.current_rollable_tier()
progress = user.get_tier_progress(curr_tier)

return {
"task": task,
"tier": curr_tier,
"progressPercentage": progress[curr_tier]["percent_complete"]
'task_id': task_id,
'tier': curr_tier,
'progress': progress.percent_complete
}

except Exception as e:
logger.exception(
"Error retrieving command information for rsn=%s",
rsn
)

return {
'error': f'Error: {e}'
}, HTTPStatus.INTERNAL_SERVER_ERROR


def _get_current_tier(progress):
if progress["easy"]["total_complete"] < progress["easy"]["total"]:
return "easy"
elif progress["medium"]["total_complete"] < progress["medium"]["total"]:
return "medium"
elif progress["hard"]["total_complete"] < progress["hard"]["total"]:
return "hard"
elif progress["elite"]["total_complete"] < progress["elite"]["total"]:
return "elite"
elif progress["master"]["total_complete"] < progress["master"]["total"]:
return "master"
else:
return "extra"
logger.exception('Error retrieving command information for rsn=%s', rsn)
return { 'error': str(e) }, HTTPStatus.INTERNAL_SERVER_ERROR