-
Notifications
You must be signed in to change notification settings - Fork 187
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
This file was deleted.
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" |
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: {:?}", ®ion); | ||
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); | ||
} | ||
|
||
Ok(()) | ||
} |
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: {:?}", ®ion); | ||
} | ||
|
||
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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Likewise here There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
} | ||
|
||
Ok(()) | ||
} |
There was a problem hiding this comment.
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:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated, thanks.