Skip to content

Commit

Permalink
Merge pull request #94 from rneswold/pull-request
Browse files Browse the repository at this point in the history
Pull request
  • Loading branch information
rneswold authored Oct 5, 2023
2 parents 9ead1af + ce59600 commit 68b75f4
Show file tree
Hide file tree
Showing 11 changed files with 127 additions and 97 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ async-trait = { version = "0.1", default-features = false }

futures = { version = "0.3", features = ["std"] }

toml = { version = "0.7", default-features = false }
toml = { version = "0.8", default-features = false }
chrono = { version = "0.4", default-features = false }

tokio = { version = "1", default-features = false }
Expand Down
44 changes: 21 additions & 23 deletions backends/drmem-db-redis/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -409,8 +409,8 @@ pub struct RedisStore {
impl RedisStore {
fn make_client(
cfg: &config::Config,
name: &Option<String>,
pword: &Option<String>,
name: Option<&String>,
pword: Option<&String>,
) -> Result<redis::Client> {
use redis::{ConnectionAddr, ConnectionInfo, RedisConnectionInfo};

Expand All @@ -420,8 +420,8 @@ impl RedisStore {
addr: ConnectionAddr::Tcp(addr.ip().to_string(), addr.port()),
redis: RedisConnectionInfo {
db: cfg.get_dbn(),
username: name.clone(),
password: pword.clone(),
username: name.cloned(),
password: pword.cloned(),
},
};

Expand All @@ -435,7 +435,7 @@ impl RedisStore {
name: Option<String>,
pword: Option<String>,
) -> Result<AioConnection> {
let client = Self::make_client(cfg, &name, &pword)?;
let client = Self::make_client(cfg, name.as_ref(), pword.as_ref())?;

debug!("creating new redis connection");

Expand All @@ -452,7 +452,7 @@ impl RedisStore {
name: Option<String>,
pword: Option<String>,
) -> Result<AioMplexConnection> {
let client = Self::make_client(cfg, &name, &pword)?;
let client = Self::make_client(cfg, name.as_ref(), pword.as_ref())?;

debug!("creating new, shared redis connection");

Expand Down Expand Up @@ -500,7 +500,7 @@ impl RedisStore {
fn init_device_cmd(
name: &str,
driver: &str,
units: &Option<String>,
units: Option<&String>,
) -> redis::Pipeline {
let hist_key = Self::hist_key(name);
let info_key = Self::info_key(name);
Expand Down Expand Up @@ -543,12 +543,11 @@ impl RedisStore {
redis::Cmd::xrevrange_count(name, "+", "-", 1usize)
}

fn match_pattern_cmd(pattern: &Option<String>) -> redis::Cmd {
fn match_pattern_cmd(pattern: Option<&str>) -> redis::Cmd {
// Take the pattern from the caller and append "#info" since
// we only want to look at device information keys.

let pattern = pattern
.as_ref()
.map(|v| Self::info_key(v))
.unwrap_or_else(|| String::from("*#info"));

Expand Down Expand Up @@ -835,7 +834,7 @@ impl RedisStore {
&mut self,
name: &str,
driver: &str,
units: &Option<String>,
units: Option<&String>,
) -> Result<()> {
debug!("initializing {}", name);
Self::init_device_cmd(name, driver, units)
Expand All @@ -850,12 +849,12 @@ impl RedisStore {
fn mk_report_func(
&self,
name: &str,
max_history: &Option<usize>,
max_history: Option<usize>,
) -> ReportReading<device::Value> {
let db_con = self.db_con.clone();
let name = String::from(name);

if let Some(mh) = *max_history {
if let Some(mh) = max_history {
Box::new(move |v| {
let mut db_con = db_con.clone();
let hist_key = Self::hist_key(&name);
Expand Down Expand Up @@ -898,8 +897,8 @@ impl Store for RedisStore {
&mut self,
driver_name: &str,
name: &device::Name,
units: &Option<String>,
max_history: &Option<usize>,
units: Option<&String>,
max_history: Option<usize>,
) -> Result<(ReportReading<device::Value>, Option<device::Value>)> {
let name = name.to_string();

Expand All @@ -920,8 +919,8 @@ impl Store for RedisStore {
&mut self,
driver_name: &str,
name: &device::Name,
units: &Option<String>,
max_history: &Option<usize>,
units: Option<&String>,
max_history: Option<usize>,
) -> Result<(
ReportReading<device::Value>,
RxDeviceSetting,
Expand Down Expand Up @@ -956,7 +955,7 @@ impl Store for RedisStore {

async fn get_device_info(
&mut self,
pattern: &Option<String>,
pattern: Option<&str>,
) -> Result<Vec<client::DevInfoReply>> {
// Get a list of all the keys that match the pattern. For
// Redis, these keys will have "#info" appended at the end.
Expand Down Expand Up @@ -1339,20 +1338,19 @@ mod tests {
#[test]
fn test_pattern_cmd() {
assert_eq!(
&RedisStore::match_pattern_cmd(&None).get_packed_command(),
&RedisStore::match_pattern_cmd(None).get_packed_command(),
b"*2\r
$4\r\nKEYS\r
$6\r\n*#info\r\n"
);
assert_eq!(
&RedisStore::match_pattern_cmd(&Some(String::from("device")))
.get_packed_command(),
&RedisStore::match_pattern_cmd(Some("device")).get_packed_command(),
b"*2\r
$4\r\nKEYS\r
$11\r\ndevice#info\r\n"
);
assert_eq!(
&RedisStore::match_pattern_cmd(&Some(String::from("*weather*")))
&RedisStore::match_pattern_cmd(Some("*weather*"))
.get_packed_command(),
b"*2\r
$4\r\nKEYS\r
Expand Down Expand Up @@ -1639,7 +1637,7 @@ $10\r\nS\x00\x00\x00\x05hello\r\n"
fn test_init_dev() {
assert_eq!(
String::from_utf8_lossy(
&RedisStore::init_device_cmd("device", "mem", &None)
&RedisStore::init_device_cmd("device", "mem", None)
.get_packed_pipeline()
),
"*1\r
Expand Down Expand Up @@ -1673,7 +1671,7 @@ $4\r\nEXEC\r\n"
&RedisStore::init_device_cmd(
"device",
"pump",
&Some(String::from("gpm"))
Some(&String::from("gpm"))
)
.get_packed_pipeline()
),
Expand Down
36 changes: 18 additions & 18 deletions backends/drmem-db-simple/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ struct DeviceInfo {
impl DeviceInfo {
pub fn create(
owner: String,
units: Option<String>,
units: Option<&String>,
tx_setting: Option<TxDeviceSetting>,
) -> DeviceInfo {
let (tx, _) = broadcast::channel(CHAN_SIZE);
Expand All @@ -58,7 +58,7 @@ impl DeviceInfo {

DeviceInfo {
owner,
units,
units: units.cloned(),
tx_setting,
reading: Arc::new(Mutex::new((tx, None, time::UNIX_EPOCH))),
}
Expand Down Expand Up @@ -145,8 +145,8 @@ impl Store for SimpleStore {
&mut self,
driver: &str,
name: &device::Name,
units: &Option<String>,
_max_history: &Option<usize>,
units: Option<&String>,
_max_history: Option<usize>,
) -> Result<(ReportReading<device::Value>, Option<device::Value>)> {
// Check to see if the device name already exists.

Expand Down Expand Up @@ -203,8 +203,8 @@ impl Store for SimpleStore {
&mut self,
driver: &str,
name: &device::Name,
units: &Option<String>,
_max_history: &Option<usize>,
units: Option<&String>,
_max_history: Option<usize>,
) -> Result<(
ReportReading<device::Value>,
RxDeviceSetting,
Expand All @@ -224,7 +224,7 @@ impl Store for SimpleStore {

let di = e.insert(DeviceInfo::create(
String::from(driver),
units.clone(),
units,
Some(tx_sets),
));

Expand Down Expand Up @@ -270,7 +270,7 @@ impl Store for SimpleStore {

async fn get_device_info(
&mut self,
pattern: &Option<String>,
pattern: Option<&str>,
) -> Result<Vec<client::DevInfoReply>> {
let pred: Box<dyn FnMut(&(&device::Name, &DeviceInfo)) -> bool> =
if let Some(pattern) = pattern {
Expand Down Expand Up @@ -489,7 +489,7 @@ mod tests {
let name = "test:device".parse::<device::Name>().unwrap();

if let Ok((f, None)) = db
.register_read_only_device("test", &name, &None, &None)
.register_read_only_device("test", &name, None, None)
.await
{
// Test that priming the history with one value returns
Expand Down Expand Up @@ -573,7 +573,7 @@ mod tests {
let name = "test:device".parse::<device::Name>().unwrap();

if let Ok((f, None)) = db
.register_read_only_device("test", &name, &None, &None)
.register_read_only_device("test", &name, None, None)
.await
{
// Verify that monitoring device, starting now, picks up
Expand Down Expand Up @@ -660,7 +660,7 @@ mod tests {
let name = "test:device".parse::<device::Name>().unwrap();

if let Ok((f, None)) = db
.register_read_only_device("test", &name, &None, &None)
.register_read_only_device("test", &name, None, None)
.await
{
// Verify that, if the latest point is before the starting
Expand Down Expand Up @@ -704,7 +704,7 @@ mod tests {
let name = "test:device".parse::<device::Name>().unwrap();

if let Ok((f, None)) = db
.register_read_only_device("test", &name, &None, &None)
.register_read_only_device("test", &name, None, None)
.await
{
// Verify that, if both times are before the data, nothing
Expand Down Expand Up @@ -893,7 +893,7 @@ mod tests {
// driver named "test". We don't define units for this device.

if let Ok((f, None)) = db
.register_read_only_device("test", &name, &None, &None)
.register_read_only_device("test", &name, None, None)
.await
{
// Make sure the device was defined and the setting
Expand All @@ -920,15 +920,15 @@ mod tests {
// driver name results in an error.

assert!(db
.register_read_only_device("test2", &name, &None, &None)
.register_read_only_device("test2", &name, None, None)
.await
.is_err());

// Assert that re-registering this device with the same
// driver name is successful.

if let Ok((f, Some(device::Value::Int(1)))) = db
.register_read_only_device("test", &name, &None, &None)
.register_read_only_device("test", &name, None, None)
.await
{
// Also, verify that the device update channel wasn't
Expand All @@ -954,7 +954,7 @@ mod tests {
// driver named "test". We don't define units for this device.

if let Ok((f, mut set_chan, None)) = db
.register_read_write_device("test", &name, &None, &None)
.register_read_write_device("test", &name, None, None)
.await
{
// Make sure the device was defined and a setting channel
Expand Down Expand Up @@ -1002,7 +1002,7 @@ mod tests {
// didn't affect the setting channel.

assert!(db
.register_read_only_device("test2", &name, &None, &None)
.register_read_only_device("test2", &name, None, None)
.await
.is_err());
assert_eq!(
Expand All @@ -1014,7 +1014,7 @@ mod tests {
// driver name is successful.

if let Ok((f, _, Some(device::Value::Int(1)))) = db
.register_read_write_device("test", &name, &None, &None)
.register_read_write_device("test", &name, None, None)
.await
{
assert_eq!(
Expand Down
37 changes: 29 additions & 8 deletions drivers/drmem-drv-sump/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,19 +166,22 @@ impl Instance {

pub const DESCRIPTION: &'static str = include_str!("../README.md");

fn elapsed(dur: u64) -> String {
match (dur + 500) / 1000 {
dur if dur >= 3600 * 24 => {
fn elapsed(millis: u64) -> String {
match (millis + 500) / 1000 {
dur if dur >= 3600 * 24 - 30 => {
let dur = dur + 30;

format!(
"{}d{}h{}m{}s",
"{}d{}h{}m",
dur / (3600 * 24),
(dur / 3600) % 24,
(dur / 60) % 60,
dur % 60
(dur / 60) % 60
)
}
dur if dur >= 3600 => {
format!("{}h{}m{}s", dur / 3600, (dur / 60) % 60, dur % 60)
dur if dur >= 3570 => {
let dur = dur + 30;

format!("{}h{}m", dur / 3600, (dur / 60) % 60)
}
dur if dur >= 60 => {
format!("{}m{}s", dur / 60, dur % 60)
Expand Down Expand Up @@ -497,4 +500,22 @@ mod tests {
assert_eq!(state.off_event(60000, 60.0), Some((60000, 10.0, 6.0)));
assert_eq!(state, State::Off { off_time: 60000 });
}

#[test]
fn test_elapsed() {
assert_eq!(Instance::elapsed(0), "0s");
assert_eq!(Instance::elapsed(1000), "1s");
assert_eq!(Instance::elapsed(59000), "59s");
assert_eq!(Instance::elapsed(60000), "1m0s");

assert_eq!(Instance::elapsed(3569000), "59m29s");
assert_eq!(Instance::elapsed(3570000), "1h0m");
assert_eq!(Instance::elapsed(3599000), "1h0m");
assert_eq!(Instance::elapsed(3600000), "1h0m");

assert_eq!(Instance::elapsed(3600000 * 24 - 31000), "23h59m");
assert_eq!(Instance::elapsed(3600000 * 24 - 30000), "1d0h0m");
assert_eq!(Instance::elapsed(3600000 * 24 - 1000), "1d0h0m");
assert_eq!(Instance::elapsed(3600000 * 24), "1d0h0m");
}
}
Loading

0 comments on commit 68b75f4

Please sign in to comment.