From 182751df9cab43e3d314980a79966ee4bfbb67e7 Mon Sep 17 00:00:00 2001 From: mrjones-plip Date: Sun, 12 Feb 2023 21:09:55 -0800 Subject: [PATCH 01/10] WIP to add ajax loading and bug fixes, multi user support --- docker-compose.yml | 2 + main.py | 21 +++-- templates/index.html | 183 ++++++++++++++++++++++++------------------- timekpr-next-web.py | 2 +- 4 files changed, 120 insertions(+), 88 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index ae21f99..ffc6cba 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -10,3 +10,5 @@ services: restart: unless-stopped ports: - "${TIMEKPR_IP:-0.0.0.0}:${TIMEKPR_PORT:-8080}:8080" + environment: + TZ: ${TIMEKPR_TZ:-America/Los_Angeles} \ No newline at end of file diff --git a/main.py b/main.py index 62320de..a1e4e50 100644 --- a/main.py +++ b/main.py @@ -4,6 +4,7 @@ import conf, re from fabric import Connection from paramiko.ssh_exception import AuthenticationException +from paramiko.ssh_exception import NoValidConnectionsError def get_config(): @@ -12,10 +13,16 @@ def get_config(): def get_usage(user, computer, ssh): # to do - maybe check if user is in timekpr first? (/usr/bin/timekpra --userlist) - timekpra_userinfo_output = str(ssh.run( - conf.ssh_timekpra_bin + ' --userinfo ' + user, - hide=True - )) + global timekpra_userinfo_output + try: + timekpra_userinfo_output = str(ssh.run( + conf.ssh_timekpra_bin + ' --userinfo ' + user, + hide=True + )) + except NoValidConnectionsError as e: + print(f"Cannot connect to SSH server on host '{computer}'. " + f"Check address in conf.py or try again later.") + return {'time_left': 0, 'time_spent': 0, 'result': 'fail'} search = r"(TIME_LEFT_DAY: )([0-9]+)" time_left = re.search(search, timekpra_userinfo_output) search = r"(TIME_SPENT_DAY: )([0-9]+)" @@ -30,7 +37,7 @@ def get_usage(user, computer, ssh): time_spent = str(time_spent.group(2)) print(f"Time left for {user} at {computer}: {time_spent}") - return {'time_left': time_left, 'time_spent': time_spent} + return {'time_left': time_left, 'time_spent': time_spent, 'result': 'success'} def get_connection(computer): @@ -49,9 +56,9 @@ def get_connection(computer): ) except AuthenticationException as e: quit(f"Wrong credentials for user '{conf.ssh_user}' on host '{computer}'. " - f"Check `ssh_user` and `ssh_password` credentials in config.py.") + f"Check `ssh_user` and `ssh_password` credentials in conf.py.") except Exception as e: - quit(f"Error logging in as user '{conf.ssh_user}' on host '{computer}', check config. \n\n\t" + str(e)) + quit(f"Error logging in as user '{conf.ssh_user}' on host '{computer}', check conf.py. \n\n\t" + str(e)) finally: return connection diff --git a/templates/index.html b/templates/index.html index 7953971..95292ce 100644 --- a/templates/index.html +++ b/templates/index.html @@ -16,6 +16,14 @@ color: blue; margin-left: 4px; } + .error { + color: red; + margin-left: 4px; + } + .user { + padding-bottom: 15px; + display: block; + } /* ========================================================================== Fork Me on Github Stuffs @@ -108,100 +116,115 @@ + + diff --git a/timekpr-next-web.py b/timekpr-next-web.py index 8319639..9e48fab 100644 --- a/timekpr-next-web.py +++ b/timekpr-next-web.py @@ -32,7 +32,7 @@ def get_usage(computer, user): return validate_request(computer, user), 500 ssh = main.get_connection(computer) usage = main.get_usage(user, computer, ssh) - return {'result': "success", "time_left": usage['time_left'], "time_spent": usage['time_spent']}, 200 + return {'result': usage['result'], "time_left": usage['time_left'], "time_spent": usage['time_spent']}, 200 @app.route("/increase_time///") From b7dcb74f3dffd6156c3ce929ab4c1db2ec255beb Mon Sep 17 00:00:00 2001 From: mrjones-plip Date: Mon, 13 Feb 2023 19:51:15 -0800 Subject: [PATCH 02/10] WIP moar ajax --- templates/index.html | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/templates/index.html b/templates/index.html index 95292ce..c8498b2 100644 --- a/templates/index.html +++ b/templates/index.html @@ -111,7 +111,7 @@
-
+
@@ -124,9 +124,12 @@ "Unused: ... " + "Used: ...
" ); - let usage = $.parseJSON(get_usage(user, computer)); - console.log("got results for user:", user); - render_user_to_dom(user, computer, usage); + + $('#' + user + " .notify").show().html("Loading..."); + $.getJSON( "/get_usage/" + computer + "/" + user, function( usage ) { + console.log("got results for user:", user); + render_user_to_dom(user, computer, usage); + }); }); $(".save_me" ).click(function() { @@ -156,6 +159,11 @@ ""; }); if(usage.result === 'success') { + $( '#' + user + ' .notify').fadeOut().html('Loaded!').fadeIn(function() { + setTimeout(function() { + $('#' + user + ' .notify').fadeOut("slow"); + }, 1000); + }); $('#' + user + " #time_left").html(secondsToHms(usage.time_left)); $('#' + user + " #time_spent").html(secondsToHms(usage.time_spent)); $("#" + user).append( @@ -198,14 +206,6 @@ }); } - function get_usage(user, computer){ - return $.ajax({ - type: "GET", - url: "/get_usage/" + computer + "/" + user, - async: false - }).responseText; - } - // thanks https://stackoverflow.com/a/37096512 ! function secondsToHms(d) { if (Number(d) === 0){ From 3bd43229ac13da56c3fe3cec19c13f28b4889a07 Mon Sep 17 00:00:00 2001 From: mrjones-plip Date: Wed, 15 Feb 2023 21:00:13 -0800 Subject: [PATCH 03/10] wip ajax --- main.py | 7 +++++++ templates/index.html | 11 +++++++++-- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/main.py b/main.py index a1e4e50..6c831b2 100644 --- a/main.py +++ b/main.py @@ -23,6 +23,13 @@ def get_usage(user, computer, ssh): print(f"Cannot connect to SSH server on host '{computer}'. " f"Check address in conf.py or try again later.") return {'time_left': 0, 'time_spent': 0, 'result': 'fail'} + except AuthenticationException as e: + print(f"Wrong credentials for user '{conf.ssh_user}' on host '{computer}'. " + f"Check `ssh_user` and `ssh_password` credentials in conf.py.") + return {'time_left': 0, 'time_spent': 0, 'result': 'fail'} + except Exception as e: + quit(f"Error logging in as user '{conf.ssh_user}' on host '{computer}', check conf.py. \n\n\t" + str(e)) + return {'time_left': 0, 'time_spent': 0, 'result': 'fail'} search = r"(TIME_LEFT_DAY: )([0-9]+)" time_left = re.search(search, timekpra_userinfo_output) search = r"(TIME_SPENT_DAY: )([0-9]+)" diff --git a/templates/index.html b/templates/index.html index c8498b2..1cd377d 100644 --- a/templates/index.html +++ b/templates/index.html @@ -16,6 +16,9 @@ color: blue; margin-left: 4px; } + .loading { + color: lightgray; + } .error { color: red; margin-left: 4px; @@ -120,14 +123,17 @@ $.each( data, function( computer, user ) { $("#people").append( "" + - "

