servers/
opentsdb.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
15pub mod codec;
16
17use api::v1::RowInsertRequests;
18use common_grpc::precision::Precision;
19use common_query::prelude::{GREPTIME_TIMESTAMP, GREPTIME_VALUE};
20
21use self::codec::DataPoint;
22use crate::error::Result;
23use crate::row_writer::{self, MultiTableData};
24
25pub fn data_point_to_grpc_row_insert_requests(
26    data_points: Vec<DataPoint>,
27) -> Result<(RowInsertRequests, usize)> {
28    let mut multi_table_data = MultiTableData::new();
29
30    for mut data_point in data_points {
31        let tags: Vec<(String, String)> = std::mem::take(data_point.tags_mut());
32        let table_name = data_point.metric();
33        let value = data_point.value();
34        let timestamp = data_point.ts_millis();
35        // length of tags + 2 extra columns for greptime_timestamp and the value
36        let num_columns = tags.len() + 2;
37
38        let table_data = multi_table_data.get_or_default_table_data(table_name, num_columns, 1);
39        let mut one_row = table_data.alloc_one_row();
40
41        // tags
42        row_writer::write_tags(table_data, tags.into_iter(), &mut one_row)?;
43
44        // value
45        row_writer::write_f64(table_data, GREPTIME_VALUE, value, &mut one_row)?;
46        // timestamp
47        row_writer::write_ts_to_millis(
48            table_data,
49            GREPTIME_TIMESTAMP,
50            Some(timestamp),
51            Precision::Millisecond,
52            &mut one_row,
53        )?;
54
55        table_data.add_row(one_row);
56    }
57
58    Ok(multi_table_data.into_row_insert_requests())
59}