Skip to content
This repository has been archived by the owner on Jul 25, 2022. It is now read-only.

EX-1687 graphql commands #2240

Merged
merged 7 commits into from
Sep 16, 2020
Merged
Show file tree
Hide file tree
Changes from 6 commits
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
76 changes: 74 additions & 2 deletions iml-api/src/graphql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{command::get_command, error::ImlApiError};
use futures::{TryFutureExt, TryStreamExt};
use iml_postgres::{sqlx, PgPool};
use iml_rabbit::Pool;
use iml_wire_types::{snapshot::Snapshot, Command};
use iml_wire_types::{snapshot::Snapshot, Command, EndpointName, Job};
use itertools::Itertools;
use juniper::{
http::{graphiql::graphiql_source, GraphQLRequest},
Expand Down Expand Up @@ -284,10 +284,11 @@ impl QueryRoot {

Ok(xs)
}

#[graphql(arguments(
limit(description = "paging limit, defaults to 20"),
offset(description = "Offset into items, defaults to 0"),
dir(description = "Sort direction, defaults to asc"),
dir(description = "Sort direction, defaults to ASC"),
fsname(description = "Filesystem the snapshot was taken from"),
name(description = "Name of the snapshot"),
))]
Expand Down Expand Up @@ -322,6 +323,77 @@ impl QueryRoot {

Ok(snapshots)
}

/// Fetch the list of commands
#[graphql(arguments(
limit(description = "paging limit, defaults to 20"),
offset(description = "Offset into items, defaults to 0"),
dir(description = "Sort direction, defaults to ASC"),
is_active(description = "Command status, active means not completed, default is false"),
msg(description = "Substring of the command's message, null or empty matches all"),
))]
async fn commands(
context: &Context,
limit: Option<i32>,
offset: Option<i32>,
dir: Option<SortDir>,
is_active: Option<bool>,
msg: Option<String>,
) -> juniper::FieldResult<Vec<Command>> {
let dir = dir.unwrap_or_default();
let is_completed = !is_active.unwrap_or(false);
let commands: Vec<Command> = sqlx::query!(
r#"
SELECT
c.id AS id,
cancelled,
complete,
errored,
created_at,
array_agg(cj.job_id)::INT[] AS job_ids,
message
FROM chroma_core_command c
JOIN chroma_core_command_jobs cj ON c.id = cj.command_id
WHERE complete = $4
AND ($5::TEXT IS NULL OR c.message ILIKE '%' || $5 || '%')
GROUP BY c.id
ORDER BY
CASE WHEN $3 = 'asc' THEN c.id END ASC,
CASE WHEN $3 = 'desc' THEN c.id END DESC
OFFSET $1 LIMIT $2 "#,
offset.unwrap_or(0) as i64,
limit.unwrap_or(20) as i64,
dir.deref(),
is_completed,
msg,
)
.fetch_all(&context.pg_pool)
.map_ok(|xs: Vec<_>| {
xs.into_iter()
.map(|x| Command {
id: x.id,
cancelled: x.cancelled,
complete: x.complete,
errored: x.errored,
created_at: x.created_at.format("%Y-%m-%dT%T%.6f").to_string(),
jobs: {
x.job_ids
.unwrap_or_default()
.into_iter()
.map(|job_id: i32| {
format!("/api/{}/{}/", Job::<()>::endpoint_name(), job_id)
})
.collect::<Vec<_>>()
},
logs: "".to_string(),
message: x.message.clone(),
resource_uri: format!("/api/{}/{}/", Command::endpoint_name(), x.id),
})
.collect::<Vec<_>>()
})
.await?;
Ok(commands)
}
}

#[juniper::graphql_object(Context = Context)]
Expand Down
4 changes: 4 additions & 0 deletions iml-manager-env/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,10 @@ pub fn get_db_host() -> Option<String> {
empty_str_to_none(get_var("DB_HOST"))
}

pub fn get_db_port() -> Option<u16> {
env::var("DB_PORT").ok().map(|l| l.parse().ok()).flatten()
}

pub fn get_db_name() -> Option<String> {
empty_str_to_none(get_var("DB_NAME"))
}
Expand Down
6 changes: 6 additions & 0 deletions iml-postgres/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ pub async fn get_db_pool(pool_size: u32) -> Result<PgPool, sqlx::Error> {
opts
};

opts = if let Some(x) = iml_manager_env::get_db_port() {
opts.port(x)
} else {
opts
};

