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
#![allow(dead_code)]

use crate::{
    data::Data,
    model::{algorithm::Algorithm, App},
    observability::tracing::create_tracer_from_env,
    routes::{graphql_playground, health},
};
use async_graphql_poem::GraphQL;
use poem::{get, listener::TcpListener, middleware::Cors, EndpointExt, Route, Server};
use raphtory::{
    db::graph::{edge::EdgeView, vertex::VertexView},
    prelude::Graph,
    vectors::{vectorizable::Vectorizable, Embedding},
};
use std::{collections::HashMap, future::Future, ops::Deref, path::Path};
use tokio::{io::Result as IoResult, signal};
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt, EnvFilter, Registry};

pub struct RaphtoryServer {
    data: Data,
}

impl RaphtoryServer {
    pub fn from_map(graphs: HashMap<String, Graph>) -> Self {
        let data = Data::from_map(graphs);
        Self { data }
    }

    pub fn from_directory(graph_directory: &str) -> Self {
        let data = Data::from_directory(graph_directory);
        Self { data }
    }

    pub fn from_map_and_directory(graphs: HashMap<String, Graph>, graph_directory: &str) -> Self {
        let data = Data::from_map_and_directory(graphs, graph_directory);
        Self { data }
    }

    pub async fn with_vectorized<F, U, N, E>(
        self,
        graph_names: Vec<String>,
        embedding: F,
        cache_dir: &Path,
        templates: Option<(N, E)>,
    ) -> Self
    where
        F: Fn(Vec<String>) -> U + Send + Sync + Copy + 'static,
        U: Future<Output = Vec<Embedding>> + Send + 'static,
        N: Fn(&VertexView<Graph>) -> String + Sync + Send + Copy + 'static,
        E: Fn(&EdgeView<Graph>) -> String + Sync + Send + Copy + 'static,
    {
        {
            let graphs_map = self.data.graphs.read();
            let mut stores_map = self.data.vector_stores.write();

            for graph_name in graph_names {
                let graph_cache = cache_dir.join(&graph_name);
                let graph = graphs_map.get(&graph_name).unwrap().deref().clone();

                println!("Loading embeddings for {graph_name} using cache from {graph_cache:?}");
                let vectorized = match templates {
                    Some((node_template, edge_template)) => {
                        graph
                            .vectorize_with_templates(
                                Box::new(embedding),
                                &graph_cache,
                                node_template,
                                edge_template,
                            )
                            .await
                    }
                    None => graph.vectorize(Box::new(embedding), &graph_cache).await,
                };
                stores_map.insert(graph_name, vectorized);
            }
        }

        println!("Embeddings were loaded successfully");

        self
    }

    pub fn register_algorithm<T: Algorithm>(self, name: &str) -> Self {
        crate::model::algorithm::PLUGIN_ALGOS
            .lock()
            .unwrap()
            .insert(name.to_string(), T::register_algo);
        self
    }

    pub async fn run(self) -> IoResult<()> {
        self.run_with_port(1736).await
    }

    pub async fn run_with_port(self, port: u16) -> IoResult<()> {
        let registry = Registry::default().with(tracing_subscriber::fmt::layer().pretty());
        let env_filter = EnvFilter::try_from_default_env().unwrap_or(EnvFilter::new("INFO"));

        match create_tracer_from_env() {
            Some(tracer) => registry
                .with(tracing_opentelemetry::layer().with_tracer(tracer))
                .with(env_filter)
                .try_init(),
            None => registry.with(env_filter).try_init(),
        }
        .unwrap_or(());

        // it is important that this runs after algorithms have been pushed to PLUGIN_ALGOS static variable
        let schema_builder = App::create_schema();
        let schema_builder = schema_builder.data(self.data);
        let schema = schema_builder.finish().unwrap();
        let app = Route::new()
            .at("/", get(graphql_playground).post(GraphQL::new(schema)))
            .at("/health", get(health))
            .with(Cors::new());

        println!("Playground: http://localhost:{port}");
        Server::new(TcpListener::bind(format!("0.0.0.0:{port}")))
            .run_with_graceful_shutdown(app, shutdown_signal(), None)
            .await
    }
}

async fn shutdown_signal() {
    let ctrl_c = async {
        signal::ctrl_c()
            .await
            .expect("failed to install Ctrl+C handler");
    };

    #[cfg(unix)]
    let terminate = async {
        signal::unix::signal(signal::unix::SignalKind::terminate())
            .expect("failed to install signal handler")
            .recv()
            .await;
    };

    #[cfg(not(unix))]
    let terminate = std::future::pending::<()>();

    tokio::select! {
        _ = ctrl_c => {},
        _ = terminate => {},
    }

    opentelemetry::global::shutdown_tracer_provider();
}