Skip to content

Commit d0c67db

Browse files
WIP: Boosted ride stat events over websocket
1 parent 7b018e4 commit d0c67db

File tree

8 files changed

+97
-10
lines changed

8 files changed

+97
-10
lines changed

src/connectivity/rabbit.rs

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@ use lapin::{
55
BasicProperties, Channel, Connection, ConnectionProperties, Queue,
66
};
77
use serde::{Deserialize, Serialize};
8-
use serde_json::json;
8+
use serde_json::{json, Value};
99

10-
use crate::{config::Config, structs::spotify::CurrentPlaying};
10+
use crate::{
11+
config::Config,
12+
structs::{boosted::BoostedRideUpdate, spotify::CurrentPlaying},
13+
};
1114

1215
#[derive(Clone)]
1316
pub struct RabbitManager {
@@ -16,9 +19,9 @@ pub struct RabbitManager {
1619
}
1720

1821
#[derive(Debug, Serialize, Deserialize, Clone)]
19-
struct SpotifySocketData {
22+
struct RabbitEventsData<Data = Value> {
2023
t: u8,
21-
d: CurrentPlaying,
24+
d: Data,
2225
}
2326

2427
impl RabbitManager {
@@ -50,8 +53,28 @@ impl RabbitManager {
5053
Self { channel, queue }
5154
}
5255

56+
pub async fn publish_ride_state(&mut self, data: &BoostedRideUpdate) {
57+
let message = RabbitEventsData {
58+
t: 0,
59+
d: data.to_owned(),
60+
};
61+
let json = serde_json::to_string(&message).unwrap();
62+
63+
self
64+
.channel
65+
.basic_publish(
66+
"",
67+
"dstn-gateway-ingest",
68+
BasicPublishOptions::default(),
69+
json.as_bytes(),
70+
BasicProperties::default(),
71+
)
72+
.await
73+
.unwrap();
74+
}
75+
5376
pub async fn publish_spotify_current(&mut self, data: &CurrentPlaying) {
54-
let message = SpotifySocketData {
77+
let message = RabbitEventsData {
5578
t: 0,
5679
d: data.to_owned(),
5780
};

src/helpers/boosted.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
use envconfig::Envconfig;
2+
use redis::aio::ConnectionManager;
3+
4+
use crate::{
5+
config::Config,
6+
connectivity::{rabbit::RabbitManager, valkey::ValkeyManager},
7+
services::boosted::structs::BoostedStats,
8+
structs::boosted::BoostedRideUpdate,
9+
};
10+
11+
pub async fn send_boosted_event(
12+
valkey: &mut ValkeyManager,
13+
rabbit: &mut RabbitManager,
14+
) {
15+
let config = Config::init_from_env().unwrap();
16+
17+
let in_ride = redis::cmd("GET")
18+
.arg("boosted/in-ride")
19+
.query_async::<ConnectionManager, String>(&mut valkey.cm)
20+
.await
21+
.unwrap_or(String::from("false"))
22+
== "true";
23+
24+
let client = reqwest::Client::new();
25+
let res = client
26+
.get(format!("{}/v1/users/stats", config.boosted_api_endpoint))
27+
.header("Authorization", config.boosted_api_token)
28+
.send()
29+
.await
30+
.unwrap();
31+
32+
let json = res.json::<BoostedStats>().await.unwrap();
33+
34+
let state = BoostedRideUpdate {
35+
riding: in_ride,
36+
latest_ride: json.latest_ride,
37+
stats: json.stats,
38+
};
39+
40+
rabbit.publish_ride_state(&state).await;
41+
}

src/helpers/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pub mod boosted;

src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
pub mod config;
22
pub mod connectivity;
3+
pub mod helpers;
34
pub mod modules;
45
pub mod services;
56
pub mod structs;

src/services/boosted/structs.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,33 @@
11
use serde::{Deserialize, Serialize};
22

3-
#[derive(Serialize, Deserialize)]
3+
#[derive(Serialize, Deserialize, Debug)]
44
pub struct BoostedStats {
55
pub latest_ride: RideStats,
66
pub stats: Stats,
77
}
88

9-
#[derive(Serialize, Deserialize)]
9+
#[derive(Serialize, Deserialize, Debug)]
1010
pub struct RideStats {
1111
pub started_at: String,
1212
pub ended_at: String,
1313
pub duration: f64,
1414
pub distance: f64,
1515
}
1616

17-
#[derive(Serialize, Deserialize)]
17+
#[derive(Serialize, Deserialize, Debug)]
1818
pub struct Stats {
1919
pub boards: Boards,
2020
pub rides: ValueEntry,
2121
pub duration: ValueEntry,
2222
pub distance: ValueEntry,
2323
}
2424

25-
#[derive(Serialize, Deserialize)]
25+
#[derive(Serialize, Deserialize, Debug)]
2626
pub struct Boards {
2727
pub distance: f64,
2828
}
2929

30-
#[derive(Serialize, Deserialize)]
30+
#[derive(Serialize, Deserialize, Debug)]
3131
pub struct ValueEntry {
3232
pub day: f64,
3333
pub week: f64,

src/services/hooks/boosted.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use redis::aio::ConnectionManager;
44

55
use crate::{
66
config::Config,
7+
helpers::boosted::send_boosted_event,
78
services::hooks::structs::{BoostedHookPayload, BoostedHookType},
89
ServerState,
910
};
@@ -28,6 +29,7 @@ async fn execute(
2829
}
2930

3031
let valkey = &mut state.valkey.clone();
32+
let rabbit = &mut state.rabbit.clone();
3133

3234
match payload.hook_type {
3335
BoostedHookType::RideStarted => {
@@ -36,18 +38,24 @@ async fn execute(
3638
.arg("true")
3739
.query_async::<ConnectionManager, String>(&mut valkey.cm)
3840
.await;
41+
42+
send_boosted_event(valkey, rabbit).await;
3943
}
4044
BoostedHookType::RideEnded => {
4145
let _ = redis::cmd("DEL")
4246
.arg("boosted/in-ride")
4347
.query_async::<ConnectionManager, String>(&mut valkey.cm)
4448
.await;
49+
50+
send_boosted_event(valkey, rabbit).await;
4551
}
4652
BoostedHookType::RideDiscarded => {
4753
let _ = redis::cmd("DEL")
4854
.arg("boosted/in-ride")
4955
.query_async::<ConnectionManager, String>(&mut valkey.cm)
5056
.await;
57+
58+
send_boosted_event(valkey, rabbit).await;
5159
}
5260
}
5361

src/structs/boosted.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
use serde::{Deserialize, Serialize};
2+
use serde_with::skip_serializing_none;
3+
4+
use crate::services::boosted::structs::{RideStats, Stats};
5+
6+
#[skip_serializing_none]
7+
#[derive(Debug, Serialize, Deserialize)]
8+
pub struct BoostedRideUpdate {
9+
pub riding: bool,
10+
pub latest_ride: RideStats,
11+
pub stats: Stats,
12+
}

src/structs/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
pub mod blog;
2+
pub mod boosted;
23
pub mod github;
34
pub mod spotify;
45
pub mod uploads;

0 commit comments

Comments
 (0)