From 47ad4681b22b045745cd67c8de4562c05a2f28fd Mon Sep 17 00:00:00 2001 From: jk jensen Date: Fri, 6 Sep 2024 11:23:42 -0700 Subject: [PATCH] [suiop] add date to message output (#19233) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Description Add date output to make it clear when an incident was closed. Add the slack channel url to short output. ## Test plan ``` DEBUG=true cargo run -- i r -i --limit 1 Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.54s Running `/Users/jkjensen/mysten/sui/target/debug/suiop i r -i --limit 1` 2024-09-05T20:52:01.898863Z INFO suiop: Debug mode enabled 3511:0d [FIRING:1] disk used % Infrastructure (/dev/md1 mainnet ext4 ord-mnt-rpcbig-03 localhost:9091 node-exporter /opt/sui mainnet pagerduty/nre rpc active) (https://mystenlabs.pagerduty.com/incidents/Q1OS8GCY2HHNEB) > Keep this incident for review? Yes Incidents marked for review: 3511 Here is the message to send in the channel: Hello everyone and happy Thursday! We have selected the following incidents for review: • 3511 09/05/24 [FIRING:1] disk used % Infrastructure (/dev/md1 mainnet ext4 ord-mnt-rpcbig-03 localhost:9091 node-exporter /opt/sui mainnet pagerduty/nre rpc active) and the following incidents have been excluded from review: Please comment in the thread to request an adjustment to the list. > Send this message to the #test-notifications channel? Yes ``` --- ## Release notes Check each box that your changes affect. If none of the boxes relate to your changes, release notes aren't required. For each box you select, include information after the relevant heading that describes the impact of your changes that a user might notice and any actions they must take to implement updates. - [ ] Protocol: - [ ] Nodes (Validators and Full nodes): - [ ] Indexer: - [ ] JSON-RPC: - [ ] GraphQL: - [ ] CLI: - [ ] Rust SDK: - [ ] REST API: --- crates/suiop-cli/src/cli/incidents/pd.rs | 32 ++++++++++++++------- crates/suiop-cli/src/cli/incidents/slack.rs | 4 ++- 2 files changed, 25 insertions(+), 11 deletions(-) diff --git a/crates/suiop-cli/src/cli/incidents/pd.rs b/crates/suiop-cli/src/cli/incidents/pd.rs index 2bf74006ba30f..2434f775e9a70 100644 --- a/crates/suiop-cli/src/cli/incidents/pd.rs +++ b/crates/suiop-cli/src/cli/incidents/pd.rs @@ -37,13 +37,14 @@ pub struct Incident { priority: Option, slack_channel: Option, } +const DATE_FORMAT_IN: &str = "%Y-%m-%dT%H:%M:%SZ"; +const DATE_FORMAT_OUT: &str = "%m/%d/%Y %H:%M"; +const DATE_FORMAT_OUT_SHORT: &str = "%m/%d/%y"; impl Incident { fn print(&self, long_output: bool) -> Result<()> { - let date_format_in = "%Y-%m-%dT%H:%M:%SZ"; let priority = self.priority(); if long_output { - let date_format_out = "%m/%d/%Y %H:%M"; println!( "Incident #: {} {}", self.number.to_string().bright_purple(), @@ -57,8 +58,8 @@ impl Incident { if let Some(created_at) = self.created_at.clone() { println!( "Created at: {}", - NaiveDateTime::parse_from_str(&created_at, date_format_in)? - .format(date_format_out) + NaiveDateTime::parse_from_str(&created_at, DATE_FORMAT_IN)? + .format(DATE_FORMAT_OUT) .to_string() .yellow() ); @@ -66,8 +67,8 @@ impl Incident { if let Some(resolved_at) = self.resolved_at.clone() { println!( "Resolved at: {}", - NaiveDateTime::parse_from_str(&resolved_at, date_format_in)? - .format(date_format_out) + NaiveDateTime::parse_from_str(&resolved_at, DATE_FORMAT_IN)? + .format(DATE_FORMAT_OUT) .to_string() .yellow() ); @@ -81,7 +82,7 @@ impl Incident { let resolved_at = if let Some(resolved_at) = self.resolved_at.clone() { let now = Utc::now().naive_utc(); - Some(now - NaiveDateTime::parse_from_str(&resolved_at, date_format_in)?) + Some(now - NaiveDateTime::parse_from_str(&resolved_at, DATE_FORMAT_IN)?) } else { None }; @@ -97,7 +98,11 @@ impl Incident { format!(" {} ", priority) }, self.title.green(), - self.html_url.bright_purple(), + if let Some(channel) = self.slack_channel.clone() { + format!("({})", channel.url().bright_magenta()) + } else { + self.html_url.bright_purple().to_string() + } ); } Ok(()) @@ -247,17 +252,24 @@ pub async fn review_recent_incidents(incidents: Vec) -> Result<()> { fn short_print(i: &Incident) -> String { format!( - "• {} {} {}", + "• {} {} {} {}", if let Some(channel) = i.slack_channel.clone() { format!("{} ({})", i.number, channel.url()) } else { i.number.to_string() }, + i.resolved_at + .clone() + .map(|c| NaiveDateTime::parse_from_str(&c, DATE_FORMAT_IN) + .expect("parsing closed date") + .format(DATE_FORMAT_OUT_SHORT) + .to_string()) + .unwrap_or("".to_owned()), i.title, i.slack_channel .clone() .map(|c| c.name) - .unwrap_or("".to_string()) + .unwrap_or("".to_string()), ) } diff --git a/crates/suiop-cli/src/cli/incidents/slack.rs b/crates/suiop-cli/src/cli/incidents/slack.rs index 82a3e58968a73..e58cfb7df4896 100644 --- a/crates/suiop-cli/src/cli/incidents/slack.rs +++ b/crates/suiop-cli/src/cli/incidents/slack.rs @@ -55,7 +55,9 @@ struct SendMessageBody { impl Slack { pub async fn new() -> Self { - let token = std::env::var("SLACK_BOT_TOKEN").expect("Please set SLACK_BOT_TOKEN env var"); + let token = std::env::var("SLACK_BOT_TOKEN").expect( + "Please set SLACK_BOT_TOKEN env var ('slack bot token (incidentbot)' in 1password)", + ); let mut headers = header::HeaderMap::new(); headers.insert( header::AUTHORIZATION,