pub mod codec;
use api::v1::RowInsertRequests;
use common_grpc::precision::Precision;
use common_query::prelude::{GREPTIME_TIMESTAMP, GREPTIME_VALUE};
use self::codec::DataPoint;
use crate::error::Result;
use crate::row_writer::{self, MultiTableData};
pub fn data_point_to_grpc_row_insert_requests(
data_points: Vec<DataPoint>,
) -> Result<(RowInsertRequests, usize)> {
let mut multi_table_data = MultiTableData::new();
for mut data_point in data_points {
let tags: Vec<(String, String)> = std::mem::take(data_point.tags_mut());
let table_name = data_point.metric();
let value = data_point.value();
let timestamp = data_point.ts_millis();
let num_columns = tags.len() + 2;
let table_data = multi_table_data.get_or_default_table_data(table_name, num_columns, 1);
let mut one_row = table_data.alloc_one_row();
row_writer::write_tags(table_data, tags.into_iter(), &mut one_row)?;
row_writer::write_f64(table_data, GREPTIME_VALUE, value, &mut one_row)?;
row_writer::write_ts_to_millis(
table_data,
GREPTIME_TIMESTAMP,
Some(timestamp),
Precision::Millisecond,
&mut one_row,
)?;
table_data.add_row(one_row);
}
Ok(multi_table_data.into_row_insert_requests())
}