diff --git a/index.js b/index.js deleted file mode 100644 index 180ea80..0000000 --- a/index.js +++ /dev/null @@ -1,121 +0,0 @@ -function formatDataRateIEC(num, precision = -1) { - if (num <= 0) { - value = '0b/s'; - } else { - var k = 1024; - var sizes = ['b/s', 'Kb/s', 'Mb/s', 'Gb/s', 'Tb/s', 'Pb/s', 'Eb/s', 'Zb/s', 'Yb/s']; - //这里是取自然对数,也就是log(k)(num),求出以k为底的多少次方是num - var c = Math.floor(Math.log(num) / Math.log(k)); - if (precision == -1) { - value = (num / Math.pow(k, c)) + ' ' + sizes[c]; - } else { - value = (num / Math.pow(k, c)).toPrecision(precision) + ' ' + sizes[c]; - } - console.log(value); - } - return value; -} -var baseSeries = { - itemStyle: { - color: '#ef0000', - }, - "markLine": { - "data": [{ - "type": "average", - "name": "平均值" - }], - "label": { - formatter: value => formatDataRateIEC(value.value, 4) - } - }, - "markPoint": { - "data": [{ - "type": "max", - "name": "最大值" - }], - symbol: "roundRect", - symbolSize: [70, 30], - "label": { - formatter: value => formatDataRateIEC(value.value, 4) - } - }, - "smooth": true, - "type": "line" -} -var xAxisData = ["16:00:22", "16:09:27", "16:09:32", "16:09:37", "16:09:42", "16:09:47", "16:09:52", "16:09:57", "16:10:02", "16:10:07", "16:10:12", "16:10:17", "16:10:22", "16:10:27", "16:10:32", "16:10:37", "16:10:42", "16:10:47", "16:10:52", "16:10:57", "16:11:02", "16:11:07", "16:11:12", "16:11:17", "16:11:22", "16:11:27", "16:11:32", "16:11:37", "16:11:42", "16:11:47", "16:11:52", "16:11:57", "16:12:02", "16:12:07", "16:12:12", "16:12:17", "16:12:22", "16:12:27", "16:12:32", "16:12:37", "16:12:42", "16:12:47", "16:12:52", "16:12:57", "16:13:02", "16:13:07", "16:13:12", "16:13:17", "16:13:22", "16:13:27", "16:13:32", "16:13:37", "16:13:42", "16:13:47", "16:13:52", "16:13:57", "16:14:02", "16:14:07", "16:14:12", "16:14:17", "16:14:22", "16:14:27", "16:14:32", "16:14:37", "16:14:42", "16:14:47", "16:14:52", "16:14:57", "16:15:02", "16:15:07", "16:15:12", "16:15:17", "16:15:22", "16:15:27", "16:15:32", "16:15:37", "16:15:42", "16:15:47", "16:15:52", "16:15:57", "16:16:02", "16:16:07", "16:16:12", "16:16:17", "16:16:22", "16:16:27", "16:16:32", "16:16:37", "16:16:42", "16:16:47", "16:16:52", "16:16:57", "16:17:02", "16:17:07", "16:17:12", "16:17:17", "16:17:22", "16:17:27", "16:17:32", "16:17:37", "16:17:42", "16:17:47", "16:17:52", "16:17:57", "16:18:02", "16:18:07", "16:18:12", "16:18:17", "16:18:22", "16:18:27", "16:18:32", "16:18:37", "16:18:42", "16:18:47", "16:18:52", "16:18:57", "16:19:02", "16:19:07", "16:19:12", "16:19:17", "16:19:22", "16:19:27", "16:19:32", "16:19:37", "16:19:42", "16:19:47", "16:19:52", "16:19:57", "16:20:02", "16:20:07", "16:20:12", "16:20:17", "16:20:22", "16:20:27", "16:20:32", "16:20:37", "16:20:42", "16:20:47", "16:20:52", "16:20:57", "16:21:02", "16:21:07", "16:21:12", "16:21:17", "16:21:22", "16:21:27", "16:21:32", "16:21:37", "16:21:42", "16:21:47", "16:21:52", "16:21:57", "16:22:02", "16:22:07", "16:22:12", "16:22:17", "16:22:22", "16:22:27", "16:22:32", "16:22:37", "16:22:42", "16:22:47", "16:22:52", "16:22:57", "16:23:02", "16:23:07", "16:23:12", "16:23:17", "16:23:22", "16:23:27", "16:23:32", "16:23:37", "16:23:42", "16:23:47", "16:23:52", "16:23:57", "16:24:02", "16:24:07", "16:24:12", "16:24:17"] -var series = [ - { - ...baseSeries, - "data": [22401, 24920, 44075, 80001, 33564, 17705, 149862, 20472, 20104, 22595, 22460, 20289, 19464, 98560, 48974, 58428, 174364, 139939, 76558, 94769, 18092, 18120, 18180, 21836, 22331, 20329, 18072, 17574, 41708, 22502, 264985, 164155, 168273, 292262, 314764, 290873, 79948, 20878, 18156, 90105, 47017, 53025, 163912, 397180, 17870, 21073, 18025, 17787, 19012, 17574, 22625, 17865, 2214612, 18044, 26464, 18126, 19508, 18560, 18048, 18001, 23923, 17801, 33249, 22216, 18174, 30697, 20812, 49860, 17921, 19937, 17697, 22174, 18081, 18227, 18016, 17696, 46099, 18756, 25027, 17692, 30668, 18529, 18224, 28620, 19084, 17806, 17832, 23312, 28137, 18281, 22288, 17636, 18035, 20134, 18209, 17843, 17851, 17700, 17934, 17630, 17840, 18054, 25401, 18049, 18057, 17740, 17844, 17819, 18219, 17632, 17843, 17907, 18035, 17633, 18555, 24528, 18550, 18224, 20262, 50228, 18083, 17822, 49830, 17844, 18113, 18539, 24323, 18299, 25025, 17862, 17993, 18288, 18171, 150160, 26512, 32521, 17720, 50036, 62606, 136796, 750452, 1152323, 1318662, 1271281, 1428193, 1873827, 1766390, 45793, 17619, 18121, 32843, 25798, 21752, 25558, 19739, 17878, 84790, 187249, 17828, 20284, 24081, 68267, 65760, 21937, 19284, 17686, 19660, 17988, 23868, 18054, 17776, 17779, 17673, 18920316, 1840708, 1418969, 22595, 17939, 112825, 1429664], - "name": "上行网速", - }, - { - ...baseSeries, - "data": [2401, 4920, 4075, 1, 33564, 17705, 149862, 20472, 20104, 22595, 22460, 20289, 19464, 98560, 48974, 58428, 174364, 139939, 76558, 94769, 18092, 18120, 18180, 21836, 22331, 20329, 18072, 17574, 41708, 22502, 264985, 164155, 168273, 292262, 314764, 290873, 79948, 20878, 18156, 90105, 47017, 53025, 163912, 397180, 17870, 21073, 18025, 17787, 19012, 17574, 22625, 17865, 2214612, 18044, 26464, 18126, 19508, 18560, 18048, 18001, 23923, 17801, 33249, 22216, 18174, 30697, 20812, 49860, 17921, 19937, 17697, 22174, 18081, 18227, 18016, 17696, 46099, 18756, 25027, 17692, 30668, 18529, 18224, 28620, 19084, 17806, 17832, 23312, 28137, 18281, 22288, 17636, 18035, 20134, 18209, 17843, 17851, 17700, 17934, 17630, 17840, 18054, 25401, 18049, 18057, 17740, 17844, 17819, 18219, 17632, 17843, 17907, 18035, 17633, 18555, 24528, 18550, 18224, 20262, 50228, 18083, 17822, 49830, 17844, 18113, 18539, 24323, 18299, 25025, 17862, 17993, 18288, 18171, 150160, 26512, 32521, 17720, 50036, 62606, 136796, 750452, 1152323, 1318662, 1271281, 1428193, 1873827, 1766390, 45793, 17619, 18121, 32843, 25798, 21752, 25558, 19739, 17878, 84790, 187249, 17828, 20284, 24081, 68267, 65760, 21937, 19284, 17686, 19660, 17988, 23868, 18054, 17776, 17779, 17673, 18920316, 1840708, 1418969, 22595, 17939, 112825, 1429664], - "name": "下行网速", - itemStyle: { - color: '#0f00f0', - }, - } -]; -// 指定图表的配置项和数据 -var option = { - title: { - text: '' - }, - tooltip: { - trigger: 'axis', - formatter: series => { - return series[0].name + series.map(s => '
' + s.seriesName + ': ' + formatDataRateIEC(s.value, 4)).join(''); - } - }, - legend: { - data: series.map(s => s.name) - }, - toolbox: { - feature: { - mark: { - show: true - }, - dataView: { - show: true, - readOnly: false - }, - magicType: { - show: true, - type: ['line', 'bar'] - }, - restore: { - show: true - }, - saveAsImage: { - show: true - } - } - }, - xAxis: { - type: 'category', - boundaryGap: false, - data: xAxisData - }, - yAxis: { - type: "value", - max: value => { - var k = 1024; - var c = Math.floor(Math.log(value.max) / Math.log(k)); - interval = Math.pow(k, c); - return Math.ceil(value.max / interval) * interval; - }, - interval: 8388608, - axisLabel: { - formatter: (value, index) => formatDataRateIEC(value) - }, - }, - series: series, - animation: false, - animationDuration: 5 -}; -// 基于准备好的dom,初始化echarts实例 -var myChart = echarts.init(document.getElementById('main')); -// 使用刚指定的配置项和数据显示图表。 -myChart.setOption(option); \ No newline at end of file diff --git a/rust_http_proxy/html/net.html b/rust_http_proxy/html/net.html index 7795839..e1f2ea9 100644 --- a/rust_http_proxy/html/net.html +++ b/rust_http_proxy/html/net.html @@ -9,7 +9,121 @@
- + \ No newline at end of file diff --git a/rust_http_proxy/src/linux_monitor.rs b/rust_http_proxy/src/linux_monitor.rs index 42790ea..157387e 100644 --- a/rust_http_proxy/src/linux_monitor.rs +++ b/rust_http_proxy/src/linux_monitor.rs @@ -8,7 +8,7 @@ use log::warn; use std::collections::VecDeque; use std::io; -use std::sync::Arc; +use std::sync::{Arc, LazyLock}; use std::time::{Duration, SystemTime}; use tokio::sync::RwLock; @@ -109,10 +109,15 @@ impl NetMonitor { if max_up / interval > 10 { interval = (max_up / interval / 10) * interval; } - let body = format!( - "{} {}网速 {} {:?} {} {} {} {:?} {}", - PART0, hostname, PART1, scales, PART2, interval, PART3, series_up, PART4 - ); + // 创建上下文并插入数据 + let mut context = tera::Context::new(); + context.insert("hostname", hostname); + context.insert("interval", &interval); + context.insert("series_up", format!("{:?}", series_up).as_str()); + context.insert("scales", format!("{:?}", scales).as_str()); + + // 渲染模板 + let body: String = TERA.render(NET_HTML, &context).unwrap_or("".to_string()); let builder = Response::builder() .status(StatusCode::OK) .header(http::header::SERVER, SERVER_NAME) @@ -158,11 +163,14 @@ pub fn count_stream() -> Result>, Error> { } } -const PART0: &str = include_str!("../html/part0.html"); -const PART1: &str = include_str!("../html/part1.html"); -const PART2: &str = include_str!("../html/part2.html"); -const PART3: &str = include_str!("../html/part3.html"); -const PART4: &str = include_str!("../html/part4.html"); +const NET_HTML: &str = "net.html"; +const NET_HTML_TEMPLATE: &str = include_str!("../html/net.html"); +static TERA: LazyLock = LazyLock::new(|| { + let mut tmp = tera::Tera::default(); + tmp.add_raw_template(NET_HTML, NET_HTML_TEMPLATE) + .unwrap_or(()); + tmp +}); // Inter-| Receive | Transmit // face |bytes packets errs drop fifo frame compressed multicast|bytes packets errs drop fifo colls carrier compressed