re_data_source/
data_source.rs

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
use re_grpc_client::message_proxy;
use re_log_types::LogMsg;
use re_smart_channel::{Receiver, SmartChannelSource, SmartMessageSource};

use crate::FileContents;

#[cfg(not(target_arch = "wasm32"))]
use anyhow::Context as _;

/// Somewhere we can get Rerun data from.
#[derive(Clone, Debug)]
pub enum DataSource {
    /// A remote RRD file, served over http.
    ///
    /// If `follow` is `true`, the viewer will open the stream in `Following` mode rather than `Playing` mode.
    ///
    /// Could be either an `.rrd` recording or a `.rbl` blueprint.
    RrdHttpUrl { uri: String, follow: bool },

    /// A path to a local file.
    #[cfg(not(target_arch = "wasm32"))]
    FilePath(re_log_types::FileSource, std::path::PathBuf),

    /// The contents of a file.
    ///
    /// This is what you get when loading a file on Web, or when using drag-n-drop.
    FileContents(re_log_types::FileSource, FileContents),

    // RRD data streaming in from standard input.
    #[cfg(not(target_arch = "wasm32"))]
    Stdin,

    /// A `rerun://` URI pointing to a recording or catalog.
    RerunGrpcStream(re_uri::RedapUri),
}

// TODO(#9058): Temporary hack, see issue for how to fix this.
pub enum StreamSource {
    LogMessages(Receiver<LogMsg>),
    CatalogUri(re_uri::CatalogUri),
    EntryUri(re_uri::EntryUri),
}

impl DataSource {
    /// Tried to classify a URI into a [`DataSource`].
    ///
    /// Tries to figure out if it looks like a local path,
    /// a web-socket address, or a http url.
    #[cfg_attr(target_arch = "wasm32", allow(clippy::needless_pass_by_value))]
    pub fn from_uri(_file_source: re_log_types::FileSource, uri: String) -> Self {
        #[cfg(not(target_arch = "wasm32"))]
        {
            use itertools::Itertools as _;

            fn looks_like_windows_abs_path(path: &str) -> bool {
                let path = path.as_bytes();
                // "C:/" etc
                path.get(1).copied() == Some(b':') && path.get(2).copied() == Some(b'/')
            }

            fn looks_like_a_file_path(uri: &str) -> bool {
                // How do we distinguish a file path from a web url? "example.zip" could be either.

                if uri.starts_with('/') {
                    return true; // Unix absolute path
                }
                if looks_like_windows_abs_path(uri) {
                    return true;
                }

                // We use a simple heuristic here: if there are multiple dots, it is likely an url,
                // like "example.com/foo.zip".
                // If there is only one dot, we treat it as an extension and look it up in a list of common
                // file extensions:

                let parts = uri.split('.').collect_vec();
                if parts.len() == 2 {
                    // Extension or `.com` etc?
                    re_data_loader::is_supported_file_extension(parts[1])
                } else {
                    false // Too many dots; assume an url
                }
            }

            // Reading from standard input in non-TTY environments (e.g. GitHub Actions, but I'm sure we can
            // come up with more convoluted than that…) can lead to many unexpected,
            // platform-specific problems that aren't even necessarily consistent across runs.
            //
            // In order to avoid having to swallow errors based on unreliable heuristics (or inversely:
            // throwing errors when we shouldn't), we just make reading from standard input explicit.
            if uri == "-" {
                return Self::Stdin;
            }

            let path = std::path::Path::new(&uri).to_path_buf();

            if uri.starts_with("file://") || path.exists() {
                return Self::FilePath(_file_source, path);
            }

            if looks_like_a_file_path(&uri) {
                return Self::FilePath(_file_source, path);
            }
        }

        if let Ok(uri) = uri.as_str().parse::<re_uri::RedapUri>() {
            return Self::RerunGrpcStream(uri);
        }

        // by default, we just assume an rrd over http
        Self::RrdHttpUrl { uri, follow: false }
    }

    pub fn file_name(&self) -> Option<String> {
        match self {
            Self::RrdHttpUrl { uri: url, .. } => url.split('/').last().map(|r| r.to_owned()),
            #[cfg(not(target_arch = "wasm32"))]
            Self::FilePath(_, path) => path.file_name().map(|s| s.to_string_lossy().to_string()),
            Self::FileContents(_, file_contents) => Some(file_contents.name.clone()),
            #[cfg(not(target_arch = "wasm32"))]
            Self::Stdin => None,
            Self::RerunGrpcStream { .. } => None,
        }
    }

    pub fn is_blueprint(&self) -> Option<bool> {
        self.file_name().map(|name| name.ends_with(".rbl"))
    }

    /// Stream the data from the given data source.
    ///
    /// Will do minimal checks (e.g. that the file exists), for synchronous errors,
    /// but the loading is done in a background task.
    ///
    /// `on_cmd` is used to respond to UI commands.
    ///
    /// `on_msg` can be used to wake up the UI thread on Wasm.
    pub fn stream(
        self,
        on_cmd: Box<dyn Fn(DataSourceCommand) + Send + Sync>,
        on_msg: Option<Box<dyn Fn() + Send + Sync>>,
    ) -> anyhow::Result<StreamSource> {
        re_tracing::profile_function!();

        match self {
            Self::RrdHttpUrl { uri: url, follow } => Ok(StreamSource::LogMessages(
                re_log_encoding::stream_rrd_from_http::stream_rrd_from_http_to_channel(
                    url, follow, on_msg,
                ),
            )),

            #[cfg(not(target_arch = "wasm32"))]
            Self::FilePath(file_source, path) => {
                let (tx, rx) = re_smart_channel::smart_channel(
                    SmartMessageSource::File(path.clone()),
                    SmartChannelSource::File(path.clone()),
                );

                // This `StoreId` will be communicated to all `DataLoader`s, which may or may not
                // decide to use it depending on whether they want to share a common recording
                // or not.
                let shared_store_id =
                    re_log_types::StoreId::random(re_log_types::StoreKind::Recording);
                let settings = re_data_loader::DataLoaderSettings {
                    opened_application_id: file_source.recommended_application_id().cloned(),
                    opened_store_id: file_source.recommended_recording_id().cloned(),
                    force_store_info: file_source.force_store_info(),
                    ..re_data_loader::DataLoaderSettings::recommended(shared_store_id)
                };
                re_data_loader::load_from_path(&settings, file_source, &path, &tx)
                    .with_context(|| format!("{path:?}"))?;

                if let Some(on_msg) = on_msg {
                    on_msg();
                }

                Ok(StreamSource::LogMessages(rx))
            }

            // When loading a file on Web, or when using drag-n-drop.
            Self::FileContents(file_source, file_contents) => {
                let name = file_contents.name.clone();
                let (tx, rx) = re_smart_channel::smart_channel(
                    SmartMessageSource::File(name.clone().into()),
                    SmartChannelSource::File(name.clone().into()),
                );

                // This `StoreId` will be communicated to all `DataLoader`s, which may or may not
                // decide to use it depending on whether they want to share a common recording
                // or not.
                let shared_store_id =
                    re_log_types::StoreId::random(re_log_types::StoreKind::Recording);
                let settings = re_data_loader::DataLoaderSettings {
                    opened_application_id: file_source.recommended_application_id().cloned(),
                    opened_store_id: file_source.recommended_recording_id().cloned(),
                    force_store_info: file_source.force_store_info(),
                    ..re_data_loader::DataLoaderSettings::recommended(shared_store_id)
                };
                re_data_loader::load_from_file_contents(
                    &settings,
                    file_source,
                    &std::path::PathBuf::from(file_contents.name),
                    std::borrow::Cow::Borrowed(&file_contents.bytes),
                    &tx,
                )?;

                if let Some(on_msg) = on_msg {
                    on_msg();
                }

                Ok(StreamSource::LogMessages(rx))
            }

            #[cfg(not(target_arch = "wasm32"))]
            Self::Stdin => {
                let (tx, rx) = re_smart_channel::smart_channel(
                    SmartMessageSource::Stdin,
                    SmartChannelSource::Stdin,
                );

                crate::load_stdin::load_stdin(tx).with_context(|| "stdin".to_owned())?;

                if let Some(on_msg) = on_msg {
                    on_msg();
                }

                Ok(StreamSource::LogMessages(rx))
            }

            Self::RerunGrpcStream(re_uri::RedapUri::DatasetData(uri)) => {
                let (tx, rx) = re_smart_channel::smart_channel(
                    re_smart_channel::SmartMessageSource::RedapGrpcStream(uri.clone()),
                    re_smart_channel::SmartChannelSource::RedapGrpcStream(uri.clone()),
                );

                let on_cmd = Box::new(move |cmd: re_grpc_client::redap::Command| match cmd {
                    re_grpc_client::redap::Command::SetLoopSelection {
                        recording_id,
                        timeline,
                        time_range,
                    } => on_cmd(DataSourceCommand::SetLoopSelection {
                        recording_id,
                        timeline,
                        time_range,
                    }),
                });

                spawn_future(async move {
                    if let Err(err) = re_grpc_client::redap::stream_partition_async(
                        tx,
                        uri.clone(),
                        on_cmd,
                        on_msg,
                    )
                    .await
                    {
                        re_log::warn!(
                            "Error while streaming {uri}: {}",
                            re_error::format_ref(&err)
                        );
                    }
                });
                Ok(StreamSource::LogMessages(rx))
            }

            Self::RerunGrpcStream(re_uri::RedapUri::Catalog(uri)) => {
                Ok(StreamSource::CatalogUri(uri))
            }

            Self::RerunGrpcStream(re_uri::RedapUri::Entry(uri)) => Ok(StreamSource::EntryUri(uri)),

            Self::RerunGrpcStream(re_uri::RedapUri::Proxy(uri)) => Ok(StreamSource::LogMessages(
                message_proxy::stream(uri, on_msg),
            )),
        }
    }
}

pub enum DataSourceCommand {
    SetLoopSelection {
        recording_id: re_log_types::StoreId,
        timeline: re_log_types::Timeline,
        time_range: re_log_types::ResolvedTimeRangeF,
    },
}

// TODO(ab, andreas): This should be replaced by the use of `AsyncRuntimeHandle`. However, this
// requires:
// - `AsyncRuntimeHandle` to be moved lower in the crate hierarchy to be available here (unsure
//   where).
// - Make sure that all callers of `DataSource::stream` have access to an `AsyncRuntimeHandle`
//   (maybe it should be in `GlobalContext`?).
#[cfg(target_arch = "wasm32")]
fn spawn_future<F>(future: F)
where
    F: std::future::Future<Output = ()> + 'static,
{
    wasm_bindgen_futures::spawn_local(future);
}

#[cfg(not(target_arch = "wasm32"))]
fn spawn_future<F>(future: F)
where
    F: std::future::Future<Output = ()> + 'static + Send,
{
    tokio::spawn(future);
}

#[cfg(not(target_arch = "wasm32"))]
#[test]
fn test_data_source_from_uri() {
    use re_log_types::FileSource;

    let mut failed = false;

    let file = [
        "file://foo",
        "foo.rrd",
        "foo.png",
        "/foo/bar/baz",
        "D:/file",
    ];
    let http = [
        "example.zip/foo.rrd",
        "www.foo.zip/foo.rrd",
        "www.foo.zip/blueprint.rbl",
    ];
    let grpc = [
        "rerun://foo.zip",
        "rerun+http://foo.zip",
        "rerun+https://foo.zip",
        "rerun://127.0.0.1:9876",
        "rerun+http://127.0.0.1:9876",
        "rerun://redap.rerun.io",
        "rerun+https://redap.rerun.io",
    ];

    let file_source = FileSource::DragAndDrop {
        recommended_application_id: None,
        recommended_recording_id: None,
        force_store_info: false,
    };

    for uri in file {
        if !matches!(
            DataSource::from_uri(file_source.clone(), uri.to_owned()),
            DataSource::FilePath { .. }
        ) {
            eprintln!("Expected {uri:?} to be categorized as FilePath");
            failed = true;
        }
    }

    for uri in http {
        if !matches!(
            DataSource::from_uri(file_source.clone(), uri.to_owned()),
            DataSource::RrdHttpUrl { .. }
        ) {
            eprintln!("Expected {uri:?} to be categorized as RrdHttpUrl");
            failed = true;
        }
    }

    for uri in grpc {
        if !matches!(
            DataSource::from_uri(file_source.clone(), uri.to_owned()),
            DataSource::RerunGrpcStream { .. }
        ) {
            eprintln!("Expected {uri:?} to be categorized as MessageProxy");
            failed = true;
        }
    }

    assert!(!failed, "one or more test cases failed");
}