Skip to content

Commit

Permalink
backend/feat: Bus Tracking
Browse files Browse the repository at this point in the history
  • Loading branch information
khuzema786 committed Nov 27, 2024
1 parent f00da81 commit 668ec34
Show file tree
Hide file tree
Showing 16 changed files with 415 additions and 247 deletions.
71 changes: 40 additions & 31 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions crates/location_tracking_service/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ serde_dhall = "0.12.1"
chrono = { version = "0.4", features = ["serde"] }
log = "0.4.14"
tokio = "1.29.1"
fred = { version = "6.0.0", features = ["metrics", "partial-tracing"] }
fred = { version = "9.4.0", features = ["metrics", "partial-tracing", "i-geo", "i-cluster", "i-client"] }
reqwest = {version = "0.11.18", features = ["json"]}
futures = "0.3.28"
rand = "0.8.5"
Expand All @@ -43,8 +43,9 @@ tracing-appender = "0.2.2"
tracing-subscriber = { version = "0.3.16", features = ["env-filter", "registry", "json"] }
prometheus = { version = "0.13.3", features = ["process"] }

shared = { git = "https://github.com/nammayatri/shared-kernel-rs", rev = "2d6cf2e" }
macros = { git = "https://github.com/nammayatri/shared-kernel-rs", rev = "2d6cf2e" }
shared = { git = "https://github.com/nammayatri/shared-kernel-rs", rev = "f8d80ea" }
# shared = { version = "0.1.0", path = "/Users/khuzema.khomosi/Documents/shared-kernel-rs/crates/shared" }
macros = { git = "https://github.com/nammayatri/shared-kernel-rs", rev = "f8d80ea" }

[dev-dependencies]
pprof = { version = "0.12", features = ["flamegraph"] }
35 changes: 30 additions & 5 deletions crates/location_tracking_service/src/common/types.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::collections::VecDeque;

/* Copyright 2022-23, Juspay India Pvt Ltd
This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License
as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program
Expand All @@ -12,6 +10,7 @@ use fred::types::GeoValue;
use geo::MultiPolygon;
use rustc_hash::FxHashMap;
use serde::{Deserialize, Serialize};
use std::collections::VecDeque;
use strum_macros::{Display, EnumIter, EnumString};

#[derive(Deserialize, Serialize, Clone, Debug, Eq, PartialEq)]
Expand Down Expand Up @@ -99,6 +98,22 @@ pub enum VehicleType {
#[strum(serialize = "AMBULANCE_VENTILATOR")]
#[serde(rename = "AMBULANCE_VENTILATOR")]
AmbulanceVentilator,
#[strum(serialize = "BUS_AC")]
#[serde(rename = "BUS_AC")]
BusAc,
#[strum(serialize = "BUS_NON_AC")]
#[serde(rename = "BUS_NON_AC")]
BusNonAc,
}

#[derive(Deserialize, Serialize, Clone, Debug, Display, Eq, PartialEq)]
#[serde(rename_all = "camelCase")]
pub enum RideInfo {
#[serde(rename_all = "camelCase")]
Bus {
route_code: String,
bus_number: String,
},
}

#[derive(Debug, Clone, EnumString, Display, Serialize, Deserialize, Eq, Hash, PartialEq)]
Expand Down Expand Up @@ -150,9 +165,7 @@ pub struct MultiPolygonBody {
pub struct RideDetails {
pub ride_id: RideId,
pub ride_status: RideStatus,
pub vehicle_number: Option<String>,
pub ride_start_otp: Option<u32>,
pub estimated_pickup_distance: Option<Meters>,
pub ride_info: Option<RideInfo>,
}

#[derive(Deserialize, Serialize, Clone, Debug, Eq, PartialEq)]
Expand Down Expand Up @@ -211,3 +224,15 @@ pub struct DriverAllDetails {
pub struct RideBookingDetails {
pub driver_last_known_location: DriverLastKnownLocation,
}

#[derive(Serialize, Deserialize, Clone, Debug)]
#[serde(rename_all = "camelCase")]
pub struct VehicleTrackingInfo {
pub start_time: Option<TimeStamp>,
pub schedule_relationship: Option<String>,
pub trip_id: Option<String>,
pub latitude: Latitude,
pub longitude: Longitude,
pub speed: Option<SpeedInMeterPerSecond>,
pub timestamp: TimeStamp,
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
the GNU Affero General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#![allow(clippy::all)]
use std::collections::HashMap;

use crate::tools::error::AppError;
use crate::{
common::{
Expand Down Expand Up @@ -207,3 +209,33 @@ pub async fn driver_block_till(
};
Ok(APISuccess::default())
}

#[macros::measure_duration]
pub async fn track_vehicles(
data: Data<AppState>,
request_body: TrackVehicleRequest,
) -> Result<Vec<TrackVehicleResponse>, AppError> {
let track_vehicle_info = match request_body {
TrackVehicleRequest::RouteCode(route_code) => {
get_route_location(&data.redis, &route_code).await?
}
TrackVehicleRequest::TripCodes(trip_codes) => {
let mut track_vehicles_info = HashMap::new();
for trip_code in trip_codes {
let location = get_trip_location(&data.redis, &trip_code).await?;
for (vehicle_number, vehicle_info) in location.into_iter() {
track_vehicles_info.insert(vehicle_number, vehicle_info);
}
}
track_vehicles_info
}
};

Ok(track_vehicle_info
.into_iter()
.map(|(vehicle_number, vehicle_info)| TrackVehicleResponse {
vehicle_number,
vehicle_info,
})
.collect())
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@ pub async fn ride_create(
&request_body.driver_id,
ride_id.to_owned(),
RideStatus::NEW,
Some(request_body.vehicle_number),
Some(request_body.ride_start_otp),
Some(request_body.estimated_pickup_distance),
None,
)
.await?;

Expand All @@ -51,9 +49,7 @@ pub async fn ride_start(
&request_body.driver_id,
ride_id,
RideStatus::INPROGRESS,
None,
None,
None,
request_body.ride_info,
)
.await?;

Expand Down Expand Up @@ -138,8 +134,6 @@ pub async fn ride_details(
request_body.ride_id.to_owned(),
request_body.ride_status,
None,
None,
None,
)
.await?;

Expand Down
Loading

0 comments on commit 668ec34

Please sign in to comment.