feat: add dynamic status monitoring

This commit is contained in:
SunBK201 2025-12-05 04:09:14 +08:00
parent 4689d7074e
commit 86a1f2bebf
3 changed files with 76 additions and 12 deletions

View File

@ -2,6 +2,7 @@ module("luci.controller.ua3f", package.seeall)
function index() function index()
entry({ "admin", "services", "ua3f" }, cbi("ua3f"), _("UA3F"), 1) entry({ "admin", "services", "ua3f" }, cbi("ua3f"), _("UA3F"), 1)
entry({ "admin", "services", "ua3f", "status" }, call("get_status")).leaf = true
entry({ "admin", "services", "ua3f", "download_log" }, call("action_download_log")).leaf = true entry({ "admin", "services", "ua3f", "download_log" }, call("action_download_log")).leaf = true
entry({ "admin", "services", "ua3f", "clear_log" }, call("clear_log")).leaf = true entry({ "admin", "services", "ua3f", "clear_log" }, call("clear_log")).leaf = true
entry({ "admin", "services", "ua3f", "get_rules" }, call("get_rules")).leaf = true entry({ "admin", "services", "ua3f", "get_rules" }, call("get_rules")).leaf = true
@ -10,6 +11,22 @@ end
local fs = require("nixio.fs") local fs = require("nixio.fs")
function get_status()
local http = require("luci.http")
local sys = require("luci.sys")
local json = require("luci.jsonc")
http.prepare_content("application/json")
local pid = sys.exec("pidof ua3f")
local running = (pid ~= nil and pid ~= "")
http.write(json.stringify({
running = running,
pid = running and pid:gsub("%s+", "") or nil
}))
end
function create_log_archive() function create_log_archive()
local tmpfile = "/tmp/ua3f_logs.tar.gz" local tmpfile = "/tmp/ua3f_logs.tar.gz"
local copyCfg = "cp /etc/config/ua3f /var/log/ua3f/config >/dev/null 2>&1" local copyCfg = "cp /etc/config/ua3f /var/log/ua3f/config >/dev/null 2>&1"

View File

@ -2,7 +2,6 @@ local M = {}
local cbi = require("luci.cbi") local cbi = require("luci.cbi")
local i18n = require("luci.i18n") local i18n = require("luci.i18n")
local sys = require("luci.sys")
local translate = i18n.translate local translate = i18n.translate
local Flag = cbi.Flag local Flag = cbi.Flag
@ -14,17 +13,7 @@ function M.add_status_fields(section)
-- Running Status Display -- Running Status Display
local running = section:option(DummyValue, "running", translate("Status")) local running = section:option(DummyValue, "running", translate("Status"))
running.rawhtml = true running.template = "ua3f/status"
running.cfgvalue = function(self, section)
local pid = sys.exec("pidof ua3f")
if pid == "" then
return "<input disabled type='button' style='opacity: 1;' class='btn cbi-button cbi-button-reset' value='" ..
translate("Stop") .. "'/>"
else
return "<input disabled type='button' style='opacity: 1;' class='btn cbi-button cbi-button-add' value='" ..
translate("Running") .. "'/>"
end
end
end end
return M return M

View File

@ -0,0 +1,58 @@
<div class="cbi-value">
<label class="cbi-value-title">
<%:Status%>
</label>
<div id="ua3f-status" class="cbi-value-field">
<input disabled type="button" style="opacity: 1;" class="btn cbi-button cbi-button-neutral"
value="<%:Loading%>" />
</div>
</div>
<script type="text/javascript">
(function () {
var statusContainer = document.getElementById('ua3f-status');
var updateInterval = 5000; // 5 seconds
var statusUrl = '<%=luci.dispatcher.build_url("admin/services/ua3f/status")%>';
function updateStatus() {
var xhr = new XMLHttpRequest();
xhr.open('GET', statusUrl, true);
xhr.timeout = 4000;
xhr.onreadystatechange = function () {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
try {
var data = JSON.parse(xhr.responseText);
if (data.running) {
statusContainer.innerHTML = '<input disabled type="button" style="opacity: 1;" class="btn cbi-button cbi-button-add" value="<%:Running%>" />';
} else {
statusContainer.innerHTML = '<input disabled type="button" style="opacity: 1;" class="btn cbi-button cbi-button-reset" value="<%:Stop%>" />';
}
} catch (e) {
statusContainer.innerHTML = '<input disabled type="button" style="opacity: 1;" class="btn cbi-button cbi-button-neutral" value="<%:Error%>" />';
}
} else {
statusContainer.innerHTML = '<input disabled type="button" style="opacity: 1;" class="btn cbi-button cbi-button-neutral" value="<%:Error%>" />';
}
}
};
xhr.onerror = function () {
statusContainer.innerHTML = '<input disabled type="button" style="opacity: 1;" class="btn cbi-button cbi-button-neutral" value="<%:Error%>" />';
};
xhr.ontimeout = function () {
statusContainer.innerHTML = '<input disabled type="button" style="opacity: 1;" class="btn cbi-button cbi-button-neutral" value="<%:Timeout%>" />';
};
xhr.send();
}
// Initial update
updateStatus();
// Set up periodic updates
setInterval(updateStatus, updateInterval);
})();
</script>