Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated SageMaker examples to take a region arg; added doc comments. #524

Merged
merged 3 commits into from
Jun 22, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 0 additions & 24 deletions aws/sdk/examples/sagemaker-helloworld/src/main.rs

This file was deleted.

13 changes: 0 additions & 13 deletions aws/sdk/examples/sagemaker-list-training-jobs/Cargo.toml

This file was deleted.

26 changes: 0 additions & 26 deletions aws/sdk/examples/sagemaker-list-training-jobs/src/main.rs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
[package]
name = "sagemaker-helloworld"
name = "sagemaker-code-examples"
version = "0.1.0"
authors = ["Alistair McLean <mclean@amazon.com>"]
authors = ["Alistair McLean <mclean@amazon.com>", "Doug Schwartz <dougsch@amazon.com>"]
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
env_logger = "0.8.2"
sagemaker = {package = "aws-sdk-sagemaker", path = "../../build/aws-sdk/sagemaker"}
aws-types = { path = "../../build/aws-sdk/aws-types" }

tokio = { version = "1", features = ["full"] }


env_logger = "0.8.2"
chrono = "0.4.19"
structopt = { version = "0.3", default-features = false }
tracing-subscriber = "0.2.18"
75 changes: 75 additions & 0 deletions aws/sdk/examples/sagemaker/src/bin/list-training-jobs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0.
*/

use aws_types::region::ProvideRegion;

use sagemaker::{Client, Config, Region};

use structopt::StructOpt;

#[derive(Debug, StructOpt)]
struct Opt {
/// The AWS Region. Overrides environment variable AWS_DEFAULT_REGION.
#[structopt(short, long)]
default_region: Option<String>,

/// Whether to display additional runtime information
#[structopt(short, long)]
verbose: bool,
}

/// Lists the your SageMaker jobs in an AWS Region.
/// /// # Arguments
///
/// * `[-d DEFAULT-REGION]` - The region in which the client is created.
/// If not supplied, uses the value of the **AWS_DEFAULT_REGION** environment variable.
/// If the environment variable is not set, defaults to **us-west-2**.
/// * `[-v]` - Whether to display additional information.#[tokio::main]
#[tokio::main]
async fn main() -> Result<(), sagemaker::Error> {
tracing_subscriber::fmt::init();

let Opt {
default_region,
verbose,
} = Opt::from_args();

let region = default_region
.as_ref()
.map(|region| Region::new(region.clone()))
.or_else(|| aws_types::region::default_provider().region())
.unwrap_or_else(|| Region::new("us-west-2"));

if verbose {
println!("SageMaker client version: {}", sagemaker::PKG_VERSION);
println!("Region: {:?}", &region);
println!();
}

let conf = Config::builder().region(region).build();
let client = Client::from_conf(conf);
let job_details = client.list_training_jobs().send().await?;

println!("Job Name\tCreation DateTime\tDuration\tStatus");
for j in job_details.training_job_summaries.unwrap_or_default() {
let name = j.training_job_name.as_deref().unwrap_or_default();
let creation_time = j.creation_time.unwrap().to_chrono();
let training_end_time = j.training_end_time.unwrap().to_chrono();

let status = j.training_job_status.unwrap();
let duration = training_end_time - creation_time;

let deets = format!(
"{}\t{}\t{}\t{:#?}",
name,
creation_time.format("%Y-%m-%d@%H:%M:%S"),
duration.num_seconds(),
status
);
println!("{}", deets);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can combine these two lines:

println!(
    "{}\t{}\t{}\t{:#?}",
    name,
    ...
);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated, thanks.

}

Ok(())
}
67 changes: 67 additions & 0 deletions aws/sdk/examples/sagemaker/src/bin/sagemaker-helloworld.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0.
*/

use aws_types::region::ProvideRegion;

use sagemaker::{Client, Config, Region};

use structopt::StructOpt;

#[derive(Debug, StructOpt)]
struct Opt {
/// The region. Overrides environment variable AWS_DEFAULT_REGION.
#[structopt(short, long)]
default_region: Option<String>,

/// Whether to display additional runtime information
#[structopt(short, long)]
verbose: bool,
}

/// Lists the name, status, and type of your SageMaker instances in an AWS Region.
/// /// # Arguments
///
/// * `[-d DEFAULT-REGION]` - The region in which the client is created.
/// If not supplied, uses the value of the **AWS_DEFAULT_REGION** environment variable.
/// If the environment variable is not set, defaults to **us-west-2**.
/// * `[-v]` - Whether to display additional information.
#[tokio::main]
async fn main() -> Result<(), sagemaker::Error> {
tracing_subscriber::fmt::init();

let Opt {
default_region,
verbose,
} = Opt::from_args();

let region = default_region
.as_ref()
.map(|region| Region::new(region.clone()))
.or_else(|| aws_types::region::default_provider().region())
.unwrap_or_else(|| Region::new("us-west-2"));

if verbose {
println!("SageMaker client version: {}", sagemaker::PKG_VERSION);
println!("Region: {:?}", &region);
}

let conf = Config::builder().region(region).build();
let client = Client::from_conf(conf);
let notebooks = client.list_notebook_instances().send().await?;

for n in notebooks.notebook_instances.unwrap_or_default() {
let n_instance_type = n.instance_type.unwrap();
let n_status = n.notebook_instance_status.unwrap();
let n_name = n.notebook_instance_name.as_deref().unwrap_or_default();

let details = format!(
"Notebook Name : {}, Notebook Status : {:#?}, Notebook Instance Type : {:#?}",
n_name, n_status, n_instance_type
);
println!("{}", details);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Likewise here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

}

Ok(())
}