-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
177 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,3 +23,4 @@ prisma.rs | |
# protobuf | ||
serverdata.rs | ||
command_data.rs | ||
playback_data.rs |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
scylla-server/src/controllers/file_insertion_controller.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
use axum::extract::{Multipart, State}; | ||
use axum_macros::debug_handler; | ||
use chrono::DateTime; | ||
use protobuf::CodedInputStream; | ||
use rangemap::RangeInclusiveMap; | ||
use tokio_util::bytes::Buf; | ||
use tracing::{debug, info, trace}; | ||
|
||
use crate::{ | ||
error::ScyllaError, | ||
playback_data, | ||
services::{data_service, run_service}, | ||
ClientData, PoolHandle, | ||
}; | ||
|
||
// super cool: adding this tag tells you what variable is misbehaving in cases of axum Send+Sync Handler fails | ||
#[debug_handler] | ||
pub async fn insert_file( | ||
State(pool): State<PoolHandle>, | ||
mut multipart: Multipart, | ||
) -> Result<String, ScyllaError> { | ||
// create a run ID cache | ||
let mut db = pool.get()?; | ||
debug!("Warming up run ID map!"); | ||
let mut run_iter = run_service::get_all_runs(&mut db) | ||
.await? | ||
.into_iter() | ||
.map(|f| (f.id, f.time.timestamp_micros() as u64)) | ||
.peekable(); | ||
let mut run_rng: RangeInclusiveMap<u64, i32> = RangeInclusiveMap::new(); | ||
// this actual formulates the list, where keys are ranges of times (us) and the values are the run IDs | ||
while let Some(it) = run_iter.next() { | ||
match run_iter.peek() { | ||
Some(next) => { | ||
run_rng.insert(it.1..=next.1, it.0); | ||
} | ||
// if this is the last item in the list | ||
None => { | ||
run_rng.insert(it.1..=std::u64::MAX, it.0); | ||
continue; | ||
} | ||
} | ||
} | ||
|
||
// iterate through all files | ||
while let Some(field) = multipart.next_field().await.unwrap() { | ||
// round up all of the protobuf segments as a giant list | ||
let mut data = field.bytes().await.unwrap().reader(); | ||
let mut insertable_data: Vec<playback_data::PlaybackData> = vec![]; | ||
{ | ||
// this cannot be used across an await, hence scoped | ||
let mut stream = CodedInputStream::new(&mut data); | ||
loop { | ||
match stream.read_message::<playback_data::PlaybackData>() { | ||
Ok(a) => { | ||
trace!("Decoded file msg: {}", a); | ||
insertable_data.push(a); | ||
} | ||
Err(e) => { | ||
trace!("Exiting from read loop {}", e); | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
|
||
debug!("Mapping data to ClientData type, with inferred run IDs!"); | ||
let new_data: Vec<ClientData> = insertable_data | ||
.into_iter() | ||
.map(|f| match run_rng.get(&f.time_us) { | ||
Some(a) => ClientData { | ||
run_id: *a, | ||
name: f.topic.clone(), | ||
unit: f.unit, | ||
values: f.values, | ||
timestamp: DateTime::from_timestamp_micros(f.time_us as i64).unwrap(), | ||
node: f.topic.split_once('/').unwrap_or_default().0.to_owned(), | ||
}, | ||
None => ClientData { | ||
run_id: -1, | ||
name: f.topic.clone(), | ||
unit: f.unit, | ||
values: f.values, | ||
timestamp: DateTime::from_timestamp_micros(f.time_us as i64).unwrap(), | ||
node: f.topic.split_once('/').unwrap_or_default().0.to_owned(), | ||
}, | ||
}) | ||
.collect(); | ||
info!( | ||
"Inserting {} points, {} of which have no run ID (-1).", | ||
new_data.len(), | ||
new_data | ||
.iter() | ||
.filter(|x| x.run_id == -1) | ||
.collect::<Vec<_>>() | ||
.len() | ||
); | ||
data_service::add_many(&mut db, new_data).await?; | ||
} | ||
Ok("Successfully inserted all!".to_string()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
// @generated | ||
|
||
pub mod command_data; | ||
pub mod playback_data; | ||
pub mod serverdata; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
syntax = "proto3"; | ||
|
||
package playbackdata.v1; | ||
|
||
message PlaybackData { | ||
string topic = 1; | ||
string unit = 2; | ||
// time since unix epoch in MICROSECONDS | ||
uint64 time_us = 3; | ||
repeated float values = 4; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters