From c3d1f9e9b0dedef70909fcee807ceaaa23819d49 Mon Sep 17 00:00:00 2001 From: aj Date: Sun, 20 Oct 2019 21:07:13 +0100 Subject: [PATCH] added current tasks view --- music/api/__init__.py | 1 + music/api/admin.py | 43 ++++++++++++++++++++++++ music/music.py | 4 ++- src/js/Admin/Admin.js | 3 ++ src/js/Admin/Functions.js | 19 +++++++++++ src/js/Admin/Tasks.js | 71 +++++++++++++++++++++++++++++++++++++++ 6 files changed, 140 insertions(+), 1 deletion(-) create mode 100644 music/api/admin.py create mode 100644 src/js/Admin/Tasks.js 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 { + ); diff --git a/src/js/Admin/Functions.js b/src/js/Admin/Functions.js index 3af096f..23c4084 100644 --- a/src/js/Admin/Functions.js +++ b/src/js/Admin/Functions.js @@ -9,10 +9,24 @@ class Functions extends Component { super(props); this.runAllUsers = this.runAllUsers.bind(this); + this.runStats = this.runStats.bind(this); } runAllUsers(event){ axios.get('/api/playlist/run/users') + .then((response) => { + showMessage('users run'); + }) + .catch((error) => { + showMessage(`error running all users (${error.response.status})`); + }); + } + + runStats(event){ + axios.get('/api/spotfm/playlist/refresh/users') + .then((response) => { + showMessage('stats run'); + }) .catch((error) => { showMessage(`error running all users (${error.response.status})`); }); @@ -34,6 +48,11 @@ class Functions extends Component { + + + + + ); } diff --git a/src/js/Admin/Tasks.js b/src/js/Admin/Tasks.js new file mode 100644 index 0000000..34cfd8a --- /dev/null +++ b/src/js/Admin/Tasks.js @@ -0,0 +1,71 @@ +import React, { Component } from "react"; +const axios = require('axios'); + +import showMessage from "../Toast.js" + +class Tasks extends Component { + + constructor(props){ + super(props); + this.state = { + total_tasks: 0, + tasks: [], + schedule_times: [] + } + this.getTasks(); + } + + getTasks(){ + var self = this; + axios.get('/api/admin/tasks') + .then((response) => { + self.setState({ + total_tasks: response.data.total_tasks, + tasks: response.data.tasks + }); + }) + .catch((error) => { + showMessage(`error getting tasks (${error.response.status})`); + }); + } + + render () { + return ( + + + + + + + { this.state.tasks.map((entry) => )} + + + + + +
+

running tasks

+
+ {this.state.total_tasks} running tasks +
); + } +} + +function TaskType(props) { + return ( + + + + {props.url}: {props.count} + + + {props.times.map((entry) => + + {entry} + + )} + + ); +} + +export default Tasks; \ No newline at end of file