v8.0.2: Refactor and optimize code handling in multiple files

Removed unused imports, improved error handling in JavaScript, and refactored server command construction for better reliability. Enhanced type checking and streamlined list processing to improve code readability and efficiency.
pull/399/head
Aidaho 2024-09-26 17:38:35 +03:00
parent 3e849e75fe
commit 34907769e6
6 changed files with 39 additions and 46 deletions

View File

@ -1,6 +1,6 @@
import json import json
from flask import render_template from flask import render_template, jsonify
import app.modules.db.sql as sql import app.modules.db.sql as sql
import app.modules.db.server as server_sql import app.modules.db.server as server_sql
@ -438,14 +438,14 @@ def clear_stick_table(serv, table) -> str:
return 'ok' return 'ok'
def list_of_lists(serv) -> str: def list_of_lists(serv) -> dict:
haproxy_sock_port = sql.get_setting('haproxy_sock_port') haproxy_sock_port = sql.get_setting('haproxy_sock_port')
cmd = f'echo "show acl"|nc {serv} {haproxy_sock_port} |grep "loaded from" |awk \'{{print $1,$2}}\'' cmd = f'echo "show acl"|nc {serv} {haproxy_sock_port} |grep "loaded from" |awk \'{{print $1,$2}}\''
output, stderr = server_mod.subprocess_execute(cmd) output, stderr = server_mod.subprocess_execute(cmd)
try: acl_lists = []
return output[0] for i in output:
except Exception: acl_lists.append(i)
return '------' return jsonify(acl_lists)
def show_lists(serv, list_id, color, list_name) -> str: def show_lists(serv, list_id, color, list_name) -> str:

View File

@ -5,6 +5,7 @@ from app.routes.runtime import bp
from app.middleware import get_user_params from app.middleware import get_user_params
import app.modules.common.common as common import app.modules.common.common as common
import app.modules.config.runtime as runtime import app.modules.config.runtime as runtime
import app.modules.db.server as server_sql
import app.modules.service.haproxy as service_haproxy import app.modules.service.haproxy as service_haproxy
@ -21,10 +22,14 @@ def runtimeapi():
return render_template('runtimeapi.html', lang=g.user_params['lang']) return render_template('runtimeapi.html', lang=g.user_params['lang'])
@bp.route('/backends/<int:server_ip>')
@bp.route('/backends/<server_ip>') @bp.route('/backends/<server_ip>')
def show_backends(server_ip): def show_backends(server_ip):
server_ip = common.is_ip_or_dns(server_ip) if isinstance(server_ip, str):
server_ip = common.is_ip_or_dns(server_ip)
elif isinstance(server_ip, int):
server = server_sql.get_server_by_id(server_ip)
server_ip = server.ip
try: try:
return runtime.show_backends(server_ip) return runtime.show_backends(server_ip)
except Exception as e: except Exception as e:

View File

