-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Sensapp in rust work in progress. This may change a lot.
- Loading branch information
1 parent
cb8d608
commit 05e1f9a
Showing
31 changed files
with
1,921 additions
and
7 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
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,8 +1,36 @@ | ||
[package] | ||
name = "sensapp" | ||
version = "0.1.0" | ||
version = "0.2.0" | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
anyhow = "1.0" | ||
#async-stream = "0.3" | ||
async-trait = "0.1" | ||
axum = { version = "0.7" } | ||
#axum-streams = { version = "0.12", features = ["json", "csv", "text"] } | ||
#bytes = "1.5" | ||
futures = "0.3" | ||
#futures-util = { version = "0.3", features = ["io"] } | ||
#http-body = "1.0" | ||
#http-body-util = "0.1" | ||
polars = { version = "0.36" } | ||
sqlx = { version = "0.7", features = ["runtime-tokio", "sqlite"] } | ||
tokio = { version = "1.35", features = ["full"] } | ||
tokio-stream = { version = "0.1", features = ["io-util"] } | ||
tokio-util = "0.7" | ||
tower = { version = "0.4", features = ["full"] } | ||
tower-http = { version = "0.5", features = ["full"] } | ||
tracing = { version = "0.1" } | ||
tracing-subscriber = { version = "0.3", features = ["env-filter"] } | ||
uuid = "1.6" | ||
csv-async = "1.2" | ||
rust_decimal = "1.33.1" | ||
geo = "0.27.0" | ||
async-broadcast = "0.6.0" | ||
cached = { version = "0.47.0", features = ["async", "tokio", "async-trait"] } | ||
nom = "7.1" | ||
sindit-senml = "0.2.0" | ||
serde_json = "1.0" |
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,5 @@ | ||
fn main() { | ||
// trigger recompilation when a new migration is added | ||
println!("cargo:rerun-if-changed=src/storage/postgresql/migrations"); | ||
println!("cargo:rerun-if-changed=src/storage/sqlite/migrations"); | ||
} |
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 |
---|---|---|
@@ -0,0 +1,73 @@ | ||
use super::message::{Message, PublishMessage}; | ||
use crate::datamodel::batch::Batch; | ||
use anyhow::Result; | ||
use std::sync::Arc; | ||
|
||
#[derive(Debug)] | ||
pub struct EventBus { | ||
pub name: String, | ||
//main_bus_sender: mpsc::Sender<u8>, | ||
//main_bus_receiver: mpsc::Receiver<u8>, | ||
//pub main_bus_sender: async_channel::Sender<u8>, | ||
//pub main_bus_receiver: async_channel::Receiver<u8>, | ||
//pub main_bus_sender: tokio::sync::broadcast::Sender<u8>, | ||
//pub main_bus_receiver: tokio::sync::broadcast::Receiver<u8>, | ||
pub main_bus_sender: async_broadcast::Sender<Message>, | ||
pub main_bus_receiver: async_broadcast::InactiveReceiver<Message>, | ||
} | ||
|
||
impl EventBus { | ||
// Create a new event bus. | ||
// Please note that the receiver is inactive by default as it may be cloned many times. | ||
// Consider using .activate() or .activate_cloned() to activate it. | ||
pub fn init(name: String) -> Self { | ||
// let (tx, rx) = mpsc::channel(10); | ||
//let (s, _) = tokio::sync::broadcast::channel::<u8>(1000); | ||
//let (s, r) = async_broadcast::broadcast(128); | ||
let (s, r) = async_broadcast::broadcast(128); | ||
let r = r.deactivate(); | ||
Self { | ||
name, | ||
main_bus_sender: s, | ||
main_bus_receiver: r, | ||
} | ||
} | ||
|
||
async fn broadcast(&self, message: Message) -> Result<()> { | ||
//self.main_bus_sender.send(event).await?; | ||
//self.main_bus_sender.send(event)?; | ||
self.main_bus_sender.broadcast(message).await?; | ||
Ok(()) | ||
} | ||
|
||
pub async fn publish(&self, batch: Batch) -> Result<async_broadcast::InactiveReceiver<()>> { | ||
// We create a new broadcast channel to receive the sync message. | ||
// It can technically have multiple emitters and multiple receivers. | ||
// In most cases, it should be a one to one relationship, but | ||
// it could be possible to have multiple storage backends and a single | ||
// receiver that waits for the first one to sync, or all. | ||
let (sync_sender, sync_receiver) = async_broadcast::broadcast(1); | ||
let sync_receiver = sync_receiver.deactivate(); | ||
|
||
self.broadcast(Message::Publish(PublishMessage { | ||
batch: Arc::new(batch), | ||
sync_sender, | ||
sync_receiver: sync_receiver.clone(), | ||
})) | ||
.await?; | ||
|
||
Ok(sync_receiver) | ||
} | ||
|
||
// receive | ||
/*pub async fn receive_one(&mut self) -> Result<u8> { | ||
self.main_bus_receiver | ||
.recv() | ||
.await | ||
.map_err(|e| anyhow::anyhow!("Failed to receive event: {}", e)) | ||
}*/ | ||
} | ||
|
||
pub fn init_event_bus() -> Arc<EventBus> { | ||
Arc::new(EventBus::init("SensApp".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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
use std::sync::Arc; | ||
|
||
#[derive(Debug, Clone)] | ||
pub enum Message { | ||
Publish(PublishMessage), | ||
} | ||
|
||
#[derive(Debug, Clone)] | ||
pub struct PublishMessage { | ||
pub batch: Arc<crate::datamodel::batch::Batch>, | ||
// A request sync message is sent to ask the storage backends | ||
// to sync. This is done to ensure that the data is persisted. | ||
// | ||
// However, some storage backends many support syncing, or may lie about | ||
// syncing. This is also true for some storage hardware nowadays. | ||
pub sync_sender: async_broadcast::Sender<()>, | ||
pub sync_receiver: async_broadcast::InactiveReceiver<()>, | ||
} |
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,4 @@ | ||
pub mod event_bus; | ||
pub mod message; | ||
pub mod utils; | ||
pub use event_bus::EventBus; |
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,57 @@ | ||
use std::sync::Arc; | ||
|
||
use anyhow::Result; | ||
use async_broadcast::{InactiveReceiver, Receiver, Sender}; | ||
use tokio::sync::Mutex; | ||
|
||
#[derive(Debug)] | ||
pub struct WaitForAll { | ||
nb_started: Arc<Mutex<usize>>, | ||
nb_finished: Arc<Mutex<usize>>, | ||
finished_sender: Sender<()>, | ||
finished_receiver: InactiveReceiver<()>, | ||
} | ||
|
||
impl WaitForAll { | ||
pub fn new() -> Self { | ||
let (s, r) = async_broadcast::broadcast(1); | ||
Self { | ||
nb_started: Arc::new(Mutex::new(0)), | ||
nb_finished: Arc::new(Mutex::new(0)), | ||
finished_sender: s, | ||
finished_receiver: r.deactivate(), | ||
} | ||
} | ||
|
||
pub async fn add(&mut self, mut receiver: Receiver<()>) { | ||
{ | ||
let mut nb_started = self.nb_started.lock().await; | ||
*nb_started += 1; | ||
} | ||
let nb_finished_clone = self.nb_finished.clone(); | ||
let finished_sender_clone = self.finished_sender.clone(); | ||
|
||
tokio::spawn(async move { | ||
let _ = receiver.recv().await; | ||
{ | ||
let mut nb_finished = nb_finished_clone.lock().await; | ||
*nb_finished += 1; | ||
} | ||
if !finished_sender_clone.is_closed() && finished_sender_clone.receiver_count() > 0 { | ||
let _ = finished_sender_clone.broadcast(()).await; | ||
} | ||
}); | ||
} | ||
|
||
pub async fn wait(&mut self) -> Result<()> { | ||
// If already finished, return immediately | ||
if *self.nb_started.lock().await == *self.nb_finished.lock().await { | ||
return Ok(()); | ||
} | ||
|
||
let mut receiver = self.finished_receiver.activate_cloned(); | ||
receiver.recv().await?; | ||
|
||
Ok(()) | ||
} | ||
} |
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,25 @@ | ||
use std::sync::Arc; | ||
|
||
#[derive(Debug)] | ||
pub struct Sample<V> { | ||
pub timestamp_ms: i64, | ||
pub value: V, | ||
} | ||
|
||
#[derive(Debug)] | ||
pub enum TypedSamples { | ||
Integer(Vec<Sample<i64>>), | ||
Numeric(Vec<Sample<rust_decimal::Decimal>>), | ||
Float(Vec<Sample<f64>>), | ||
String(Vec<Sample<String>>), | ||
Boolean(Vec<Sample<bool>>), | ||
Location(Vec<Sample<geo::Point>>), | ||
Blob(Vec<Sample<Vec<u8>>>), | ||
} | ||
|
||
#[derive(Debug)] | ||
pub struct Batch { | ||
pub sensor_uuid: uuid::Uuid, | ||
pub sensor_name: String, | ||
pub samples: Arc<TypedSamples>, | ||
} |
Oops, something went wrong.