diff --git a/src/rust/lqos_bus/src/bus/request.rs b/src/rust/lqos_bus/src/bus/request.rs index 962bf614..a29a04b5 100644 --- a/src/rust/lqos_bus/src/bus/request.rs +++ b/src/rust/lqos_bus/src/bus/request.rs @@ -26,6 +26,14 @@ pub enum BusRequest { end: u32, }, + /// Retrieve the top N uploads by bandwidth use. + GetTopNUploaders { + /// First row to retrieve (usually 0 unless you are paging) + start: u32, + /// Last row to retrieve (10 for top-10 starting at 0) + end: u32, + }, + /// Retrieves the TopN hosts with the worst RTT, sorted by RTT descending. GetWorstRtt { /// First row to retrieve (usually 0 unless you are paging) diff --git a/src/rust/lqos_bus/src/bus/response.rs b/src/rust/lqos_bus/src/bus/response.rs index e421e46b..fe14bbe0 100644 --- a/src/rust/lqos_bus/src/bus/response.rs +++ b/src/rust/lqos_bus/src/bus/response.rs @@ -61,6 +61,9 @@ pub enum BusResponse { /// Provides the Top N downloaders IP stats. TopDownloaders(Vec), + /// Provides the Top N uploaders IP stats. + TopUploaders(Vec), + /// Provides the worst N RTT scores, sorted in descending order. WorstRtt(Vec), diff --git a/src/rust/lqosd/src/main.rs b/src/rust/lqosd/src/main.rs index f3d4e5b4..2ed0bf4e 100644 --- a/src/rust/lqosd/src/main.rs +++ b/src/rust/lqosd/src/main.rs @@ -340,6 +340,9 @@ fn handle_bus_requests(requests: &[BusRequest], responses: &mut Vec BusRequest::GetTopNDownloaders { start, end } => { throughput_tracker::top_n(*start, *end) } + BusRequest::GetTopNUploaders { start, end } => { + throughput_tracker::top_n_up(*start, *end) + } BusRequest::GetWorstRtt { start, end } => throughput_tracker::worst_n(*start, *end), BusRequest::GetWorstRetransmits { start, end } => { throughput_tracker::worst_n_retransmits(*start, *end) diff --git a/src/rust/lqosd/src/node_manager/js_build/src/dashlets/dashlet_index.js b/src/rust/lqosd/src/node_manager/js_build/src/dashlets/dashlet_index.js index 0b8edded..f5e09df6 100644 --- a/src/rust/lqosd/src/node_manager/js_build/src/dashlets/dashlet_index.js +++ b/src/rust/lqosd/src/node_manager/js_build/src/dashlets/dashlet_index.js @@ -30,6 +30,8 @@ import {LtsLast24Hours} from "./ltsLast24Hours"; import {TcpRetransmitsDash} from "./total_retransmits"; import {StormguardStatusDashlet} from "./stormguard_status"; import {BakeryStatusDashlet} from "./bakery_status"; +import {Top10UploadersVisual} from "./top10_uploads_graphic"; +import {Top10Uploaders} from "./top10_uploaders"; export const DashletMenu = [ { name: "Throughput Bits/Second", tag: "throughputBps", size: 3, category: "Throughput" }, @@ -40,6 +42,8 @@ export const DashletMenu = [ { name: "Round-Trip Time Histogram", tag: "rttHistogram", size: 6, category: "RTT" }, { name: "Top 10 Downloaders", tag: "top10downloaders", size: 6, category: "Top 10" }, { name: "Top 10 Downloaders (Visual)", tag: "top10downloadersV", size: 6, category: "Top 10" }, + { name: "Top 10 Uploaders", tag: "top10uploaders", size: 6, category: "Top 10" }, + { name: "Top 10 Uploaders (Visual)", tag: "top10uploadersV", size: 6, category: "Top 10" }, { name: "Worst 10 Round-Trip Time", tag: "worst10downloaders", size: 6, category: "Top 10" }, { name: "Worst 10 Round-Trip Time (Visual)", tag: "worst10downloadersV", size: 6, category: "Top 10" }, { name: "Worst 10 Retransmits", tag: "worst10retransmits", size: 6, category: "Top 10" }, @@ -78,6 +82,8 @@ export function widgetFactory(widgetName, count) { case "rttHistogram3D": widget = new RttHisto3dDash(count); break; case "top10downloaders":widget = new Top10Downloaders(count); break; case "top10downloadersV":widget = new Top10DownloadersVisual(count); break; + case "top10uploaders":widget = new Top10Uploaders(count); break; + case "top10uploadersV":widget = new Top10UploadersVisual(count); break; case "worst10downloaders":widget = new Worst10Downloaders(count); break; case "worst10downloadersV":widget = new Worst10DownloadersVisual(count); break; case "worst10retransmits":widget = new Worst10Retransmits(count); break; diff --git a/src/rust/lqosd/src/node_manager/js_build/src/dashlets/top10_uploaders.js b/src/rust/lqosd/src/node_manager/js_build/src/dashlets/top10_uploaders.js new file mode 100644 index 00000000..12f1bb79 --- /dev/null +++ b/src/rust/lqosd/src/node_manager/js_build/src/dashlets/top10_uploaders.js @@ -0,0 +1,57 @@ +import {clearDashDiv, TopNTableFromMsgData} from "../helpers/builders"; +import {TimedCache} from "../lq_js_common/helpers/timed_cache"; +import {DashletBaseInsight} from "./insight_dashlet_base"; + +export class Top10Uploaders extends DashletBaseInsight { + constructor(slot) { + super(slot); + this.timeCache = new TimedCache(10); + } + + title() { + return "Top 10 Uploaders"; + } + + tooltip() { + return "
Top 10 Uploaders

Top 10 Uploaders by bits per second, including IP address, download and upload rates, round-trip time, TCP retransmits, and shaping plan.

"; + } + + subscribeTo() { + return [ "TopUploads" ]; + } + + buildContainer() { + let base = super.buildContainer(); + base.style.height = "250px"; + base.style.overflow = "auto"; + return base; + } + + canBeSlowedDown() { + return true; + } + + setup() { + super.setup(); + window.timeGraphs.push(this); + } + + onMessage(msg) { + if (msg.event === "TopUploads" && window.timePeriods.activePeriod === "Live") { + let target = document.getElementById(this.id); + + msg.data.forEach((r) => { + let key = r.circuit_id; + this.timeCache.addOrUpdate(key, r, r.bits_per_second.up); + }); + this.timeCache.tick(); + + let items = this.timeCache.get(); + let t = TopNTableFromMsgData(items); + + // Display it + clearDashDiv(this.id, target); + target.appendChild(t); + } + } +} \ No newline at end of file diff --git a/src/rust/lqosd/src/node_manager/js_build/src/dashlets/top10_uploads_graphic.js b/src/rust/lqosd/src/node_manager/js_build/src/dashlets/top10_uploads_graphic.js new file mode 100644 index 00000000..400070a8 --- /dev/null +++ b/src/rust/lqosd/src/node_manager/js_build/src/dashlets/top10_uploads_graphic.js @@ -0,0 +1,49 @@ +import {TopNSankey} from "../graphs/top_n_sankey"; +import {TimedCache} from "../lq_js_common/helpers/timed_cache"; +import {DashletBaseInsight} from "./insight_dashlet_base"; + +export class Top10UploadersVisual extends DashletBaseInsight { + constructor(slot) { + super(slot); + this.timeCache = new TimedCache(10); + } + + title() { + return "Top 10 Uploaders (Visual)"; + } + + tooltip() { + return "
Top 10 Uploaders

The top-10 users by bits-per-second. The ribbon goes red as they approach capacity. The name border indicates round-trip time, while the main label color indicates TCP retransmits. The legend is current upload speed, followed by number of retransmits.

