Skip to content

Commit 2b57c7b

Browse files
authored
Merge pull request #44 from silicon-heaven/implement-root-node-methods
Implement version and uptime methods on the root node
2 parents 0a4c3d7 + 907caa4 commit 2b57c7b

7 files changed

Lines changed: 28 additions & 11 deletions

File tree

Cargo.lock

Lines changed: 10 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ serde_yaml_ng = "0.10.0"
2727
tokio-stream = { version = "0.1.17", features = ["fs"] }
2828
time = { version = "0.3.41", features = ["formatting"] }
2929
chrono = "0.4.42"
30+
humantime = "2.3.0"
3031

3132
# [patch."https://github.com/silicon-heaven/libshvclient-rs"]
3233
# shvclient = { path = "../libshvclient-rs" }

src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ impl From<AlarmWithTimestamp> for RpcValue {
6868
}
6969

7070
struct State {
71+
start_time: std::time::Instant,
7172
sites_data: RwLock<sites::SitesData>,
7273
sync_info: sync::SyncInfo,
7374
alarms: RwLock<BTreeMap<String, Vec<AlarmWithTimestamp>>>,
@@ -89,6 +90,7 @@ pub async fn run(hp_config: &HpConfig, client_config: &ClientConfig) -> shvrpc::
8990
let (dirtylog_cmd_tx, dirtylog_cmd_rx) = unbounded();
9091

9192
let app_state = AppState::new(State {
93+
start_time: std::time::Instant::now(),
9294
sites_data: RwLock::default(),
9395
sync_info: Default::default(),
9496
alarms: Default::default(),

src/sites.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -570,8 +570,6 @@ pub(crate) async fn sites_task(
570570

571571
}
572572
_ => {
573-
// TODO: Drain subscribers
574-
575573
subscribers.clear();
576574
mntchng_subscribers.clear();
577575
online_status_channels.clear();

src/sync.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -805,7 +805,7 @@ pub(crate) async fn sync_task(
805805
sync_tasks.push(sync_task);
806806
}
807807
SubHpInfo::PushLog => {
808-
// TODO
808+
// No op
809809
}
810810
}
811811
}
@@ -876,7 +876,7 @@ pub(crate) async fn sync_task(
876876
sync_logger.log(log::Level::Info, "syncing done");
877877
}
878878
SubHpInfo::PushLog => {
879-
// TODO
879+
// No op
880880
}
881881
}
882882
app_state.dirtylog_cmd_tx.unbounded_send(DirtyLogCommand::Trim { site: site.clone() })

src/tree.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ const META_METHOD_PUSH_LOG: MetaMethod = MetaMethod {
6969
};
7070

7171
// Root node methods
72+
const METH_VERSION: &str = "version";
7273
const METH_UPTIME: &str = "uptime";
7374
const METH_RELOAD_SITES: &str = "reloadSites";
7475

@@ -218,7 +219,6 @@ async fn shvjournal_methods_getter(path: impl AsRef<str>, app_state: AppState<St
218219
// probe the path on the fs
219220
let path_meta = tokio::fs::metadata(path).await.ok()?;
220221
if path_meta.is_dir() {
221-
// TODO: mkfile, mkdir, rmdir
222222
Some(MetaMethods::from(&[
223223
&MetaMethod {
224224
name: METH_LS_FILES,
@@ -301,6 +301,8 @@ async fn root_request_handler(
301301

302302
const ROOT_PATH: &str = "";
303303
match method {
304+
METH_VERSION => Ok(env!("CARGO_PKG_VERSION").into()),
305+
METH_UPTIME => Ok(humantime::format_duration(std::time::Duration::from_secs(app_state.start_time.elapsed().as_secs())).to_string().into()),
304306
METH_LS => {
305307
let mut nodes = vec![
306308
".app".to_string(),
@@ -761,6 +763,15 @@ pub(crate) async fn methods_getter(
761763
])),
762764
NodeType::Root =>
763765
Some(MetaMethods::from(&[
766+
&MetaMethod {
767+
name: METH_VERSION,
768+
flags: 0,
769+
access: shvrpc::metamethod::AccessLevel::Read,
770+
param: "Null",
771+
result: "String",
772+
signals: &[],
773+
description: "",
774+
},
764775
&MetaMethod {
765776
name: METH_UPTIME,
766777
flags: 0,
@@ -785,11 +796,9 @@ pub(crate) async fn methods_getter(
785796
// appName
786797
// deviceId
787798
// deviceType
788-
// version
789799
// gitCommit
790800
// shvVersion
791801
// shvGitCommit
792-
// uptime
793802
// reloadSites (wr) -> Bool
794803
])),
795804
NodeType::History => {
@@ -816,7 +825,6 @@ pub(crate) async fn methods_getter(
816825
}
817826
}
818827
}
819-
// TODO: put the processed data to a request cache in the app state: path -> children
820828
}
821829

822830
pub(crate) async fn request_handler(

src/util.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,7 @@ pub mod testing {
396396
}
397397

398398
let state = AppState::new(State {
399+
start_time: std::time::Instant::now(),
399400
config: crate::HpConfig {
400401
journal_dir: journal_dir.path().to_str().expect("path must work").to_string(),
401402
max_sync_tasks: None,

0 commit comments

Comments
 (0)