Skip to content

Commit

Permalink
Add support for retrieving metadata for an specific comm. (#589)
Browse files Browse the repository at this point in the history
  • Loading branch information
dfalbel authored Oct 16, 2024
1 parent 67ff702 commit 92d91b5
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 0 deletions.
35 changes: 35 additions & 0 deletions crates/amalthea/src/comm/connections_comm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,26 @@ pub struct FieldSchema {
pub dtype: String
}

/// MetadataSchema in Schemas
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
pub struct MetadataSchema {
/// Connection name
pub name: String,

/// Language ID for the connections. Essentially just R or python
pub language_id: String,

/// Connection host
pub host: Option<String>,

/// Connection type
#[serde(rename = "type")]
pub metadata_schema_type: Option<String>,

/// Code used to re-create the connection
pub code: Option<String>
}

/// Parameters for the ListObjects method.
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
pub struct ListObjectsParams {
Expand Down Expand Up @@ -66,6 +86,13 @@ pub struct PreviewObjectParams {
pub path: Vec<ObjectSchema>,
}

/// Parameters for the GetMetadata method.
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
pub struct GetMetadataParams {
/// The comm_id of the client we want to retrieve metdata for.
pub comm_id: String,
}

/**
* Backend RPC request types for the connections comm
*/
Expand Down Expand Up @@ -103,6 +130,12 @@ pub enum ConnectionsBackendRequest {
#[serde(rename = "preview_object")]
PreviewObject(PreviewObjectParams),

/// Gets metadata from the connections
///
/// A connection has tied metadata such as an icon, the host, etc.
#[serde(rename = "get_metadata")]
GetMetadata(GetMetadataParams),

}

/**
Expand All @@ -125,6 +158,8 @@ pub enum ConnectionsBackendReply {

PreviewObjectReply(),

GetMetadataReply(MetadataSchema),

}

/**
Expand Down
29 changes: 29 additions & 0 deletions crates/ark/src/connections/r_connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,25 @@
// Copyright (C) 2023 by Posit Software, PBC
//

use std::collections::HashMap;

use amalthea::comm::comm_channel::CommMsg;
use amalthea::comm::connections_comm::ConnectionsBackendReply;
use amalthea::comm::connections_comm::ConnectionsBackendRequest;
use amalthea::comm::connections_comm::ConnectionsFrontendEvent;
use amalthea::comm::connections_comm::ContainsDataParams;
use amalthea::comm::connections_comm::FieldSchema;
use amalthea::comm::connections_comm::GetIconParams;
use amalthea::comm::connections_comm::GetMetadataParams;
use amalthea::comm::connections_comm::ListFieldsParams;
use amalthea::comm::connections_comm::ListObjectsParams;
use amalthea::comm::connections_comm::MetadataSchema;
use amalthea::comm::connections_comm::ObjectSchema;
use amalthea::comm::connections_comm::PreviewObjectParams;
use amalthea::comm::event::CommManagerEvent;
use amalthea::socket::comm::CommInitiator;
use amalthea::socket::comm::CommSocket;
use anyhow::anyhow;
use crossbeam::channel::Sender;
use harp::exec::RFunction;
use harp::exec::RFunctionExt;
Expand All @@ -32,6 +37,7 @@ use stdext::unwrap;
use uuid::Uuid;

use crate::interface::RMain;
use crate::modules::ARK_ENVS;
use crate::r_task;

#[derive(Deserialize, Serialize, Clone)]
Expand Down Expand Up @@ -216,6 +222,29 @@ impl RConnection {
})?;
Ok(ConnectionsBackendReply::ContainsDataReply(contains_data))
},
ConnectionsBackendRequest::GetMetadata(GetMetadataParams { comm_id }) => {
let metadata = r_task(|| -> Result<_, anyhow::Error> {
let r_metadata: HashMap<String, String> =
RFunction::new("", ".ps.connection_metadata")
.add(comm_id)
.call_in(ARK_ENVS.positron_ns)?
.try_into()?;

let schema = MetadataSchema {
language_id: String::from("r"),
name: r_metadata
.get("displayName")
.ok_or(anyhow!("Need a display name"))?
.clone(),
host: r_metadata.get("host").cloned(),
metadata_schema_type: r_metadata.get("type").cloned(),
code: r_metadata.get("connectionCode").cloned(),
};

Ok(schema)
})?;
Ok(ConnectionsBackendReply::GetMetadataReply(metadata))
},
}
}

Expand Down
15 changes: 15 additions & 0 deletions crates/ark/src/modules/positron/connection.R
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,21 @@ connection_flatten_object_types <- function(object_tree) {
identical(object_types$contains, "data")
}

.ps.connection_metadata <- function(id) {
con <- get(id, getOption("connectionObserver")$.connections)

if (is.null(con)) {
stop("Expected a valid comm_id")
}

list(
type = con$type,
host = con$host,
displayName = con$displayName,
connectCode = con$connectCode
)
}

.ps.register_dummy_connection <- function() {
# This is used for testing the connections service
observer <- getOption("connectionObserver")
Expand Down

0 comments on commit 92d91b5

Please sign in to comment.