"; + } + + subscribeTo() { + return [ "TopUploads" ]; + } + + buildContainer() { + let base = super.buildContainer(); + base.appendChild(this.graphDiv()); + return base; + } + + canBeSlowedDown() { + return true; + } + + setup() { + super.setup(); + this.graph = new TopNSankey(this.graphDivId(), true); + } + + onMessage(msg) { + if (msg.event === "TopUploads") { + msg.data.forEach((r) => { + let key = r.circuit_id; + this.timeCache.addOrUpdate(key, r, r.bits_per_second.up); + }); + this.timeCache.tick(); + let items = this.timeCache.get(); + this.graph.processMessage({ data: items }); + } + } +} diff --git a/src/rust/lqosd/src/node_manager/js_build/src/graphs/top_n_sankey.js b/src/rust/lqosd/src/node_manager/js_build/src/graphs/top_n_sankey.js index 087b5369..d622c429 100644 --- a/src/rust/lqosd/src/node_manager/js_build/src/graphs/top_n_sankey.js +++ b/src/rust/lqosd/src/node_manager/js_build/src/graphs/top_n_sankey.js @@ -24,12 +24,12 @@ function lerpViridis(t) { let b = Math.round(c0[2] + frac * (c1[2] - c0[2])); return "#" + ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1); } -import {scaleNumber} from "../lq_js_common/helpers/scaling"; import {isRedacted} from "../helpers/redact"; export class TopNSankey extends DashboardGraph { - constructor(id) { + constructor(id, upload=false) { super(id); + this.upload = upload; this.nodeMap = {}; this.option = { series: [ @@ -49,23 +49,6 @@ export class TopNSankey extends DashboardGraph { this.option.series[0].links = links; this.chart.hideLoading(); this.chart.setOption(this.option); - - /*this.chart.on('click', (params) => { - let name = params.name; - // Trim to before " (" - name = name.substring(0, name.indexOf(" (")); - if (name.indexOf(" > ") === -1) { - if (this.nodeMap[name] !== undefined) { - window.location.href = "/circuit.html?id=" + encodeURI(this.nodeMap[name]); - } - } else { - let actualName = params.data.target; - actualName = actualName.substring(0, actualName.indexOf(" (")); - if (this.nodeMap[actualName] !== undefined) { - window.location.href = "/circuit.html?id=" + encodeURI(this.nodeMap[actualName]); - } - } - });*/ } processMessage(msg) { @@ -92,10 +75,14 @@ export class TopNSankey extends DashboardGraph { if (isRedacted()) label.fontFamily = "Illegible"; let name = r.ip_address; - let bytes = r.bits_per_second.down / 8; - let bytesAsMegabits = bytes / 1000000; - let maxBytes = r.plan.down / 8; - let percent = Math.min(100, (bytesAsMegabits / maxBytes) * 100); + // Choose the correct direction for value and capacity coloring + const bps = this.upload ? r.bits_per_second.up : r.bits_per_second.down; + const planMbps = this.upload ? r.plan.up : r.plan.down; + // Convert bits/s to MB/s (decimal) and Mbps plan to MB/s for a comparable ratio + const bytes = bps / 8; + const bytesAsMegabytes = bytes / 1000000; + const maxBytes = planMbps / 8; + const percent = Math.min(100, (maxBytes > 0 ? (bytesAsMegabytes / maxBytes) * 100 : 0)); let capacityColor = isColorBlindMode() ? lerpViridis(percent / 100) : lerpGreenToRedViaOrange(100 - percent, 100); @@ -119,11 +106,12 @@ export class TopNSankey extends DashboardGraph { borderColor: rttColor, } }); - + + let value = this.upload ? r.bits_per_second.up : r.bits_per_second.down; links.push({ source: "Root", target: name, - value: r.bits_per_second.down, + value, lineStyle: { color: capacityColor } @@ -132,4 +120,4 @@ export class TopNSankey extends DashboardGraph { this.update(nodes, links); } -} \ No newline at end of file +} diff --git a/src/rust/lqosd/src/node_manager/ws/published_channels.rs b/src/rust/lqosd/src/node_manager/ws/published_channels.rs index 15e03d6a..d83dd545 100644 --- a/src/rust/lqosd/src/node_manager/ws/published_channels.rs +++ b/src/rust/lqosd/src/node_manager/ws/published_channels.rs @@ -10,6 +10,7 @@ pub enum PublishedChannels { RttHistogram, FlowCount, TopDownloads, + TopUploads, WorstRTT, WorstRetransmits, TopFlowsBytes, diff --git a/src/rust/lqosd/src/node_manager/ws/ticker.rs b/src/rust/lqosd/src/node_manager/ws/ticker.rs index cbae34f1..d01e15b1 100644 --- a/src/rust/lqosd/src/node_manager/ws/ticker.rs +++ b/src/rust/lqosd/src/node_manager/ws/ticker.rs @@ -52,6 +52,7 @@ async fn one_second_cadence( rtt_histogram::rtt_histo(channels.clone(), bus_tx.clone()), flow_counter::flow_count(channels.clone(), bus_tx.clone()), top_10::top_10_downloaders(channels.clone(), bus_tx.clone()), + top_10::top_10_uploaders(channels.clone(), bus_tx.clone()), top_10::worst_10_downloaders(channels.clone(), bus_tx.clone()), top_10::worst_10_retransmit(channels.clone(), bus_tx.clone()), top_flows::top_flows_bytes(channels.clone(), bus_tx.clone()), diff --git a/src/rust/lqosd/src/node_manager/ws/ticker/top_10.rs b/src/rust/lqosd/src/node_manager/ws/ticker/top_10.rs index 94b062e5..593bf615 100644 --- a/src/rust/lqosd/src/node_manager/ws/ticker/top_10.rs +++ b/src/rust/lqosd/src/node_manager/ws/ticker/top_10.rs @@ -48,6 +48,48 @@ pub async fn top_10_downloaders( } } +pub async fn top_10_uploaders( + channels: Arc, + bus_tx: Sender<(tokio::sync::oneshot::Sender, BusRequest)>, +) { + if !channels + .is_channel_alive(PublishedChannels::TopUploads) + .await + { + return; + } + + let (tx, rx) = tokio::sync::oneshot::channel::(); + let request = BusRequest::GetTopNUploaders { start: 0, end: 10 }; + if let Err(e) = bus_tx.send((tx, request)).await { + tracing::warn!("TopUploads: failed to send request to bus: {:?}", e); + return; + } + let replies = match rx.await { + Ok(r) => r, + Err(e) => { + tracing::warn!("TopUploads: failed to receive throughput from bus: {:?}", e); + return; + } + }; + for reply in replies.responses.into_iter() { + if let BusResponse::TopUploaders(top) = reply { + let result: Vec = top.iter().map(|stat| stat.into()).collect(); + + let message = json!( + { + "event": PublishedChannels::TopUploads.to_string(), + "data": result + } + ) + .to_string(); + channels + .send(PublishedChannels::TopUploads, message) + .await; + } + } +} + pub async fn worst_10_downloaders( channels: Arc, bus_tx: Sender<(tokio::sync::oneshot::Sender, BusRequest)>, diff --git a/src/rust/lqosd/src/throughput_tracker/mod.rs b/src/rust/lqosd/src/throughput_tracker/mod.rs index a2e8a70e..26dab992 100644 --- a/src/rust/lqosd/src/throughput_tracker/mod.rs +++ b/src/rust/lqosd/src/throughput_tracker/mod.rs @@ -347,6 +347,50 @@ pub fn top_n(start: u32, end: u32) -> BusResponse { BusResponse::TopDownloaders(result) } +pub fn top_n_up(start: u32, end: u32) -> BusResponse { + let mut full_list: Vec = { + let tp_cycle = THROUGHPUT_TRACKER + .cycle + .load(std::sync::atomic::Ordering::Relaxed); + THROUGHPUT_TRACKER + .raw_data + .lock() + .iter() + .filter(|(k, _v)| !k.as_ip().is_loopback()) + .filter(|(_k, d)| retire_check(tp_cycle, d.most_recent_cycle)) + .map(|(k, te)| { + ( + *k, + te.bytes_per_second, + te.packets_per_second, + te.median_latency().unwrap_or(0.0), + te.tc_handle, + te.circuit_id.as_ref().unwrap_or(&String::new()).clone(), + down_up_divide(te.tcp_retransmits, te.tcp_packets), + ) + }) + .collect() + }; + full_list.sort_by(|a, b| b.1.up.cmp(&a.1.up)); + let result = full_list + .iter() + //.skip(start as usize) + .take((end as usize) - (start as usize)) + .map( + |(ip, bytes, packets, median_rtt, tc_handle, circuit_id, tcp_retransmits)| IpStats { + ip_address: ip.as_ip().to_string(), + circuit_id: circuit_id.clone(), + bits_per_second: bytes.to_bits_from_bytes(), + packets_per_second: *packets, + median_tcp_rtt: *median_rtt, + tc_handle: *tc_handle, + tcp_retransmits: *tcp_retransmits, + }, + ) + .collect(); + BusResponse::TopUploaders(result) +} + pub fn worst_n(start: u32, end: u32) -> BusResponse { let mut full_list: Vec = { let tp_cycle = THROUGHPUT_TRACKER -- knotmirror 0.0.1 From a3b2ef4ae5196062bd3d2084fc25f6eb2f26059b Mon Sep 17 00:00:00 2001 From: Herbert Wolverson Date: Thu, 6 Nov 2025 09:24:55 -0600 Subject: [PATCH 02/13] Feat: New binpacking algorithm, aiming for fewer changes over time (reduced reload churn). --- .gitignore | 1 + src/LibreQoS.py | 259 ++++++++++++++++++++++++++++++++++++++-------- src/README.md | 4 + src/build_dpkg.sh | 6 +- 4 files changed, 223 insertions(+), 47 deletions(-) diff --git a/.gitignore b/.gitignore index c670003c..c32041b0 100644 --- a/.gitignore +++ b/.gitignore @@ -141,3 +141,4 @@ src/bin/lqos_api /src/linux_tc_rust.txt /src/linux_tc_rust_live.txt /src/linux_tc_rust_prune.txt +/src/planner_state.json diff --git a/src/LibreQoS.py b/src/LibreQoS.py index f02ba0ec..97a28638 100755 --- a/src/LibreQoS.py +++ b/src/LibreQoS.py @@ -18,7 +18,7 @@ import psutil import argparse import logging import shutil -import binpacking +import time from deepdiff import DeepDiff from liblqos_python import is_lqosd_alive, clear_ip_mappings, delete_ip_mapping, validate_shaped_devices, \ @@ -665,29 +665,110 @@ def refreshShapers(): } generatedPNs.append(genPNname) if use_bin_packing_to_balance_cpu(): - print("Using binpacking module to sort circuits by CPU core") - weights = get_weights() - if weights is not None and dictForCircuitsWithoutParentNodes is not None: - for circuitId in dictForCircuitsWithoutParentNodes: - for circuit in subscriberCircuits: - if 'idForCircuitsWithoutParentNodes' in circuit and circuit['idForCircuitsWithoutParentNodes'] == circuitId: - for w in weights: - if w.circuit_id == circuit['circuitID']: - dictForCircuitsWithoutParentNodes[circuitId] = w.weight - bins = binpacking.to_constant_bin_number(dictForCircuitsWithoutParentNodes, numberOfGeneratedPNs) - genPNcounter = 0 - for binItem in bins: - sumItem = 0 - logging.info(generatedPNs[genPNcounter] + " will contain " + str(len(binItem)) + " circuits") - for key in binItem.keys(): - for circuit in subscriberCircuits: - if circuit['ParentNode'] == 'none': - if circuit['idForCircuitsWithoutParentNodes'] == key: - circuit['ParentNode'] = generatedPNs[genPNcounter] - genPNcounter += 1 - if genPNcounter >= queuesAvailable: - genPNcounter = 0 - print("Finished binpacking") + print("Using internal planner to sort circuits by CPU core") + # Build item list with weights for circuits lacking a ParentNode + items = [] + try: + weights = get_weights() + except Exception as e: + warnings.warn("get_weights() failed; defaulting to equal weights (" + str(e) + ")", stacklevel=2) + weights = None + weight_by_circuit_id = {} + if weights is not None: + try: + for w in weights: + weight_by_circuit_id[str(w.circuit_id)] = float(w.weight) + except Exception: + pass + for circuit in subscriberCircuits: + if circuit.get('ParentNode') == 'none' and 'idForCircuitsWithoutParentNodes' in circuit: + item_id = circuit['idForCircuitsWithoutParentNodes'] + # Prefer provided weights; default to 1.0 + w = dictForCircuitsWithoutParentNodes.get(item_id, 1.0) + # If a specific circuit weight exists, prefer it + if 'circuitID' in circuit and str(circuit['circuitID']) in weight_by_circuit_id: + w = weight_by_circuit_id[str(circuit['circuitID'])] + items.append({"id": item_id, "weight": float(w)}) + + # Prepare bins and capacities + bins_list = [{"id": pn} for pn in generatedPNs] + capacities = {pn: 1.0 for pn in generatedPNs} + + # Load planner state + try: + import bin_planner + except ImportError: + bin_planner = None + # Store planner state directly in lqos_directory (no hidden subdirs) + state_path = os.path.join(get_libreqos_directory(), "planner_state.json") + state = {} + if bin_planner is not None: + state = bin_planner.load_state(state_path) + now_ts = time.time() + prev_assign = {} + last_change_ts = {} + if isinstance(state, dict): + prev_assign = state.get("assignments", {}) or {} + last_change_ts = state.get("last_change_ts", {}) or {} + # Filter previous assignments to only items/bins in this context + item_ids = {str(it["id"]) for it in items} + valid_bins = set(capacities.keys()) + prev_assign = {iid: b for iid, b in prev_assign.items() if iid in item_ids and b in valid_bins} + last_change_ts = {iid: last_change_ts.get(iid, 0.0) for iid in item_ids} + + # Planner parameters + params = { + "candidate_set_size": 4, + "headroom": 0.05, + "alpha": 0.1, + "hysteresis_threshold": 0.03, + "cooldown_seconds": 3600, + "move_budget_per_run": max(1, min(32, int(0.01 * max(1, len(items))))), + "salt": state.get("salt", "default_salt") if isinstance(state, dict) else "default_salt", + "last_change_ts_by_item": last_change_ts, + } + if monitor_mode_only() == True: + params["move_budget_per_run"] = 0 + + if bin_planner is not None: + assignments, changed = bin_planner.plan_assignments( + items, bins_list, capacities, prev_assign, now_ts, params + ) + else: + # Fallback to simple greedy if planner unavailable + bin_loads = {pn: 0.0 for pn in generatedPNs} + pairs = [(str(it["id"]), float(it["weight"])) for it in items] + pairs.sort(key=lambda iw: (-iw[1], str(iw[0]))) + assignments = {} + for item_id, w in pairs: + target_pn = min(bin_loads.items(), key=lambda kv: (kv[1], kv[0]))[0] + assignments[item_id] = target_pn + bin_loads[target_pn] += w + changed = list(assignments.keys()) + + # Apply assignments to circuits + for circuit in subscriberCircuits: + if circuit.get('ParentNode') == 'none' and 'idForCircuitsWithoutParentNodes' in circuit: + item_id = circuit['idForCircuitsWithoutParentNodes'] + if item_id in assignments: + circuit['ParentNode'] = assignments[item_id] + + # Update and save state + if bin_planner is not None and isinstance(state, dict): + if state.get("salt") is None: + state["salt"] = "default_salt" + if "assignments" not in state or not isinstance(state["assignments"], dict): + state["assignments"] = {} + if "last_change_ts" not in state or not isinstance(state["last_change_ts"], dict): + state["last_change_ts"] = {} + for iid, b in assignments.items(): + # record last change time if changed + if iid in changed: + state["last_change_ts"][iid] = now_ts + state["assignments"][iid] = b + bin_planner.save_state(state_path, state) + + print("Finished planning generated parent nodes") else: genPNcounter = 0 for circuit in subscriberCircuits: @@ -1008,21 +1089,87 @@ def refreshShapers(): return minorByCPU # If we're in binpacking mode, we need to sort the network structure a bit - bins = [] if use_bin_packing_to_balance_cpu() and not is_network_flat(): - print("BinPacking is enabled, so we're going to sort your network.") - cpuBin = {} - weights = get_tree_weights() - # Stable tie-breaking: sort by (-weight, name) so equal weights have deterministic order + print("Planner is enabled, so we're going to sort your network across CPU queues.") + # Build items from top-level nodes with weights + items = [] try: - weights = sorted(weights, key=lambda w: (-int(w.weight), str(w.name))) - except Exception: - pass - for w in weights: - cpuBin[w.name] = w.weight - bins = binpacking.to_constant_bin_number(cpuBin, queuesAvailable) + weights = get_tree_weights() + except Exception as e: + warnings.warn("get_tree_weights() failed; defaulting to equal weights (" + str(e) + ")", stacklevel=2) + weights = None + weight_by_name = {} + if weights is not None: + try: + for w in weights: + weight_by_name[str(w.name)] = float(w.weight) + except Exception: + pass + for node in network: + w = weight_by_name.get(str(node), 1.0) + items.append({"id": str(node), "weight": float(w)}) + + # Prepare bins and capacities + cpu_keys = ["CpueQueue" + str(cpu) for cpu in range(queuesAvailable)] + bins_list = [{"id": key} for key in cpu_keys] + capacities = {key: 1.0 for key in cpu_keys} + + # Load planner state + try: + import bin_planner + except ImportError: + bin_planner = None + # Store planner state directly in lqos_directory (no hidden subdirs) + state_path = os.path.join(get_libreqos_directory(), "planner_state.json") + state = {} + if bin_planner is not None: + state = bin_planner.load_state(state_path) + now_ts = time.time() + prev_assign = {} + last_change_ts = {} + if isinstance(state, dict): + prev_assign = state.get("assignments", {}) or {} + last_change_ts = state.get("last_change_ts", {}) or {} + item_ids = {str(it["id"]) for it in items} + valid_bins = set(capacities.keys()) + prev_assign = {iid: b for iid, b in prev_assign.items() if iid in item_ids and b in valid_bins} + last_change_ts = {iid: last_change_ts.get(iid, 0.0) for iid in item_ids} + + params = { + "candidate_set_size": 4, + "headroom": 0.05, + "alpha": 0.1, + "hysteresis_threshold": 0.03, + "cooldown_seconds": 3600, + "move_budget_per_run": max(1, min(32, int(0.01 * max(1, len(items))))), + "salt": state.get("salt", "default_salt") if isinstance(state, dict) else "default_salt", + "last_change_ts_by_item": last_change_ts, + } + if monitor_mode_only() == True: + params["move_budget_per_run"] = 0 + + if bin_planner is not None: + assignment, changed = bin_planner.plan_assignments( + items, bins_list, capacities, prev_assign, now_ts, params + ) + else: + # Fallback to simple greedy if planner unavailable + bin_loads = {key: 0.0 for key in cpu_keys} + pairs = [(str(it["id"]), float(it["weight"])) for it in items] + pairs.sort(key=lambda nw: (-nw[1], nw[0])) + assignment = {} + for name, w in pairs: + target = min(bin_loads.items(), key=lambda kv: (kv[1], kv[0]))[0] + assignment[name] = target + bin_loads[target] += w + changed = list(assignment.keys()) + for x in range(queuesAvailable): - print("Bin " + str(x) + " = ", bins[x]) + key = "CpueQueue" + str(x) + assigned = [name for name, tgt in assignment.items() if tgt == key] + print("Bin " + str(x) + " = ", assigned) + + # Build the binned network structure binnedNetwork = {} for cpu in range(queuesAvailable): cpuKey = "CpueQueue" + str(cpu) @@ -1036,16 +1183,26 @@ def refreshShapers(): 'name': cpuKey } for node in network: - found = False - for bin in range(queuesAvailable): - if node in bins[bin]: - binnedNetwork["CpueQueue" + str(bin)]['children'][node] = network[node] - found = True - if found == False: - newQueueId = queuesAvailable-1 - binnedNetwork["CpueQueue" + str(newQueueId)]['children'][node] = network[node] + tgt = assignment.get(node) + if tgt is None: + tgt = "CpueQueue" + str(queuesAvailable - 1) + binnedNetwork[tgt]['children'][node] = network[node] network = binnedNetwork + # Update and save state + if bin_planner is not None and isinstance(state, dict): + if state.get("salt") is None: + state["salt"] = "default_salt" + if "assignments" not in state or not isinstance(state["assignments"], dict): + state["assignments"] = {} + if "last_change_ts" not in state or not isinstance(state["last_change_ts"], dict): + state["last_change_ts"] = {} + for iid, b in assignment.items(): + if iid in changed: + state["last_change_ts"][iid] = now_ts + state["assignments"][iid] = b + bin_planner.save_state(state_path, state) + # Here is the actual call to the recursive traverseNetwork() function. finalMinor is not used. minorByCPU = traverseNetwork(network, 0, major=1, minorByCPU=minorByCPUpreloaded, queue=1, parentClassID=None, upParentClassID=None, parentMaxDL=upstream_bandwidth_capacity_download_mbps(), parentMaxUL=upstream_bandwidth_capacity_upload_mbps(), parentMinDL=upstream_bandwidth_capacity_download_mbps(), parentMinUL=upstream_bandwidth_capacity_upload_mbps()) @@ -1535,9 +1692,23 @@ if __name__ == '__main__': help="Clear ip filters, qdiscs, and xdp setup if any", action=argparse.BooleanOptionalAction, ) + parser.add_argument( + '--planner-reset', + help="Delete planner state file before running", + action=argparse.BooleanOptionalAction, + ) args = parser.parse_args() logging.basicConfig(level=args.loglevel) + if getattr(args, 'planner_reset', False): + try: + state_path = os.path.join(get_libreqos_directory(), "planner_state.json") + if os.path.exists(state_path): + os.remove(state_path) + print(f"Removed planner state: {state_path}") + except Exception as e: + print(f"Warning: could not remove planner state: {e}") + if args.validate: status = validateNetworkAndDevices() elif args.clearrules: diff --git a/src/README.md b/src/README.md index 965f51ca..b85aeee4 100644 --- a/src/README.md +++ b/src/README.md @@ -3,3 +3,7 @@ ![image](https://i0.wp.com/libreqos.io/wp-content/uploads/2023/01/v1.4-alpha-2.png?w=3664&ssl=1) See [wiki here](https://github.com/LibreQoE/LibreQoS/wiki/v1.4) + +## Planner (binpacking replacement) + +LibreQoS now uses an internal planner for assigning circuits and nodes to CPU queues and generated parent nodes. The planner favors stable, stateful assignments to minimize churn and reduce rebuilds, replacing the previous external binpacking dependency. diff --git a/src/build_dpkg.sh b/src/build_dpkg.sh index 401f0e10..f0167bb0 100755 --- a/src/build_dpkg.sh +++ b/src/build_dpkg.sh @@ -113,9 +113,9 @@ pushd /opt/libreqos PIP_BREAK_SYSTEM_PACKAGES=1 python3 -m pip install -r src/requirements.txt sudo PIP_BREAK_SYSTEM_PACKAGES=1 python3 -m pip install -r src/requirements.txt # - Setup Python dependencies as a post-install task - handle issue with packages on Ubuntu Server 24.04 -sudo PIP_BREAK_SYSTEM_PACKAGES=1 pip uninstall binpacking apscheduler deepdiff --yes -PIP_BREAK_SYSTEM_PACKAGES=1 pip uninstall binpacking apscheduler deepdiff --yes -sudo PIP_BREAK_SYSTEM_PACKAGES=1 pip install binpacking apscheduler deepdiff +sudo PIP_BREAK_SYSTEM_PACKAGES=1 pip uninstall apscheduler deepdiff --yes +PIP_BREAK_SYSTEM_PACKAGES=1 pip uninstall apscheduler deepdiff --yes +sudo PIP_BREAK_SYSTEM_PACKAGES=1 pip install apscheduler deepdiff # Ensure folder permissions are correct post-install sudo chown -R $USER /opt/libreqos -- knotmirror 0.0.1 From 3d28df9714c4fc772d30ef05a7a1406c39a551f2 Mon Sep 17 00:00:00 2001 From: Herbert Wolverson Date: Thu, 6 Nov 2025 11:49:33 -0600 Subject: [PATCH 03/13] Feat: UI for viewing CPU affinity. --- .../src/node_manager/js_build/esbuild.sh | 2 +- .../node_manager/js_build/src/cpu_weights.js | 288 +++++++++++ src/rust/lqosd/src/node_manager/local_api.rs | 14 + .../node_manager/local_api/cpu_affinity.rs | 466 ++++++++++++++++++ .../src/node_manager/static2/cpu_weights.html | 50 ++ .../src/node_manager/static2/template.html | 6 + .../lqosd/src/node_manager/static_pages.rs | 1 + 7 files changed, 826 insertions(+), 1 deletion(-) create mode 100644 src/rust/lqosd/src/node_manager/js_build/src/cpu_weights.js create mode 100644 src/rust/lqosd/src/node_manager/local_api/cpu_affinity.rs create mode 100644 src/rust/lqosd/src/node_manager/static2/cpu_weights.html diff --git a/src/rust/lqosd/src/node_manager/js_build/esbuild.sh b/src/rust/lqosd/src/node_manager/js_build/esbuild.sh index 88ceb596..32e763c8 100755 --- a/src/rust/lqosd/src/node_manager/js_build/esbuild.sh +++ b/src/rust/lqosd/src/node_manager/js_build/esbuild.sh @@ -1,6 +1,6 @@ #!/bin/bash set -e -scripts=( index.js template.js login.js first-run.js shaped-devices.js tree.js help.js unknown-ips.js configuration.js circuit.js flow_map.js all_tree_sankey.js asn_explorer.js lts_trial.js config_general.js config_tuning.js config_queues.js config_stormguard.js config_lts.js config_iprange.js config_flows.js config_integration.js config_spylnx.js config_netzur.js config_uisp.js config_powercode.js config_sonar.js config_interface.js config_network.js config_devices.js config_users.js config_wispgate.js chatbot.js) +scripts=( index.js template.js login.js first-run.js shaped-devices.js tree.js help.js unknown-ips.js configuration.js circuit.js flow_map.js all_tree_sankey.js asn_explorer.js lts_trial.js config_general.js config_tuning.js config_queues.js config_stormguard.js config_lts.js config_iprange.js config_flows.js config_integration.js config_spylnx.js config_netzur.js config_uisp.js config_powercode.js config_sonar.js config_interface.js config_network.js config_devices.js config_users.js config_wispgate.js chatbot.js cpu_weights.js) for script in "${scripts[@]}" do echo "Building {$script}" diff --git a/src/rust/lqosd/src/node_manager/js_build/src/cpu_weights.js b/src/rust/lqosd/src/node_manager/js_build/src/cpu_weights.js new file mode 100644 index 00000000..6d2c311a --- /dev/null +++ b/src/rust/lqosd/src/node_manager/js_build/src/cpu_weights.js @@ -0,0 +1,288 @@ +import { clearDiv, theading, simpleRow, simpleRowHtml, enableTooltips } from "./helpers/builders"; +import { DashboardGraph } from "./graphs/dashboard_graph"; + +let state = { + selectedCpu: null, + direction: "down", + page: 1, + pageSize: 50, + plannerEnabled: null, +}; + +let dlMaxPie = null; // dashboard-graph wrapped chart for DL Max distribution + +function fmtMbps(x) { + if (x === null || x === undefined) return "-"; + // keep up to 2 decimals, trim trailing zeros + let s = Number(x).toFixed(2); + s = s.replace(/\.00$/, ""); + return s; +} + +function renderSummary(data) { + const target = document.getElementById("cpuSummary"); + clearDiv(target); + if (!data || !Array.isArray(data) || data.length === 0) { + target.innerHTML = '

No queue structure found. Run LibreQoS to generate queuingStructure.json.

'; + return; + } + + // Update binpacking badge in header bar + const badge = document.getElementById('binpackBadge'); + const enabled = !!state.plannerEnabled; + if (badge) { + badge.className = `badge ${enabled ? 'bg-success' : 'bg-secondary'}`; + badge.textContent = enabled ? "Binpacking: Enabled" : "Binpacking: Disabled"; + } + // Optional Insight promo below summary + if (!window.hasInsight) { + const promo = document.createElement("div"); + promo.className = "text-muted small mb-2"; + promo.innerHTML = `Enable Insight to make binpacking smarter with historical and recent data.`; + target.appendChild(promo); + } + + // Build a row with table (left) and chart (right) + const row = document.createElement('div'); + row.className = 'row'; + const left = document.createElement('div'); + left.className = 'col-12 col-lg-8'; + const right = document.createElement('div'); + right.className = 'col-12 col-lg-4'; + + const table = document.createElement("table"); + table.classList.add("table", "table-striped", "table-sm"); + const thead = document.createElement("thead"); + thead.appendChild(theading("CPU")); + thead.appendChild(theading("DL Circuits")); + thead.appendChild(theading("DL Min (Mbps)")); + thead.appendChild(theading("DL Max (Mbps)")); + thead.appendChild(theading("Actions")); + table.appendChild(thead); + const tbody = document.createElement("tbody"); + + // helper to build a centered cell + const centerCell = (text) => { + const td = simpleRow(String(text)); + td.classList.add('text-center'); + return td; + }; + + data.forEach((row) => { + const tr = document.createElement("tr"); + tr.appendChild(centerCell(row.cpu)); + tr.appendChild(centerCell(row.download.circuits)); + tr.appendChild(centerCell(fmtMbps(row.download.min_sum_mbps))); + tr.appendChild(centerCell(fmtMbps(row.download.max_sum_mbps))); + + const actions = document.createElement("td"); + actions.classList.add('text-center'); + const downBtn = document.createElement("button"); + downBtn.className = "btn btn-sm btn-outline-primary me-1"; + downBtn.innerHTML = " View"; + downBtn.onclick = () => { + state.selectedCpu = row.cpu; + state.direction = "down"; + state.page = 1; + renderDetailsHeader(); + fetchCircuits(); + }; + + actions.appendChild(downBtn); + tr.appendChild(actions); + tbody.appendChild(tr); + }); + + table.appendChild(tbody); + left.appendChild(table); + + // Right column pie chart (DL Max per CPU) + const chartDiv = document.createElement('div'); + chartDiv.id = 'cpuDlMaxPie'; + chartDiv.style.width = '100%'; + chartDiv.style.height = '260px'; + right.appendChild(chartDiv); + + row.appendChild(left); + row.appendChild(right); + target.appendChild(row); + + // Build donut (ring) chart with theme support + try { + // Recreate the graph each render to bind to the fresh DOM + if (dlMaxPie && dlMaxPie.chart) { + try { dlMaxPie.chart.dispose(); } catch (_) {} + dlMaxPie = null; + } + dlMaxPie = new DashboardGraph('cpuDlMaxPie'); + const seriesData = data.map((row) => ({ + name: `CPU ${row.cpu}`, + value: Number(row.download.max_sum_mbps || 0) + })); + dlMaxPie.option = { + tooltip: { trigger: 'item' }, + legend: { show: false }, + series: [ + { + name: 'DL Max by CPU', + type: 'pie', + radius: ['55%','80%'], + center: ['50%', '50%'], + avoidLabelOverlap: true, + data: seriesData, + label: { + show: true, + formatter: '{b}: {d}%' + } + } + ] + }; + dlMaxPie.chart.setOption(dlMaxPie.option); + dlMaxPie.chart.hideLoading(); + } catch (e) { + // If echarts is unavailable, leave chart blank + } +} + +function renderDetailsHeader() { + const label = document.getElementById("currentCpuLabel"); + if (state.selectedCpu === null) { + label.textContent = ""; + } else { + label.textContent = ` – CPU ${state.selectedCpu}`; + } +} + +function renderCircuits(page) { + const target = document.getElementById("circuitsTable"); + clearDiv(target); + + if (!page || !Array.isArray(page.items)) { + target.innerHTML = '

