Files
pezkuwi-subxt/substrate/client/grafana-data-source/src/util.rs
T
Ashley 145efab68f Grafana integration (#3913)
* Very WIP

* record_metrics macro works

* Integrate into service

* Licenses and documentation

* Remove unused Debugs, make respond function clearer

* Conform to line widths, fix service test

* Switch to storing the timestamps as millis instead

* Update core/grafana-data-source/src/lib.rs

Co-Authored-By: Kian Paimani <5588131+kianenigma@users.noreply.github.com>

* Transform timestamps to i64 in serialization

* Fix license date

* Binary sort to find selection range for metrics

* Obey maxDataPoints

* Run a cleaning future

* Newlines at EOF

* Update core/service/Cargo.toml

Co-Authored-By: Pierre Krieger <pierre.krieger1708@gmail.com>

* Update core/grafana-data-source/src/lib.rs

Co-Authored-By: Pierre Krieger <pierre.krieger1708@gmail.com>

* Fix indentation

* Improve select_points

* Made test more accurate

* Inprogress

* Use the same futures version as hyper for now

* Error handling

* Remove dependence on hyper's tokio feature

* Added target_os flag

* Update Cargo.toml

Co-Authored-By: Pierre Krieger <pierre.krieger1708@gmail.com>

* Simplify example

* Remove compat wildcard

* Updated lock file

* Fix indentation 😉
2019-11-22 15:49:49 +01:00

53 lines
1.8 KiB
Rust

// Copyright 2019 Parity Technologies (UK) Ltd.
// This file is part of Substrate.
// Substrate is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Substrate is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
/// Get the current unix timestamp in milliseconds.
pub fn now_millis() -> i64 {
chrono::Utc::now().timestamp_millis()
}
// find the index of a timestamp
pub fn find_index(slice: &[(f32, i64)], timestamp: i64) -> usize {
slice.binary_search_by_key(&timestamp, |&(_, timestamp)| timestamp)
.unwrap_or_else(|index| index)
}
// Evenly select up to `num_points` points from a slice
pub fn select_points<T: Copy>(slice: &[T], num_points: usize) -> Vec<T> {
if num_points == 0 {
return Vec::new();
} else if num_points >= slice.len() {
return slice.to_owned();
}
(0 .. num_points - 1)
.map(|i| slice[i * slice.len() / (num_points - 1)])
.chain(slice.last().cloned())
.collect()
}
#[test]
fn test_select_points() {
let array = [1, 2, 3, 4, 5];
assert_eq!(select_points(&array, 0), Vec::<u8>::new());
assert_eq!(select_points(&array, 1), vec![5]);
assert_eq!(select_points(&array, 2), vec![1, 5]);
assert_eq!(select_points(&array, 3), vec![1, 3, 5]);
assert_eq!(select_points(&array, 4), vec![1, 2, 4, 5]);
assert_eq!(select_points(&array, 5), vec![1, 2, 3, 4, 5]);
assert_eq!(select_points(&array, 6), vec![1, 2, 3, 4, 5]);
}