1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
use crate::python::client::{
build_property_string, build_query, raphtory_client::PyRaphtoryClient,
};
use minijinja::context;
use pyo3::{pyclass, pymethods, Python};
use raphtory::{
core::{
utils::{errors::GraphError, time::IntoTime},
Prop,
},
python::utils::PyTime,
};
use std::collections::HashMap;
#[derive(Clone)]
#[pyclass(name = "RemoteNode")]
pub struct PyRemoteNode {
pub(crate) path: String,
pub(crate) client: PyRaphtoryClient,
pub(crate) id: String,
}
#[pymethods]
impl PyRemoteNode {
#[new]
pub(crate) fn new(path: String, client: PyRaphtoryClient, id: String) -> Self {
Self { path, client, id }
}
/// Set the type on the node. This only works if the type has not been previously set, otherwise will
/// throw an error
///
/// Parameters:
/// new_type (str): The new type to be set
///
/// Returns:
/// Result: A result object indicating success or failure.
pub fn set_node_type(&self, py: Python, new_type: &str) -> Result<(), GraphError> {
let template = r#"
{
updateGraph(path: "{{path}}") {
node(name: "{{name}}") {
setNodeType(newType: "{{new_type}}")
}
}
}
"#;
let query_context = context! {
path => self.path,
name => self.id,
new_type => new_type
};
let query = build_query(template, query_context)?;
let _ = &self.client.query(py, query, None)?;
Ok(())
}
/// Add updates to a node in the remote graph at a specified time.
/// This function allows for the addition of property updates to a node within the graph. The updates are time-stamped, meaning they are applied at the specified time.
///
/// Parameters:
/// t (int | str | DateTime): The timestamp at which the updates should be applied.
/// properties (Dict[str, Prop]): A dictionary of properties to update.
pub fn add_updates(
&self,
py: Python,
t: PyTime,
properties: Option<HashMap<String, Prop>>,
) -> Result<(), GraphError> {
let template = r#"
{
updateGraph(path: "{{path}}") {
node(name: "{{name}}") {
addUpdates(time: {{t}} {% if properties is not none %}, properties: {{ properties | safe }} {% endif %})
}
}
}
"#;
let query_context = context! {
path => self.path,
name => self.id,
t => t.into_time(),
properties => properties.map(|p| build_property_string(p)),
};
let query = build_query(template, query_context)?;
let _ = &self.client.query(py, query, None)?;
Ok(())
}
/// Add constant properties to a node in the remote graph.
/// This function is used to add properties to a node that remain constant and does not
/// change over time. These properties are fundamental attributes of the node.
///
/// Parameters:
/// properties (Dict[str, Prop]): A dictionary of properties to be added to the node.
pub fn add_constant_properties(
&self,
py: Python,
properties: HashMap<String, Prop>,
) -> Result<(), GraphError> {
let template = r#"
{
updateGraph(path: "{{path}}") {
node(name: "{{name}}") {
addConstantProperties(properties: {{ properties | safe }} )
}
}
}
"#;
let query_context = context! {
path => self.path,
name => self.id,
properties => build_property_string(properties),
};
let query = build_query(template, query_context)?;
let _ = &self.client.query(py, query, None)?;
Ok(())
}
/// Update constant properties of a node in the remote graph overwriting existing values.
/// This function is used to add properties to a node that remain constant and do not
/// change over time. These properties are fundamental attributes of the node.
///
/// Parameters:
/// properties (Dict[str, Prop]): A dictionary of properties to be added to the node.
pub fn update_constant_properties(
&self,
py: Python,
properties: HashMap<String, Prop>,
) -> Result<(), GraphError> {
let template = r#"
{
updateGraph(path: "{{path}}") {
node(name: "{{name}}") {
updateConstantProperties(properties: {{ properties | safe }} )
}
}
}
"#;
let query_context = context! {
path => self.path,
name => self.id,
properties => build_property_string(properties)
};
let query = build_query(template, query_context)?;
let _ = &self.client.query(py, query, None)?;
Ok(())
}
}