Skip to main content

frontend/service_config/
influxdb.rs

1// Copyright 2023 Greptime Team
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use serde::{Deserialize, Serialize};
16
17#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
18#[serde(default)]
19pub struct InfluxdbOptions {
20    pub enable: bool,
21    pub default_merge_mode: InfluxdbMergeMode,
22}
23
24#[derive(Clone, Copy, Debug, Default, Serialize, Deserialize, PartialEq, Eq)]
25#[serde(rename_all = "snake_case")]
26pub enum InfluxdbMergeMode {
27    #[default]
28    LastNonNull,
29    LastRow,
30}
31
32impl InfluxdbMergeMode {
33    pub fn as_str(&self) -> &'static str {
34        match self {
35            InfluxdbMergeMode::LastNonNull => "last_non_null",
36            InfluxdbMergeMode::LastRow => "last_row",
37        }
38    }
39}
40
41impl Default for InfluxdbOptions {
42    fn default() -> Self {
43        Self {
44            enable: true,
45            default_merge_mode: InfluxdbMergeMode::default(),
46        }
47    }
48}
49
50#[cfg(test)]
51mod tests {
52    use super::InfluxdbOptions;
53
54    #[test]
55    fn test_influxdb_options() {
56        let default = InfluxdbOptions::default();
57        assert!(default.enable);
58        assert_eq!("last_non_null", default.default_merge_mode.as_str());
59    }
60
61    #[test]
62    fn test_influxdb_options_default_merge_mode() {
63        let options: InfluxdbOptions = toml::from_str("default_merge_mode = 'last_row'").unwrap();
64        assert_eq!("last_row", options.default_merge_mode.as_str());
65    }
66}