diff --git a/music/api/__init__.py b/music/api/__init__.py index 46c891a..67d1484 100644 --- a/music/api/__init__.py +++ b/music/api/__init__.py @@ -3,3 +3,4 @@ from .player import blueprint as player_blueprint from .fm import blueprint as fm_blueprint from .spotfm import blueprint as spotfm_blueprint from .spotify import blueprint as spotify_blueprint +from .admin import blueprint as admin_blueprint diff --git a/music/api/admin.py b/music/api/admin.py new file mode 100644 index 0000000..8312b10 --- /dev/null +++ b/music/api/admin.py @@ -0,0 +1,43 @@ +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 diff --git a/music/music.py b/music/music.py index 2007e6e..0322789 100644 --- a/music/music.py +++ b/music/music.py @@ -4,7 +4,8 @@ from google.cloud import firestore import os from music.auth import auth_blueprint -from music.api import api_blueprint, player_blueprint, fm_blueprint, spotfm_blueprint, spotify_blueprint +from music.api import api_blueprint, player_blueprint, fm_blueprint, \ + spotfm_blueprint, spotify_blueprint, admin_blueprint db = firestore.Client() @@ -16,6 +17,7 @@ app.register_blueprint(player_blueprint, url_prefix='/api/player') app.register_blueprint(fm_blueprint, url_prefix='/api/fm') app.register_blueprint(spotfm_blueprint, url_prefix='/api/spotfm') app.register_blueprint(spotify_blueprint, url_prefix='/api/spotify') +app.register_blueprint(admin_blueprint, url_prefix='/api/admin') @app.route('/') diff --git a/src/js/Admin/Admin.js b/src/js/Admin/Admin.js index 97ce1bb..1028609 100644 --- a/src/js/Admin/Admin.js +++ b/src/js/Admin/Admin.js @@ -4,6 +4,7 @@ const axios = require('axios'); import Lock from "./Lock.js"; import Functions from "./Functions.js"; +import Tasks from "./Tasks.js"; class Admin extends Component { render(){ @@ -12,10 +13,12 @@ class Admin extends Component {
+ running tasks+ |
+ |
---|---|
+ {this.state.total_tasks} running tasks + | +