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
from flask import render_template
from flask import render_template, jsonify
import app.modules.db.sql as sql
import app.modules.db.server as server_sql
@ -438,14 +438,14 @@ def clear_stick_table(serv, table) -> str:
return 'ok'
def list_of_lists(serv) -> str:
def list_of_lists(serv) -> dict:
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}}\''
output, stderr = server_mod.subprocess_execute(cmd)
try:
return output[0]
except Exception:
return '------'
acl_lists = []
for i in output:
acl_lists.append(i)
return jsonify(acl_lists)
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
import app.modules.common.common as common
import app.modules.config.runtime as runtime
import app.modules.db.server as server_sql
import app.modules.service.haproxy as service_haproxy
@ -21,10 +22,14 @@ def runtimeapi():
return render_template('runtimeapi.html', lang=g.user_params['lang'])
@bp.route('/backends/<int:server_ip>')
@bp.route('/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:
return runtime.show_backends(server_ip)
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.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[2].data.push(data.value2);
}
@ -85,7 +85,7 @@ function stream_chart(chart_id, service, service_ip, is_http=0) {
}
function getHttpChartData(server) {
let hide_http_metrics = localStorage.getItem('hide_http_metrics');
if (hide_http_metrics == 'disabled') {
if (hide_http_metrics === 'disabled') {
return false;
}
$.ajax({
@ -94,7 +94,7 @@ function getHttpChartData(server) {
time_range: $( "#time-range option:selected" ).val(),
},
type: "POST",
success: function (result) {
success: function (result) {
let data = [];
data.push(result.chartData.http_2xx);
data.push(result.chartData.http_3xx);
@ -207,6 +207,7 @@ function renderHttpChart(data, labels, server) {
},
},
});
charts.push(myChart)
stream_chart(myChart, 'haproxy', server, 1);
}
function getChartData(server) {
@ -311,6 +312,7 @@ function renderChart(data, labels, server) {
}
}
});
charts.push(myChart)
stream_chart(myChart, 'haproxy', server);
}
function getWafChartData(server) {
@ -343,7 +345,7 @@ function renderServiceChart(data, labels, server, service) {
if (service === 'waf') {
additional_title = 'WAF ';
}
config = return_service_chart_config();
let config = return_service_chart_config();
for (let i=0; i<label.length; i++) {
config.data.labels.push(label[i])
config.data.datasets[0].data.push(dataArray[i])

View File

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

View File

@ -170,16 +170,8 @@ class ServiceView(MethodView):
except Exception as e:
data = ErrorResponse(error=str(e)).model_dump(mode='json')
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'"
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:
out = server_mod.ssh_command(server.ip, cmd)
out1 = out.split()