opts = if let Some(x) = iml_manager_env::get_db_name() {
opts.database(&x)
} else {
Expand Down
146 changes: 60 additions & 86 deletions sqlx-data.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,6 @@
"nullable": []
}
},
"07317ab9fddc66855470ba840c4b68a340b188eabcfce0bc8fed4f410df1b7db": {
"query": "INSERT INTO chroma_core_managedhost\n (\n state_modified_at,\n state,\n immutable_state,\n not_deleted,\n address,\n fqdn,\n nodename,\n boot_time,\n needs_update,\n corosync_ring0,\n install_method,\n content_type_id,\n server_profile_id)\n VALUES\n ('2020-07-02 15:50:34.356076-04', 'unconfigured', 'f', 't', 'foo', 'foo.bar', '', Null, 'f', '', '', Null, 'foo')\n ON CONFLICT DO NOTHING",
"describe": {
"columns": [],
"parameters": {
"Left": []
},
"nullable": []
}
},
"07d03b70c5d9813b0432b582ad8239edd1a3ce7ef7ce9be7c22dbb8189db8861": {
"query": "select id from chroma_core_managedfilesystem where name = $1 and not_deleted = 't'",
"describe": {
Expand Down Expand Up @@ -595,72 +585,6 @@
]
}
},
"2eedae727c7336f9edd217051685ebfd1e7a8e28597a0803aad92147c49f6805": {
"query": "SELECT * FROM chroma_core_lustreclientmount",
"describe": {
"columns": [
{
"ordinal": 0,
"name": "id",
"type_info": "Int4"
},
{
"ordinal": 1,
"name": "state_modified_at",
"type_info": "Timestamptz"
},
{
"ordinal": 2,
"name": "state",
"type_info": "Varchar"
},
{
"ordinal": 3,
"name": "immutable_state",
"type_info": "Bool"
},
{
"ordinal": 4,
"name": "not_deleted",
"type_info": "Bool"
},
{
"ordinal": 5,
"name": "content_type_id",
"type_info": "Int4"
},
{
"ordinal": 6,
"name": "filesystem",
"type_info": "Varchar"
},
{
"ordinal": 7,
"name": "host_id",
"type_info": "Int4"
},
{
"ordinal": 8,
"name": "mountpoints",
"type_info": "TextArray"
}
],
"parameters": {
"Left": []
},
"nullable": [
false,
false,
false,
false,
true,
true,
false,
false,
false
]
}
},
"36188079437a0e3df0d4584e22b15673bfadcf66128168b59e13717eedcd8470": {
"query": "\n DELETE FROM corosync_node\n USING corosync_node_managed_host\n WHERE id = corosync_node_id\n AND host_id = $1\n AND corosync_node_id != ALL($2::corosync_node_key[])\n ",
"describe": {
Expand Down Expand Up @@ -2034,6 +1958,66 @@
]
}
},
"96578c531d3344f51586bd7f502d258fe105acafebd361a5458d14fa4c26af8d": {
"query": "\n SELECT\n c.id AS id,\n cancelled,\n complete,\n errored,\n created_at,\n array_agg(cj.job_id)::INT[] AS job_ids,\n message\n FROM chroma_core_command c\n JOIN chroma_core_command_jobs cj ON c.id = cj.command_id\n WHERE complete = $4\n AND ($5::TEXT IS NULL OR c.message ILIKE '%' || $5 || '%')\n GROUP BY c.id\n ORDER BY\n CASE WHEN $3 = 'asc' THEN c.id END ASC,\n CASE WHEN $3 = 'desc' THEN c.id END DESC\n OFFSET $1 LIMIT $2 ",
"describe": {
"columns": [
{
"ordinal": 0,
"name": "id",
"type_info": "Int4"
},
{
"ordinal": 1,
"name": "cancelled",
"type_info": "Bool"
},
{
"ordinal": 2,
"name": "complete",
"type_info": "Bool"
},
{
"ordinal": 3,
"name": "errored",
"type_info": "Bool"
},
{
"ordinal": 4,
"name": "created_at",
"type_info": "Timestamptz"
},
{
"ordinal": 5,
"name": "job_ids",
"type_info": "Int4Array"
},
{
"ordinal": 6,
"name": "message",
"type_info": "Varchar"
}
],
"parameters": {
"Left": [
"Int8",
"Int8",
"Text",
"Bool",
"Text"
]
},
"nullable": [
false,
false,
false,
false,
false,
null,
false
]
}
},
"9a4c05da9d9233e6b3fa63ca2f50cf90feb0c305b1cc05e0eb2edcf2572db4ba": {
"query": "select * from chroma_core_volume where not_deleted = 't'",
"describe": {
Expand Down Expand Up @@ -2687,16 +2671,6 @@
]
}
},
"b8e774ab32b79eb3f2d87b7f373b609831f72f63f716dcbb7917871531466b1a": {
"query": "\n INSERT INTO chroma_core_serverprofile\n (name, ui_name, ui_description, managed, worker, user_selectable, initial_state, ntp, corosync, corosync2, pacemaker, \"default\")\n VALUES\n ('foo', 'foo', 'foo', 'f', 'f', 't', 'bar', 'f', 'f', 'f', 'f', 't')\n ON CONFLICT DO NOTHING\n ",
"describe": {
"columns": [],
"parameters": {
"Left": []
},
"nullable": []
}
},
"bb7fb337cfeac9a17eefb21a498e76be70c2f395473ba42331f51cc5e6d48679": {
"query": "\n INSERT INTO chroma_core_device\n (fqdn, devices)\n VALUES ($1, $2)\n ON CONFLICT (fqdn) DO UPDATE\n SET devices = EXCLUDED.devices\n ",
"describe": {
Expand Down
1 change: 1 addition & 0 deletions vagrant/Vagrantfile
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ Vagrant.configure('2') do |config|
adm.vm.network 'forwarded_port', guest: 5432, host: 8432
adm.vm.network 'forwarded_port', guest: 443, host: 8443
adm.vm.network 'forwarded_port', guest: 7443, host: 7443
adm.vm.network 'forwarded_port', guest: 5672, host: 8672

# Admin / management network
provision_mgmt_net adm, '10'
Expand Down