@ -73,7 +73,7 @@ function stream_chart(chart_id, service, service_ip, is_http=0) {
} }
chart_id.data.labels.push(data.time); chart_id.data.labels.push(data.time);
chart_id.data.datasets[0].data.push(data.value); chart_id.data.datasets[0].data.push(data.value);
if (service == 'haproxy') { if (service === 'haproxy') {
chart_id.data.datasets[1].data.push(data.value1); chart_id.data.datasets[1].data.push(data.value1);
chart_id.data.datasets[2].data.push(data.value2); chart_id.data.datasets[2].data.push(data.value2);
} }
@ -85,7 +85,7 @@ function stream_chart(chart_id, service, service_ip, is_http=0) {
} }
function getHttpChartData(server) { function getHttpChartData(server) {
let hide_http_metrics = localStorage.getItem('hide_http_metrics'); let hide_http_metrics = localStorage.getItem('hide_http_metrics');
if (hide_http_metrics == 'disabled') { if (hide_http_metrics === 'disabled') {
return false; return false;
} }
$.ajax({ $.ajax({
@ -207,6 +207,7 @@ function renderHttpChart(data, labels, server) {
}, },
}, },
}); });
charts.push(myChart)
stream_chart(myChart, 'haproxy', server, 1); stream_chart(myChart, 'haproxy', server, 1);
} }
function getChartData(server) { function getChartData(server) {
@ -311,6 +312,7 @@ function renderChart(data, labels, server) {
} }
} }
}); });
charts.push(myChart)
stream_chart(myChart, 'haproxy', server); stream_chart(myChart, 'haproxy', server);
} }
function getWafChartData(server) { function getWafChartData(server) {
@ -343,7 +345,7 @@ function renderServiceChart(data, labels, server, service) {
if (service === 'waf') { if (service === 'waf') {
additional_title = 'WAF '; additional_title = 'WAF ';
} }
config = return_service_chart_config(); let config = return_service_chart_config();
for (let i=0; i<label.length; i++) { for (let i=0; i<label.length; i++) {
config.data.labels.push(label[i]) config.data.labels.push(label[i])
config.data.datasets[0].data.push(dataArray[i]) config.data.datasets[0].data.push(dataArray[i])

View File

@ -255,33 +255,29 @@ $( function() {
$("#list_serv_select").on('selectmenuchange', function () { $("#list_serv_select").on('selectmenuchange', function () {
$.ajax({ $.ajax({
url: "/runtimeapi/list/" + $('#list_serv_select').val(), url: "/runtimeapi/list/" + $('#list_serv_select').val(),
contentType: "application/json; charset=utf-8",
success: function (data) { success: function (data) {
data = data.replace(/, /g, ','); if (data.status === 'failed') {
if (data.indexOf('error: ') != '-1') { toastr.error(data.error);
toastr.error(data);
} else { } else {
let value = data.split(',');
$('#list_select').find('option').remove(); $('#list_select').find('option').remove();
for (let value of data ) {
for (let i = 0; i < data.split(',').length; i++) { try {
if (value[i] != '') { value = value.replace(/\'/g, '');
try { value = value.replace('(', '');
value[i] = value[i].replace(/\'/g, ''); value = value.replace(')', '');
value[i] = value[i].replace('(', ''); value = value.replace('[', '');
value[i] = value[i].replace(')', ''); value = value.replace(']', '');
value[i] = value[i].replace('[', ''); id = value.split(' ')[0];
value[i] = value[i].replace(']', ''); full_text_option = value.split(' ')[1]
id = value[i].split(' ')[0]; text_option = full_text_option.split('/').slice(-2)[0];
full_text_option = value[i].split(' ')[1] text_option = text_option + '/' + full_text_option.split('/').slice(-1)[0];
text_option = full_text_option.split('/').slice(-2)[0]; } catch (err) {
text_option = text_option + '/' + full_text_option.split('/').slice(-1)[0]; text_option = value;
} catch (err) {
text_option = value[i];
}
$('#list_select').append($("<option title=\"Show list " + text_option + "\"></option>")
.attr("value", id)
.text(text_option));
} }
$('#list_select').append($("<option title=\"Show list " + text_option + "\"></option>")
.attr("value", id)
.text(text_option));
} }
$('#list_select').selectmenu("refresh"); $('#list_select').selectmenu("refresh");
} }
@ -333,7 +329,6 @@ function getTable() {
function getList() { function getList() {
let color = $('#list_select option:selected').text().split('/')[0]; let color = $('#list_select option:selected').text().split('/')[0];
let list_name = $('#list_select option:selected').text().split('/')[1]; let list_name = $('#list_select option:selected').text().split('/')[1];
console.log(list_name)
$.ajax({ $.ajax({
url: "/runtimeapi/list/" + $('#list_serv_select').val() + "/" + $('#list_select').val() + "/" + color + "/" + list_name, url: "/runtimeapi/list/" + $('#list_serv_select').val() + "/" + $('#list_select').val() + "/" + color + "/" + list_name,
success: function (data) { success: function (data) {

View File

@ -11,7 +11,6 @@ import app.modules.service.ha_cluster as ha_cluster
import app.modules.service.installation as service_mod import app.modules.service.installation as service_mod
from app.middleware import get_user_params, page_for_admin, check_group, check_services from app.middleware import get_user_params, page_for_admin, check_group, check_services
from app.modules.roxywi.class_models import BaseResponse, IdResponse, HAClusterRequest, HAClusterVIP from app.modules.roxywi.class_models import BaseResponse, IdResponse, HAClusterRequest, HAClusterVIP
from app.routes.service.routes import services
class HAView(MethodView): class HAView(MethodView):

View File

@ -170,16 +170,8 @@ class ServiceView(MethodView):
except Exception as e: except Exception as e:
data = ErrorResponse(error=str(e)).model_dump(mode='json') data = ErrorResponse(error=str(e)).model_dump(mode='json')
elif service == 'keepalived': elif service == 'keepalived':
try:
os_info = server_sql.select_os_info(server_id)
except RoxywiResourceNotFound:
raise RoxywiResourceNotFound('Cannot find system information')
if "CentOS" in os_info or "Redhat" in os_info:
kp_proc_name = 'keepalived -D'
else:
kp_proc_name = 'keepalived --dont-fork'
cmd = ("sudo /usr/sbin/keepalived -v 2>&1|head -1|awk '{print $2}' && sudo systemctl status keepalived |grep -e 'Active'" cmd = ("sudo /usr/sbin/keepalived -v 2>&1|head -1|awk '{print $2}' && sudo systemctl status keepalived |grep -e 'Active'"
f"|awk '{{print $2, $9$10$11$12$13}}' && ps ax |grep '{kp_proc_name}'|grep -v grep |wc -l") f"|awk '{{print $2, $9$10$11$12$13}}' && ps ax |grep keepalived|grep -v udp|grep -v grep |wc -l")
try: try:
out = server_mod.ssh_command(server.ip, cmd) out = server_mod.ssh_command(server.ip, cmd)
out1 = out.split() out1 = out.split()