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
use std::{
    collections::{HashMap, HashSet},
    ops::Deref,
};

use crate::model::{
    algorithm::Algorithms,
    filters::{edgefilter::EdgeFilter, nodefilter::NodeFilter},
    graph::{edge::Edge, get_expanded_edges, node::Node, property::Property},
    schema::graph_schema::GraphSchema,
};
use dynamic_graphql::{ResolvedObject, ResolvedObjectFields};
use itertools::Itertools;
use raphtory::{
    db::{
        api::view::{
            internal::{DynamicGraph, IntoDynamic},
            GraphViewOps, TimeOps, VertexViewOps,
        },
        graph::edge::EdgeView,
    },
    prelude::EdgeViewOps,
    search::IndexedGraph,
};

#[derive(ResolvedObject)]
pub(crate) struct GraphMeta {
    name: String,
    graph: DynamicGraph,
}

impl GraphMeta {
    pub fn new(name: String, graph: DynamicGraph) -> Self {
        Self { name, graph }
    }
}

#[ResolvedObjectFields]
impl GraphMeta {
    async fn name(&self) -> String {
        self.name.clone()
    }

    async fn static_properties(&self) -> Vec<Property> {
        self.graph
            .properties()
            .constant()
            .into_iter()
            .map(|(k, v)| Property::new(k, v))
            .collect()
    }

    async fn node_names(&self) -> Vec<String> {
        self.graph
            .vertices()
            .into_iter()
            .map(|v| v.name())
            .collect_vec()
    }
}

#[derive(ResolvedObject)]
pub(crate) struct GqlGraph {
    graph: IndexedGraph<DynamicGraph>,
}

impl<G: GraphViewOps + IntoDynamic> From<G> for GqlGraph {
    fn from(value: G) -> Self {
        Self {
            graph: value.into_dynamic().into(),
        }
    }
}

impl GqlGraph {
    pub(crate) fn new(graph: IndexedGraph<DynamicGraph>) -> Self {
        Self { graph }
    }
}

#[ResolvedObjectFields]
impl GqlGraph {
    async fn window(&self, t_start: i64, t_end: i64) -> GqlGraph {
        let w = self.graph.window(t_start, t_end);
        w.into()
    }

    async fn layer_names(&self) -> Vec<String> {
        self.graph.get_unique_layers()
    }

    async fn static_properties(&self) -> Vec<Property> {
        self.graph
            .properties()
            .constant()
            .into_iter()
            .map(|(k, v)| Property::new(k, v))
            .collect()
    }

    async fn nodes(&self, filter: Option<NodeFilter>) -> Vec<Node> {
        match filter {
            Some(filter) => self
                .graph
                .vertices()
                .iter()
                .map(|vv| vv.into())
                .filter(|n| filter.matches(n))
                .collect(),
            None => self.graph.vertices().iter().map(|vv| vv.into()).collect(),
        }
    }

    /// Returns the schema of this graph
    async fn schema(&self) -> GraphSchema {
        GraphSchema::new(&self.graph)
    }

    async fn search(&self, query: String, limit: usize, offset: usize) -> Vec<Node> {
        self.graph
            .search(&query, limit, offset)
            .into_iter()
            .flat_map(|vv| vv)
            .map(|vv| vv.into())
            .collect()
    }

    async fn search_edges(&self, query: String, limit: usize, offset: usize) -> Vec<Edge> {
        self.graph
            .search_edges(&query, limit, offset)
            .into_iter()
            .flat_map(|vv| vv)
            .map(|vv| vv.into())
            .collect()
    }

    async fn edges<'a>(&self, filter: Option<EdgeFilter>) -> Vec<Edge> {
        match filter {
            Some(filter) => self
                .graph
                .edges()
                .into_iter()
                .map(|ev| ev.into())
                .filter(|ev| filter.matches(ev))
                .collect(),
            None => self.graph.edges().into_iter().map(|ev| ev.into()).collect(),
        }
    }

    async fn expanded_edges(
        &self,
        nodes_to_expand: Vec<String>,
        graph_nodes: Vec<String>,
        filter: Option<EdgeFilter>,
    ) -> Vec<Edge> {
        if nodes_to_expand.is_empty() {
            return vec![];
        }

        let nodes: Vec<Node> = self
            .graph
            .vertices()
            .iter()
            .map(|vv| vv.into())
            .filter(|n| NodeFilter::new(nodes_to_expand.clone()).matches(n))
            .collect();

        let mut all_graph_nodes: HashSet<String> = graph_nodes.into_iter().collect();
        let mut all_expanded_edges: HashMap<String, EdgeView<DynamicGraph>> = HashMap::new();

        let mut maybe_layers: Option<Vec<String>> = None;
        if filter.is_some() {
            maybe_layers = filter.clone().unwrap().layer_names.map(|l| l.contains);
        }

        for node in nodes {
            let expanded_edges =
                get_expanded_edges(all_graph_nodes.clone(), node.vv, maybe_layers.clone());
            expanded_edges.clone().into_iter().for_each(|e| {
                let src = e.src().name();
                let dst = e.dst().name();
                all_expanded_edges.insert(src.to_owned() + &dst, e);
                all_graph_nodes.insert(src);
                all_graph_nodes.insert(dst);
            });
        }

        let fetched_edges = all_expanded_edges
            .values()
            .map(|ee| ee.clone().into())
            .collect_vec();

        match filter {
            Some(filter) => fetched_edges
                .into_iter()
                .filter(|ev| filter.matches(ev))
                .collect(),
            None => fetched_edges,
        }
    }

    async fn node(&self, name: String) -> Option<Node> {
        self.graph
            .vertices()
            .iter()
            .find(|vv| &vv.name() == &name)
            .map(|vv| vv.into())
    }

    async fn node_id(&self, id: u64) -> Option<Node> {
        self.graph
            .vertices()
            .iter()
            .find(|vv| vv.id() == id)
            .map(|vv| vv.into())
    }

    async fn algorithms(&self) -> Algorithms {
        self.graph.deref().clone().into()
    }
}