diff options
author | Matthias Beyer <mail@beyermatthias.de> | 2021-01-08 15:50:33 +0100 |
---|---|---|
committer | Matthias Beyer <mail@beyermatthias.de> | 2021-01-14 13:35:34 +0100 |
commit | cb5ad5e7b573d147d8d1726fb057336fb77fa42a (patch) | |
tree | 973581a70bdbb576fa66f0c4cc0f35de86576f07 /src/commands | |
parent | 399cdf75fcdacfb4c68165a1bf401b12ecad1cb9 (diff) |
Add ENV filtering for "db jobs" subcommand
This patch implements filtering for environment variables via a key-value pair.
The implementation is altered to build the database query with a
filter if the commandline flag was passed.
The implementation was also slightly changed to not fetch the Submit
object first, but filter for it.
Signed-off-by: Matthias Beyer <mail@beyermatthias.de>
Diffstat (limited to 'src/commands')
-rw-r--r-- | src/commands/db.rs | 28 |
1 files changed, 21 insertions, 7 deletions
diff --git a/src/commands/db.rs b/src/commands/db.rs index 38e1de5..7242ee9 100644 --- a/src/commands/db.rs +++ b/src/commands/db.rs @@ -251,26 +251,40 @@ fn jobs(conn_cfg: DbConnectionConfig, matches: &ArgMatches) -> Result<()> { .map(uuid::Uuid::parse_str) .transpose()? .map(|submit_uuid| { - let submit = models::Submit::with_id(&conn, &submit_uuid)?; - - models::Job::belonging_to(&submit) + let sel = schema::jobs::table .inner_join(schema::submits::table) .inner_join(schema::endpoints::table) .inner_join(schema::packages::table) - .load::<(models::Job, models::Submit, models::Endpoint, models::Package)>(&conn) - .map_err(Error::from) + .left_outer_join(schema::job_envs::table.inner_join(schema::envvars::table)) + .filter(schema::submits::uuid.eq(&submit_uuid)); + + if let Some((env_name, env_value)) = matches.value_of("filter_env").map(crate::util::env::parse_to_env).transpose()? { + sel.filter({ + use crate::diesel::BoolExpressionMethods; + + schema::envvars::dsl::name + .eq(env_name.as_ref()) + .and(schema::envvars::dsl::value.eq(env_value)) + }) + .load::<(models::Job, models::Submit, models::Endpoint, models::Package, Option<(models::JobEnv, models::EnvVar)>)>(&conn) + .map_err(Error::from) + } else { + sel.load::<(models::Job, models::Submit, models::Endpoint, models::Package, Option<(models::JobEnv, models::EnvVar)>)>(&conn) + .map_err(Error::from) + } }) .unwrap_or_else(|| { dsl::jobs .inner_join(crate::schema::submits::table) .inner_join(crate::schema::endpoints::table) .inner_join(crate::schema::packages::table) - .load::<(models::Job, models::Submit, models::Endpoint, models::Package)>(&conn) + .left_outer_join(schema::job_envs::table.inner_join(schema::envvars::table)) + .load::<(models::Job, models::Submit, models::Endpoint, models::Package, Option<(models::JobEnv, models::EnvVar)>)>(&conn) .map_err(Error::from) })?; let data = jobs.into_iter() - .map(|(job, submit, ep, package)| { + .map(|(job, submit, ep, package, _)| { let success = crate::log::ParsedLog::build_from(&job.log_text)? .is_successfull() .map(|b| if b { |