No circuits found for this CPU.

'; + return; + } + + const smallNote = document.createElement("div"); + smallNote.className = "text-muted small mb-1"; + smallNote.innerText = `Total: ${page.total.toLocaleString()} circuits`; + target.appendChild(smallNote); + + const table = document.createElement("table"); + table.classList.add("table", "table-striped", "table-sm"); + const thead = document.createElement("thead"); + thead.appendChild(theading("Circuit ID")); + thead.appendChild(theading("Circuit Name")); + thead.appendChild(theading("Parent")); + thead.appendChild(theading("ClassID")); + thead.appendChild(theading("Min (Mbps)")); + thead.appendChild(theading("Max (Mbps)")); + thead.appendChild(theading("IPs")); + table.appendChild(thead); + const tbody = document.createElement("tbody"); + + page.items.forEach((c) => { + const tr = document.createElement("tr"); + const idCell = document.createElement("td"); + if (c.circuit_id) { + const a = document.createElement("a"); + a.href = `circuit.html?id=${encodeURIComponent(c.circuit_id)}`; + a.textContent = c.circuit_id; + idCell.appendChild(a); + } else { + idCell.textContent = ""; + } + tr.appendChild(idCell); + tr.appendChild(simpleRow(c.circuit_name || "")); + tr.appendChild(simpleRow(c.parent_node || "")); + tr.appendChild(simpleRow(c.classid || "")); + tr.appendChild(simpleRow(fmtMbps(c.min_mbps))); + tr.appendChild(simpleRow(fmtMbps(c.max_mbps))); + tr.appendChild(simpleRow(String(c.ip_count || 0))); + tbody.appendChild(tr); + }); + + table.appendChild(tbody); + target.appendChild(table); +} + +async function fetchSummary() { + try { + const r = await fetch("/local-api/cpuAffinity/summary"); + if (!r.ok) throw new Error(`${r.status}`); + const data = await r.json(); + // data is { entries: [...] } or just array depending on server; we returned a tuple-like wrapper + const entries = Array.isArray(data) ? data : (data["0"] ? data : (data["entries"] || data)); + // Get planner enabled flag from config + try { + const c = await fetch("/local-api/getConfig"); + if (c.ok) { + const cfg = await c.json(); + state.plannerEnabled = !!(cfg.queues && cfg.queues.use_binpacking); + } + } catch (_) {} + renderSummary(entries); // In our implementation: array + } catch (e) { + document.getElementById("cpuSummary").innerHTML = `
Failed to load summary: ${e}
`; + } +} + +async function fetchCircuits() { + const target = document.getElementById("circuitsTable"); + target.innerHTML = ' Loading circuits...'; + const dir = state.direction; + const cpu = state.selectedCpu; + if (cpu === null || cpu === undefined) { + target.innerHTML = ' Select a CPU from the table above.'; + return; + } + const params = new URLSearchParams(); + params.set("direction", dir); + params.set("page", String(state.page)); + params.set("page_size", String(state.pageSize)); + // no search param + try { + const r = await fetch(`/local-api/cpuAffinity/circuits/${cpu}?` + params.toString()); + if (!r.ok) throw new Error(`${r.status}`); + const data = await r.json(); + renderCircuits(data); + } catch (e) { + target.innerHTML = `
Failed to load circuits: ${e}
`; + } +} + +// Wire up controls +function initControls() { + const pageSizeInput = document.getElementById("pageSize"); + const btnPrev = document.getElementById("btnPrev"); + const btnNext = document.getElementById("btnNext"); + const btnRefresh = document.getElementById("btnRefresh"); + pageSizeInput.onchange = () => { + let v = parseInt(pageSizeInput.value || "50", 10); + if (!Number.isFinite(v) || v < 10) v = 10; + if (v > 1000) v = 1000; + state.pageSize = v; + state.page = 1; + fetchCircuits(); + }; + btnPrev.onclick = () => { + if (state.page > 1) { + state.page -= 1; + fetchCircuits(); + } + }; + btnNext.onclick = () => { + state.page += 1; + fetchCircuits(); + }; + btnRefresh.onclick = () => { + fetchSummary(); + if (state.selectedCpu !== null) fetchCircuits(); + }; +} + +// Initialize +initControls(); +renderDetailsHeader(); +fetchSummary().then(() => enableTooltips()); + +// No preview section (binpacking) in this view diff --git a/src/rust/lqosd/src/node_manager/local_api.rs b/src/rust/lqosd/src/node_manager/local_api.rs index a6d0ddf7..dddf2a20 100644 --- a/src/rust/lqosd/src/node_manager/local_api.rs +++ b/src/rust/lqosd/src/node_manager/local_api.rs @@ -8,6 +8,7 @@ mod flow_explorer; mod flow_map; pub mod lts; mod network_tree; +mod cpu_affinity; mod packet_analysis; mod reload_libreqos; mod scheduler; @@ -66,6 +67,19 @@ pub fn local_api(shaper_query: tokio::sync::mpsc::Sender) -> ) .route("/pcapDump/:id", get(packet_analysis::pcap_dump)) .route("/flowMap", get(flow_map::flow_lat_lon)) + .route("/cpuAffinity/summary", get(cpu_affinity::cpu_affinity_summary)) + .route( + "/cpuAffinity/circuits/:cpu", + get(cpu_affinity::cpu_affinity_circuits), + ) + .route( + "/cpuAffinity/circuitsAll", + get(cpu_affinity::cpu_affinity_circuits_all), + ) + .route( + "/cpuAffinity/previewWeights", + get(cpu_affinity::cpu_affinity_preview_weights), + ) .route("/globalWarnings", get(warnings::get_global_warnings)) .route("/asnList", get(flow_explorer::asn_list)) .route("/countryList", get(flow_explorer::country_list)) diff --git a/src/rust/lqosd/src/node_manager/local_api/cpu_affinity.rs b/src/rust/lqosd/src/node_manager/local_api/cpu_affinity.rs new file mode 100644 index 00000000..e42e6c1d --- /dev/null +++ b/src/rust/lqosd/src/node_manager/local_api/cpu_affinity.rs @@ -0,0 +1,466 @@ +use axum::{extract::Path, extract::Query, Json}; +use serde::Serialize; +use serde_json::Value; +use std::collections::HashMap; +use std::path::PathBuf; + +#[derive(Serialize, Clone, Debug)] +pub struct CpuSideSummary { + pub circuits: usize, + pub min_sum_mbps: f64, + pub max_sum_mbps: f64, +} + +#[derive(Serialize, Clone, Debug)] +pub struct CpuAffinitySummaryEntry { + pub cpu: u32, + pub download: CpuSideSummary, + pub upload: CpuSideSummary, +} + +#[derive(Serialize, Clone, Debug)] +pub struct CpuAffinitySummaryResponse(pub Vec); + +#[derive(Serialize, Clone, Debug)] +pub struct CircuitBrief { + pub circuit_id: Option, + pub circuit_name: Option, + pub parent_node: Option, + pub classid: Option, + pub min_mbps: f64, + pub max_mbps: f64, + pub ip_count: usize, +} + +#[derive(Serialize, Clone, Debug)] +pub struct CpuAffinityCircuitsPage { + pub cpu: u32, + pub direction: String, + pub total: usize, + pub page: usize, + pub page_size: usize, + pub items: Vec, +} + +#[derive(Debug, Default, Clone)] +struct CircuitRecord { + cpu_down: Option, + cpu_up: Option, + min_down: f64, + max_down: f64, + min_up: f64, + max_up: f64, + classid_down: Option, + classid_up: Option, + circuit_id: Option, + circuit_name: Option, + parent_node: Option, + ip_count: usize, +} + +// Query params for the circuits endpoint +#[derive(Debug, Default, Clone, serde::Deserialize)] +pub struct CircuitsQuery { + pub direction: Option, // "down" | "up" (default: down) + pub page: Option, // default 1 + pub page_size: Option, // default 100 + pub search: Option, // substring match on id/name +} + +fn queuing_structure_path() -> Option { + let cfg = lqos_config::load_config().ok()?; + let mut p = PathBuf::from(cfg.lqos_directory.clone()); + p.push("queuingStructure.json"); + Some(p) +} + +fn parse_hex_u32(s: &str) -> Option { + // Accept formats like "0x1" or decimal strings + if let Some(stripped) = s.strip_prefix("0x").or_else(|| s.strip_prefix("0X")) { + u32::from_str_radix(stripped, 16).ok() + } else { + s.parse::().ok() + } +} + +fn val_as_f64(v: &Value) -> Option { + if let Some(x) = v.as_f64() { + Some(x) + } else if let Some(x) = v.as_i64() { + Some(x as f64) + } else if let Some(x) = v.as_u64() { + Some(x as f64) + } else if let Some(s) = v.as_str() { + s.parse::().ok() + } else { + None + } +} + +fn collect_ip_count(circuit: &serde_json::Map) -> usize { + let mut count = 0usize; + if let Some(Value::Array(devs)) = circuit.get("devices") { + for d in devs { + if let Value::Object(dev) = d { + if let Some(Value::Array(v4s)) = dev.get("ipv4s") { + count += v4s.len(); + } + if let Some(Value::Array(v6s)) = dev.get("ipv6s") { + count += v6s.len(); + } + } + } + } + count +} + +fn add_circuit_records( + parent_name: Option<&str>, + node: &serde_json::Map, + out: &mut Vec, +) { + // Helper: parse a JSON object that represents a circuit-level entry + fn parse_circuit_map( + parent_name: Option<&str>, + cm: &serde_json::Map, + ) -> CircuitRecord { + let mut rec = CircuitRecord::default(); + // cpu numbers + if let Some(Value::String(s)) = cm.get("cpuNum") { + rec.cpu_down = parse_hex_u32(s); + } else if let Some(Value::String(s)) = cm.get("classMajor") { + if let Some(mut v) = parse_hex_u32(s) { rec.cpu_down = Some(v.saturating_sub(1)); } + } else if let Some(Value::String(s)) = cm.get("classid") { + // Fallback: parse major from TC handle like "0x1:0x51" + if let Some((maj, _)) = s.split_once(':') { + if let Some(mut v) = parse_hex_u32(maj) { rec.cpu_down = Some(v.saturating_sub(1)); } + } + } + if let Some(Value::String(s)) = cm.get("up_cpuNum") { + rec.cpu_up = parse_hex_u32(s); + } else if let Some(Value::String(s)) = cm.get("up_classMajor") { + if let Some(mut v) = parse_hex_u32(s) { rec.cpu_up = Some(v.saturating_sub(1)); } + } else if let Some(Value::String(s)) = cm.get("up_classid") { + if let Some((maj, _)) = s.split_once(':') { + if let Some(mut v) = parse_hex_u32(maj) { rec.cpu_up = Some(v.saturating_sub(1)); } + } + } + // classids + if let Some(Value::String(s)) = cm.get("classid") { + rec.classid_down = Some(s.clone()); + } + if let Some(Value::String(s)) = cm.get("up_classid") { + rec.classid_up = Some(s.clone()); + } + // bandwidths + if let Some(v) = cm.get("minDownload").and_then(val_as_f64) { + rec.min_down = v; + } + if let Some(v) = cm.get("maxDownload").and_then(val_as_f64) { + rec.max_down = v; + } + if let Some(v) = cm.get("minUpload").and_then(val_as_f64) { + rec.min_up = v; + } + if let Some(v) = cm.get("maxUpload").and_then(val_as_f64) { + rec.max_up = v; + } + // identity + if let Some(Value::String(s)) = cm.get("circuitID") { + if !s.is_empty() { rec.circuit_id = Some(s.clone()); } + } + if let Some(Value::String(s)) = cm.get("circuitName") { + if !s.is_empty() { rec.circuit_name = Some(s.clone()); } + } + if rec.circuit_name.is_none() { + // Fallback to the JSON key is not available here; leave empty + } + rec.parent_node = parent_name.map(|x| x.to_string()); + // IP count + rec.ip_count = collect_ip_count(cm); + rec + } + + // Process circuits array at this node + if let Some(Value::Array(circuits)) = node.get("circuits") { + for c in circuits { + if let Value::Object(cm) = c { + out.push(parse_circuit_map(parent_name, cm)); + } + } + } + + // Also check children: some builds embed circuits directly as child entries (rare). + if let Some(Value::Object(children)) = node.get("children") { + for (child_name, child_value) in children.iter() { + if let Value::Object(child_map) = child_value { + // Treat as circuit only if it has a devices array (avoids counting structural nodes) + let is_circuit_like = matches!(child_map.get("devices"), Some(Value::Array(_))) + || child_map.get("circuitID").is_some(); + if is_circuit_like { + out.push(parse_circuit_map(Some(child_name.as_str()), child_map)); + } + add_circuit_records(Some(child_name.as_str()), child_map, out); + } + } + } +} + +fn load_all_circuits() -> Vec { + let Some(path) = queuing_structure_path() else { + return Vec::new(); + }; + let raw = match std::fs::read_to_string(path) { + Ok(s) => s, + Err(_) => return Vec::new(), + }; + let json: Value = match serde_json::from_str(&raw) { + Ok(v) => v, + Err(_) => return Vec::new(), + }; + let mut circuits = Vec::new(); + if let Value::Object(map) = json { + if let Some(Value::Object(net)) = map.get("Network") { + for (_k, v) in net.iter() { + if let Value::Object(node) = v { + add_circuit_records(None, node, &mut circuits); + } + } + } + } + circuits +} + +pub async fn cpu_affinity_summary() -> Json { + let circuits = load_all_circuits(); + let mut down: HashMap = HashMap::new(); + let mut up: HashMap = HashMap::new(); + + for c in circuits.iter() { + if let Some(cpu) = c.cpu_down { + let entry = down.entry(cpu).or_insert((0, 0.0, 0.0)); + entry.0 += 1; + entry.1 += c.min_down; + entry.2 += c.max_down; + } + if let Some(cpu) = c.cpu_up { + let entry = up.entry(cpu).or_insert((0, 0.0, 0.0)); + entry.0 += 1; + entry.1 += c.min_up; + entry.2 += c.max_up; + } + } + + // Union of CPUs seen in either direction + let mut cpus: Vec = down.keys().chain(up.keys()).copied().collect(); + cpus.sort_unstable(); + cpus.dedup(); + + let mut entries = Vec::with_capacity(cpus.len()); + for cpu in cpus.into_iter() { + let d = down.get(&cpu).cloned().unwrap_or((0, 0.0, 0.0)); + let u = up.get(&cpu).cloned().unwrap_or((0, 0.0, 0.0)); + entries.push(CpuAffinitySummaryEntry { + cpu, + download: CpuSideSummary { + circuits: d.0, + min_sum_mbps: d.1, + max_sum_mbps: d.2, + }, + upload: CpuSideSummary { + circuits: u.0, + min_sum_mbps: u.1, + max_sum_mbps: u.2, + }, + }); + } + + Json(CpuAffinitySummaryResponse(entries)) +} + +pub async fn cpu_affinity_circuits( + Path(cpu): Path, + Query(q): Query, +) -> Json { + let direction = q + .direction + .as_deref() + .map(|s| if s.eq_ignore_ascii_case("up") { "up" } else { "down" }) + .unwrap_or("down"); + + let page = q.page.unwrap_or(1).max(1); + let page_size = q.page_size.unwrap_or(100).clamp(1, 1000); + let search = q.search.as_deref().map(|s| s.to_lowercase()); + + let circuits = load_all_circuits(); + let mut filtered: Vec = circuits + .into_iter() + .filter(|c| match direction { + "up" => c.cpu_up == Some(cpu), + _ => c.cpu_down == Some(cpu), + }) + .filter(|c| { + if let Some(ref needle) = search { + let id = c.circuit_id.as_deref().unwrap_or("").to_lowercase(); + let nm = c.circuit_name.as_deref().unwrap_or("").to_lowercase(); + id.contains(needle) || nm.contains(needle) + } else { + true + } + }) + .map(|c| CircuitBrief { + circuit_id: c.circuit_id, + circuit_name: c.circuit_name, + parent_node: c.parent_node, + classid: if direction == "up" { + c.classid_up + } else { + c.classid_down + }, + min_mbps: if direction == "up" { c.min_up } else { c.min_down }, + max_mbps: if direction == "up" { c.max_up } else { c.max_down }, + ip_count: c.ip_count, + }) + .collect(); + + // Sort by circuit name, then id + filtered.sort_by(|a, b| { + let an = a.circuit_name.as_deref().unwrap_or(""); + let bn = b.circuit_name.as_deref().unwrap_or(""); + let ord = an.cmp(bn); + if ord == std::cmp::Ordering::Equal { + a.circuit_id + .as_deref() + .unwrap_or("") + .cmp(b.circuit_id.as_deref().unwrap_or("")) + } else { + ord + } + }); + + let total = filtered.len(); + let start = (page - 1) * page_size; + let end = std::cmp::min(start + page_size, total); + let page_items = if start < total { + filtered[start..end].to_vec() + } else { + Vec::new() + }; + + Json(CpuAffinityCircuitsPage { + cpu, + direction: direction.to_string(), + total, + page, + page_size, + items: page_items, + }) +} + +// Return all circuits (direction-filtered) without pagination to support client-side previews. +pub async fn cpu_affinity_circuits_all( + Query(q): Query, +) -> Json> { + let direction = q + .direction + .as_deref() + .map(|s| if s.eq_ignore_ascii_case("up") { "up" } else { "down" }) + .unwrap_or("down"); + let search = q.search.as_deref().map(|s| s.to_lowercase()); + + let circuits = load_all_circuits(); + let mut items: Vec = circuits + .into_iter() + .filter(|c| { + if let Some(ref needle) = search { + let id = c.circuit_id.as_deref().unwrap_or("").to_lowercase(); + let nm = c.circuit_name.as_deref().unwrap_or("").to_lowercase(); + id.contains(needle) || nm.contains(needle) + } else { + true + } + }) + .map(|c| CircuitBrief { + circuit_id: c.circuit_id, + circuit_name: c.circuit_name, + parent_node: c.parent_node, + classid: if direction == "up" { + c.classid_up + } else { + c.classid_down + }, + min_mbps: if direction == "up" { c.min_up } else { c.min_down }, + max_mbps: if direction == "up" { c.max_up } else { c.max_down }, + ip_count: c.ip_count, + }) + .collect(); + + // Sort deterministically + items.sort_by(|a, b| { + let an = a.circuit_name.as_deref().unwrap_or(""); + let bn = b.circuit_name.as_deref().unwrap_or(""); + let ord = an.cmp(bn); + if ord == std::cmp::Ordering::Equal { + a.circuit_id + .as_deref() + .unwrap_or("") + .cmp(b.circuit_id.as_deref().unwrap_or("")) + } else { + ord + } + }); + + Json(items) +} + +#[derive(Serialize, Clone, Debug)] +pub struct PreviewWeightItem { + pub key: String, + pub weight: f64, +} + +// Minimal payload for preview: just (key, weight) for each circuit in a direction. +pub async fn cpu_affinity_preview_weights( + Query(q): Query, +) -> Json> { + let direction = q + .direction + .as_deref() + .map(|s| if s.eq_ignore_ascii_case("up") { "up" } else { "down" }) + .unwrap_or("down"); + let search = q.search.as_deref().map(|s| s.to_lowercase()); + + let circuits = load_all_circuits(); + let mut items: Vec = circuits + .into_iter() + .filter(|c| { + if let Some(ref needle) = search { + let id = c.circuit_id.as_deref().unwrap_or("").to_lowercase(); + let nm = c.circuit_name.as_deref().unwrap_or("").to_lowercase(); + id.contains(needle) || nm.contains(needle) + } else { + true + } + }) + .map(|c| PreviewWeightItem { + key: c + .circuit_id + .clone() + .or(c.circuit_name.clone()) + .unwrap_or_else(|| String::new()), + weight: if direction == "up" { c.max_up } else { c.max_down }, + }) + .collect(); + + // Deterministic ordering for ties + items.sort_by(|a, b| { + let ow = b.weight.partial_cmp(&a.weight).unwrap_or(std::cmp::Ordering::Equal); + if ow == std::cmp::Ordering::Equal { + a.key.cmp(&b.key) + } else { + ow + } + }); + + Json(items) +} diff --git a/src/rust/lqosd/src/node_manager/static2/cpu_weights.html b/src/rust/lqosd/src/node_manager/static2/cpu_weights.html new file mode 100644 index 00000000..b1ef93e7 --- /dev/null +++ b/src/rust/lqosd/src/node_manager/static2/cpu_weights.html @@ -0,0 +1,50 @@ +
+
+
+
+
CPU Affinity
+

View current circuit placement per CPU/queue from the active TC plan.

+
+
+
+
+ + +
+
+ Binpacking: … +
+
+ +
+
+
+
+ +
+ Loading CPU summary... +
+ +
+ +
+
+
+ Circuits + +
+
+ + +
+
+
+ Select a CPU from the table above. +
+
+
+
+ + diff --git a/src/rust/lqosd/src/node_manager/static2/template.html b/src/rust/lqosd/src/node_manager/static2/template.html index 0f192af8..88540ba2 100644 --- a/src/rust/lqosd/src/node_manager/static2/template.html +++ b/src/rust/lqosd/src/node_manager/static2/template.html @@ -87,6 +87,12 @@ ASN Explorer + + %%LTS_LINK%% %%API_LINK%% diff --git a/src/rust/lqosd/src/node_manager/static_pages.rs b/src/rust/lqosd/src/node_manager/static_pages.rs index ae69edc7..ec838011 100644 --- a/src/rust/lqosd/src/node_manager/static_pages.rs +++ b/src/rust/lqosd/src/node_manager/static_pages.rs @@ -63,6 +63,7 @@ pub(super) fn static_routes() -> Result { "config_wispgate.html", "config_stormguard.html", "api.html", + "cpu_weights.html", ]; // Iterate through pages and construct the router -- knotmirror 0.0.1 From ed2387194740701feefc7e6066bd58abc6fd3da3 Mon Sep 17 00:00:00 2001 From: Herbert Wolverson Date: Thu, 6 Nov 2025 12:12:23 -0600 Subject: [PATCH 04/13] Fix: Improve planner fairness/scoring, and expose the actual scores in the affinity page. --- src/LibreQoS.py | 20 +++++++ .../node_manager/js_build/src/cpu_weights.js | 28 +++++++--- .../node_manager/local_api/cpu_affinity.rs | 54 +++++++++++-------- 3 files changed, 72 insertions(+), 30 deletions(-) diff --git a/src/LibreQoS.py b/src/LibreQoS.py index 97a28638..378c7c21 100755 --- a/src/LibreQoS.py +++ b/src/LibreQoS.py @@ -897,6 +897,19 @@ def refreshShapers(): return newDict network = flattenA(network, 1) + # Prepare planner weights (actual weighting may come from Insight or CSV defaults) + weight_by_circuit_id = {} + try: + weights = get_weights() + for w in weights: + try: + weight_by_circuit_id[str(w.circuit_id)] = float(w.weight) + except Exception: + pass + except Exception: + # If we can't get weights, leave mapping empty; UI will fall back + weight_by_circuit_id = {} + # Group circuits by parent node. Reduces runtime for section below this one. circuits_by_parent_node = {} circuit_min_down_combined_by_parent_node = {} @@ -1053,6 +1066,13 @@ def refreshShapers(): "classMinor": hex(minorByCPU[queue]), "comment": circuit['comment'] } + # Attach the planner weight if available (real weighting used by the planner) + try: + cid = str(circuit.get('circuitID','')) + if cid in weight_by_circuit_id: + thisNewCircuitItemForNetwork['planner_weight'] = weight_by_circuit_id[cid] + except Exception: + pass # Preserve optional per-circuit SQM override for downstream bakery call if 'sqm' in circuit and circuit['sqm']: thisNewCircuitItemForNetwork['sqm'] = circuit['sqm'] diff --git a/src/rust/lqosd/src/node_manager/js_build/src/cpu_weights.js b/src/rust/lqosd/src/node_manager/js_build/src/cpu_weights.js index 6d2c311a..bc7360b1 100644 --- a/src/rust/lqosd/src/node_manager/js_build/src/cpu_weights.js +++ b/src/rust/lqosd/src/node_manager/js_build/src/cpu_weights.js @@ -55,7 +55,7 @@ function renderSummary(data) { const thead = document.createElement("thead"); thead.appendChild(theading("CPU")); thead.appendChild(theading("DL Circuits")); - thead.appendChild(theading("DL Min (Mbps)")); + thead.appendChild(theading("Weight")); thead.appendChild(theading("DL Max (Mbps)")); thead.appendChild(theading("Actions")); table.appendChild(thead); @@ -68,11 +68,17 @@ function renderSummary(data) { return td; }; + // simple weight formatter + const fmtWeight = (x) => { + const n = Number(x || 0); + return n > 0 ? n.toLocaleString() : '-'; + }; + data.forEach((row) => { const tr = document.createElement("tr"); tr.appendChild(centerCell(row.cpu)); tr.appendChild(centerCell(row.download.circuits)); - tr.appendChild(centerCell(fmtMbps(row.download.min_sum_mbps))); + tr.appendChild(centerCell(fmtWeight(row.download && row.download.weight_sum))); tr.appendChild(centerCell(fmtMbps(row.download.max_sum_mbps))); const actions = document.createElement("td"); @@ -115,10 +121,14 @@ function renderSummary(data) { dlMaxPie = null; } dlMaxPie = new DashboardGraph('cpuDlMaxPie'); - const seriesData = data.map((row) => ({ - name: `CPU ${row.cpu}`, - value: Number(row.download.max_sum_mbps || 0) - })); + const seriesData = data.map((row) => { + const w = Number((row.download && row.download.weight_sum) ? row.download.weight_sum : 0); + const v = Number(row.download.max_sum_mbps || 0); + return { + name: `CPU ${row.cpu}`, + value: (w > 0 ? w : v) + }; + }); dlMaxPie.option = { tooltip: { trigger: 'item' }, legend: { show: false }, @@ -174,7 +184,7 @@ function renderCircuits(page) { thead.appendChild(theading("Circuit Name")); thead.appendChild(theading("Parent")); thead.appendChild(theading("ClassID")); - thead.appendChild(theading("Min (Mbps)")); + thead.appendChild(theading("Weight")); thead.appendChild(theading("Max (Mbps)")); thead.appendChild(theading("IPs")); table.appendChild(thead); @@ -195,7 +205,9 @@ function renderCircuits(page) { tr.appendChild(simpleRow(c.circuit_name || "")); tr.appendChild(simpleRow(c.parent_node || "")); tr.appendChild(simpleRow(c.classid || "")); - tr.appendChild(simpleRow(fmtMbps(c.min_mbps))); + const weightCell = document.createElement('td'); + weightCell.innerText = (c.weight && c.weight > 0) ? c.weight.toLocaleString() : '-'; + tr.appendChild(weightCell); tr.appendChild(simpleRow(fmtMbps(c.max_mbps))); tr.appendChild(simpleRow(String(c.ip_count || 0))); tbody.appendChild(tr); diff --git a/src/rust/lqosd/src/node_manager/local_api/cpu_affinity.rs b/src/rust/lqosd/src/node_manager/local_api/cpu_affinity.rs index e42e6c1d..652213ab 100644 --- a/src/rust/lqosd/src/node_manager/local_api/cpu_affinity.rs +++ b/src/rust/lqosd/src/node_manager/local_api/cpu_affinity.rs @@ -9,6 +9,7 @@ pub struct CpuSideSummary { pub circuits: usize, pub min_sum_mbps: f64, pub max_sum_mbps: f64, + pub weight_sum: f64, } #[derive(Serialize, Clone, Debug)] @@ -27,8 +28,8 @@ pub struct CircuitBrief { pub circuit_name: Option, pub parent_node: Option, pub classid: Option, - pub min_mbps: f64, pub max_mbps: f64, + pub weight: f64, pub ip_count: usize, } @@ -50,6 +51,7 @@ struct CircuitRecord { max_down: f64, min_up: f64, max_up: f64, + planner_weight: f64, classid_down: Option, classid_up: Option, circuit_id: Option, @@ -152,19 +154,23 @@ fn add_circuit_records( if let Some(Value::String(s)) = cm.get("up_classid") { rec.classid_up = Some(s.clone()); } - // bandwidths - if let Some(v) = cm.get("minDownload").and_then(val_as_f64) { - rec.min_down = v; - } - if let Some(v) = cm.get("maxDownload").and_then(val_as_f64) { - rec.max_down = v; - } - if let Some(v) = cm.get("minUpload").and_then(val_as_f64) { - rec.min_up = v; - } - if let Some(v) = cm.get("maxUpload").and_then(val_as_f64) { - rec.max_up = v; - } + // bandwidths + if let Some(v) = cm.get("minDownload").and_then(val_as_f64) { + rec.min_down = v; + } + if let Some(v) = cm.get("maxDownload").and_then(val_as_f64) { + rec.max_down = v; + } + if let Some(v) = cm.get("minUpload").and_then(val_as_f64) { + rec.min_up = v; + } + if let Some(v) = cm.get("maxUpload").and_then(val_as_f64) { + rec.max_up = v; + } + // planner weight if present + if let Some(v) = cm.get("planner_weight").and_then(val_as_f64) { + rec.planner_weight = v; + } // identity if let Some(Value::String(s)) = cm.get("circuitID") { if !s.is_empty() { rec.circuit_id = Some(s.clone()); } @@ -233,21 +239,23 @@ fn load_all_circuits() -> Vec { pub async fn cpu_affinity_summary() -> Json { let circuits = load_all_circuits(); - let mut down: HashMap = HashMap::new(); - let mut up: HashMap = HashMap::new(); + let mut down: HashMap = HashMap::new(); + let mut up: HashMap = HashMap::new(); for c in circuits.iter() { if let Some(cpu) = c.cpu_down { - let entry = down.entry(cpu).or_insert((0, 0.0, 0.0)); + let entry = down.entry(cpu).or_insert((0, 0.0, 0.0, 0.0)); entry.0 += 1; entry.1 += c.min_down; entry.2 += c.max_down; + entry.3 += c.planner_weight; } if let Some(cpu) = c.cpu_up { - let entry = up.entry(cpu).or_insert((0, 0.0, 0.0)); + let entry = up.entry(cpu).or_insert((0, 0.0, 0.0, 0.0)); entry.0 += 1; entry.1 += c.min_up; entry.2 += c.max_up; + entry.3 += c.planner_weight; } } @@ -258,19 +266,21 @@ pub async fn cpu_affinity_summary() -> Json { let mut entries = Vec::with_capacity(cpus.len()); for cpu in cpus.into_iter() { - let d = down.get(&cpu).cloned().unwrap_or((0, 0.0, 0.0)); - let u = up.get(&cpu).cloned().unwrap_or((0, 0.0, 0.0)); + let d = down.get(&cpu).cloned().unwrap_or((0, 0.0, 0.0, 0.0)); + let u = up.get(&cpu).cloned().unwrap_or((0, 0.0, 0.0, 0.0)); entries.push(CpuAffinitySummaryEntry { cpu, download: CpuSideSummary { circuits: d.0, min_sum_mbps: d.1, max_sum_mbps: d.2, + weight_sum: d.3, }, upload: CpuSideSummary { circuits: u.0, min_sum_mbps: u.1, max_sum_mbps: u.2, + weight_sum: u.3, }, }); } @@ -317,8 +327,8 @@ pub async fn cpu_affinity_circuits( } else { c.classid_down }, - min_mbps: if direction == "up" { c.min_up } else { c.min_down }, max_mbps: if direction == "up" { c.max_up } else { c.max_down }, + weight: c.planner_weight, ip_count: c.ip_count, }) .collect(); @@ -389,8 +399,8 @@ pub async fn cpu_affinity_circuits_all( } else { c.classid_down }, - min_mbps: if direction == "up" { c.min_up } else { c.min_down }, max_mbps: if direction == "up" { c.max_up } else { c.max_down }, + weight: c.planner_weight, ip_count: c.ip_count, }) .collect(); -- knotmirror 0.0.1 From 2b250925419c5e4957f846e944c03c1e93a01539 Mon Sep 17 00:00:00 2001 From: Herbert Wolverson Date: Thu, 6 Nov 2025 12:30:55 -0600 Subject: [PATCH 05/13] Fix: Add override for 1000 as calculated weight, because it acts as a sentinel for incomplete data. --- src/LibreQoS.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/LibreQoS.py b/src/LibreQoS.py index 378c7c21..6648af02 100755 --- a/src/LibreQoS.py +++ b/src/LibreQoS.py @@ -1070,7 +1070,11 @@ def refreshShapers(): try: cid = str(circuit.get('circuitID','')) if cid in weight_by_circuit_id: - thisNewCircuitItemForNetwork['planner_weight'] = weight_by_circuit_id[cid] + w = float(weight_by_circuit_id[cid]) + # Treat 1000 as a sentinel default from Insight; use maxDownload instead + if abs(w - 1000.0) < 1e-6: + w = float(maxDownload) + thisNewCircuitItemForNetwork['planner_weight'] = w except Exception: pass # Preserve optional per-circuit SQM override for downstream bakery call -- knotmirror 0.0.1 From b88548aadafabd4f0f131002fcc58c3f2fb0e34f Mon Sep 17 00:00:00 2001 From: Herbert Wolverson Date: Thu, 6 Nov 2025 13:02:59 -0600 Subject: [PATCH 06/13] Fix: Missing script --- src/LibreQoS.py | 12 +- src/bin_planner.py | 277 +++++++++++++++++++++++++++++++++++++++++++++ src/build_dpkg.sh | 1 + src/build_rust.sh | 8 ++ 4 files changed, 296 insertions(+), 2 deletions(-) create mode 100644 src/bin_planner.py diff --git a/src/LibreQoS.py b/src/LibreQoS.py index 6648af02..a85643b3 100755 --- a/src/LibreQoS.py +++ b/src/LibreQoS.py @@ -766,7 +766,11 @@ def refreshShapers(): if iid in changed: state["last_change_ts"][iid] = now_ts state["assignments"][iid] = b - bin_planner.save_state(state_path, state) + try: + print(f"Saving planner state to {state_path} (generated PNs)") + bin_planner.save_state(state_path, state) + except Exception as e: + warnings.warn(f"Failed to save planner state at {state_path}: {e}", stacklevel=2) print("Finished planning generated parent nodes") else: @@ -1225,7 +1229,11 @@ def refreshShapers(): if iid in changed: state["last_change_ts"][iid] = now_ts state["assignments"][iid] = b - bin_planner.save_state(state_path, state) + try: + print(f"Saving planner state to {state_path} (top-level CPU binning)") + bin_planner.save_state(state_path, state) + except Exception as e: + warnings.warn(f"Failed to save planner state at {state_path}: {e}", stacklevel=2) # Here is the actual call to the recursive traverseNetwork() function. finalMinor is not used. minorByCPU = traverseNetwork(network, 0, major=1, minorByCPU=minorByCPUpreloaded, queue=1, parentClassID=None, upParentClassID=None, parentMaxDL=upstream_bandwidth_capacity_download_mbps(), parentMaxUL=upstream_bandwidth_capacity_upload_mbps(), parentMinDL=upstream_bandwidth_capacity_download_mbps(), parentMinUL=upstream_bandwidth_capacity_upload_mbps()) diff --git a/src/bin_planner.py b/src/bin_planner.py new file mode 100644 index 00000000..f1b4a478 --- /dev/null +++ b/src/bin_planner.py @@ -0,0 +1,277 @@ +""" +Internal planner for stable, damped item-to-bin assignments. + +This module exposes a minimal interface initially, to be expanded in later +steps. The baseline implementation uses a simple greedy balancing strategy +as a placeholder to replace the external binpacking dependency. +""" + +from typing import Dict, List, Tuple, Any, Iterable +import hashlib +import math +import json +import os +import time + + +def plan_assignments( + items: List[Dict[str, Any]], + bins: List[Dict[str, Any]], + capacities: Dict[str, float], + previous_assignments: Dict[str, str], + now_ts: float, + params: Dict[str, Any], +) -> Tuple[Dict[str, str], List[str]]: + """ + Compute item -> bin assignments with a repair-oriented approach. + + items: list of { "id": str, "weight": float } + bins: list of { "id": str } + capacities: { bin_id: capacity_float } + previous_assignments: { item_id: bin_id } + now_ts: current UNIX timestamp + params: planner parameters (optional keys): + - candidate_set_size: int + - headroom: float in [0,1) + - alpha: float >= 0 + - hysteresis_threshold: float >= 0 + - cooldown_seconds: int >= 0 + - move_budget_per_run: int >= 0 + - salt: str + - last_change_ts_by_item: { item_id: unix_ts } + - movement_penalty_k: float >= 0 (default 0) + - penalty_tau_seconds: float > 0 (default 3600) + + Returns (assignments, changed_items) + """ + # Defaults + L = int(params.get("candidate_set_size", 4)) + headroom = float(params.get("headroom", 0.05)) + alpha = float(params.get("alpha", 0.1)) + hysteresis = float(params.get("hysteresis_threshold", 0.03)) + cooldown_s = int(params.get("cooldown_seconds", 3600)) + salt = str(params.get("salt", "default_salt")) + last_change_ts = params.get("last_change_ts_by_item", {}) or {} + penalty_k = float(params.get("movement_penalty_k", 0.0)) + penalty_tau = float(params.get("penalty_tau_seconds", 3600.0)) + + bin_ids: List[str] = [b["id"] for b in bins] + cap = {bid: float(capacities.get(bid, 1.0)) for bid in bin_ids} + # Avoid zero/negative capacity + for bid, c in list(cap.items()): + if c <= 0: + cap[bid] = 1e-9 + + # Normalize items + item_list: List[Tuple[str, float]] = [] + for it in items: + it_id = str(it.get("id")) + w = float(it.get("weight", 1.0)) + item_list.append((it_id, w)) + + # Current assignment: keep valid previous assignments + assignment: Dict[str, str] = {} + load: Dict[str, float] = {bid: 0.0 for bid in bin_ids} + for it_id, w in item_list: + prev = previous_assignments.get(it_id) + if prev in cap: + assignment[it_id] = prev + load[prev] += w + + # Cold start: assign items without previous placement via greedy using HRW candidates + missing = [(it_id, w) for it_id, w in item_list if it_id not in assignment] + weights_by_bin = {bid: cap[bid] for bid in bin_ids} + missing.sort(key=lambda t: (-t[1], t[0])) + initially_assigned: List[str] = [] + for it_id, w in missing: + candidates = hrw_candidates(it_id, bin_ids, weights_by_bin, max(L, 1), salt) + # Choose the least loaded among candidates; break ties by HRW rank, not lexicographic bin id + if candidates: + rank = {bid: idx for idx, bid in enumerate(candidates)} + tgt = min(candidates, key=lambda bid: (load[bid] / cap[bid], rank[bid])) + else: + # As a fallback, rank all bins by HRW order to avoid lexicographic bias + all_rank_list = hrw_candidates(it_id, bin_ids, weights_by_bin, len(bin_ids), salt) + all_rank = {bid: idx for idx, bid in enumerate(all_rank_list)} + tgt = min(bin_ids, key=lambda bid: (load[bid] / cap[bid], all_rank.get(bid, 1_000_000))) + assignment[it_id] = tgt + load[tgt] += w + initially_assigned.append(it_id) + + def score(loads: Dict[str, float]) -> float: + s = 0.0 + for bid in bin_ids: + denom = cap[bid] * max(1.0 - headroom, 1e-6) + lj = loads[bid] / denom + over = max(0.0, lj - 1.0) + s += over * over + # Add a small balancing term + if alpha > 0.0: + for bid in bin_ids: + lj = loads[bid] / cap[bid] + s += alpha * (lj * lj) + return s + + # Initial score + base_score = score(load) + + # Decide moves by considering HRW top-L candidates per item + move_budget = params.get("move_budget_per_run") + if move_budget is None: + move_budget = max(1, min(32, int(0.01 * max(1, len(item_list))))) + moves_left = int(move_budget) + + # Worklist: heavier items first + work = sorted(item_list, key=lambda t: (-t[1], t[0])) + changed_items: List[str] = [] + + for it_id, w in work: + if moves_left <= 0: + break + cur = assignment.get(it_id) + candidates = hrw_candidates(it_id, bin_ids, weights_by_bin, max(L, 1), salt) + # Ensure current bin is considered + if cur and cur not in candidates: + candidates.append(cur) + best_gain = 0.0 + best_tgt = cur + for tgt in candidates: + if tgt == cur: + continue + # simulate move + if cur: + load[cur] -= w + load[tgt] += w + new_score = score(load) + # revert + load[tgt] -= w + if cur: + load[cur] += w + gain = base_score - new_score + if gain > best_gain: + best_gain = gain + best_tgt = tgt + + if best_tgt and best_tgt != cur: + # Cooldown check + last_ts = float(last_change_ts.get(it_id, 0.0)) + age = max(0.0, float(now_ts) - last_ts) + if age < float(cooldown_s): + continue + # Movement penalty + penalty = 0.0 + if penalty_k > 0.0 and penalty_tau > 0.0: + penalty = penalty_k * float(w) * math.exp(-age / penalty_tau) + net_gain = best_gain - penalty + # Hysteresis threshold: relative to base_score magnitude or absolute small value if base is tiny + threshold = max(hysteresis * max(1.0, base_score), 1e-6) + if net_gain >= threshold: + # apply move + if cur: + load[cur] -= w + load[best_tgt] += w + assignment[it_id] = best_tgt + changed_items.append(it_id) + # update base score and budget + base_score -= best_gain + moves_left -= 1 + + # Mark initial placements as changes so callers can timestamp them + for iid in initially_assigned: + if iid not in changed_items: + changed_items.append(iid) + + return assignment, changed_items + + +def load_state(path: str) -> Dict[str, Any]: + """ + Load planner state from JSON. If missing or invalid, return a fresh state. + """ + fresh = { + "algo_version": "v1", + "salt": None, + "assignments": {}, + "last_change_ts": {}, + "updated_at": None, + } + try: + if not os.path.exists(path): + return fresh + with open(path, 'r', encoding='utf-8') as f: + data = json.load(f) + # Minimal validation + if not isinstance(data, dict): + return fresh + for k in ["algo_version", "salt", "assignments", "last_change_ts", "updated_at"]: + if k not in data: + data[k] = fresh.get(k) + if data.get("salt") is None: + data["salt"] = _generate_salt() + # TTL: invalidate if older than 24 hours + try: + updated_at = float(data.get("updated_at") or 0.0) + if updated_at <= 0.0: + return data + now = time.time() + if (now - updated_at) > 24 * 3600: + # Regenerate: return a fresh state with a new salt + fresh["salt"] = _generate_salt() + return fresh + except Exception: + # If parsing fails, return fresh + fresh["salt"] = _generate_salt() + return fresh + return data + except Exception: + return fresh + + +def save_state(path: str, state: Dict[str, Any]) -> None: + """ + Save planner state to JSON. Creates parent directory if necessary. + """ + try: + os.makedirs(os.path.dirname(path), exist_ok=True) + except Exception: + pass + state = dict(state) + state["updated_at"] = time.time() + if state.get("salt") is None: + state["salt"] = _generate_salt() + with open(path, 'w', encoding='utf-8') as f: + json.dump(state, f, indent=2, sort_keys=True) + + +def _generate_salt() -> str: + return hashlib.sha256(str(time.time_ns()).encode('utf-8')).hexdigest() + + +def hrw_candidates( + item_id: str, + bin_ids: List[str], + weights_by_bin: Dict[str, float], + L: int, + salt: str, +) -> List[str]: + """ + Weighted Rendezvous (HRW) hashing: return top-L candidate bins for an item. + + score(bin) = weight(bin) / -ln(U), where U derives deterministically from + SHA-256(salt || item_id || bin_id). + """ + scores: List[Tuple[float, str]] = [] + for bid in bin_ids: + w = float(weights_by_bin.get(bid, 1.0)) + # Deterministic uniform in (0,1) + h = hashlib.sha256((salt + '|' + str(item_id) + '|' + str(bid)).encode('utf-8')).digest() + # Use first 8 bytes for a 64-bit integer + r = int.from_bytes(h[:8], byteorder='big', signed=False) + # Map to (0,1). Avoid 0 by adding a tiny epsilon. + U = max((r / 2**64), 1e-12) + score = w / (-math.log(U)) if U < 1.0 else float('inf') + scores.append((score, bid)) + scores.sort(key=lambda t: (-t[0], t[1])) + if L <= 0: + return [] + return [bid for _, bid in scores[:min(L, len(scores))]] diff --git a/src/build_dpkg.sh b/src/build_dpkg.sh index f0167bb0..54044266 100755 --- a/src/build_dpkg.sh +++ b/src/build_dpkg.sh @@ -24,6 +24,7 @@ LQOS_FILES=( integrationRestHttp.py integrationSonar.py integrationSplynx.py + bin_planner.py integrationUISP.py LibreQoS.py lqos.example diff --git a/src/build_rust.sh b/src/build_rust.sh index d57b8b99..64e3193e 100755 --- a/src/build_rust.sh +++ b/src/build_rust.sh @@ -98,6 +98,14 @@ popd > /dev/null || exit cp rust/target/$TARGET/liblqos_python.so ./liblqos_python.so.new mv liblqos_python.so.new liblqos_python.so +# Ensure the internal planner module is present for LibreQoS.py +if [ ! -f "bin_planner.py" ]; then + echo "Warning: bin_planner.py not found in $(pwd). The planner will be unavailable to LibreQoS.py." + echo "If you are updating a running system, make sure bin_planner.py is deployed alongside LibreQoS.py." +else + echo "bin_planner.py present." +fi + # Update the lqos_api binary echo "Updating lqos_api binary..." bash ./update_api.sh || echo "Warning: Failed to update lqos_api (continuing)." -- knotmirror 0.0.1 From 5fc06d6693e1a228969e1ab8444358281d64c147 Mon Sep 17 00:00:00 2001 From: Herbert Wolverson Date: Thu, 6 Nov 2025 13:23:41 -0600 Subject: [PATCH 07/13] Fix: Ignore obviously defaulted circuits. --- src/LibreQoS.py | 30 +++++++++++--- .../node_manager/js_build/src/cpu_weights.js | 9 ++++- .../node_manager/local_api/cpu_affinity.rs | 40 ++++++++++++------- 3 files changed, 58 insertions(+), 21 deletions(-) diff --git a/src/LibreQoS.py b/src/LibreQoS.py index a85643b3..b68acdf7 100755 --- a/src/LibreQoS.py +++ b/src/LibreQoS.py @@ -688,6 +688,14 @@ def refreshShapers(): # If a specific circuit weight exists, prefer it if 'circuitID' in circuit and str(circuit['circuitID']) in weight_by_circuit_id: w = weight_by_circuit_id[str(circuit['circuitID'])] + # Ignore placeholder default rates for weight purposes + try: + default_rate = float(generated_pn_download_mbps()) + max_dl = float(circuit.get('maxDownload', 0)) + if abs(max_dl - default_rate) < 1e-6: + w = 0.0 + except Exception: + pass items.append({"id": item_id, "weight": float(w)}) # Prepare bins and capacities @@ -1070,15 +1078,27 @@ def refreshShapers(): "classMinor": hex(minorByCPU[queue]), "comment": circuit['comment'] } - # Attach the planner weight if available (real weighting used by the planner) + # Attach the planner weight used by the planner/UI summary + # Priority: explicit weight -> fallback to maxDownload try: cid = str(circuit.get('circuitID','')) + w = None if cid in weight_by_circuit_id: w = float(weight_by_circuit_id[cid]) - # Treat 1000 as a sentinel default from Insight; use maxDownload instead - if abs(w - 1000.0) < 1e-6: - w = float(maxDownload) - thisNewCircuitItemForNetwork['planner_weight'] = w + if w is None: + w = float(maxDownload) + # Treat 1000 as a sentinel default from Insight; use maxDownload instead + if abs(w - 1000.0) < 1e-6: + w = float(maxDownload) + # If the circuit's configured rate equals the generated PN default, + # ignore it for weight purposes. + try: + default_rate = float(generated_pn_download_mbps()) + if abs(float(maxDownload) - default_rate) < 1e-6: + w = 0.0 + except Exception: + pass + thisNewCircuitItemForNetwork['planner_weight'] = w except Exception: pass # Preserve optional per-circuit SQM override for downstream bakery call diff --git a/src/rust/lqosd/src/node_manager/js_build/src/cpu_weights.js b/src/rust/lqosd/src/node_manager/js_build/src/cpu_weights.js index bc7360b1..42e5890f 100644 --- a/src/rust/lqosd/src/node_manager/js_build/src/cpu_weights.js +++ b/src/rust/lqosd/src/node_manager/js_build/src/cpu_weights.js @@ -192,6 +192,9 @@ function renderCircuits(page) { page.items.forEach((c) => { const tr = document.createElement("tr"); + if (c.ignored || (c.weight !== undefined && c.weight <= 0)) { + tr.classList.add('text-muted'); + } const idCell = document.createElement("td"); if (c.circuit_id) { const a = document.createElement("a"); @@ -206,7 +209,11 @@ function renderCircuits(page) { tr.appendChild(simpleRow(c.parent_node || "")); tr.appendChild(simpleRow(c.classid || "")); const weightCell = document.createElement('td'); - weightCell.innerText = (c.weight && c.weight > 0) ? c.weight.toLocaleString() : '-'; + if (c.ignored || (c.weight !== undefined && c.weight <= 0)) { + weightCell.innerHTML = "ignored"; + } else { + weightCell.innerText = (c.weight && c.weight > 0) ? c.weight.toLocaleString() : '-'; + } tr.appendChild(weightCell); tr.appendChild(simpleRow(fmtMbps(c.max_mbps))); tr.appendChild(simpleRow(String(c.ip_count || 0))); diff --git a/src/rust/lqosd/src/node_manager/local_api/cpu_affinity.rs b/src/rust/lqosd/src/node_manager/local_api/cpu_affinity.rs index 652213ab..1ea5b0a4 100644 --- a/src/rust/lqosd/src/node_manager/local_api/cpu_affinity.rs +++ b/src/rust/lqosd/src/node_manager/local_api/cpu_affinity.rs @@ -31,6 +31,7 @@ pub struct CircuitBrief { pub max_mbps: f64, pub weight: f64, pub ip_count: usize, + pub ignored: bool, } #[derive(Serialize, Clone, Debug)] @@ -52,6 +53,7 @@ struct CircuitRecord { min_up: f64, max_up: f64, planner_weight: f64, + has_planner_weight: bool, classid_down: Option, classid_up: Option, circuit_id: Option, @@ -131,20 +133,20 @@ fn add_circuit_records( if let Some(Value::String(s)) = cm.get("cpuNum") { rec.cpu_down = parse_hex_u32(s); } else if let Some(Value::String(s)) = cm.get("classMajor") { - if let Some(mut v) = parse_hex_u32(s) { rec.cpu_down = Some(v.saturating_sub(1)); } + if let Some(v) = parse_hex_u32(s) { rec.cpu_down = Some(v.saturating_sub(1)); } } else if let Some(Value::String(s)) = cm.get("classid") { // Fallback: parse major from TC handle like "0x1:0x51" if let Some((maj, _)) = s.split_once(':') { - if let Some(mut v) = parse_hex_u32(maj) { rec.cpu_down = Some(v.saturating_sub(1)); } + if let Some(v) = parse_hex_u32(maj) { rec.cpu_down = Some(v.saturating_sub(1)); } } } if let Some(Value::String(s)) = cm.get("up_cpuNum") { rec.cpu_up = parse_hex_u32(s); } else if let Some(Value::String(s)) = cm.get("up_classMajor") { - if let Some(mut v) = parse_hex_u32(s) { rec.cpu_up = Some(v.saturating_sub(1)); } + if let Some(v) = parse_hex_u32(s) { rec.cpu_up = Some(v.saturating_sub(1)); } } else if let Some(Value::String(s)) = cm.get("up_classid") { if let Some((maj, _)) = s.split_once(':') { - if let Some(mut v) = parse_hex_u32(maj) { rec.cpu_up = Some(v.saturating_sub(1)); } + if let Some(v) = parse_hex_u32(maj) { rec.cpu_up = Some(v.saturating_sub(1)); } } } // classids @@ -170,6 +172,7 @@ fn add_circuit_records( // planner weight if present if let Some(v) = cm.get("planner_weight").and_then(val_as_f64) { rec.planner_weight = v; + rec.has_planner_weight = true; } // identity if let Some(Value::String(s)) = cm.get("circuitID") { @@ -243,19 +246,24 @@ pub async fn cpu_affinity_summary() -> Json { let mut up: HashMap = HashMap::new(); for c in circuits.iter() { + let ignored = c.has_planner_weight && c.planner_weight <= 0.0; if let Some(cpu) = c.cpu_down { - let entry = down.entry(cpu).or_insert((0, 0.0, 0.0, 0.0)); - entry.0 += 1; - entry.1 += c.min_down; - entry.2 += c.max_down; - entry.3 += c.planner_weight; + if !ignored { + let entry = down.entry(cpu).or_insert((0, 0.0, 0.0, 0.0)); + entry.0 += 1; + entry.1 += c.min_down; + entry.2 += c.max_down; + entry.3 += c.planner_weight; + } } if let Some(cpu) = c.cpu_up { - let entry = up.entry(cpu).or_insert((0, 0.0, 0.0, 0.0)); - entry.0 += 1; - entry.1 += c.min_up; - entry.2 += c.max_up; - entry.3 += c.planner_weight; + if !ignored { + let entry = up.entry(cpu).or_insert((0, 0.0, 0.0, 0.0)); + entry.0 += 1; + entry.1 += c.min_up; + entry.2 += c.max_up; + entry.3 += c.planner_weight; + } } } @@ -330,6 +338,7 @@ pub async fn cpu_affinity_circuits( max_mbps: if direction == "up" { c.max_up } else { c.max_down }, weight: c.planner_weight, ip_count: c.ip_count, + ignored: c.has_planner_weight && c.planner_weight <= 0.0, }) .collect(); @@ -402,6 +411,7 @@ pub async fn cpu_affinity_circuits_all( max_mbps: if direction == "up" { c.max_up } else { c.max_down }, weight: c.planner_weight, ip_count: c.ip_count, + ignored: c.has_planner_weight && c.planner_weight <= 0.0, }) .collect(); @@ -457,7 +467,7 @@ pub async fn cpu_affinity_preview_weights( .circuit_id .clone() .or(c.circuit_name.clone()) - .unwrap_or_else(|| String::new()), + .unwrap_or_default(), weight: if direction == "up" { c.max_up } else { c.max_down }, }) .collect(); -- knotmirror 0.0.1 From a1d2b605609f463ce040a8cf6ef61c868869f6d0 Mon Sep 17 00:00:00 2001 From: LuisDanielEA Date: Thu, 6 Nov 2025 15:36:11 -0700 Subject: [PATCH 08/13] Make copyright year dynamic in template.html Update footer copyright year dynamically using JavaScript. --- src/rust/lqosd/src/node_manager/static2/template.html | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/rust/lqosd/src/node_manager/static2/template.html b/src/rust/lqosd/src/node_manager/static2/template.html index 0f192af8..c7d91bd6 100644 --- a/src/rust/lqosd/src/node_manager/static2/template.html +++ b/src/rust/lqosd/src/node_manager/static2/template.html @@ -205,7 +205,7 @@
- LibreQoS %%VERSION%%. © Copyright 2022-2024, LibreQoE LLC. All Rights Reserved. + LibreQoS %%VERSION%%. © Copyright 2022-, LibreQoE LLC. All Rights Reserved.
@@ -225,6 +225,9 @@ }); obs.observe(container, { childList: true, subtree: true }); })(); + + document.getElementById('year').textContent = new Date().getFullYear(); + -- knotmirror 0.0.1 From 767c3e937063237fa9e4356beda2f682650627f8 Mon Sep 17 00:00:00 2001 From: Herbert Wolverson Date: Fri, 7 Nov 2025 12:11:34 -0600 Subject: [PATCH 09/13] Fix: Shaped Devices editor in the UI now saves. FIXES #835 --- .../js_build/src/config/config_helper.js | 67 ++++++++----------- .../js_build/src/configuration.js | 34 +++++----- .../lqosd/src/shaped_devices_tracker/mod.rs | 7 +- 3 files changed, 48 insertions(+), 60 deletions(-) diff --git a/src/rust/lqosd/src/node_manager/js_build/src/config/config_helper.js b/src/rust/lqosd/src/node_manager/js_build/src/config/config_helper.js index 2a33606a..dadf51f2 100644 --- a/src/rust/lqosd/src/node_manager/js_build/src/config/config_helper.js +++ b/src/rust/lqosd/src/node_manager/js_build/src/config/config_helper.js @@ -52,10 +52,22 @@ export function saveNetworkAndDevices(network_json, shaped_devices, onComplete) validationErrors.push(`Device ${index + 1}: Parent node '${device.parent_node}' does not exist`); } - // Bandwidth validation - if (device.download_min_mbps < 1 || device.upload_min_mbps < 1 || - device.download_max_mbps < 1 || device.upload_max_mbps < 1) { - validationErrors.push(`Device ${index + 1}: Bandwidth values must be greater than 0`); + // Bandwidth validation (supports fractional Mbps) + // Minimums must be >= 0.1 Mbps, maximums must be >= 0.2 Mbps + const dmin = parseFloat(device.download_min_mbps); + const umin = parseFloat(device.upload_min_mbps); + const dmax = parseFloat(device.download_max_mbps); + const umax = parseFloat(device.upload_max_mbps); + + if (Number.isNaN(dmin) || Number.isNaN(umin) || Number.isNaN(dmax) || Number.isNaN(umax)) { + validationErrors.push(`Device ${index + 1}: One or more bandwidth fields are not valid numbers`); + } else { + if (dmin < 0.1 || umin < 0.1) { + validationErrors.push(`Device ${index + 1}: Min rates must be >= 0.1 Mbps`); + } + if (dmax < 0.2 || umax < 0.2) { + validationErrors.push(`Device ${index + 1}: Max rates must be >= 0.2 Mbps`); + } } }); @@ -71,51 +83,26 @@ export function saveNetworkAndDevices(network_json, shaped_devices, onComplete) }; console.log(submission); - // Send to server with enhanced error handling - /*$.ajax({ + // Send to server and handle plain-text "Ok" response + $.ajax({ type: "POST", url: "/local-api/updateNetworkAndDevices", contentType: 'application/json', data: JSON.stringify(submission), - dataType: 'json', // Expect JSON response - success: (response) => { - try { - if (response && response.success) { - if (onComplete) onComplete(true, "Saved successfully"); - } else { - const msg = response?.message || "Unknown error occurred"; - if (onComplete) onComplete(false, msg); - alert("Failed to save: " + msg); - } - } catch (e) { - console.error("Error parsing response:", e); - if (onComplete) onComplete(false, "Invalid server response"); - alert("Invalid server response format"); - } + success: (data) => { + if (onComplete) onComplete(data === "Ok", data); }, error: (xhr) => { - let errorMsg = "Request failed"; - try { - if (xhr.responseText) { - const json = JSON.parse(xhr.responseText); - errorMsg = json.message || xhr.responseText; - } else if (xhr.statusText) { - errorMsg = xhr.statusText; - } - console.error("AJAX Error:", { - status: xhr.status, - statusText: xhr.statusText, - response: xhr.responseText - }); - } catch (e) { - console.error("Error parsing error response:", e); - errorMsg = "Unknown error occurred"; - } - + const errorMsg = xhr.responseText || xhr.statusText || "Request failed"; + console.error("AJAX Error:", { + status: xhr.status, + statusText: xhr.statusText, + response: xhr.responseText + }); if (onComplete) onComplete(false, errorMsg); alert("Error saving configuration: " + errorMsg); } - });*/ + }); } export function validNodeList(network_json) { diff --git a/src/rust/lqosd/src/node_manager/js_build/src/configuration.js b/src/rust/lqosd/src/node_manager/js_build/src/configuration.js index b9dba0ae..aab50514 100644 --- a/src/rust/lqosd/src/node_manager/js_build/src/configuration.js +++ b/src/rust/lqosd/src/node_manager/js_build/src/configuration.js @@ -640,7 +640,7 @@ function makeSheetBox(rowId, boxId, value, small=false) { } function makeSheetNumberBox(rowId, boxId, value) { - let html = "" + let html = "" return html; } @@ -1026,56 +1026,56 @@ function validateSd() { // Download Min controlId = "#" + rowPrefix(i, "download_min_mbps"); let download_min = $(controlId).val(); - download_min = parseInt(download_min); + download_min = parseFloat(download_min); if (isNaN(download_min)) { valid = false; errors.push("Download min is not a valid number"); $(controlId).addClass("invalid"); - } else if (download_min < 1) { + } else if (download_min < 0.1) { valid = false; - errors.push("Download min must be 1 or more"); + errors.push("Download min must be 0.1 or more"); $(controlId).addClass("invalid"); } // Upload Min controlId = "#" + rowPrefix(i, "upload_min_mbps"); let upload_min = $(controlId).val(); - upload_min = parseInt(upload_min); + upload_min = parseFloat(upload_min); if (isNaN(upload_min)) { valid = false; errors.push("Upload min is not a valid number"); $(controlId).addClass("invalid"); - } else if (upload_min < 1) { + } else if (upload_min < 0.1) { valid = false; - errors.push("Upload min must be 1 or more"); + errors.push("Upload min must be 0.1 or more"); $(controlId).addClass("invalid"); } // Download Max controlId = "#" + rowPrefix(i, "download_max_mbps"); let download_max = $(controlId).val(); - upload_min = parseInt(download_max); + download_max = parseFloat(download_max); if (isNaN(download_max)) { valid = false; errors.push("Download Max is not a valid number"); $(controlId).addClass("invalid"); - } else if (download_max < 1) { + } else if (download_max < 0.2) { valid = false; - errors.push("Download Max must be 1 or more"); + errors.push("Download Max must be 0.2 or more"); $(controlId).addClass("invalid"); } // Upload Max controlId = "#" + rowPrefix(i, "upload_max_mbps"); let upload_max = $(controlId).val(); - upload_min = parseInt(upload_max); + upload_max = parseFloat(upload_max); if (isNaN(upload_max)) { valid = false; errors.push("Upload Max is not a valid number"); $(controlId).addClass("invalid"); - } else if (upload_max < 1) { + } else if (upload_max < 0.2) { valid = false; - errors.push("Upload Max must be 1 or more"); + errors.push("Upload Max must be 0.2 or more"); $(controlId).addClass("invalid"); } } @@ -1123,10 +1123,10 @@ function saveNetAndDevices() { row.mac = $("#" + rowPrefix(i, "mac")).val(); row.ipv4 = ipAddressesToTuple($("#" + rowPrefix(i, "ipv4")).val()); row.ipv6 = ipAddressesToTuple($("#" + rowPrefix(i, "ipv6")).val()); - row.download_min_mbps = parseInt($("#" + rowPrefix(i, "download_min_mbps")).val()); - row.upload_min_mbps = parseInt($("#" + rowPrefix(i, "upload_min_mbps")).val()); - row.download_max_mbps = parseInt($("#" + rowPrefix(i, "download_max_mbps")).val()); - row.upload_max_mbps = parseInt($("#" + rowPrefix(i, "upload_max_mbps")).val()); + row.download_min_mbps = parseFloat($("#" + rowPrefix(i, "download_min_mbps")).val()); + row.upload_min_mbps = parseFloat($("#" + rowPrefix(i, "upload_min_mbps")).val()); + row.download_max_mbps = parseFloat($("#" + rowPrefix(i, "download_max_mbps")).val()); + row.upload_max_mbps = parseFloat($("#" + rowPrefix(i, "upload_max_mbps")).val()); row.comment = $("#" + rowPrefix(i, "comment")).val(); } diff --git a/src/rust/lqosd/src/shaped_devices_tracker/mod.rs b/src/rust/lqosd/src/shaped_devices_tracker/mod.rs index 555033c8..d941c8ae 100644 --- a/src/rust/lqosd/src/shaped_devices_tracker/mod.rs +++ b/src/rust/lqosd/src/shaped_devices_tracker/mod.rs @@ -217,7 +217,8 @@ pub fn get_all_circuits() -> BusResponse { let mut device_id = None; let mut device_name = None; let mut parent_node = None; - let mut plan = DownUpOrder { down: 0.0, up: 0.0 }; + // Plan is expressed in Mbps as f32 + let mut plan: DownUpOrder = DownUpOrder { down: 0.0, up: 0.0 }; let lookup = match ip { IpAddr::V4(ip) => ip.to_ipv6_mapped(), IpAddr::V6(ip) => ip, @@ -228,8 +229,8 @@ pub fn get_all_circuits() -> BusResponse { device_id = Some(devices.devices[*c.1].device_id.clone()); device_name = Some(devices.devices[*c.1].device_name.clone()); parent_node = Some(devices.devices[*c.1].parent_node.clone()); - plan.down = devices.devices[*c.1].download_max_mbps; - plan.up = devices.devices[*c.1].upload_max_mbps; + plan.down = devices.devices[*c.1].download_max_mbps.round(); + plan.up = devices.devices[*c.1].upload_max_mbps.round(); } Circuit { -- knotmirror 0.0.1 From ddda727e04b0fe69aad6f65294068fc6f1704e17 Mon Sep 17 00:00:00 2001 From: Herbert Wolverson Date: Fri, 7 Nov 2025 12:39:44 -0600 Subject: [PATCH 10/13] Fix: All websocket connections now check for an http vs https prefix. --- .../node_manager/js_build/src/lq_js_common/dashboard/ws.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/rust/lqosd/src/node_manager/js_build/src/lq_js_common/dashboard/ws.js b/src/rust/lqosd/src/node_manager/js_build/src/lq_js_common/dashboard/ws.js index 240e2516..02ef9010 100644 --- a/src/rust/lqosd/src/node_manager/js_build/src/lq_js_common/dashboard/ws.js +++ b/src/rust/lqosd/src/node_manager/js_build/src/lq_js_common/dashboard/ws.js @@ -1,4 +1,5 @@ // Setup any WS feeds for this page +import { ws_proto } from '../../pubsub/ws.js'; let ws = null; export function subscribeWS(channels, handler) { @@ -9,7 +10,7 @@ export function subscribeWS(channels, handler) { ws.close(); } - ws = new WebSocket('ws://' + window.location.host + '/websocket/ws'); + ws = new WebSocket(ws_proto() + window.location.host + '/websocket/ws'); ws.onopen = () => { for (let i=0; i Date: Fri, 7 Nov 2025 13:05:49 -0600 Subject: [PATCH 11/13] Feat: Now show 2 layers (clamped) on the live sankey displays. --- .../src/dashlets/children_sankey_down.js | 96 ++++++++-- .../src/dashlets/children_sankey_up.js | 81 +++++++-- .../js_build/src/dashlets/top_tree_sankey.js | 168 +++++++++++++----- .../src/node_manager/ws/published_channels.rs | 1 + src/rust/lqosd/src/node_manager/ws/ticker.rs | 2 + 5 files changed, 280 insertions(+), 68 deletions(-) diff --git a/src/rust/lqosd/src/node_manager/js_build/src/dashlets/children_sankey_down.js b/src/rust/lqosd/src/node_manager/js_build/src/dashlets/children_sankey_down.js index 5a6dcc4e..74d6f3c6 100644 --- a/src/rust/lqosd/src/node_manager/js_build/src/dashlets/children_sankey_down.js +++ b/src/rust/lqosd/src/node_manager/js_build/src/dashlets/children_sankey_down.js @@ -15,19 +15,26 @@ class ChildrenSankeyGraph extends DashboardGraph { this.chart.setOption(this.option); } update(items) { - const nodes = [{ name: 'Shaper' }]; - const links = []; - items.forEach(it => { - const color = colorByRetransmitPct(it.rxmit ?? 0); - nodes.push({ name: it.name, itemStyle: { color } }); - if (this.direction === 'down') { - links.push({ source: it.name, target: 'Shaper', value: it.value }); - } else { - links.push({ source: 'Shaper', target: it.name, value: it.value }); - } - }); - this.option.series[0].data = nodes; - this.option.series[0].links = links; + // Backward compatibility: if items is an array of {name,value,rxmit}, draw 1-level + if (Array.isArray(items)) { + const nodes = [{ name: 'Shaper' }]; + const links = []; + items.forEach(it => { + const color = colorByRetransmitPct(it.rxmit ?? 0); + nodes.push({ name: it.name, itemStyle: { color } }); + if (this.direction === 'down') { + links.push({ source: it.name, target: 'Shaper', value: it.value }); + } else { + links.push({ source: 'Shaper', target: it.name, value: it.value }); + } + }); + this.option.series[0].data = nodes; + this.option.series[0].links = links; + } else if (items && items.nodes && items.links) { + // New 2-level path: items = { nodes, links } + this.option.series[0].data = items.nodes; + this.option.series[0].links = items.links; + } this.chart.hideLoading(); this.chart.setOption(this.option); } @@ -39,11 +46,13 @@ export class ShaperChildrenDown extends BaseDashlet { this.last=null; this._emptyId = this.id + "_empty"; this._smooth = new Map(); this._ALPHA = 0.3; this._ALPHA_RX = 0.2; this._DECAY = 0.15; this._LINGER = 3; + this._lastSummary = null; // raw TreeSummary + this._lastL2 = null; // TreeSummaryL2 } canBeSlowedDown(){ return true; } title(){ return "Shaper Children (Download)"; } tooltip(){ return "
Child Throughput

Top child nodes by download throughput."; } - subscribeTo(){ return ["TreeSummary"]; } + subscribeTo(){ return ["TreeSummary", "TreeSummaryL2"]; } buildContainer(){ let b = super.buildContainer(); b.appendChild(this.graphDiv()); return b; } setup(){ this.graph = new ChildrenSankeyGraph(this.graphDivId(), 'down'); if (this.last) this.graph.update(this.last); } _showEmpty(show, msg = "No recent data"){ @@ -87,9 +96,66 @@ export class ShaperChildrenDown extends BaseDashlet { out.sort((a,b)=> b.value - a.value); return out.slice(0,9); } + _renderTwoLevel(){ + if (!this._lastSummary || !this._lastL2) return false; + // Build parent map + const parentMap = new Map(); + (this._lastSummary || []).slice(1).forEach(r => parentMap.set(r[0], r[1])); + + const nodes = [{ name: 'Shaper' }]; + const links = []; + let hasData = false; + + for (const [parentId, children] of this._lastL2) { + const p = parentMap.get(parentId); + if (!p) continue; + const pName = p.name || String(parentId); + // Parent color by rxmit (download) + let pRx = 0; + if ((p.current_tcp_packets?.[0]||0) > 0) { + pRx = ((p.current_retransmits?.[0]||0) / Math.max(1, p.current_tcp_packets?.[0]||0)) * 100.0; + } + nodes.push({ name: pName, itemStyle: { color: colorByRetransmitPct(pRx) } }); + + let parentSum = 0; + for (const [, c] of children) { + const v = Number((c.current_throughput?.[0]||0)); + if (v <= 0) continue; + parentSum += v; + hasData = hasData || v > 0.5; + let cRx = 0; + if ((c.current_tcp_packets?.[0]||0) > 0) { + cRx = ((c.current_retransmits?.[0]||0) / Math.max(1, c.current_tcp_packets?.[0]||0)) * 100.0; + } + nodes.push({ name: c.name, itemStyle: { color: colorByRetransmitPct(cRx) } }); + links.push({ source: c.name, target: pName, value: v }); + } + if (parentSum > 0) { + links.push({ source: pName, target: 'Shaper', value: parentSum }); + } + } + + this._showEmpty(!hasData); + if (hasData) { + this.graph.update({ nodes, links }); + return true; + } + return false; + } + onMessage(msg){ + if (msg.event === "TreeSummary") { + this._lastSummary = msg.data; + } else if (msg.event === "TreeSummaryL2") { + this._lastL2 = msg.data; + } else { + return; + } + + // Prefer 2-level; if unavailable, fallback to 1-level smoothing + if (this._renderTwoLevel()) return; + if (msg.event !== "TreeSummary") return; - // msg.data: [ [id, metrics], [id, metrics], ... ] - first is root let rows = (msg.data || []).slice(1).map(r => { const m = r[1] || {}; const name = m.name || String(r[0]); diff --git a/src/rust/lqosd/src/node_manager/js_build/src/dashlets/children_sankey_up.js b/src/rust/lqosd/src/node_manager/js_build/src/dashlets/children_sankey_up.js index 00015933..16f9ea11 100644 --- a/src/rust/lqosd/src/node_manager/js_build/src/dashlets/children_sankey_up.js +++ b/src/rust/lqosd/src/node_manager/js_build/src/dashlets/children_sankey_up.js @@ -12,15 +12,20 @@ class ChildrenSankeyGraphUp extends DashboardGraph { this.chart.setOption(this.option); } update(items){ - const nodes = [{ name: 'Shaper' }]; - const links = []; - items.forEach(it => { - const color = colorByRetransmitPct(it.rxmit ?? 0); - nodes.push({ name: it.name, itemStyle: { color } }); - links.push({ source: 'Shaper', target: it.name, value: it.value }); - }); - this.option.series[0].data = nodes; - this.option.series[0].links = links; + if (Array.isArray(items)) { + const nodes = [{ name: 'Shaper' }]; + const links = []; + items.forEach(it => { + const color = colorByRetransmitPct(it.rxmit ?? 0); + nodes.push({ name: it.name, itemStyle: { color } }); + links.push({ source: 'Shaper', target: it.name, value: it.value }); + }); + this.option.series[0].data = nodes; + this.option.series[0].links = links; + } else if (items && items.nodes && items.links) { + this.option.series[0].data = items.nodes; + this.option.series[0].links = items.links; + } this.chart.hideLoading(); this.chart.setOption(this.option); } @@ -32,11 +37,13 @@ export class ShaperChildrenUp extends BaseDashlet { this.last=null; this._emptyId = this.id + "_empty"; this._smooth = new Map(); this._ALPHA = 0.3; this._ALPHA_RX = 0.2; this._DECAY = 0.15; this._LINGER = 3; + this._lastSummary = null; + this._lastL2 = null; } canBeSlowedDown(){ return true; } title(){ return "Shaper Children (Upload)"; } tooltip(){ return "

Child Throughput

Top child nodes by upload throughput."; } - subscribeTo(){ return ["TreeSummary"]; } + subscribeTo(){ return ["TreeSummary", "TreeSummaryL2"]; } buildContainer(){ let b=super.buildContainer(); b.appendChild(this.graphDiv()); return b; } setup(){ this.graph = new ChildrenSankeyGraphUp(this.graphDivId()); if (this.last) this.graph.update(this.last); } _showEmpty(show, msg = "No recent data"){ @@ -80,7 +87,61 @@ export class ShaperChildrenUp extends BaseDashlet { out.sort((a,b)=> b.value - a.value); return out.slice(0,9); } + _renderTwoLevel(){ + if (!this._lastSummary || !this._lastL2) return false; + const parentMap = new Map(); + (this._lastSummary || []).slice(1).forEach(r => parentMap.set(r[0], r[1])); + + const nodes = [{ name: 'Shaper' }]; + const links = []; + let hasData = false; + + for (const [parentId, children] of this._lastL2) { + const p = parentMap.get(parentId); + if (!p) continue; + const pName = p.name || String(parentId); + // Parent color by rxmit (upload) + let pRx = 0; + if ((p.current_tcp_packets?.[1]||0) > 0) { + pRx = ((p.current_retransmits?.[1]||0) / Math.max(1, p.current_tcp_packets?.[1]||0)) * 100.0; + } + nodes.push({ name: pName, itemStyle: { color: colorByRetransmitPct(pRx) } }); + + let parentSum = 0; + for (const [, c] of children) { + const v = Number((c.current_throughput?.[1]||0)); + if (v <= 0) continue; + parentSum += v; + hasData = hasData || v > 0.5; + let cRx = 0; + if ((c.current_tcp_packets?.[1]||0) > 0) { + cRx = ((c.current_retransmits?.[1]||0) / Math.max(1, c.current_tcp_packets?.[1]||0)) * 100.0; + } + nodes.push({ name: c.name, itemStyle: { color: colorByRetransmitPct(cRx) } }); + links.push({ source: pName, target: c.name, value: v }); + } + if (parentSum > 0) { + links.push({ source: 'Shaper', target: pName, value: parentSum }); + } + } + + this._showEmpty(!hasData); + if (hasData) { + this.graph.update({ nodes, links }); + return true; + } + return false; + } + onMessage(msg){ + if (msg.event === "TreeSummary") { + this._lastSummary = msg.data; + } else if (msg.event === "TreeSummaryL2") { + this._lastL2 = msg.data; + } else { return; } + + if (this._renderTwoLevel()) return; + if (msg.event !== "TreeSummary") return; let rows = (msg.data || []).slice(1).map(r => { const m = r[1] || {}; diff --git a/src/rust/lqosd/src/node_manager/js_build/src/dashlets/top_tree_sankey.js b/src/rust/lqosd/src/node_manager/js_build/src/dashlets/top_tree_sankey.js index 2191cb76..29ecc036 100644 --- a/src/rust/lqosd/src/node_manager/js_build/src/dashlets/top_tree_sankey.js +++ b/src/rust/lqosd/src/node_manager/js_build/src/dashlets/top_tree_sankey.js @@ -69,6 +69,8 @@ class TopTreeSankeyGraph extends DashboardGraph { export class TopTreeSankey extends BaseDashlet { constructor(slot) { super(slot); + this._lastTreeSummary = null; + this._lastL2 = null; } canBeSlowedDown() { @@ -84,7 +86,7 @@ export class TopTreeSankey extends BaseDashlet { } subscribeTo() { - return [ "TreeSummary" ]; + return [ "TreeSummary", "TreeSummaryL2" ]; } buildContainer() { @@ -100,63 +102,143 @@ export class TopTreeSankey extends BaseDashlet { onMessage(msg) { if (msg.event === "TreeSummary") { - //console.log(msg.data); + this._lastTreeSummary = msg.data; + } else if (msg.event === "TreeSummaryL2") { + this._lastL2 = msg.data; + } else { + return; + } + + if (!this._lastTreeSummary) return; + + let redact = isRedacted(); - let redact = isRedacted(); + // Build 2-level sankey if L2 is present, else fallback to 1-level + const nodes = []; + const links = []; + nodes.push({ name: "Root", label: "Root" }); - let nodes = []; - let links = []; + const firstLevel = (this._lastTreeSummary || []).slice(1); - nodes.push({ - name: "Root", - label: "Root", - }); + // Map of parent id -> parent transport for quick lookup + const parentMap = new Map(); + firstLevel.forEach((r) => parentMap.set(r[0], r[1])); - msg.data.slice(1).forEach((r) => { - let label = { - fontSize: 9, - color: "#999" - }; + // Helper to build node style from RTT + const nodeStyleFromRtt = (name, rttsArr) => { + let rtt = 0; + if (rttsArr && rttsArr.length > 0) { + rtt = rttsArr[0]; + } else if (lastRtt[name] !== undefined) { + rtt = lastRtt[name]; + } + lastRtt[name] = rtt; + const rttPercent = Math.min(100, (rtt / 200) * 100); + const color = isColorBlindMode() + ? lerpViridis(rttPercent / 100) + : lerpGreenToRedViaOrange(200 - rtt, 200); + return { itemStyle: { color } }; + }; + + if (this._lastL2 && Array.isArray(this._lastL2) && this._lastL2.length > 0) { + // L2 data shape: [ [parent_id, [ [child_id, child_transport], ... ] ], ... ] + for (const [parentId, children] of this._lastL2) { + const p = parentMap.get(parentId); + if (!p) continue; + + // Parent node styling + const pName = p.name; + const label = { fontSize: 9, color: "#999" }; if (redact) label.fontFamily = "Illegible"; - let name = r[1].name; - let bytes = r[1].current_throughput[0]; - let bytesAsMegabits = bytes / 1000000; - let maxBytes = r[1].max_throughput[0] / 8; - let percent = Math.min(100, (bytesAsMegabits / maxBytes) * 100); - let capacityColor = isColorBlindMode() + const pStyle = nodeStyleFromRtt(pName, p.rtts); + nodes.push({ name: pName, label, ...pStyle }); + + // Compute Root->Parent value as sum of included child totals (down + up) + let parentSum = 0; + // Compute link color from parent's capacity percent (as before) + const bytesAsMegabits = p.current_throughput[0] / 1000000; + const maxBytes = p.max_throughput[0] / 8; + const percent = Math.min(100, maxBytes > 0 ? (bytesAsMegabits / maxBytes) * 100 : 0); + const capacityColor = isColorBlindMode() ? lerpViridis(percent / 100) : lerpGreenToRedViaOrange(100 - percent, 100); - if (r[1].rtts.length > 0) { - lastRtt[name] = r[1].rtts[0]; - } else { - lastRtt[name] = 0; - } - let rttPercent = Math.min(100, (lastRtt[name] / 200) * 100); - let color = isColorBlindMode() - ? lerpViridis(rttPercent / 100) - : lerpGreenToRedViaOrange(200 - lastRtt[name], 200); - - if (bytesAsMegabits > 0) { - nodes.push({ - name: r[1].name, - label: label, - itemStyle: { - color: color - } + for (const [, child] of children) { + const cName = child.name; + const cTotal = (child.current_throughput?.[0] || 0) + (child.current_throughput?.[1] || 0); + if (cTotal <= 0) continue; + parentSum += cTotal; + + // Child node and link parent->child + const cLabel = { fontSize: 9, color: "#999" }; + if (redact) cLabel.fontFamily = "Illegible"; + const cStyle = nodeStyleFromRtt(cName, child.rtts); + nodes.push({ name: cName, label: cLabel, ...cStyle }); + + // Link color for child can use child's capacity percent + const cBytesAsMegabits = (child.current_throughput?.[0] || 0) / 1000000; + const cMaxBytes = (child.max_throughput?.[0] || 0) / 8; + const cPercent = Math.min(100, cMaxBytes > 0 ? (cBytesAsMegabits / cMaxBytes) * 100 : 0); + const cCapacityColor = isColorBlindMode() + ? lerpViridis(cPercent / 100) + : lerpGreenToRedViaOrange(100 - cPercent, 100); + + links.push({ + source: pName, + target: cName, + value: cTotal, + lineStyle: { color: cCapacityColor }, }); + } + + if (parentSum > 0) { links.push({ source: "Root", - target: r[1].name, - value: r[1].current_throughput[0] + r[1].current_throughput[1], - lineStyle: { - color: capacityColor, - } + target: pName, + value: parentSum, + lineStyle: { color: capacityColor }, }); } - }); + } this.graph.update(nodes, links); + return; } + + // Fallback to 1-level sankey using current logic + firstLevel.forEach((r) => { + let label = { fontSize: 9, color: "#999" }; + if (redact) label.fontFamily = "Illegible"; + + let name = r[1].name; + let bytes = r[1].current_throughput[0]; + let bytesAsMegabits = bytes / 1000000; + let maxBytes = r[1].max_throughput[0] / 8; + let percent = Math.min(100, maxBytes > 0 ? (bytesAsMegabits / maxBytes) * 100 : 0); + let capacityColor = isColorBlindMode() + ? lerpViridis(percent / 100) + : lerpGreenToRedViaOrange(100 - percent, 100); + + if (r[1].rtts.length > 0) { + lastRtt[name] = r[1].rtts[0]; + } else { + lastRtt[name] = 0; + } + let rttPercent = Math.min(100, (lastRtt[name] / 200) * 100); + let color = isColorBlindMode() + ? lerpViridis(rttPercent / 100) + : lerpGreenToRedViaOrange(200 - lastRtt[name], 200); + + if (bytesAsMegabits > 0) { + nodes.push({ name: r[1].name, label, itemStyle: { color } }); + links.push({ + source: "Root", + target: r[1].name, + value: r[1].current_throughput[0] + r[1].current_throughput[1], + lineStyle: { color: capacityColor }, + }); + } + }); + this.graph.update(nodes, links); } } diff --git a/src/rust/lqosd/src/node_manager/ws/published_channels.rs b/src/rust/lqosd/src/node_manager/ws/published_channels.rs index ff94c917..47c3a591 100644 --- a/src/rust/lqosd/src/node_manager/ws/published_channels.rs +++ b/src/rust/lqosd/src/node_manager/ws/published_channels.rs @@ -21,6 +21,7 @@ pub enum PublishedChannels { Cpu, Ram, TreeSummary, + TreeSummaryL2, QueueStatsTotal, NetworkTree, NetworkTreeClients, diff --git a/src/rust/lqosd/src/node_manager/ws/ticker.rs b/src/rust/lqosd/src/node_manager/ws/ticker.rs index ba6e14c7..879f7874 100644 --- a/src/rust/lqosd/src/node_manager/ws/ticker.rs +++ b/src/rust/lqosd/src/node_manager/ws/ticker.rs @@ -24,6 +24,7 @@ mod top_flows; mod asn_top; mod tree_capacity; mod tree_summary; +mod tree_summary_l2; use crate::system_stats::SystemStats; pub use network_tree::all_circuits; @@ -65,6 +66,7 @@ async fn one_second_cadence( flow_endpoints::flow_duration(channels.clone(), bus_tx.clone()), endpoint_latlon::endpoint_latlon(channels.clone(), bus_tx.clone()), tree_summary::tree_summary(channels.clone(), bus_tx.clone()), + tree_summary_l2::tree_summary_l2(channels.clone()), network_tree::all_subscribers(channels.clone(), bus_tx.clone()), queue_stats_total::queue_stats_totals(channels.clone()), network_tree::network_tree(channels.clone(), bus_tx.clone()), -- knotmirror 0.0.1 From 1012aa017c9a992eef1cb84e8f232d121906429e Mon Sep 17 00:00:00 2001 From: Herbert Wolverson Date: Fri, 7 Nov 2025 13:07:30 -0600 Subject: [PATCH 12/13] Feat: Now show 2 layers (clamped) on the live sankey displays. --- .../node_manager/ws/ticker/tree_summary_l2.rs | 70 +++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 src/rust/lqosd/src/node_manager/ws/ticker/tree_summary_l2.rs diff --git a/src/rust/lqosd/src/node_manager/ws/ticker/tree_summary_l2.rs b/src/rust/lqosd/src/node_manager/ws/ticker/tree_summary_l2.rs new file mode 100644 index 00000000..000f6d1b --- /dev/null +++ b/src/rust/lqosd/src/node_manager/ws/ticker/tree_summary_l2.rs @@ -0,0 +1,70 @@ +use crate::node_manager::ws::publish_subscribe::PubSub; +use crate::node_manager::ws::published_channels::PublishedChannels; +use crate::shaped_devices_tracker::NETWORK_JSON; +use lqos_config::NetworkJsonTransport; +use serde_json::json; +use std::collections::BTreeMap; +use std::sync::Arc; + +/// Publishes a curated two-level summary of the network tree: +/// root -> first-level parents -> top-N second-level children across all parents. +/// N is currently fixed at 10. Only includes second-level items, grouped by parent id. +/// Data shape: +/// { +/// "event": "TreeSummaryL2", +/// "data": [ [ parent_id, [ [child_id, NetworkJsonTransport], ... ] ], ... ] +/// } +pub async fn tree_summary_l2(channels: Arc) { + if !channels + .is_channel_alive(PublishedChannels::TreeSummaryL2) + .await + { + return; + } + + // Build top-N second-level children across all first-level parents + let grouped: Vec<(usize, Vec<(usize, NetworkJsonTransport)>)> = { + let net_json = NETWORK_JSON.read(); + let nodes = net_json.get_nodes_when_ready(); + // Collect candidates as (parent_idx, child_idx, transport, total_bytes_per_sec) + let mut candidates: Vec<(usize, usize, NetworkJsonTransport, u64)> = Vec::new(); + + // Identify first-level parents (immediate_parent == Some(0)) + for (p_idx, p_node) in nodes.iter().enumerate() { + if p_node.immediate_parent == Some(0) { + // For each child-of-child under this parent + for (c_idx, c_node) in nodes.iter().enumerate() { + if c_node.immediate_parent == Some(p_idx) { + let t = c_node.clone_to_transit(); + let total = t.current_throughput.0 + t.current_throughput.1; + candidates.push((p_idx, c_idx, t, total)); + } + } + } + } + + // Sort by total throughput descending and cap to N + candidates.sort_by(|a, b| b.3.cmp(&a.3)); + let n: usize = 10; + if candidates.len() > n { + candidates.truncate(n); + } + + // Group by parent id + let mut map: BTreeMap> = BTreeMap::new(); + for (p_idx, c_idx, t, _total) in candidates.into_iter() { + map.entry(p_idx).or_default().push((c_idx, t)); + } + map.into_iter().collect() + }; + + let message = json!({ + "event": PublishedChannels::TreeSummaryL2.to_string(), + "data": grouped, + }) + .to_string(); + channels + .send(PublishedChannels::TreeSummaryL2, message) + .await; +} + -- knotmirror 0.0.1 From 8bf287f33e2747c30e432cc9839a56daffe73cd0 Mon Sep 17 00:00:00 2001 From: Herbert Wolverson Date: Fri, 7 Nov 2025 14:13:53 -0600 Subject: [PATCH 13/13] Feat: Adds an "urgent issues" channel allowing Python (and other external systems) to provide emergency alerts to the node manager display. Uses that to alert of the "tc overflow". FIXES #801 --- src/LibreQoS.py | 23 +++- src/rust/lqos_bus/src/bus/mod.rs | 4 +- src/rust/lqos_bus/src/bus/request.rs | 44 +++++++ src/rust/lqos_bus/src/bus/response.rs | 23 ++++ src/rust/lqos_bus/src/lib.rs | 4 +- src/rust/lqos_python/src/lib.rs | 49 ++++++- src/rust/lqosd/src/main.rs | 13 ++ src/rust/lqosd/src/node_manager/local_api.rs | 5 + .../src/node_manager/local_api/urgent.rs | 123 ++++++++++++++++++ .../src/node_manager/static2/template.html | 119 +++++++++++++++++ src/rust/lqosd/src/node_manager/template.rs | 11 ++ src/rust/lqosd/src/urgent.rs | 100 ++++++++++++++ 12 files changed, 511 insertions(+), 7 deletions(-) create mode 100644 src/rust/lqosd/src/node_manager/local_api/urgent.rs create mode 100644 src/rust/lqosd/src/urgent.rs diff --git a/src/LibreQoS.py b/src/LibreQoS.py index b68acdf7..37a6ce5b 100755 --- a/src/LibreQoS.py +++ b/src/LibreQoS.py @@ -30,6 +30,13 @@ from liblqos_python import is_lqosd_alive, clear_ip_mappings, delete_ip_mapping, fast_queues_fq_codel, \ Bakery +# Optional: urgent issue submission (available in newer liblqos_python) +try: + from liblqos_python import submit_urgent_issue # type: ignore +except Exception: + def submit_urgent_issue(*_args, **_kwargs): + return False + R2Q = 10 #MAX_R2Q = 200_000 MAX_R2Q = 60_000 # See https://lartc.vger.kernel.narkive.com/NKaH1ZNG/htb-quantum-of-class-100001-is-small-consider-r2q-change @@ -1022,7 +1029,13 @@ def refreshShapers(): minorByCPU[queue] = minorByCPU[queue] + 1 # Check for overflow - TC uses u16 for minor class ID (max 65535) if minorByCPU[queue] > 0xFFFF: - logging.error(f"Minor class ID overflow on CPU {queue}: {minorByCPU[queue]} exceeds TC's u16 limit (65535). Consider increasing queue count or restructuring network hierarchy.") + msg = f"Minor class ID overflow on CPU {queue}: {minorByCPU[queue]} exceeds TC's u16 limit (65535). Consider increasing queue count or restructuring network hierarchy." + logging.error(msg) + try: + ctx = json.dumps({"cpu": queue, "minor": minorByCPU[queue]}) + submit_urgent_issue("LibreQoS", "Error", "TC_U16_OVERFLOW", msg, ctx, f"TC_U16_OVERFLOW_CPU_{queue}") + except Exception: + pass raise ValueError(f"Minor class ID overflow on CPU {queue}: {minorByCPU[queue]} exceeds limit of 65535") # If a device from ShapedDevices.csv lists this node as its Parent Node, attach it as a leaf to this node HTB if node in circuits_by_parent_node: @@ -1123,7 +1136,13 @@ def refreshShapers(): minorByCPU[queue] = minorByCPU[queue] + 1 # Check for overflow - TC uses u16 for minor class ID (max 65535) if minorByCPU[queue] > 0xFFFF: - logging.error(f"Minor class ID overflow on CPU {queue}: {minorByCPU[queue]} exceeds TC's u16 limit (65535). Consider increasing queue count or restructuring network hierarchy.") + msg = f"Minor class ID overflow on CPU {queue}: {minorByCPU[queue]} exceeds TC's u16 limit (65535). Consider increasing queue count or restructuring network hierarchy." + logging.error(msg) + try: + ctx = json.dumps({"cpu": queue, "minor": minorByCPU[queue]}) + submit_urgent_issue("LibreQoS", "Error", "TC_U16_OVERFLOW", msg, ctx, f"TC_U16_OVERFLOW_CPU_{queue}") + except Exception: + pass raise ValueError(f"Minor class ID overflow on CPU {queue}: {minorByCPU[queue]} exceeds limit of 65535") minorByCPU = traverseNetwork(sorted_children, depth+1, major, minorByCPU, queue, nodeClassID, upNodeClassID, data[node]['downloadBandwidthMbps'], data[node]['uploadBandwidthMbps'], data[node]['downloadBandwidthMbpsMin'], data[node]['uploadBandwidthMbpsMin']) # If top level node, increment to next queue / cpu core diff --git a/src/rust/lqos_bus/src/bus/mod.rs b/src/rust/lqos_bus/src/bus/mod.rs index ee1e08db..8aed0a53 100644 --- a/src/rust/lqos_bus/src/bus/mod.rs +++ b/src/rust/lqos_bus/src/bus/mod.rs @@ -11,8 +11,8 @@ mod unix_socket_server; pub use client::{LibreqosBusClient, bus_request}; pub use queue_data::*; pub use reply::BusReply; -pub use request::{BlackboardSystem, BusRequest, TopFlowType}; -pub use response::BusResponse; +pub use request::{BlackboardSystem, BusRequest, TopFlowType, UrgentSeverity, UrgentSource}; +pub use response::{BakeryStatsSnapshot, BusResponse, UrgentIssue}; pub use session::BusSession; use thiserror::Error; use tracing::error; diff --git a/src/rust/lqos_bus/src/bus/request.rs b/src/rust/lqos_bus/src/bus/request.rs index c4d96641..128b19c9 100644 --- a/src/rust/lqos_bus/src/bus/request.rs +++ b/src/rust/lqos_bus/src/bus/request.rs @@ -6,6 +6,28 @@ use allocative::Allocative; use lqos_config::Tunables; use serde::{Deserialize, Serialize}; +/// Source system for an urgent issue +#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, Copy, Allocative)] +pub enum UrgentSource { + /// Raised by the scheduler process + Scheduler, + /// Raised by the LibreQoS Python orchestrator + LibreQoS, + /// Raised by the local API server + API, + /// Raised by lqosd or other components + System, +} + +/// Severity of an urgent issue +#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, Copy, Allocative)] +pub enum UrgentSeverity { + /// Error requires attention + Error, + /// Warning is informative/high-visibility + Warning, +} + /// One or more `BusRequest` objects must be included in a `BusSession` /// request. Each `BusRequest` represents a single request for action /// or data. @@ -308,6 +330,28 @@ pub enum BusRequest { /// Check the scheduler status CheckSchedulerStatus, + + /// Submit an urgent issue for high-priority operator visibility + SubmitUrgentIssue { + /// Source of the issue + source: UrgentSource, + /// Severity of the issue + severity: UrgentSeverity, + /// Machine-readable code for the issue (e.g. TC_U16_OVERFLOW) + code: String, + /// Human-readable message for display + message: String, + /// Optional JSON context payload + context: Option, + /// Optional key to deduplicate repeated submissions + dedupe_key: Option, + }, + + /// Retrieve current urgent issues + GetUrgentIssues, + + /// Clear a specific urgent issue by ID + ClearUrgentIssue(u64), } /// Defines the parts of the blackboard diff --git a/src/rust/lqos_bus/src/bus/response.rs b/src/rust/lqos_bus/src/bus/response.rs index e421e46b..2dff1922 100644 --- a/src/rust/lqos_bus/src/bus/response.rs +++ b/src/rust/lqos_bus/src/bus/response.rs @@ -11,6 +11,26 @@ use lqos_utils::units::DownUpOrder; use serde::{Deserialize, Serialize}; use std::net::IpAddr; +/// An urgent issue to be displayed prominently in the UI +#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Allocative)] +pub struct UrgentIssue { + /// Unique identifier + pub id: u64, + /// Unix timestamp (seconds) + pub ts: u64, + /// Source component + pub source: crate::bus::request::UrgentSource, + /// Severity level + pub severity: crate::bus::request::UrgentSeverity, + /// Machine-readable code (e.g., TC_U16_OVERFLOW) + pub code: String, + /// Human-readable message + pub message: String, + /// Optional JSON context + pub context: Option, + /// Optional dedupe key + pub dedupe_key: Option, +} /// Serializable snapshot of BakeryStats for bus transmission #[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Allocative)] pub struct BakeryStatsSnapshot { @@ -187,4 +207,7 @@ pub enum BusResponse { /// Any error message from integrations error: Option, }, + + /// List of urgent issues + UrgentIssues(Vec), } diff --git a/src/rust/lqos_bus/src/lib.rs b/src/rust/lqos_bus/src/lib.rs index 9b249d48..e016094d 100644 --- a/src/rust/lqos_bus/src/lib.rs +++ b/src/rust/lqos_bus/src/lib.rs @@ -21,11 +21,11 @@ pub use ip_stats::{ tos_parser, }; mod tc_handle; -pub use bus::response::BakeryStatsSnapshot; +pub use bus::response::{BakeryStatsSnapshot, UrgentIssue}; pub use bus::{ BUS_SOCKET_PATH, BlackboardSystem, BusReply, BusRequest, BusResponse, BusSession, CakeDiffTinTransit, CakeDiffTransit, CakeTransit, LibreqosBusClient, QueueStoreTransit, - TopFlowType, UnixSocketServer, bus_request, + TopFlowType, UnixSocketServer, UrgentSeverity, UrgentSource, bus_request, }; pub use tc_handle::TcHandle; diff --git a/src/rust/lqos_python/src/lib.rs b/src/rust/lqos_python/src/lib.rs index 988b2141..5774d612 100644 --- a/src/rust/lqos_python/src/lib.rs +++ b/src/rust/lqos_python/src/lib.rs @@ -1,6 +1,6 @@ #![allow(non_local_definitions)] // Temporary: rewrite required for much of this, for newer PyO3. #![allow(unsafe_op_in_unsafe_fn)] -use lqos_bus::{BlackboardSystem, BusRequest, BusResponse, TcHandle}; +use lqos_bus::{BlackboardSystem, BusRequest, BusResponse, TcHandle, UrgentSource, UrgentSeverity}; use lqos_utils::hex_string::read_hex_string; use nix::libc::getpid; use pyo3::exceptions::PyOSError; @@ -117,6 +117,7 @@ fn liblqos_python(m: &Bound<'_, PyModule>) -> PyResult<()> { m.add_function(wrap_pyfunction!(calculate_hash, m)?)?; m.add_function(wrap_pyfunction!(scheduler_alive, m)?)?; m.add_function(wrap_pyfunction!(scheduler_error, m)?)?; + m.add_function(wrap_pyfunction!(submit_urgent_issue, m)?)?; m.add_class::()?; Ok(()) @@ -1170,3 +1171,49 @@ fn scheduler_error(_py: Python, error: String) -> PyResult { } Ok(false) } + +/// Submit an urgent issue for prominent display in the Node Manager UI. +/// +/// Parameters: +/// - source: one of "Scheduler", "LibreQoS", "API", "System" +/// - severity: "Error" or "Warning" +/// - code: short machine-readable code (e.g., "TC_U16_OVERFLOW") +/// - message: human-readable description +/// - context: optional JSON string with extra details +/// - dedupe_key: optional key to deduplicate repeats (e.g., code+cpu) +#[pyfunction] +fn submit_urgent_issue( + _py: Python, + source: String, + severity: String, + code: String, + message: String, + context: Option, + dedupe_key: Option, +) -> PyResult { + let src = match source.to_ascii_lowercase().as_str() { + "scheduler" => UrgentSource::Scheduler, + "libreqos" => UrgentSource::LibreQoS, + "api" => UrgentSource::API, + _ => UrgentSource::System, + }; + let sev = match severity.to_ascii_lowercase().as_str() { + "warning" => UrgentSeverity::Warning, + _ => UrgentSeverity::Error, + }; + if let Ok(reply) = run_query(vec![BusRequest::SubmitUrgentIssue { + source: src, + severity: sev, + code, + message, + context, + dedupe_key, + }]) { + for resp in reply.iter() { + if let BusResponse::Ack = resp { + return Ok(true); + } + } + } + Ok(false) +} diff --git a/src/rust/lqosd/src/main.rs b/src/rust/lqosd/src/main.rs index 70255c70..1f66eb0a 100644 --- a/src/rust/lqosd/src/main.rs +++ b/src/rust/lqosd/src/main.rs @@ -19,6 +19,7 @@ mod stats; mod system_stats; mod throughput_tracker; mod tool_status; +mod urgent; mod tuning; mod validation; mod version_checks; @@ -639,6 +640,18 @@ fn handle_bus_requests(requests: &[BusRequest], responses: &mut Vec let error = tool_status::scheduler_error_message(); BusResponse::SchedulerStatus { running, error } } + BusRequest::SubmitUrgentIssue { source, severity, code, message, context, dedupe_key } => { + urgent::submit(*source, *severity, code.clone(), message.clone(), context.clone(), dedupe_key.clone()); + BusResponse::Ack + } + BusRequest::GetUrgentIssues => { + let list = urgent::list(); + BusResponse::UrgentIssues(list) + } + BusRequest::ClearUrgentIssue(id) => { + urgent::clear(*id); + BusResponse::Ack + } }); } } diff --git a/src/rust/lqosd/src/node_manager/local_api.rs b/src/rust/lqosd/src/node_manager/local_api.rs index dddf2a20..df6b8fea 100644 --- a/src/rust/lqosd/src/node_manager/local_api.rs +++ b/src/rust/lqosd/src/node_manager/local_api.rs @@ -12,6 +12,7 @@ mod cpu_affinity; mod packet_analysis; mod reload_libreqos; mod scheduler; +mod urgent; mod search; mod shaped_device_api; mod support; @@ -117,6 +118,10 @@ pub fn local_api(shaper_query: tokio::sync::mpsc::Sender) -> .route("/ltsRecentMedian", get(lts::recent_medians)) .route("/scheduler/status", get(scheduler::scheduler_status)) .route("/scheduler/details", get(scheduler::scheduler_details)) + .route("/urgent/status", get(urgent::urgent_status)) + .route("/urgent/list", get(urgent::urgent_list)) + .route("/urgent/clear/:id", post(urgent::urgent_clear)) + .route("/urgent/clear_all", post(urgent::urgent_clear_all)) .route("/chatbot_sso_token", get(chatbot_sso_token)) .layer(Extension(shaper_query)) .layer(CorsLayer::very_permissive()) diff --git a/src/rust/lqosd/src/node_manager/local_api/urgent.rs b/src/rust/lqosd/src/node_manager/local_api/urgent.rs new file mode 100644 index 00000000..03c7c93e --- /dev/null +++ b/src/rust/lqosd/src/node_manager/local_api/urgent.rs @@ -0,0 +1,123 @@ +use axum::extract::Path; +use axum::http::StatusCode; +use axum::Json; +use serde::Serialize; + +use crate::urgent; + +// Remove ANSI escape sequences (basic CSI/OSC handling) for browser display +fn strip_ansi(input: &str) -> String { + let bytes = input.as_bytes(); + let mut out = String::with_capacity(bytes.len()); + let mut i = 0; + while i < bytes.len() { + if bytes[i] == 0x1B { + // ESC sequence + i += 1; + if i >= bytes.len() { + break; + } + match bytes[i] as char { + '[' => { + // CSI: ESC [ ... final byte 0x40..=0x7E + i += 1; + while i < bytes.len() { + let b = bytes[i]; + if (0x40..=0x7E).contains(&b) { + i += 1; + break; + } + i += 1; + } + } + ']' => { + // OSC: ESC ] ... BEL (0x07) or ESC \ + i += 1; + while i < bytes.len() { + if bytes[i] == 0x07 { + i += 1; + break; + } + if bytes[i] == 0x1B { + // ESC + if i + 1 < bytes.len() && bytes[i + 1] as char == '\\' { + i += 2; // ESC \ + break; + } + } + i += 1; + } + } + _ => { + // Other ESC-seq: skip next char at least + i += 1; + } + } + } else { + out.push(bytes[i] as char); + i += 1; + } + } + out +} + +#[derive(Serialize)] +pub struct UrgentStatus { + pub has_urgent: bool, + pub count: usize, +} + +#[derive(Serialize)] +pub struct UrgentItem { + pub id: u64, + pub ts: u64, + pub source: String, + pub severity: String, + pub code: String, + pub message: String, + pub context: Option, +} + +#[derive(Serialize)] +pub struct UrgentList { + pub items: Vec, +} + +pub async fn urgent_status() -> (StatusCode, Json) { + let items = urgent::list(); + ( + StatusCode::OK, + Json(UrgentStatus { has_urgent: !items.is_empty(), count: items.len() }), + ) +} + +pub async fn urgent_list() -> (StatusCode, Json) { + let items = urgent::list(); + let items: Vec = items + .into_iter() + .map(|i| UrgentItem { + id: i.id, + ts: i.ts, + source: format!("{:?}", i.source), + severity: format!("{:?}", i.severity), + code: i.code, + message: strip_ansi(&i.message), + context: i.context, + }) + .collect(); + (StatusCode::OK, Json(UrgentList { items })) +} + +pub async fn urgent_clear(Path(id): Path) -> (StatusCode, &'static str) { + let ok = urgent::clear(id); + if ok { + (StatusCode::OK, "OK") + } else { + (StatusCode::NOT_FOUND, "NOT_FOUND") + } +} + +pub async fn urgent_clear_all() -> (StatusCode, &'static str) { + urgent::clear_all(); + (StatusCode::OK, "OK") +} diff --git a/src/rust/lqosd/src/node_manager/static2/template.html b/src/rust/lqosd/src/node_manager/static2/template.html index 88540ba2..0c2fa7c6 100644 --- a/src/rust/lqosd/src/node_manager/static2/template.html +++ b/src/rust/lqosd/src/node_manager/static2/template.html @@ -116,6 +116,7 @@ Reload LibreQoS + %%URGENT_STATUS%% %%SCHEDULER_STATUS%%


@@ -210,6 +211,27 @@ + + +
LibreQoS %%VERSION%%. © Copyright 2022-2024, LibreQoE LLC. All Rights Reserved.
@@ -231,6 +253,103 @@ }); obs.observe(container, { childList: true, subtree: true }); })(); + + // Urgent Issues: poll status, update badge, and show modal with details + (function(){ + const containerId = 'urgentStatus'; + const linkId = 'urgentStatusLink'; + const badgeId = 'urgentBadge'; + + function ensurePlaceholder(){ + // no-op: server injects a placeholder; if not present, skip + return document.getElementById(containerId) !== null; + } + + function renderStatus(count){ + const cont = document.getElementById(containerId); + if (!cont) return; + const cls = count > 0 ? 'text-danger' : 'text-secondary'; + const icon = count > 0 ? 'fa-bell' : 'fa-bell-slash'; + cont.innerHTML = ` + + Urgent Issues + ${count} + `; + $("#"+containerId).off("click").on("click", `#${linkId}`, (e)=>{ + e.preventDefault(); + showModal(); + }); + } + + function poll(){ + if (!ensurePlaceholder()) return; + $.get('/local-api/urgent/status', (res)=>{ + renderStatus(res.count||0); + }); + } + + function showModal(){ + const modalEl = document.getElementById('urgentModal'); + if (!modalEl) return; + new bootstrap.Modal(modalEl,{focus:true}).show(); + const holder = document.getElementById('urgentListContainer'); + holder.innerHTML = `
Loading...
`; + $.get('/local-api/urgent/list', (res)=>{ + const items = res.items||[]; + if (items.length===0){ holder.innerHTML = '
No urgent issues.
'; return; } + const table = document.createElement('table'); + table.className = 'table table-sm table-striped'; + const tbody = document.createElement('tbody'); + items.forEach(it=>{ + const tr = document.createElement('tr'); + const td = document.createElement('td'); + const when = new Date(it.ts*1000).toLocaleString(); + const sev = it.severity === 'Error' ? 'danger' : 'warning'; + td.innerHTML = ` +
+ ${it.severity} + ${it.code} + (${it.source}) + ${when} + +
+
${it.message}
+ ${it.context?`
${it.context}
`:''} + `; + tr.appendChild(td); + tbody.appendChild(tr); + }); + table.appendChild(tbody); + // Clear existing content and insert table + holder.innerHTML = ''; + holder.appendChild(table); + // Bind clear buttons (acknowledge) + $(holder).off('click').on('click','a.urgent-clear', function(e){ + e.preventDefault(); + const id = $(this).data('id'); + $.post(`/local-api/urgent/clear/${id}`, ()=>{ showModal(); poll(); }); + }); + }).fail(()=>{ + holder.innerHTML = '
Failed to load urgent issues.
'; + }); + } + + // Initial placeholder in case server didn’t inject content (defensive) + if (!document.getElementById(containerId)){ + const ul = document.querySelector('.sidebar .navbar-nav'); + if (ul){ + const li = document.createElement('li'); li.className='nav-item'; li.id=containerId; ul.appendChild(li); + } + } + // Clear all action + $(document).off('click','#urgentClearAll').on('click','#urgentClearAll', function(){ + $.post('/local-api/urgent/clear_all', ()=>{ showModal(); poll(); }); + }); + + // Start polling + poll(); + setInterval(poll, 30000); + })(); diff --git a/src/rust/lqosd/src/node_manager/template.rs b/src/rust/lqosd/src/node_manager/template.rs index 12c5473d..2fd818d7 100644 --- a/src/rust/lqosd/src/node_manager/template.rs +++ b/src/rust/lqosd/src/node_manager/template.rs @@ -167,6 +167,17 @@ pub async fn apply_templates( "##; let byte_string = byte_string.replace("%%SCHEDULER_STATUS%%", scheduler_placeholder); + // Placeholder for urgent issues indicator + let urgent_placeholder = r##" + +"##; + let byte_string = byte_string.replace("%%URGENT_STATUS%%", urgent_placeholder); + let byte_string = byte_string.replace("%CACHEBUSTERS%", &format!("?gh={}", GIT_HASH)); if let Some(length) = res_parts.headers.get_mut("content-length") { *length = HeaderValue::from(byte_string.len()); diff --git a/src/rust/lqosd/src/urgent.rs b/src/rust/lqosd/src/urgent.rs new file mode 100644 index 00000000..483798af --- /dev/null +++ b/src/rust/lqosd/src/urgent.rs @@ -0,0 +1,100 @@ +use std::collections::VecDeque; +use std::sync::atomic::{AtomicU64, Ordering}; + +use lqos_bus::{UrgentIssue, UrgentSeverity, UrgentSource}; +use parking_lot::Mutex; + +use lqos_utils::unix_time::unix_now; + +static NEXT_ID: AtomicU64 = AtomicU64::new(0); +static URGENT: Mutex> = Mutex::new(VecDeque::new()); + +const MAX_ISSUES: usize = 100; +const TTL_SECONDS: u64 = 24 * 60 * 60; // 24h +const DEDUPE_WINDOW_SECONDS: u64 = 300; // 5 minutes + +fn now_unix() -> u64 { + if let Ok(n) = unix_now() { + n + } else { + 0 + } +} + +fn prune_expired(q: &mut VecDeque) { + let now = now_unix(); + while let Some(front) = q.front() { + if front.ts + TTL_SECONDS < now { + q.pop_front(); + } else { + break; + } + } + while q.len() > MAX_ISSUES { + q.pop_front(); + } +} + +pub fn submit( + source: UrgentSource, + severity: UrgentSeverity, + code: String, + message: String, + context: Option, + dedupe_key: Option, +) { + let ts = now_unix(); + let id = NEXT_ID.fetch_add(1, Ordering::Relaxed) + 1; + let mut guard = URGENT.lock(); + prune_expired(&mut guard); + + // Dedupe: same (code, dedupe_key) within window → update timestamp/message + let key = dedupe_key.clone().unwrap_or_else(|| code.clone()); + if let Some(existing) = guard.iter_mut().rev().find(|i| { + (i.code == code) + && (i.dedupe_key.as_deref().unwrap_or("") == key) + && (ts.saturating_sub(i.ts) <= DEDUPE_WINDOW_SECONDS) + }) { + existing.ts = ts; + existing.message = message; + existing.context = context; + return; + } + + let issue = UrgentIssue { + id, + ts, + source, + severity, + code, + message, + context, + dedupe_key: Some(key), + }; + guard.push_back(issue); + prune_expired(&mut guard); +} + +pub fn list() -> Vec { + let mut guard = URGENT.lock(); + prune_expired(&mut guard); + let mut v: Vec = guard.iter().cloned().collect(); + v.sort_by_key(|i| i.ts); + v.reverse(); + v +} + +pub fn clear(id: u64) -> bool { + let mut guard = URGENT.lock(); + if let Some(pos) = guard.iter().position(|i| i.id == id) { + guard.remove(pos); + true + } else { + false + } +} + +pub fn clear_all() { + let mut guard = URGENT.lock(); + guard.clear(); +}