Skip to content

Commit 8cf1e9f

Browse files
committed
feat: add metric enable config
1 parent 33a2daa commit 8cf1e9f

File tree

11 files changed

+44
-1
lines changed

11 files changed

+44
-1
lines changed

landscape-common/src/config/loader.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ impl RuntimeConfig {
9595
};
9696

9797
let metric = MetricRuntimeConfig {
98+
enable: config.metric.enable.unwrap_or(crate::DEFAULT_METRIC_ENABLE),
9899
raw_retention_minutes: config
99100
.metric
100101
.raw_retention_minutes

landscape-common/src/config/runtime.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ pub struct StoreRuntimeConfig {
5050

5151
#[derive(Clone, Debug)]
5252
pub struct MetricRuntimeConfig {
53+
pub enable: bool,
5354
pub raw_retention_minutes: u64,
5455
pub rollup_1m_retention_days: u64,
5556
pub rollup_1h_retention_days: u64,
@@ -107,6 +108,7 @@ impl RuntimeConfig {
107108
Database Connect: {}\n\
108109
\n\
109110
[Metric]\n\
111+
Enabled: {}\n\
110112
Raw Retention: {} mins\n\
111113
Rollup 1m Retention: {} days\n\
112114
Rollup 1h Retention: {} days\n\
@@ -131,6 +133,7 @@ impl RuntimeConfig {
131133
address_http_str,
132134
address_https_str,
133135
self.store.database_path,
136+
self.metric.enable,
134137
self.metric.raw_retention_minutes,
135138
self.metric.rollup_1m_retention_days,
136139
self.metric.rollup_1h_retention_days,
@@ -150,6 +153,9 @@ impl RuntimeConfig {
150153

151154
impl MetricRuntimeConfig {
152155
pub fn update_from_file_config(&mut self, config: &LandscapeMetricConfig) {
156+
if let Some(v) = config.enable {
157+
self.enable = v;
158+
}
153159
if let Some(v) = config.raw_retention_minutes {
154160
self.raw_retention_minutes = v;
155161
}

landscape-common/src/config/settings.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ pub struct LandscapeStoreConfig {
4949
#[derive(Debug, Serialize, Deserialize, Clone, Default)]
5050
#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
5151
pub struct LandscapeMetricConfig {
52+
#[serde(default, skip_serializing_if = "Option::is_none")]
53+
#[cfg_attr(feature = "openapi", schema(required = false, nullable = false))]
54+
pub enable: Option<bool>,
5255
#[serde(default, skip_serializing_if = "Option::is_none")]
5356
#[cfg_attr(feature = "openapi", schema(required = false, nullable = false))]
5457
pub raw_retention_minutes: Option<u64>,

landscape-common/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ pub const LANDSCAPE_METRIC_DIR_NAME: &str = "metric";
7777
pub const LANDSCAPE_METRIC_DB_VERSION: u32 = 10;
7878

7979
// Metric Retention Defaults
80+
pub const DEFAULT_METRIC_ENABLE: bool = true;
8081
pub const DEFAULT_METRIC_RAW_RETENTION_MINUTES: u64 = 5;
8182
pub const DEFAULT_METRIC_ROLLUP_1M_RETENTION_DAYS: u64 = 1;
8283
pub const DEFAULT_METRIC_ROLLUP_1H_RETENTION_DAYS: u64 = 7;

landscape-webserver/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ async fn run(home_path: PathBuf, config: RuntimeConfig) -> LdResult<()> {
366366
dns_upstream_service.clone(),
367367
config.dns.clone(),
368368
cert_service.clone(),
369-
Some(metric_service.data.dns_metric.get_msg_channel()),
369+
metric_service.get_dns_metric_channel(),
370370
)
371371
.await;
372372
let fire_wall_rule_service = FirewallRuleService::new(db_store_provider.clone()).await;

landscape-webui/src/i18n/en/config.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ export default {
99
save_dns: "Save DNS Config",
1010
save_metric: "Save Metric Config",
1111

12+
metric_enabled: "Enable Metric Service",
13+
metric_enabled_desc:
14+
"When disabled, new connection and DNS metric data will no longer be collected",
15+
1216
language: "Language",
1317
theme: "Theme",
1418
theme_placeholder: "Light mode is under development",

landscape-webui/src/i18n/zh/config.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ export default {
99
save_dns: "保存 DNS 配置",
1010
save_metric: "保存指标配置",
1111

12+
metric_enabled: "启用指标服务",
13+
metric_enabled_desc: "关闭后不再采集新的连接与 DNS 指标数据",
14+
1215
language: "语言设定",
1316
theme: "外观主题",
1417
theme_placeholder: "浅色模式适配中,暂不可用",

landscape-webui/src/stores/metric_config.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import type { LandscapeMetricConfig } from "@landscape-router/types/api/schemas"
44
import { get_metric_config_edit, update_metric_config } from "@/api/sys/config";
55

66
export const useMetricConfigStore = defineStore("metric_config", () => {
7+
const enabled = ref<boolean>(true);
78
const rawRetentionMinutes = ref<number | undefined>(undefined);
89
const rollup1mRetentionDays = ref<number | undefined>(undefined);
910
const rollup1hRetentionDays = ref<number | undefined>(undefined);
@@ -21,6 +22,7 @@ export const useMetricConfigStore = defineStore("metric_config", () => {
2122

2223
async function loadMetricConfig() {
2324
const { metric, hash } = await get_metric_config_edit();
25+
enabled.value = metric.enable ?? true;
2426
rawRetentionMinutes.value = metric.raw_retention_minutes ?? undefined;
2527
rollup1mRetentionDays.value = metric.rollup_1m_retention_days ?? undefined;
2628
rollup1hRetentionDays.value = metric.rollup_1h_retention_days ?? undefined;
@@ -41,6 +43,7 @@ export const useMetricConfigStore = defineStore("metric_config", () => {
4143

4244
async function saveMetricConfig() {
4345
const new_metric: LandscapeMetricConfig = {
46+
enable: enabled.value,
4447
raw_retention_minutes: rawRetentionMinutes.value,
4548
rollup_1m_retention_days: rollup1mRetentionDays.value,
4649
rollup_1h_retention_days: rollup1hRetentionDays.value,
@@ -66,6 +69,7 @@ export const useMetricConfigStore = defineStore("metric_config", () => {
6669
}
6770

6871
return {
72+
enabled,
6973
rawRetentionMinutes,
7074
rollup1mRetentionDays,
7175
rollup1hRetentionDays,

landscape-webui/src/views/config_parts/MetricConfigCard.vue

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@ async function handleSaveMetric() {
2929
</n-button>
3030
</template>
3131
<n-form label-placement="left" label-width="120">
32+
<n-form-item :label="t('config.metric_enabled')">
33+
<n-switch v-model:value="metricStore.enabled" />
34+
<template #feedback>
35+
{{ t("config.metric_enabled_desc") }}
36+
</template>
37+
</n-form-item>
38+
3239
<n-divider title-placement="left">
3340
{{ t("config.conn_retention_mins") }}
3441
</n-divider>

landscape/src/bin/event_metric_loop.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ async fn main() {
2929
let metric_service = MetricData::new(
3030
metric_path,
3131
landscape_common::config::MetricRuntimeConfig {
32+
enable: landscape_common::DEFAULT_METRIC_ENABLE,
3233
raw_retention_minutes: landscape_common::DEFAULT_METRIC_RAW_RETENTION_MINUTES,
3334
rollup_1m_retention_days: landscape_common::DEFAULT_METRIC_ROLLUP_1M_RETENTION_DAYS,
3435
rollup_1h_retention_days: landscape_common::DEFAULT_METRIC_ROLLUP_1H_RETENTION_DAYS,

0 commit comments

Comments
 (0)