Skip to main content

servers/opentsdb/
codec.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
15#[derive(Debug, Clone)]
16pub struct DataPoint {
17    metric: String,
18    ts_millis: i64,
19    value: f64,
20    tags: Vec<(String, String)>,
21}
22
23impl DataPoint {
24    pub fn new(metric: String, ts_millis: i64, value: f64, tags: Vec<(String, String)>) -> Self {
25        Self {
26            metric,
27            ts_millis,
28            value,
29            tags,
30        }
31    }
32
33    pub fn metric(&self) -> &str {
34        &self.metric
35    }
36
37    pub fn tags(&self) -> &Vec<(String, String)> {
38        &self.tags
39    }
40
41    pub fn tags_mut(&mut self) -> &mut Vec<(String, String)> {
42        &mut self.tags
43    }
44
45    pub fn ts_millis(&self) -> i64 {
46        self.ts_millis
47    }
48
49    pub fn value(&self) -> f64 {
50        self.value
51    }
52
53    pub fn timestamp_to_millis(t: i64) -> i64 {
54        // 9999999999999 (13 digits) is of date "Sat Nov 20 2286 17:46:39 UTC",
55        // 999999999999 (12 digits) is "Sun Sep 09 2001 01:46:39 UTC",
56        // so timestamp digits less than 13 means we got seconds here.
57        // (We are not expecting to store data that is 21 years ago, are we?)
58        if t.abs().to_string().len() < 13 {
59            t * 1000
60        } else {
61            t
62        }
63    }
64}