summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFerris Tseng <ferristseng@fastmail.fm>2017-10-09 17:27:41 -0400
committerFerris Tseng <ferristseng@fastmail.fm>2017-10-09 17:27:41 -0400
commit861b463306cf2d158b5f3fa5efa20b52bab23287 (patch)
tree5a4d1ad71c4403251ef5ed52195bc73ec155ea0c
parent2820ff2acaa154f437e7149777aafe187736f35f (diff)
add example and client calls to retrieve stats from ipfs
-rw-r--r--ipfs-api/examples/get_stats.rs48
-rw-r--r--ipfs-api/src/client.rs37
-rw-r--r--ipfs-api/src/request/mod.rs2
-rw-r--r--ipfs-api/src/request/stats.rs31
-rw-r--r--ipfs-api/src/response/bitswap.rs2
-rw-r--r--ipfs-api/src/response/repo.rs2
-rw-r--r--ipfs-api/src/response/stats.rs2
7 files changed, 114 insertions, 10 deletions
diff --git a/ipfs-api/examples/get_stats.rs b/ipfs-api/examples/get_stats.rs
new file mode 100644
index 0000000..97c1b6a
--- /dev/null
+++ b/ipfs-api/examples/get_stats.rs
@@ -0,0 +1,48 @@
+extern crate ipfs_api;
+extern crate tokio_core;
+
+use ipfs_api::IpfsClient;
+use tokio_core::reactor::Core;
+
+
+// Creates an Ipfs client, and gets some stats about the Ipfs server.
+//
+fn main() {
+ if let Ok(mut core) = Core::new() {
+ println!("connecting to localhost:5001...");
+
+ let client =
+ IpfsClient::new(&core.handle(), "localhost", 5001).expect("expected a valid url");
+
+ let bitswap_stats = client.stats_bitswap().expect("expected a valid request");
+ let bitswap_stats = core.run(bitswap_stats).expect("expected a valid response");
+
+ let bw_stats = client.stats_bw().expect("expected a valid request");
+ let bw_stats = core.run(bw_stats).expect("expected a valid response");
+
+ let repo_stats = client.stats_repo().expect("expected a valid request");
+ let repo_stats = core.run(repo_stats).expect("expected a valid response");
+
+ println!("bitswap stats:");
+ println!(" blocks recv: {}", bitswap_stats.blocks_received);
+ println!(" data recv: {}", bitswap_stats.data_received);
+ println!(" blocks sent: {}", bitswap_stats.blocks_sent);
+ println!(" data sent: {}", bitswap_stats.data_sent);
+ println!(" peers: {}", bitswap_stats.peers.join("\n "));
+ println!(" wantlist: {}", bitswap_stats.wantlist.join("\n "));
+ println!("");
+ println!("bandwidth stats:");
+ println!(" total in: {}", bw_stats.total_in);
+ println!(" total out: {}", bw_stats.total_out);
+ println!(" rate in: {}", bw_stats.rate_in);
+ println!(" rate out: {}", bw_stats.rate_out);
+ println!("");
+ println!("repo stats:");
+ println!(" num objs: {}", repo_stats.num_objects);
+ println!(" repo size: {}", repo_stats.repo_size);
+ println!(" repo path: {}", repo_stats.repo_path);
+ println!(" version : {}", repo_stats.version);
+ } else {
+ println!("failed to create event loop");
+ }
+}
diff --git a/ipfs-api/src/client.rs b/ipfs-api/src/client.rs
index ff3eb52..fc484c2 100644
--- a/ipfs-api/src/client.rs
+++ b/ipfs-api/src/client.rs
@@ -13,6 +13,11 @@ use tokio_core::reactor::Handle;
type AsyncResponse<T> = Box<Future<Item = T, Error = Error>>;
+/// A result returned by a public facing call to the Ipfs api.
+///
+type ApiResult<T> = Result<AsyncResponse<T>, Error>;
+
+
/// Asynchronous Ipfs client.
///
pub struct IpfsClient {
@@ -57,7 +62,7 @@ impl IpfsClient {
/// Generic method for making a request to the Ipfs server, and getting
/// a deserializable response.
///
- fn request<Req, Res>(&self, req: &Req) -> Result<AsyncResponse<Res>, Error>
+ fn request<Req, Res>(&self, req: &Req) -> ApiResult<Res>
where
Req: ApiRequest,
for<'de> Res: 'static + Deserialize<'de>,
@@ -76,7 +81,7 @@ impl IpfsClient {
&self,
data: R,
req: &Req,
- ) -> Result<AsyncResponse<Res>, Error>
+ ) -> ApiResult<Res>
where
Req: ApiRequest,
for<'de> Res: 'static + Deserialize<'de>,
@@ -84,7 +89,7 @@ impl IpfsClient {
{
let url = self.build_url(req)?;
let form = multipart::Form::new().part("file", multipart::Part::reader(data));
- let mut req = self.client.request(Method::Get, url); //.form(&form);
+ let mut req = self.client.request(Method::Get, url);//.form(&form);
let res = req.send().and_then(move |mut res| res.json()).from_err();
Ok(Box::new(res))
@@ -95,7 +100,7 @@ impl IpfsClient {
/// Add file to Ipfs.
///
#[inline]
- pub fn add<R>(&self, data: R) -> Result<AsyncResponse<response::AddResponse>, Error>
+ pub fn add<R>(&self, data: R) -> ApiResult<response::AddResponse>
where
R: 'static + Read + Send,
{
@@ -105,21 +110,39 @@ impl IpfsClient {
/// List available commands that the server accepts.
///
#[inline]
- pub fn commands(&self) -> Result<AsyncResponse<response::CommandsResponse>, Error> {
+ pub fn commands(&self) -> ApiResult<response::CommandsResponse> {
self.request(&request::Commands)
}
/// List the contents of an Ipfs multihash.
///
#[inline]
- pub fn ls(&self, path: Option<&str>) -> Result<AsyncResponse<response::LsResponse>, Error> {
+ pub fn ls(&self, path: Option<&str>) -> ApiResult<response::LsResponse> {
self.request(&request::LsRequest(path))
}
+ /// Returns bitswap stats.
+ ///
+ pub fn stats_bitswap(&self) -> ApiResult<response::StatsBitswapResponse> {
+ self.request(&request::StatsBitswap)
+ }
+
+ /// Returns bandwidth stats.
+ ///
+ pub fn stats_bw(&self) -> ApiResult<response::StatsBwResponse> {
+ self.request(&request::StatsBw)
+ }
+
+ /// Returns repo stats.
+ ///
+ pub fn stats_repo(&self) -> ApiResult<response::StatsRepoResponse> {
+ self.request(&request::StatsRepo)
+ }
+
/// Returns information about the Ipfs server version.
///
#[inline]
- pub fn version(&self) -> Result<AsyncResponse<response::VersionResponse>, Error> {
+ pub fn version(&self) -> ApiResult<response::VersionResponse> {
self.request(&request::Version)
}
}
diff --git a/ipfs-api/src/request/mod.rs b/ipfs-api/src/request/mod.rs
index 7681a78..45faed9 100644
--- a/ipfs-api/src/request/mod.rs
+++ b/ipfs-api/src/request/mod.rs
@@ -1,12 +1,14 @@
pub use self::add::*;
pub use self::commands::*;
pub use self::ls::*;
+pub use self::stats::*;
pub use self::version::*;
mod add;
mod commands;
mod ls;
+mod stats;
mod version;
diff --git a/ipfs-api/src/request/stats.rs b/ipfs-api/src/request/stats.rs
new file mode 100644
index 0000000..c20c71c
--- /dev/null
+++ b/ipfs-api/src/request/stats.rs
@@ -0,0 +1,31 @@
+use request::ApiRequest;
+
+
+pub struct StatsBitswap;
+
+impl ApiRequest for StatsBitswap {
+ #[inline]
+ fn path() -> &'static str {
+ "/stats/bitswap"
+ }
+}
+
+
+pub struct StatsBw;
+
+impl ApiRequest for StatsBw {
+ #[inline]
+ fn path() -> &'static str {
+ "/stats/bw"
+ }
+}
+
+
+pub struct StatsRepo;
+
+impl ApiRequest for StatsRepo {
+ #[inline]
+ fn path() -> &'static str {
+ "/stats/repo"
+ }
+}
diff --git a/ipfs-api/src/response/bitswap.rs b/ipfs-api/src/response/bitswap.rs
index c9a5e02..575decd 100644
--- a/ipfs-api/src/response/bitswap.rs
+++ b/ipfs-api/src/response/bitswap.rs
@@ -12,7 +12,7 @@ pub struct BitswapLedgerResponse {
}
-#[derive(Deserialize)]
+#[derive(Debug, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub struct BitswapStatResponse {
pub provide_buf_len: usize,
diff --git a/ipfs-api/src/response/repo.rs b/ipfs-api/src/response/repo.rs
index c0861b2..5b0fc45 100644
--- a/ipfs-api/src/response/repo.rs
+++ b/ipfs-api/src/response/repo.rs
@@ -18,7 +18,7 @@ pub struct RepoGcResponse {
}
-#[derive(Deserialize)]
+#[derive(Debug, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub struct RepoStatResponse {
pub num_objects: u64,
diff --git a/ipfs-api/src/response/stats.rs b/ipfs-api/src/response/stats.rs
index 908e6cf..a9ffd1b 100644
--- a/ipfs-api/src/response/stats.rs
+++ b/ipfs-api/src/response/stats.rs
@@ -4,7 +4,7 @@ use response::{BitswapStatResponse, RepoStatResponse};
pub type StatsBitswapResponse = BitswapStatResponse;
-#[derive(Deserialize)]
+#[derive(Debug, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub struct StatsBwResponse {
pub total_in: u64,