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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
// src/graph/data_retrieval.rs
use crate::datatypes::values::{format_value, Value};
use crate::graph::schema::{CurrentSelection, DirGraph, NodeInfo};
use crate::graph::storage::GraphRead;
use petgraph::graph::NodeIndex;
use std::borrow::Cow;
use std::collections::HashMap;
#[derive(Debug)]
pub struct LevelNodes {
pub parent_title: String,
pub parent_id: Option<Value>,
pub parent_idx: Option<NodeIndex>,
pub parent_type: Option<String>,
pub nodes: Vec<NodeInfo>,
}
#[derive(Debug)]
pub struct LevelValues {
pub parent_title: String,
pub values: Vec<Vec<Value>>,
}
pub fn get_nodes(
graph: &DirGraph,
selection: &CurrentSelection,
level_index: Option<usize>,
indices: Option<&[usize]>,
max_nodes: Option<usize>,
) -> Vec<LevelNodes> {
// If specific indices are provided, do direct lookup
if let Some(idx) = indices {
let mut direct_nodes = Vec::new();
for &index in idx {
if let Some(node_idx) = NodeIndex::new(index).into() {
if let Some(node) = graph.get_node(node_idx) {
let node_info = node.to_node_info(&graph.interner);
direct_nodes.push(node_info);
if let Some(max) = max_nodes {
if direct_nodes.len() >= max {
break;
}
}
}
}
}
if !direct_nodes.is_empty() {
return vec![LevelNodes {
parent_title: "Direct Lookup".to_string(),
parent_id: None,
parent_idx: None,
parent_type: None,
nodes: direct_nodes,
}];
}
return Vec::new();
}
// Check if selection is effectively empty (no nodes selected)
// Note: CurrentSelection always has at least one level, so we check if the level is empty
let selection_is_empty = if selection.get_level_count() > 0 {
let level_idx = selection.get_level_count().saturating_sub(1);
selection
.get_level(level_idx)
.map(|l| l.node_count() == 0)
.unwrap_or(true)
} else {
true
};
// Check if any query operations have been applied (type_filter, filter, traverse, etc.)
let has_query_operations = !selection.get_execution_plan().is_empty();
// If selection is empty AND no query operations were applied, return all regular nodes.
// If selection is empty BUT query operations were applied (e.g., filter matched 0 nodes),
// return empty to respect the query result.
if selection_is_empty && !has_query_operations {
let mut all_nodes = Vec::new();
for node_idx in GraphRead::node_indices(&graph.graph) {
if let Some(node) = graph.get_node(node_idx) {
let node_info = node.to_node_info(&graph.interner);
all_nodes.push(node_info);
if let Some(max) = max_nodes {
if all_nodes.len() >= max {
break;
}
}
}
}
if !all_nodes.is_empty() {
return vec![LevelNodes {
parent_title: "Root".to_string(),
parent_id: None,
parent_idx: None,
parent_type: None,
nodes: all_nodes,
}];
}
return Vec::new();
}
let level_idx = level_index.unwrap_or_else(|| selection.get_level_count().saturating_sub(1));
let mut result = Vec::new();
if let Some(level) = selection.get_level(level_idx) {
for (parent, children) in level.iter_groups() {
let mut nodes = Vec::new();
for &child_idx in children {
if let Some(node) = graph.get_node(child_idx) {
let node_info = node.to_node_info(&graph.interner);
nodes.push(node_info);
if let Some(max) = max_nodes {
if nodes.len() >= max {
break;
}
}
}
}
// Always create an entry for the parent, even if nodes is empty
let (parent_title, parent_id, parent_type) = match parent {
Some(p) => {
if let Some(node) = graph.get_node(*p) {
(
node.get_field_ref("title")
.as_deref()
.and_then(|v| match v {
Value::String(s) => Some(s.clone()),
_ => None,
})
.unwrap_or_else(|| "Unknown".to_string()),
node.get_field_ref("id").map(Cow::into_owned),
Some(node.get_node_type_ref(&graph.interner).to_string()),
)
} else {
("Unknown".to_string(), None, None)
}
}
None => ("Root".to_string(), None, None),
};
result.push(LevelNodes {
parent_title,
parent_id,
parent_idx: parent.map(|p| p),
parent_type,
nodes,
});
}
}
result
}
pub fn get_property_values(
graph: &DirGraph,
selection: &CurrentSelection,
level_index: Option<usize>,
properties: &[&str],
indices: Option<&[usize]>,
max_nodes: Option<usize>,
) -> Vec<LevelValues> {
let level_idx = level_index.unwrap_or_else(|| selection.get_level_count().saturating_sub(1));
let mut result = Vec::new();
if let Some(level) = selection.get_level(level_idx) {
for (parent, children) in level.iter_groups() {
let filtered_children: Vec<NodeIndex> = match indices {
Some(idx) => children
.iter()
.filter(|&c| idx.contains(&c.index()))
.take(max_nodes.unwrap_or(usize::MAX))
.cloned()
.collect(),
None => children
.iter()
.take(max_nodes.unwrap_or(usize::MAX))
.cloned()
.collect(),
};
// Always create values vector, even if empty
let values: Vec<Vec<Value>> = filtered_children
.iter()
.map(|&idx| {
properties
.iter()
.map(|&prop| {
graph
.get_node(idx)
.and_then(|node| node.get_field_ref(prop))
.map(Cow::into_owned)
.unwrap_or(Value::Null)
})
.collect()
})
.collect();
// Get parent title even if there are no children
let parent_title = match parent {
Some(p) => {
if let Some(node) = graph.get_node(*p) {
if let Some(Value::String(title)) = node.get_field_ref("title").as_deref() {
title.clone()
} else {
"Unknown".to_string()
}
} else {
"Unknown".to_string()
}
}
None => "Root".to_string(),
};
// Always add to result, even with empty values
result.push(LevelValues {
parent_title,
values,
});
}
}
result
}
#[derive(Debug)]
pub struct UniqueValues {
pub parent_title: String,
pub parent_idx: Option<NodeIndex>,
pub values: Vec<Value>,
}
pub fn get_unique_values(
graph: &DirGraph,
selection: &CurrentSelection,
property: &str,
level_index: Option<usize>,
group_by_parent: bool,
indices: Option<&[usize]>,
) -> Vec<UniqueValues> {
let level_idx = level_index.unwrap_or_else(|| selection.get_level_count().saturating_sub(1));
let mut result = Vec::new();
if let Some(level) = selection.get_level(level_idx) {
if group_by_parent {
for (parent, children) in level.iter_groups() {
let filtered_children: Vec<NodeIndex> = match indices {
Some(idx) => children
.iter()
.filter(|&c| idx.contains(&c.index()))
.cloned()
.collect(),
None => children.clone(),
};
let mut unique_values = std::collections::HashSet::new();
for &idx in &filtered_children {
if let Some(node) = graph.get_node(idx) {
if let Some(value) = node.get_field_ref(property) {
unique_values.insert(value.into_owned());
}
}
}
let parent_title = match parent {
Some(p) => {
if let Some(node) = graph.get_node(*p) {
if let Some(Value::String(title)) =
node.get_field_ref("title").as_deref()
{
title.clone()
} else {
"Unknown".to_string()
}
} else {
"Unknown".to_string()
}
}
None => "Root".to_string(),
};
result.push(UniqueValues {
parent_title,
parent_idx: parent.map(|p| p),
values: unique_values.into_iter().collect(),
});
}
} else {
let mut all_unique_values = std::collections::HashSet::new();
for (_, children) in level.iter_groups() {
let filtered_children: Vec<NodeIndex> = match indices {
Some(idx) => children
.iter()
.filter(|&c| idx.contains(&c.index()))
.cloned()
.collect(),
None => children.clone(),
};
for &idx in &filtered_children {
if let Some(node) = graph.get_node(idx) {
if let Some(value) = node.get_field_ref(property) {
all_unique_values.insert(value.into_owned());
}
}
}
}
result.push(UniqueValues {
parent_title: "All".to_string(),
parent_idx: None,
values: all_unique_values.into_iter().collect(),
});
}
}
result
}
pub fn format_unique_values_for_storage(
values: &[UniqueValues],
max_length: Option<usize>,
) -> Vec<(Option<NodeIndex>, Value)> {
values
.iter()
.map(|unique_values| {
let mut value_list: Vec<String> = unique_values
.values
.iter()
.map(|v| {
// Get formatted value
let formatted = format_value(v);
// Remove quotes from strings (if present)
match v {
Value::String(_) => {
// The format_value function wraps strings in quotes
// We need to remove the opening and closing quotes
if formatted.starts_with('"') && formatted.ends_with('"') {
formatted[1..formatted.len() - 1].to_string()
} else {
formatted
}
}
_ => formatted,
}
})
.collect::<Vec<String>>();
value_list.sort();
value_list.dedup();
if let Some(max_len) = max_length {
if value_list.len() > max_len {
println!(
"Warning: Truncating value list from {} to {} items for parent: {}",
value_list.len(),
max_len,
unique_values.parent_title
);
value_list.truncate(max_len);
}
}
// Join with comma and space
(
unique_values.parent_idx,
Value::String(value_list.join(", ")),
)
})
.collect()
}
#[derive(Debug)]
#[allow(clippy::type_complexity)]
pub struct ConnectionInfo {
pub node_id: Value,
pub node_title: String,
pub node_type: String,
pub incoming: Vec<(
String,
Value,
Value,
HashMap<String, Value>,
Option<HashMap<String, Value>>,
)>, // (type, id, title, conn_props, node_props)
pub outgoing: Vec<(
String,
Value,
Value,
HashMap<String, Value>,
Option<HashMap<String, Value>>,
)>, // (type, id, title, conn_props, node_props)
}
#[derive(Debug)]
pub struct LevelConnections {
pub parent_title: String,
pub parent_id: Option<Value>,
pub parent_idx: Option<NodeIndex>,
pub parent_type: Option<String>,
pub connections: Vec<ConnectionInfo>,
}
pub fn get_connections(
graph: &DirGraph,
selection: &CurrentSelection,
level_index: Option<usize>,
indices: Option<&[usize]>,
include_node_properties: bool,
) -> Vec<LevelConnections> {
let level_idx = level_index.unwrap_or_else(|| selection.get_level_count().saturating_sub(1));
let mut result = Vec::new();
if let Some(level) = selection.get_level(level_idx) {
// Handle direct lookup if indices provided
let nodes = if let Some(idx) = indices {
idx.iter()
.filter_map(|&i| NodeIndex::new(i).into())
.collect::<Vec<_>>()
} else {
level.get_all_nodes()
};
// If using direct indices, create a single level
let groups = if indices.is_some() {
vec![(None, nodes)]
} else {
level.iter_groups().map(|(p, c)| (*p, c.clone())).collect()
};
for (parent, children) in groups {
let mut level_connections = Vec::new();
for node_idx in children {
if let Some(node) = graph.get_node(node_idx) {
let node_title = node.title();
let title_str = match &*node_title {
Value::String(s) => s.clone(),
_ => "Unknown".to_string(),
};
let mut incoming = Vec::new();
let mut outgoing = Vec::new();
// Collect incoming connections
for edge_ref in graph
.graph
.edges_directed(node_idx, petgraph::Direction::Incoming)
{
if let Some(source_node) = graph.get_node(edge_ref.source()) {
let edge_data = edge_ref.weight();
let node_props = if include_node_properties {
Some(source_node.properties_cloned(&graph.interner))
} else {
None
};
incoming.push((
edge_data.connection_type_str(&graph.interner).to_string(),
source_node
.get_field_ref("id")
.map(Cow::into_owned)
.unwrap_or(Value::Null),
source_node
.get_field_ref("title")
.map(Cow::into_owned)
.unwrap_or(Value::Null),
edge_data.properties_cloned(&graph.interner),
node_props,
));
}
}
// Collect outgoing connections
for edge_ref in graph
.graph
.edges_directed(node_idx, petgraph::Direction::Outgoing)
{
if let Some(target_node) = graph.get_node(edge_ref.target()) {
let edge_data = edge_ref.weight();
let node_props = if include_node_properties {
Some(target_node.properties_cloned(&graph.interner))
} else {
None
};
outgoing.push((
edge_data.connection_type_str(&graph.interner).to_string(),
target_node
.get_field_ref("id")
.map(Cow::into_owned)
.unwrap_or(Value::Null),
target_node
.get_field_ref("title")
.map(Cow::into_owned)
.unwrap_or(Value::Null),
edge_data.properties_cloned(&graph.interner),
node_props,
));
}
}
if !incoming.is_empty() || !outgoing.is_empty() {
level_connections.push(ConnectionInfo {
node_id: node.id().into_owned(),
node_title: title_str,
node_type: node.node_type_str(&graph.interner).to_string(),
incoming,
outgoing,
});
}
}
}
// Rest of the function remains the same
let (parent_title, parent_id, parent_type) = if indices.is_some() {
("Direct Lookup".to_string(), None, None)
} else {
match parent {
Some(p) => {
if let Some(node) = graph.get_node(p) {
(
node.get_field_ref("title")
.as_deref()
.and_then(|v| match v {
Value::String(s) => Some(s.clone()),
_ => None,
})
.unwrap_or_else(|| "Unknown".to_string()),
node.get_field_ref("id").map(Cow::into_owned),
Some(node.get_node_type_ref(&graph.interner).to_string()),
)
} else {
("Unknown".to_string(), None, None)
}
}
None => ("Root".to_string(), None, None),
}
};
result.push(LevelConnections {
parent_title,
parent_id,
parent_idx: parent,
parent_type,
connections: level_connections,
});
}
}
result
}