" + user + " 

" + + "

" + user + "

" + "Unused: ... " + "Used: ...
" ); $('#' + user + " .notify").show().html("Loading..."); + + setInterval(function () { + $('#' + user + " .loading").fadeIn(1300).fadeOut(1300); + }, 1400); $.getJSON( "/get_usage/" + computer + "/" + user, function( usage ) { - console.log("got results for user:", user); render_user_to_dom(user, computer, usage); }); }); @@ -166,6 +172,7 @@ }); $('#' + user + " #time_left").html(secondsToHms(usage.time_left)); $('#' + user + " #time_spent").html(secondsToHms(usage.time_spent)); + $('#' + user + " .loading").removeClass('loading'); $("#" + user).append( "
" + "" + From 8ba574b6a1dbae8d79c402fdd9e32b1db9aa82b0 Mon Sep 17 00:00:00 2001 From: mrjones-plip Date: Fri, 17 Feb 2023 15:20:30 -0800 Subject: [PATCH 04/10] wip ajax and error handling --- main.py | 4 +- templates/index.html | 101 ++++++++++++++++++++++++++++--------------- 2 files changed, 68 insertions(+), 37 deletions(-) diff --git a/main.py b/main.py index 6c831b2..a4b3863 100644 --- a/main.py +++ b/main.py @@ -39,12 +39,14 @@ def get_usage(user, computer, ssh): print(f"Error getting time left, setting to 0. ssh call result: " + str(timekpra_userinfo_output)) time_left = '0' time_spent = '0' + result = 'fail' else: time_left = str(time_left.group(2)) time_spent = str(time_spent.group(2)) + result = 'success' print(f"Time left for {user} at {computer}: {time_spent}") - return {'time_left': time_left, 'time_spent': time_spent, 'result': 'success'} + return {'time_left': time_left, 'time_spent': time_spent, 'result': result} def get_connection(computer): diff --git a/templates/index.html b/templates/index.html index 1cd377d..77f3cd4 100644 --- a/templates/index.html +++ b/templates/index.html @@ -27,6 +27,13 @@ padding-bottom: 15px; display: block; } + .disabled { + border: 1px solid #ddd !important; + color: #ccc !important; + } + .failed_load { + height: 62px; + } /* ========================================================================== Fork Me on Github Stuffs @@ -120,15 +127,37 @@