Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

backend/feat: Bus Tracking #142

Merged
merged 1 commit into from
Dec 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"] }
41 changes: 34 additions & 7 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,23 @@ 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, PartialEq)]
#[serde(rename_all = "camelCase")]
pub enum RideInfo {
#[serde(rename_all = "camelCase")]
Bus {
route_code: String,
bus_number: String,
destination: Point,
},
}

#[derive(Debug, Clone, EnumString, Display, Serialize, Deserialize, Eq, Hash, PartialEq)]
Expand Down Expand Up @@ -146,13 +162,11 @@ pub struct MultiPolygonBody {
pub multipolygon: MultiPolygon,
}

#[derive(Deserialize, Serialize, Clone, Debug, Eq, PartialEq)]
#[derive(Deserialize, Serialize, Clone, Debug, PartialEq)]
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 All @@ -166,7 +180,7 @@ pub struct DriversRideStatus {
pub location: Point,
}

#[derive(Serialize, Deserialize, Clone, Debug)]
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
pub struct Point {
pub lat: Latitude,
pub lon: Longitude,
Expand Down Expand Up @@ -211,3 +225,16 @@ 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,
pub ride_status: Option<RideStatus>,
}
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())
}
39 changes: 10 additions & 29 deletions crates/location_tracking_service/src/domain/action/internal/ride.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,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),
request_body.ride_info,
)
.await?;
}
Expand All @@ -53,9 +51,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 @@ -97,6 +93,7 @@ pub async fn ride_end(
driver_id: request_body.driver_id.clone(),
lat: request_body.lat,
lon: request_body.lon,
ride_info: None,
};
ride_details(data, ride_details_request).await?;
}
Expand Down Expand Up @@ -144,31 +141,15 @@ pub async fn ride_details(
&request_body.ride_id,
)
.await?;
} else {
if let Some(false) | None = request_body.is_future_ride {
set_ride_details_for_driver(
&data.redis,
&data.redis_expiry,
&request_body.merchant_id,
&request_body.driver_id,
request_body.ride_id.to_owned(),
request_body.ride_status,
None,
None,
None,
)
.await?;
}

let driver_details = DriverDetails {
driver_id: request_body.driver_id,
};

set_on_ride_driver_details(
} else if let Some(false) | None = request_body.is_future_ride {
set_ride_details_for_driver(
&data.redis,
&data.redis_expiry,
&request_body.ride_id,
driver_details,
&request_body.merchant_id,
&request_body.driver_id,
request_body.ride_id.to_owned(),
request_body.ride_status,
request_body.ride_info,
)
.await?;
}
Expand Down
Loading