Skip to content
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

cdm timeseries dog-4175 #261

Merged
merged 22 commits into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion cognite/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ authors = [
edition = "2021"
name = "cognite"
publish = false
version = "0.3.1"
version = "0.3.0"

[features]
default = ["rustls-022"]
Expand Down
12 changes: 6 additions & 6 deletions cognite/examples/instances/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use cognite::{
models::{
data_models::CogniteExtractorFile, instances::NodeOrEdgeSpecification, ItemId,
RetrieveExtendedCollection, UpsertExtendedCollection,
instances::{CogniteExtractorFile, NodeOrEdgeSpecification},
ItemId,
},
CogniteClient, DeleteWithResponse,
};
Expand All @@ -16,8 +16,8 @@ async fn main() {
let col = CogniteExtractorFile::new(space.to_string(), external_id, name);
let res = client
.models
.files
.upsert(vec![col], None, None, None, None, None)
.instances
.apply(vec![col], None, None, None, None, None)
.await
.unwrap();
let external_id = match res.first().unwrap() {
Expand All @@ -34,8 +34,8 @@ async fn main() {
});
let res: Vec<CogniteExtractorFile> = client
.models
.files
.retrieve(vec![node_specs.clone()])
.instances
.fetch(&[node_specs.clone()], None)
.await
.unwrap();
println!("{res:#?}");
Expand Down
15 changes: 6 additions & 9 deletions cognite/src/api/data_modeling.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
pub(crate) mod containers;
pub(crate) mod data_models;
pub(crate) mod instances;
pub(crate) mod resource;
// pub(crate) mod resource;
pub(crate) mod spaces;
pub(crate) mod views;

use std::sync::Arc;

pub use resource::files::FilesResource;
pub use resource::{
DataModelsResource as ExtendedDataModelsResource, RetrieveExtendedCollection,
UpsertExtendedCollection, WithInstanceApiResource, WithView,
};
// pub use resource::{files::FilesResource, timeseries::TimeseriesResource};
// pub use resource::{
// DataModelsResource as ExtendedDataModelsResource, RetrieveExtendedCollection, WithView,
// UpsertExtendedCollection, WithInstanceApiResource
// };

use crate::api::data_modeling::{instances::Instances, views::ViewsResource};
use crate::ApiClient;
Expand All @@ -32,8 +32,6 @@ pub struct Models {
pub data_models: DataModelsResource,
/// Data modeling containers.
pub containers: ContainersResource,
/// Custom data modeling instance (nodes and edges)
pub files: FilesResource,
}

impl Models {
Expand All @@ -44,7 +42,6 @@ impl Models {
spaces: SpacesResource::new(api_client.clone()),
data_models: DataModelsResource::new(api_client.clone()),
containers: ContainersResource::new(api_client.clone()),
files: FilesResource::new(Arc::new(Instances::new(api_client))),
}
}
}
82 changes: 80 additions & 2 deletions cognite/src/api/data_modeling/instances.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ use crate::dto::data_modeling::instances::SlimNodeOrEdge;
use crate::models::instances::{
AggregateInstancesRequest, AggregateInstancesResponse, FilterInstancesRequest,
InstancesFilterResponse, NodeAndEdgeCreateCollection, NodeAndEdgeRetrieveRequest,
NodeAndEdgeRetrieveResponse, NodeOrEdge, NodeOrEdgeSpecification, QueryInstancesRequest,
QueryInstancesResponse, SearchInstancesRequest,
NodeAndEdgeRetrieveResponse, NodeOrEdge, NodeOrEdgeCreate, NodeOrEdgeSpecification,
QueryInstancesRequest, QueryInstancesResponse, SearchInstancesRequest, SourceReferenceInternal,
};
use crate::models::instances::{FromReadable, IntoWritable, WithView};
use crate::models::views::ViewReference;
use crate::Result;
use crate::{DeleteWithResponse, FilterWithRequest, RetrieveWithRequest, UpsertCollection};
use crate::{Resource, WithBasePath};
Expand Down Expand Up @@ -107,4 +109,80 @@ impl Instances {
.post(&format!("{}/search", Self::BASE_PATH), &req)
.await
}

/// Fetch special data models instance collection.
///
/// # Arguments
///
/// * `items` - A list of specifications of node/edges to retrieve.
pub async fn fetch<TEntity, TProperties>(
&self,
items: &[NodeOrEdgeSpecification],
view: Option<&ViewReference>,
) -> Result<Vec<TEntity>>
where
TProperties: Serialize + DeserializeOwned + Send + Sync,
TEntity: FromReadable<TProperties> + WithView + Send,
{
let response: NodeAndEdgeRetrieveResponse<TProperties> = self
.retrieve(&NodeAndEdgeRetrieveRequest {
sources: Some(vec![SourceReferenceInternal {
source: view
.unwrap_or(&ViewReference {
space: TEntity::SPACE.to_owned(),
external_id: TEntity::EXTERNAL_ID.to_owned(),
version: TEntity::VERSION.to_owned(),
})
.to_owned()
.into(),
}]),
items: items.to_vec(),
include_typing: None,
})
.await?;
response
.items
.into_iter()
.map(|item| TEntity::try_from_readable(item, view))
.collect()
}

/// Upsert data models instances of this type.
///
/// # Arguments
///
/// * `col` - A list of this type to be created.
/// * `auto_create_direct_relation` - Whether to auto create direct relation that do no exist.
/// * `auto_create_start_nodes` - Whether to auto create end nodes that do not exist.
/// * `auto_create_end_nodes` - Whether to auto create end nodes that do not exist.
/// * `skip_on_version_conflict` - Whether to skip when a version conflic is encountered.
/// * `replace` - Whether to replace all matching and existing values with the supplied values.
pub async fn apply<TEntity, TProperties>(
&self,
col: Vec<TEntity>,
auto_create_direct_relations: Option<bool>,
auto_create_start_nodes: Option<bool>,
auto_create_end_nodes: Option<bool>,
skip_on_version_conflict: Option<bool>,
replace: Option<bool>,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally, I find that Option<bool> should be avoided. In this case it's better if users just have to pick one, either true or false, leaving it as None as no special semantic meaning beyond "use the default", so if users want to leave them as None they have to look up what that means, which means they might as well just set a value.

) -> Result<Vec<SlimNodeOrEdge>>
where
TProperties: Serialize + DeserializeOwned + Send + Sync,
TEntity: IntoWritable<TProperties> + Send,
{
let collection: Vec<NodeOrEdgeCreate<TProperties>> = col
.into_iter()
.map(|t| t.try_into_writable())
.collect::<Result<Vec<NodeOrEdgeCreate<_>>>>()?;

let collection = NodeAndEdgeCreateCollection {
items: collection,
auto_create_direct_relations: auto_create_direct_relations.or(Some(true)),
auto_create_start_nodes,
auto_create_end_nodes,
skip_on_version_conflict,
replace,
};
self.upsert(&collection).await
}
}
150 changes: 0 additions & 150 deletions cognite/src/api/data_modeling/resource.rs

This file was deleted.

27 changes: 0 additions & 27 deletions cognite/src/api/data_modeling/resource/files.rs

This file was deleted.

6 changes: 0 additions & 6 deletions cognite/src/dto/data_modeling/data_models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,6 @@ use crate::{
to_query, IntoParams, SetCursor,
};

mod extensions;
pub use extensions::{
files::{CogniteExtractorFile, FileObject},
FromReadable, IntoWritable,
};

#[skip_serializing_none]
#[derive(Serialize, Deserialize, Clone, Debug, Default)]
#[serde(rename_all = "camelCase")]
Expand Down
Loading
Loading