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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
// Copyright 2016-2017 Kai Strempel
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

#![crate_type = "lib"]
#![crate_name = "kairosdb"]

//! A `Client` for KairosBD REST API
//!
//! The Client itself is used as the central access point, from which
//! numerous operations are defined implementing each of the specific
//! KairosDB APIs.
//!
//! ```
//! use kairosdb::Client;
//! let client = Client::new("localhost", 8080);
//! ```
//!
//! A main job of a time series database is collecting and querying data.
//! To add data to KairosDB we have to create a `Datapoints` struct and add
//! the data to the object.
//!
//! ```
//! # fn main() {
//! # use kairosdb::Client;
//! use kairosdb::datapoints::Datapoints;
//! # let client = Client::new("localhost", 8080);
//!
//! let mut datapoints = Datapoints::new("myMetric", 0);
//! datapoints.add_ms(1000, 11.0);
//! datapoints.add_ms(2000, 12.0);
//! datapoints.add_ms(3000, 13.0);
//! datapoints.add_tag("test", "first");
//! let result = client.add(&datapoints);
//! assert!(result.is_ok());
//! # }
//! ```
//!
//! To query data we have to create a `Query` Object with the start and end
//! of the query. The start and the end can be a relative time. Check the
//! 'Time' structure for more information.
//!
//! ```
//! # fn main() {
//! # use kairosdb::Client;
//! # let client = Client::new("localhost", 8080);
//! use std::collections::HashMap;
//! use kairosdb::query::{Query, Time, Metric, Tags};
//! # use kairosdb::datapoints::Datapoints;
//! # let mut datapoints = Datapoints::new("myMetric", 0);
//! # datapoints.add_ms(1000, 11.0);
//! # datapoints.add_ms(2000, 12.0);
//! # datapoints.add_ms(3000, 13.0);
//! # datapoints.add_tag("test", "first");
//! # let result = client.add(&datapoints);
//! # assert!(result.is_ok());
//!
//! let mut query = Query::new(
//!    Time::Nanoseconds(1000),
//!    Time::Nanoseconds(2000));
//!
//! let metric = Metric::new("myMetric", Tags::new(), vec![]);
//! query.add(metric);
//!
//! let result = client.query(&query).unwrap();
//!
//! assert!(result.contains_key("myMetric"));
//! assert_eq!(result["myMetric"].len(), 2);
//! assert_eq!(result["myMetric"][0].time, 1000);
//! assert_eq!(result["myMetric"][0].value, 11.0);
//! assert_eq!(result["myMetric"][1].time, 2000);
//! assert_eq!(result["myMetric"][1].value, 12.0);
//! # }
//! ```
//!
//! Optionally you can specify aggregators. Aggregators perform an operation on data
//! points. For example, you can sum all data points that exist in
//! 5 minute periods. Aggregators can be combined together. E.g you could
//! sum all data points in 5 minute periods then calculate the average of them for a
//! week period.
//! Aggregators are processed in the order they are specified in the vector for the
//! metric constructor.
//!
//! ```
//! # fn main() {
//! # use kairosdb::Client;
//! # let client = Client::new("localhost", 8080);
//! use kairosdb::query::*;
//! use kairosdb::datapoints::Datapoints;
//! # let result = client.delete_metric(&"myMetric");
//! # assert!(result.is_ok());
//! for i in 0..10 {
//!    let mut datapoints = Datapoints::new("myMetric", 0);
//!    datapoints.add_ms(i * 500, i as f64);
//!    datapoints.add_tag("test", "first");
//!    let result = client.add(&datapoints);
//!    assert!(result.is_ok());
//! }
//!
//! let mut query = Query::new(
//!    Time::Nanoseconds(0),
//!    Time::Nanoseconds(10*500));
//!
//! let aggregator = Aggregator::new(
//!     AggregatorType::AVG,
//!     RelativeTime::new(1, TimeUnit::SECONDS));
//! let metric = Metric::new("myMetric", Tags::new(), vec![aggregator]);
//! query.add(metric);
//!
//! let result = client.query(&query).unwrap();
//! assert!(result.contains_key("myMetric"));
//! assert_eq!(result["myMetric"].len(), 5);
//! assert_eq!(result["myMetric"][0].time, 0);
//! assert_eq!(result["myMetric"][0].value, 0.5);
//! # assert_eq!(result["myMetric"][1].time, 1000);
//! # assert_eq!(result["myMetric"][1].value, 2.5);
//! # assert_eq!(result["myMetric"][2].time, 2000);
//! # assert_eq!(result["myMetric"][2].value, 4.5);
//! # assert_eq!(result["myMetric"][3].time, 3000);
//! # assert_eq!(result["myMetric"][3].value, 6.5);
//! # assert_eq!(result["myMetric"][4].time, 4000);
//! # assert_eq!(result["myMetric"][4].value, 8.5);
//! # }
//! ```
//!
//! Deleting data is like querying data.
//!
//! ```
//! # fn main() {
//! # use kairosdb::Client;
//! # let client = Client::new("localhost", 8080);
//! use kairosdb::query::{Query, Time, Metric, Tags};
//!
//! let mut query = Query::new(
//!    Time::Nanoseconds(1000),
//!    Time::Nanoseconds(2000));
//!
//! let mut tags = Tags::new();
//! tags.insert("test".to_string(), vec!["first".to_string()]);
//! let metric = Metric::new("myMetric", tags, vec![]);
//! query.add(metric);
//!
//! let result = client.delete(&query);
//! assert!(result.is_ok());
//! # }
//! ```
//!
//! Getting the current set of metric names is a simple
//! function call.
//!
//! ```
//! # use kairosdb::Client;
//! # let client = Client::new("localhost", 8080);
//! # use kairosdb::datapoints::Datapoints;
//! # let mut datapoints = Datapoints::new("myMetric", 0);
//! # datapoints.add_ms(1000, 11.0);
//! # datapoints.add_ms(2000, 12.0);
//! # datapoints.add_ms(3000, 13.0);
//! # datapoints.add_tag("test", "first");
//! # let result = client.add(&datapoints);
//! # assert!(result.is_ok());
//!
//! let result = client.list_metrics();
//! assert!(result.unwrap().contains(&"myMetric".to_string()));
//! ```
//!
//! To get information about the current tags and tag values you
//! can use the tagsnames and tagvalues method.
//!
//! ```
//! # use kairosdb::Client;
//! # let client = Client::new("localhost", 8080);
//! # use kairosdb::datapoints::Datapoints;
//! # let mut datapoints = Datapoints::new("myMetric", 0);
//! # datapoints.add_tag("test", "first");
//! # let result = client.add(&datapoints);
//! # assert!(result.is_ok());
//!
//! let tagnames = client.tagnames();
//! let tagvalues = client.tagvalues();
//! assert!(tagnames.unwrap().contains(&"test".to_string()));
//! assert!(tagvalues.unwrap().contains(&"first".to_string()));
//! ```
//! Delete a metric by name
//!
//! ```
//! # use kairosdb::Client;
//! # let client = Client::new("localhost", 8080);
//!
//! let result = client.delete_metric(&"myMetric");
//! assert!(result.is_ok());
//! ```
//!
//! ## Server status
//!
//! To get the health status of the KairosDB Server
//!
//! ```
//! # use kairosdb::Client;
//! # let client = Client::new("localhost", 8080);
//! let response = client.health();
//! let result = response.unwrap();
//! assert_eq!(result[0], "JVM-Thread-Deadlock: OK");
//! assert_eq!(result[1], "Datastore-Query: OK");
//! ```
//!
//! Get the version of the KairosDB Server
/// ```
/// # use kairosdb::Client;
/// let client = Client::new("localhost", 8080);
/// assert!(client.version().unwrap().starts_with("KairosDB"));
/// ```


extern crate serde;
extern crate serde_json;
#[macro_use]
extern crate log;
extern crate env_logger;
extern crate hyper;
extern crate chrono;

pub mod datapoints;
pub mod query;
pub mod result;
mod error;
mod helper;

#[cfg(feature = "serde_macros")]
include!("lib.in.rs");

#[cfg(not(feature = "serde_macros"))]
include!(concat!(env!("OUT_DIR"), "/lib.rs"));