Mixonomer/music/api/admin.py
2019-10-20 21:07:13 +01:00

44 lines
1.2 KiB
Python

from flask import Blueprint, jsonify
import logging
from datetime import datetime
from google.cloud import firestore
from google.cloud import tasks_v2
from music.api.decorators import login_or_basic_auth, admin_required
blueprint = Blueprint('admin-api', __name__)
db = firestore.Client()
tasker = tasks_v2.CloudTasksClient()
task_path = tasker.queue_path('sarsooxyz', 'europe-west2', 'spotify-executions')
logger = logging.getLogger(__name__)
@blueprint.route('/tasks', methods=['GET'])
@login_or_basic_auth
@admin_required
def get_tasks(username=None):
tasks = [i for i in tasker.list_tasks(task_path)]
urls = {}
for task in tasks:
if urls.get(task.app_engine_http_request.relative_uri):
urls[task.app_engine_http_request.relative_uri] += 1
else:
urls[task.app_engine_http_request.relative_uri] = 1
response = {
'tasks': [{
'url': i,
'count': j,
'scheduled_times': [datetime.fromtimestamp(k.schedule_time.seconds) for k in tasks
if k.app_engine_http_request.relative_uri == i]
} for i, j in urls.items()],
'total_tasks': len(tasks),
}
return jsonify(response), 200