forked from apache/horaedb
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtypes.rs
191 lines (167 loc) · 5.76 KB
/
types.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
// Copyright 2023 CeresDB Project Authors. Licensed under Apache-2.0.
use std::collections::{HashMap, HashSet};
use bytes::Bytes;
use ceresdbproto::storage::{
value, Field, FieldGroup, Tag, Value, WriteSeriesEntry, WriteTableRequest,
};
use common_util::error::BoxError;
use itertools::Itertools;
use serde::Deserialize;
use serde_json::from_slice;
use snafu::ResultExt;
use crate::error::{Internal, InternalNoCause, Result};
const OPENTSDB_DEFAULT_FIELD: &str = "value";
#[derive(Debug)]
pub struct PutRequest {
pub points: Bytes,
pub summary: bool,
pub details: bool,
pub sync: bool,
pub sync_timeout: i32,
}
impl PutRequest {
pub fn new(points: Bytes, params: PutParams) -> Self {
PutRequest {
points,
summary: params.summary,
details: params.details,
sync: params.sync,
sync_timeout: params.sync_timeout,
}
}
}
pub type PutResponse = ();
/// Query string parameters for put api
///
/// It's derived from query string parameters of put described in
/// doc of OpenTSDB 2.4:
/// http://opentsdb.net/docs/build/html/api_http/put.html#requests
///
/// NOTE:
/// - all the params is unimplemented.
#[derive(Debug, Default, Deserialize)]
#[serde(default)]
pub struct PutParams {
// TODO: How to represent a `Present` param ?
// now use bool to represent it, so the URL must be "?summary=true/false"
// but the `Present` datatype means "?summary" is ok.
pub summary: bool,
pub details: bool,
pub sync: bool,
pub sync_timeout: i32,
}
#[derive(Debug, Deserialize)]
pub struct Point {
pub metric: String,
pub timestamp: u64,
// TODO: OpenTSDB's value support Integer, Float, String. How to represent it and parse from json ?
pub value: f64,
pub tags: HashMap<String, String>,
}
pub(crate) fn convert_put_request(req: PutRequest) -> Result<Vec<WriteTableRequest>> {
let points = {
// multi points represent as json array
let parse_array = from_slice::<Vec<Point>>(&req.points);
match parse_array {
Ok(points) => Ok(points),
Err(_e) => {
// single point represent as json object
let parse_object = from_slice::<Point>(&req.points);
match parse_object {
Ok(point) => Ok(vec![point]),
Err(e) => Err(e),
}
}
}
};
let points = points.box_err().with_context(|| Internal {
msg: "json parse error",
})?;
validate(&points)?;
let mut points_per_metric = HashMap::new();
for point in points.into_iter() {
points_per_metric
.entry(point.metric.clone()) // TODO: how to reduce string copy ?
.or_insert(Vec::new())
.push(point);
}
let requests = points_per_metric
.into_iter()
.map(|(metric, points)| {
let mut tag_names_set = HashSet::new();
for point in points.iter() {
for tag_name in point.tags.keys() {
tag_names_set.insert(tag_name.clone()); // TODO: how to reduce string copy ?
}
}
let mut tag_name_to_tag_index: HashMap<String, u32> = HashMap::new();
let mut tag_names = Vec::with_capacity(tag_names_set.len());
for (idx, tag_name) in tag_names_set.into_iter().enumerate() {
tag_name_to_tag_index.insert(tag_name.clone(), idx as u32);
tag_names.push(tag_name);
}
let mut req = WriteTableRequest {
table: metric,
tag_names,
field_names: vec![String::from(OPENTSDB_DEFAULT_FIELD)],
entries: Vec::with_capacity(points.len()),
};
for point in points {
let mut timestamp = point.timestamp;
// TODO: Is there a util function to detect timestamp precision and convert it?
if timestamp < 1000000000000 {
// seconds
timestamp *= 1000;
} // else milliseconds
let mut tags = Vec::with_capacity(point.tags.len());
for (tag_name, tag_value) in point.tags {
let &tag_index = tag_name_to_tag_index.get(&tag_name).unwrap();
tags.push(Tag {
name_index: tag_index,
value: Some(Value {
value: Some(value::Value::StringValue(tag_value)),
}),
});
}
let fields = vec![Field {
name_index: 0,
value: Some(Value {
value: Some(value::Value::Float64Value(point.value)),
}),
}];
let field_groups = vec![FieldGroup {
timestamp: timestamp as i64,
fields,
}];
req.entries.push(WriteSeriesEntry { tags, field_groups });
}
req
})
.collect_vec();
Ok(requests)
}
pub(crate) fn validate(points: &[Point]) -> Result<()> {
for point in points.iter() {
if point.metric.is_empty() {
return InternalNoCause {
msg: "metric must not be empty",
}
.fail();
}
if point.tags.is_empty() {
return InternalNoCause {
msg: "at least one tag must be supplied",
}
.fail();
}
for tag_name in point.tags.keys() {
if tag_name.is_empty() {
return InternalNoCause {
msg: "tag name must not be empty",
}
.fail();
}
}
}
Ok(())
}