Skip to content

Commit

Permalink
Use f64 for ping field in NodeStatus
Browse files Browse the repository at this point in the history
This commit changes the `ping` field in `NodeStatus` in GraphQL API from
`StringNumber`, a custom scalar type, to `Float`, a built-in scalar
type.
  • Loading branch information
msk authored Aug 27, 2024
1 parent 843e565 commit 69e2db0
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 12 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ Versioning](https://semver.org/spec/v2.0.0.html).
- Refactor `AgentManager::ping` to return `Duration` instead of `i64`. This
refactor improves the flexibility and accuracy of the `ping` method, making it
more robust and aligned with Rust's time handling conventions.
- In the GraphQL API, modified the `ping` field in `NodeStatus` to return a
`Float` (seconds) instead of a `StringNumber` (microseconds). This provides a
more standard representation of round-trip time and improves compatibility
with GraphQL clients.
- Added a `language` field to the `Account`. Consequently, the `account` and
`accountList` API responses now include this field. The `insertAccount` and
`updateAccount` GraphQL API endpoints are also updated to support the field.
Expand Down
13 changes: 7 additions & 6 deletions src/graphql/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ mod status;
use std::{
borrow::Cow,
net::{IpAddr, SocketAddr},
time::Duration,
};

use async_graphql::{
Expand Down Expand Up @@ -328,9 +329,9 @@ pub(super) struct NodeStatus {
#[graphql(skip)]
used_disk_space: Option<u64>,

/// The ping value for a specific node.
/// The round-trip time to the node.
#[graphql(skip)]
ping: Option<i64>,
ping: Option<Duration>,

/// Whether review is online or not.
review: Option<bool>,
Expand Down Expand Up @@ -372,9 +373,9 @@ impl NodeStatus {
async fn used_disk_space(&self) -> Option<StringNumber<u64>> {
self.used_disk_space.map(StringNumber)
}
/// The round-trip time in microseconds to a host, within the range representable by an `i64`
async fn ping(&self) -> Option<StringNumber<i64>> {
self.ping.map(StringNumber)
/// The round-trip time to the node in seconds.
async fn ping(&self) -> Option<f64> {
self.ping.map(|d| d.as_secs_f64())
}
}

Expand All @@ -388,7 +389,7 @@ impl NodeStatus {
used_memory: Option<u64>,
total_disk_space: Option<u64>,
used_disk_space: Option<u64>,
ping: Option<i64>,
ping: Option<Duration>,
review: Option<bool>,
piglet: Option<bool>,
piglet_config: Option<PigletConfig>,
Expand Down
10 changes: 4 additions & 6 deletions src/graphql/node/status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ async fn load(
let agents = ctx.data::<BoxedAgentManager>()?;
let apps = agents.online_apps_by_host_id().await?;
let mut usages: HashMap<String, ResourceUsage> = HashMap::new();
let mut ping: HashMap<String, i64> = HashMap::new();
let mut ping = HashMap::new();
for hostname in apps.keys() {
if let Ok(usage) = agents.get_resource_usage(hostname).await {
usages.insert(
Expand All @@ -62,9 +62,7 @@ async fn load(
);
}
if let Ok(rtt) = agents.ping(hostname).await {
let clamped_micros = i64::try_from(std::cmp::min(rtt.as_micros(), i64::MAX as u128))
.expect("within i64 range");
ping.insert(hostname.clone(), clamped_micros);
ping.insert(hostname.clone(), rtt);
}
}

Expand Down Expand Up @@ -473,7 +471,7 @@ mod tests {
"usedMemory": "100",
"totalDiskSpace": "1000",
"usedDiskSpace": "100",
"ping": "10",
"ping": 0.00001,
"review": true,
"piglet": true,
"pigletConfig": null,
Expand All @@ -490,7 +488,7 @@ mod tests {
"usedMemory": "100",
"totalDiskSpace": "1000",
"usedDiskSpace": "100",
"ping": "10",
"ping": 0.00001,
"review": false,
"piglet": false,
"pigletConfig": null,
Expand Down

0 comments on commit 69e2db0

Please sign in to comment.