-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add analytics datasets and dataverses subcommands
- Loading branch information
Showing
4 changed files
with
134 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
use super::util::convert_json_value_to_nu_value; | ||
use crate::state::State; | ||
use couchbase::AnalyticsOptions; | ||
use futures::executor::block_on; | ||
use futures::stream::StreamExt; | ||
use log::debug; | ||
use nu_cli::{CommandArgs, CommandRegistry, OutputStream}; | ||
use nu_errors::ShellError; | ||
use nu_protocol::Signature; | ||
use nu_source::Tag; | ||
use std::sync::Arc; | ||
|
||
pub struct AnalyticsDatasets { | ||
state: Arc<State>, | ||
} | ||
|
||
impl AnalyticsDatasets { | ||
pub fn new(state: Arc<State>) -> Self { | ||
Self { state } | ||
} | ||
} | ||
|
||
impl nu_cli::WholeStreamCommand for AnalyticsDatasets { | ||
fn name(&self) -> &str { | ||
"analytics datasets" | ||
} | ||
|
||
fn signature(&self) -> Signature { | ||
Signature::build("analytics datasets") | ||
} | ||
|
||
fn usage(&self) -> &str { | ||
"Lists all analytics datasets" | ||
} | ||
|
||
fn run( | ||
&self, | ||
_args: CommandArgs, | ||
_registry: &CommandRegistry, | ||
) -> Result<OutputStream, ShellError> { | ||
block_on(datasets(self.state.clone())) | ||
} | ||
} | ||
|
||
async fn datasets(state: Arc<State>) -> Result<OutputStream, ShellError> { | ||
let statement = "SELECT d.* FROM Metadata.`Dataset` d WHERE d.DataverseName <> \"Metadata\""; | ||
|
||
debug!("Running analytics query {}", &statement); | ||
let result = state | ||
.active_cluster() | ||
.cluster() | ||
.analytics_query(statement, AnalyticsOptions::default()) | ||
.await; | ||
|
||
match result { | ||
Ok(mut r) => { | ||
let stream = r | ||
.rows::<serde_json::Value>() | ||
.map(|v| convert_json_value_to_nu_value(&v.unwrap(), Tag::default())); | ||
Ok(OutputStream::from_input(stream)) | ||
} | ||
Err(e) => Err(ShellError::untagged_runtime_error(format!("{}", e))), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
use super::util::convert_json_value_to_nu_value; | ||
use crate::state::State; | ||
use couchbase::AnalyticsOptions; | ||
use futures::executor::block_on; | ||
use futures::stream::StreamExt; | ||
use log::debug; | ||
use nu_cli::{CommandArgs, CommandRegistry, OutputStream}; | ||
use nu_errors::ShellError; | ||
use nu_protocol::Signature; | ||
use nu_source::Tag; | ||
use std::sync::Arc; | ||
|
||
pub struct AnalyticsDataverses { | ||
state: Arc<State>, | ||
} | ||
|
||
impl AnalyticsDataverses { | ||
pub fn new(state: Arc<State>) -> Self { | ||
Self { state } | ||
} | ||
} | ||
|
||
impl nu_cli::WholeStreamCommand for AnalyticsDataverses { | ||
fn name(&self) -> &str { | ||
"analytics dataverses" | ||
} | ||
|
||
fn signature(&self) -> Signature { | ||
Signature::build("analytics dataverses") | ||
} | ||
|
||
fn usage(&self) -> &str { | ||
"Lists all analytics dataverses" | ||
} | ||
|
||
fn run( | ||
&self, | ||
_args: CommandArgs, | ||
_registry: &CommandRegistry, | ||
) -> Result<OutputStream, ShellError> { | ||
block_on(dataverses(self.state.clone())) | ||
} | ||
} | ||
|
||
async fn dataverses(state: Arc<State>) -> Result<OutputStream, ShellError> { | ||
let statement = "SELECT d.* FROM Metadata.`Dataverse` d WHERE d.DataverseName <> \"Metadata\""; | ||
|
||
debug!("Running analytics query {}", &statement); | ||
let result = state | ||
.active_cluster() | ||
.cluster() | ||
.analytics_query(statement, AnalyticsOptions::default()) | ||
.await; | ||
|
||
match result { | ||
Ok(mut r) => { | ||
let stream = r | ||
.rows::<serde_json::Value>() | ||
.map(|v| convert_json_value_to_nu_value(&v.unwrap(), Tag::default())); | ||
Ok(OutputStream::from_input(stream)) | ||
} | ||
Err(e) => Err(ShellError::untagged_runtime_error(format!("{}", e))), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters