implement value listing
This commit is contained in:
parent
c771f8698e
commit
8e42d763fe
|
|
@ -24,6 +24,15 @@ version = "2.0.1"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "320119579fcad9c21884f5c4861d16174d0e06250625266f50fe6898340abefa"
|
checksum = "320119579fcad9c21884f5c4861d16174d0e06250625266f50fe6898340abefa"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "aho-corasick"
|
||||||
|
version = "1.1.4"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "ddd31a130427c27518df266943a5308ed92d4b226cc639f5a8f1002816174301"
|
||||||
|
dependencies = [
|
||||||
|
"memchr",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "aligned"
|
name = "aligned"
|
||||||
version = "0.4.3"
|
version = "0.4.3"
|
||||||
|
|
@ -929,6 +938,9 @@ dependencies = [
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "protocol"
|
name = "protocol"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
|
dependencies = [
|
||||||
|
"regex",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "pxfm"
|
name = "pxfm"
|
||||||
|
|
@ -1081,6 +1093,35 @@ dependencies = [
|
||||||
"crossbeam-utils",
|
"crossbeam-utils",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "regex"
|
||||||
|
version = "1.12.3"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "e10754a14b9137dd7b1e3e5b0493cc9171fdd105e0ab477f51b72e7f3ac0e276"
|
||||||
|
dependencies = [
|
||||||
|
"aho-corasick",
|
||||||
|
"memchr",
|
||||||
|
"regex-automata",
|
||||||
|
"regex-syntax",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "regex-automata"
|
||||||
|
version = "0.4.14"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "6e1dd4122fc1595e8162618945476892eefca7b88c52820e74af6262213cae8f"
|
||||||
|
dependencies = [
|
||||||
|
"aho-corasick",
|
||||||
|
"memchr",
|
||||||
|
"regex-syntax",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "regex-syntax"
|
||||||
|
version = "0.8.10"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "dc897dd8d9e8bd1ed8cdad82b5966c3e0ecae09fb1907d58efaa013543185d0a"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "rgb"
|
name = "rgb"
|
||||||
version = "0.8.53"
|
version = "0.8.53"
|
||||||
|
|
|
||||||
|
|
@ -4,3 +4,4 @@ version = "0.1.0"
|
||||||
edition = "2024"
|
edition = "2024"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
regex = "1"
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,18 @@ pub fn create_request_message(value: &String) -> String {
|
||||||
format!("GETVAL {}\n", value)
|
format!("GETVAL {}\n", value)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn create_list_message() -> String {
|
||||||
|
format!("LISTVAL\n")
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn parse_list_response(lines: Vec<String>) -> Vec<String> {
|
||||||
|
lines
|
||||||
|
.iter()
|
||||||
|
.filter_map(|l| l.split(" ").last())
|
||||||
|
.map(|l| l.into())
|
||||||
|
.collect()
|
||||||
|
}
|
||||||
|
|
||||||
pub fn parse_response_header(header: String) -> Result<i32, ProtocolError> {
|
pub fn parse_response_header(header: String) -> Result<i32, ProtocolError> {
|
||||||
let num_values_raw = header
|
let num_values_raw = header
|
||||||
.split(" ")
|
.split(" ")
|
||||||
|
|
@ -118,4 +130,27 @@ mod tests {
|
||||||
assert!(result.is_err());
|
assert!(result.is_err());
|
||||||
assert_eq!(result, Err(ProtocolError::EmptyResponse));
|
assert_eq!(result, Err(ProtocolError::EmptyResponse));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_parse_list_response() {
|
||||||
|
let resp = vec![
|
||||||
|
"1780776337.692 tp-on/cpu-9/cpu-steal",
|
||||||
|
"1780776337.692 tp-on/cpu-9/cpu-system",
|
||||||
|
"1780776337.692 tp-on/cpu-9/cpu-user",
|
||||||
|
"1780776337.692 tp-on/cpu-9/cpu-wait",
|
||||||
|
]
|
||||||
|
.iter()
|
||||||
|
.map(|s| s.to_string())
|
||||||
|
.collect();
|
||||||
|
let result = parse_list_response(resp);
|
||||||
|
assert_eq!(
|
||||||
|
result,
|
||||||
|
vec![
|
||||||
|
"tp-on/cpu-9/cpu-steal",
|
||||||
|
"tp-on/cpu-9/cpu-system",
|
||||||
|
"tp-on/cpu-9/cpu-user",
|
||||||
|
"tp-on/cpu-9/cpu-wait",
|
||||||
|
]
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,10 @@ use std::io::{Read, Write};
|
||||||
use std::os::unix::net::UnixStream;
|
use std::os::unix::net::UnixStream;
|
||||||
|
|
||||||
use crate::err::ProtocolError;
|
use crate::err::ProtocolError;
|
||||||
use crate::message::{create_request_message, parse_response_header, parse_response_message};
|
use crate::message::{
|
||||||
|
create_list_message, create_request_message, parse_list_response, parse_response_header,
|
||||||
|
parse_response_message,
|
||||||
|
};
|
||||||
|
|
||||||
pub struct Protocol {
|
pub struct Protocol {
|
||||||
socket: UnixStream,
|
socket: UnixStream,
|
||||||
|
|
@ -34,6 +37,27 @@ impl Protocol {
|
||||||
|
|
||||||
pub fn get(&mut self, path: String, name: String) -> Result<f32, ProtocolError> {
|
pub fn get(&mut self, path: String, name: String) -> Result<f32, ProtocolError> {
|
||||||
let req = create_request_message(&path);
|
let req = create_request_message(&path);
|
||||||
|
let lines = self.rpc(&req)?;
|
||||||
|
|
||||||
|
if lines.len() < 1 {
|
||||||
|
Err(ProtocolError::ValueNotFound(name, vec![]))
|
||||||
|
} else {
|
||||||
|
parse_response_message(lines, name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn list(&mut self, path_regex: &Regex) -> Result<Vec<String>, ProtocolError> {
|
||||||
|
let req = create_list_message();
|
||||||
|
let lines = self.rpc(&req)?;
|
||||||
|
let values = parse_list_response(lines);
|
||||||
|
|
||||||
|
Ok(values
|
||||||
|
.into_iter()
|
||||||
|
.filter(|v| path_regex.is_match(v))
|
||||||
|
.collect())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn rpc(&mut self, req: &str) -> Result<Vec<String>, ProtocolError> {
|
||||||
let raw_req = req.as_bytes();
|
let raw_req = req.as_bytes();
|
||||||
self.socket
|
self.socket
|
||||||
.write_all(raw_req)
|
.write_all(raw_req)
|
||||||
|
|
@ -41,13 +65,8 @@ impl Protocol {
|
||||||
|
|
||||||
let header = self.read_line()?;
|
let header = self.read_line()?;
|
||||||
let num_values = parse_response_header(header)?;
|
let num_values = parse_response_header(header)?;
|
||||||
if num_values < 1 {
|
(0..num_values)
|
||||||
Err(ProtocolError::ValueNotFound(name, vec![]))
|
|
||||||
} else {
|
|
||||||
let lines = (0..num_values)
|
|
||||||
.map(|_| self.read_line())
|
.map(|_| self.read_line())
|
||||||
.collect::<Result<Vec<String>, ProtocolError>>()?;
|
.collect::<Result<Vec<String>, ProtocolError>>()
|
||||||
parse_response_message(lines, name)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue