|
| 1 | +use std::{ |
| 2 | + collections::{BTreeMap, BTreeSet}, |
| 3 | + io::{Cursor, Read}, |
| 4 | +}; |
| 5 | + |
| 6 | +use foxglove_data_loader::{ |
| 7 | + DataLoader, DataLoaderArgs, Initialization, Message, MessageIterator, MessageIteratorArgs, |
| 8 | + reader::{self}, |
| 9 | +}; |
| 10 | + |
| 11 | +use anyhow::bail; |
| 12 | +use csv::StringRecord; |
| 13 | +use serde_json::json; |
| 14 | + |
| 15 | +#[derive(Default)] |
| 16 | +struct CsvDataLoader { |
| 17 | + path: String, |
| 18 | + /// Index of timestamp to byte offset |
| 19 | + indexes: BTreeMap<u64, u64>, |
| 20 | + /// The index of the field containing timestamp |
| 21 | + log_time_index: usize, |
| 22 | + /// The keys from the first row of the CSV |
| 23 | + keys: Vec<String>, |
| 24 | +} |
| 25 | + |
| 26 | +impl DataLoader for CsvDataLoader { |
| 27 | + type MessageIterator = CsvMessageIterator; |
| 28 | + type Error = anyhow::Error; |
| 29 | + |
| 30 | + fn new(args: DataLoaderArgs) -> Self { |
| 31 | + let DataLoaderArgs { mut paths } = args; |
| 32 | + assert_eq!( |
| 33 | + paths.len(), |
| 34 | + 1, |
| 35 | + "data loader is configured to only get one file" |
| 36 | + ); |
| 37 | + Self { |
| 38 | + path: paths.remove(0), |
| 39 | + ..Default::default() |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + fn initialize(&mut self) -> Result<Initialization, Self::Error> { |
| 44 | + let mut reader = csv::ReaderBuilder::new() |
| 45 | + .has_headers(true) |
| 46 | + .from_reader(reader::open(&self.path)); |
| 47 | + |
| 48 | + // Read the headers of the CSV and store them on the loader. |
| 49 | + // We will turn each column into a topic so the CSV needs to have a header. |
| 50 | + let headers = reader.headers()?; |
| 51 | + self.keys = headers.iter().map(String::from).collect(); |
| 52 | + |
| 53 | + // Read through the keys and try to find a field called "timestamp_nanos". If this doesn't |
| 54 | + // exit then we can't read the file as we have no way of knowing the log time. |
| 55 | + let Some(log_time_index) = self.keys.iter().position(|k| k == "timestamp_nanos") else { |
| 56 | + bail!("expected csv to contain column called timestamp_nanos") |
| 57 | + }; |
| 58 | + |
| 59 | + // Store the column index of the timestamp to be used for the log time. |
| 60 | + self.log_time_index = log_time_index; |
| 61 | + |
| 62 | + let mut record = StringRecord::new(); |
| 63 | + let mut position = reader.position().byte(); |
| 64 | + |
| 65 | + // Read the entire file to build up an index of timestamps to byte position. |
| 66 | + // Later on we'll use this index to make sure we can immediately start reading from the |
| 67 | + // correct place. This will take a little bit of time when the file loads for the first |
| 68 | + // time, but it will mean playback is snappy later on. |
| 69 | + while reader.read_record(&mut record)? { |
| 70 | + let timestamp_nanos: u64 = record[log_time_index].parse()?; |
| 71 | + self.indexes.insert(timestamp_nanos, position); |
| 72 | + position = reader.position().byte(); |
| 73 | + } |
| 74 | + |
| 75 | + let mut builder = Initialization::builder() |
| 76 | + .start_time( |
| 77 | + self.indexes |
| 78 | + .first_key_value() |
| 79 | + .map(|(timestamp, _)| *timestamp) |
| 80 | + .unwrap_or(0), |
| 81 | + ) |
| 82 | + .end_time( |
| 83 | + self.indexes |
| 84 | + .last_key_value() |
| 85 | + .map(|(timestamp, _)| *timestamp) |
| 86 | + .unwrap_or(0), |
| 87 | + ); |
| 88 | + |
| 89 | + for (i, key) in self.keys.iter().enumerate() { |
| 90 | + // Don't add a channel for the column used for log time |
| 91 | + if i == self.log_time_index { |
| 92 | + continue; |
| 93 | + } |
| 94 | + |
| 95 | + builder |
| 96 | + .add_channel_with_id(i as _, &format!("/{key}")) |
| 97 | + .expect("channel is free") |
| 98 | + .message_encoding("json") |
| 99 | + .message_count(self.indexes.len() as _); |
| 100 | + } |
| 101 | + |
| 102 | + Ok(builder.build()) |
| 103 | + } |
| 104 | + |
| 105 | + fn create_iter( |
| 106 | + &mut self, |
| 107 | + args: MessageIteratorArgs, |
| 108 | + ) -> Result<Self::MessageIterator, Self::Error> { |
| 109 | + let requested_channel_id = args.channels.into_iter().collect(); |
| 110 | + |
| 111 | + match self.indexes.range(args.start_time.unwrap_or(0)..).next() { |
| 112 | + Some((_, byte_offset)) => { |
| 113 | + let reader = reader::open(&self.path); |
| 114 | + reader.seek(*byte_offset); |
| 115 | + |
| 116 | + Ok(CsvMessageIterator { |
| 117 | + row_to_flush: Default::default(), |
| 118 | + log_time_index: self.log_time_index, |
| 119 | + requested_channel_id, |
| 120 | + reader: csv::Reader::from_reader(Box::new(reader)), |
| 121 | + }) |
| 122 | + } |
| 123 | + // If there is no byte offset (we've gone past the last timestamp), return empty iter |
| 124 | + None => Ok(CsvMessageIterator { |
| 125 | + log_time_index: self.log_time_index, |
| 126 | + row_to_flush: Default::default(), |
| 127 | + requested_channel_id: Default::default(), |
| 128 | + reader: csv::Reader::from_reader(Box::new(Cursor::new([]))), |
| 129 | + }), |
| 130 | + } |
| 131 | + } |
| 132 | +} |
| 133 | + |
| 134 | +struct CsvMessageIterator { |
| 135 | + row_to_flush: Vec<Message>, |
| 136 | + log_time_index: usize, |
| 137 | + requested_channel_id: BTreeSet<u16>, |
| 138 | + reader: csv::Reader<Box<dyn Read>>, |
| 139 | +} |
| 140 | + |
| 141 | +/// Try and coerce the string into a JSON value. |
| 142 | +/// |
| 143 | +/// Try to convert to a f64, then bool, else finally return a string. |
| 144 | +fn to_json_value(value: &str) -> serde_json::Value { |
| 145 | + if let Ok(v) = value.parse::<f64>() { |
| 146 | + return json!(v); |
| 147 | + } |
| 148 | + |
| 149 | + if let Ok(v) = value.parse::<bool>() { |
| 150 | + return json!(v); |
| 151 | + } |
| 152 | + |
| 153 | + json!(value) |
| 154 | +} |
| 155 | + |
| 156 | +impl MessageIterator for CsvMessageIterator { |
| 157 | + type Error = anyhow::Error; |
| 158 | + |
| 159 | + fn next(&mut self) -> Option<Result<Message, Self::Error>> { |
| 160 | + loop { |
| 161 | + // We emit each column of a row as its own message. |
| 162 | + if let Some(message) = self.row_to_flush.pop() { |
| 163 | + return Some(Ok(message)); |
| 164 | + } |
| 165 | + |
| 166 | + let mut columns = StringRecord::new(); |
| 167 | + |
| 168 | + match self.reader.read_record(&mut columns) { |
| 169 | + Err(e) => { |
| 170 | + return Some(Err(e.into())); |
| 171 | + } |
| 172 | + Ok(false) => { |
| 173 | + return None; |
| 174 | + } |
| 175 | + // fall through |
| 176 | + Ok(true) => {} |
| 177 | + } |
| 178 | + |
| 179 | + // Get the log time for the row. This will need to be on every message. |
| 180 | + let timestamp = match columns[self.log_time_index].parse::<u64>() { |
| 181 | + Ok(t) => t, |
| 182 | + Err(e) => { |
| 183 | + return Some(Err(e.into())); |
| 184 | + } |
| 185 | + }; |
| 186 | + |
| 187 | + for (index, cell) in columns.iter().enumerate() { |
| 188 | + // Don't emit the timestamp column as a message |
| 189 | + if index == self.log_time_index { |
| 190 | + continue; |
| 191 | + } |
| 192 | + |
| 193 | + let channel_id = index as u16; |
| 194 | + |
| 195 | + // If this column wasn't requested, skip it |
| 196 | + if !self.requested_channel_id.contains(&channel_id) { |
| 197 | + continue; |
| 198 | + } |
| 199 | + |
| 200 | + let data = serde_json::to_vec(&json!({ "value": to_json_value(cell) })) |
| 201 | + .expect("json will not fail to serialize"); |
| 202 | + |
| 203 | + // Add this message to the row and continue onto the next column |
| 204 | + self.row_to_flush.push(Message { |
| 205 | + channel_id, |
| 206 | + log_time: timestamp, |
| 207 | + publish_time: timestamp, |
| 208 | + data, |
| 209 | + }); |
| 210 | + } |
| 211 | + } |
| 212 | + } |
| 213 | +} |
| 214 | + |
| 215 | +foxglove_data_loader::export!(CsvDataLoader); |
0 commit comments