1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
//! Implementation of pricing logics based on the service type.

use log::debug;
use snafu::prelude::Snafu;

use crate::grpc::server::{PricingRequest, PricingRequests, ServiceType};

/// Errors that can occur when getting pricing.
#[derive(Snafu, Copy, Clone, Debug, PartialEq)]
pub enum PricingError {
    /// Pricing requests contain multiple service types.
    #[snafu(display("All pricing requests must have the same service type"))]
    MultipleServiceTypes,

    /// Request contains negative weight.
    #[snafu(display("Request contains negative weight"))]
    NegativeWeight,

    /// Request contains negative distance.
    #[snafu(display("Request contains negative distance"))]
    NegativeDistance,

    /// No pricing requests were provided.
    #[snafu(display("No pricing requests were provided"))]
    NoRequests,

    /// Unknown service type error.
    #[snafu(display("Unknown service type; cannot parse service type"))]
    UnknownServiceType,
}

/// Get pricing given a [`PricingRequests`], which contains an array of
/// [`PricingRequest`]s.
///
/// The array of [`PricingRequest`]s is used to allow pricing for
/// multiple legs of a trip to be calculated.
///
/// # Notes
/// The `service_type` field of all [`PricingRequest`]s in the array
/// must be the same. It means the pricing service currently does not
/// allow for a trip that involves different types of services.
///
/// This may change in the future as per business requirements.
///
/// # Returns
/// If no errors occur, returns a vector of prices in dollars,
/// corresponding to the order of the input requests.
pub fn get_pricing(query: PricingRequests) -> Result<Vec<f32>, PricingError> {
    let requests = query.requests;
    debug!(
        "(get_pricing) Getting pricing for {:?} requests",
        requests.len()
    );
    check_pricing_requests(&requests)?;
    let mut prices = Vec::new();
    match ServiceType::from_i32(requests[0].service_type) {
        Some(ServiceType::Cargo) => {
            debug!("Cargo pricing");
            for request in requests {
                prices.push(get_cargo_pricing(request));
            }
            Ok(prices)
        }
        Some(ServiceType::Rideshare) => {
            // debug!("Rideshare pricing");
            // for request in requests {
            //     prices.push(get_rideshare_pricing(request));
            // }
            // Ok(prices)
            debug!("(get_pricing) Rideshare pricing not supported");
            Err(PricingError::UnknownServiceType)
        }
        Some(ServiceType::Charter) => {
            // debug!("Charter pricing");
            // for request in requests {
            //     prices.push(get_charter_pricing(request));
            // }
            // Ok(prices)
            debug!("(get_pricing) Charter pricing not supported");
            Err(PricingError::UnknownServiceType)
        }
        None => {
            debug!("Error parsing service type");
            Err(PricingError::UnknownServiceType)
        }
    }
}

/// Check that all pricing requests have the same service type.
fn check_pricing_requests(requests: &[PricingRequest]) -> Result<(), PricingError> {
    debug!("(check_pricing_requests) entry.");
    if requests.is_empty() {
        return Err(PricingError::NoRequests);
    }
    let service_type = requests[0].service_type;
    for request in requests {
        if request.service_type != service_type {
            return Err(PricingError::MultipleServiceTypes);
        } else if request.weight_kg < 0.0 {
            return Err(PricingError::NegativeWeight);
        } else if request.distance_km < 0.0 {
            return Err(PricingError::NegativeDistance);
        }
    }
    Ok(())
}

// ------------------------------------------------------------------
// Cargo pricing assumptions
// Expect these constants to be pulled from svc-storage in the future
// https://docs.google.com/spreadsheets/d/1mjPtaIn3E5m7r4nyKt_sJKG9BSFm2ty7Gzo7OqERxwo
// ------------------------------------------------------------------

/// Take off and landing cost in dollars.
const CARGO_TOL_COST_USD: f32 = 2.8;
/// Cruise speed in kilometers per hour.
const CARGO_CRUISE_SPEED_KM_PER_HR: f32 = 240.0;
/// Electricity (kw) needed to power every hour of cruise flight.
const CARGO_CRUISE_POWER_CONSUMPTION_KW: f32 = 71.0;
/// Electricity cost per kilowatt hour in dollars.
const CARGO_ELECTRICITY_COST_USD_PER_KWH: f32 = 0.3335;
/// Depreciation rate of the aircraft in dollars per hour.
const CARGO_DEPRECIATION_RATE_USD_PER_HR: f32 = 10.5;
/// Repair and maintenance cost in dollars per hour.
const CARGO_REPAIR_AND_MAINTENANCE_RATE_USD_PER_HR: f32 = 0.3 * CARGO_DEPRECIATION_RATE_USD_PER_HR;

// ------------------------------------------------------------------
// private functions
// ------------------------------------------------------------------

/// Pricing for cargo.
///
/// Pricing is based on distance for now. The unit economics are modeled
/// after [Project
/// Apollo](https://docs.google.com/spreadsheets/d/1mjPtaIn3E5m7r4nyKt_sJKG9BSFm2ty7Gzo7OqERxwo).
fn get_cargo_pricing(query: PricingRequest) -> f32 {
    debug!("Getting cargo pricing for query: {:?}", query);
    let distance = query.distance_km;
    debug!("Cargo take off and landing cost: {}", CARGO_TOL_COST_USD);
    debug!("Distance: {}", distance);
    let trip_duration = distance / CARGO_CRUISE_SPEED_KM_PER_HR;
    debug!("Trip duration: {}", trip_duration);
    let trip_cruise_cost =
        trip_duration * CARGO_ELECTRICITY_COST_USD_PER_KWH * CARGO_CRUISE_POWER_CONSUMPTION_KW;
    debug!("Trip cruise cost: {}", trip_cruise_cost);
    let depreciation_cost = trip_duration * CARGO_DEPRECIATION_RATE_USD_PER_HR;
    debug!("Depreciation cost: {}", depreciation_cost);
    let repair_and_maintenance_cost = trip_duration * CARGO_REPAIR_AND_MAINTENANCE_RATE_USD_PER_HR;
    debug!(
        "Repair and maintenance cost: {}",
        repair_and_maintenance_cost
    );
    let total_cost =
        CARGO_TOL_COST_USD + trip_cruise_cost + depreciation_cost + repair_and_maintenance_cost;
    debug!("Total cost: {}", total_cost);
    total_cost
}

// /// TODO: Pricing for rideshare.
// fn get_rideshare_pricing(query: PricingRequest) -> f32 {
//     //TODO
//     0.5 * get_cargo_pricing(query)
// }

// /// TODO: Pricing for charter.
// fn get_charter_pricing(query: PricingRequest) -> f32 {
//     //TODO
//     2.0 * get_cargo_pricing(query)
// }

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_get_cargo_pricing() {
        let query = PricingRequests {
            requests: vec![PricingRequest {
                service_type: ServiceType::Cargo as i32,
                weight_kg: 100.0,
                distance_km: 100.0,
            }],
        };
        let prices = get_pricing(query).unwrap();
        assert_eq!(prices[0], 18.353542);
    }

    #[test]
    fn test_get_rideshare_pricing() {
        // let query = PricingRequests {
        //     requests: vec![PricingRequest {
        //         service_type: ServiceType::Rideshare as i32,
        //         weight_kg: 100.0,
        //         distance_km: 100.0,
        //     }],
        // };
        // let prices = get_pricing(query).unwrap();
        // assert_eq!(prices[0], 9.176771);
    }

    #[test]
    fn test_get_charter_pricing() {
        // let query = PricingRequests {
        //     requests: vec![PricingRequest {
        //         service_type: ServiceType::Charter as i32,
        //         weight_kg: 100.0,
        //         distance_km: 100.0,
        //     }],
        // };
        // let prices = get_pricing(query).unwrap();
        // assert_eq!(prices[0], 36.707085);
    }

    #[test]
    fn test_multiple_leg_pricing() {
        let query1 = PricingRequest {
            service_type: ServiceType::Cargo as i32,
            weight_kg: 100.0,
            distance_km: 100.0,
        };
        let query2 = PricingRequest {
            service_type: ServiceType::Cargo as i32,
            weight_kg: 100.0,
            distance_km: 50.0,
        };
        let query3 = PricingRequest {
            service_type: ServiceType::Cargo as i32,
            weight_kg: 100.0,
            distance_km: 10.0,
        };
        let pricing_requests = vec![query1, query2, query3];
        let query = PricingRequests {
            requests: pricing_requests,
        };
        let prices = get_pricing(query).unwrap();
        let total = prices.iter().fold(0.0, |acc, x| acc + x);

        let price_leg_1 = get_cargo_pricing(query1);
        let price_leg_2 = get_cargo_pricing(query2);
        let price_leg_3 = get_cargo_pricing(query3);

        assert_eq!(prices.len(), 3);
        assert_eq!(prices[0], price_leg_1);
        assert_eq!(prices[1], price_leg_2);
        assert_eq!(prices[2], price_leg_3);
        assert_eq!(total, price_leg_1 + price_leg_2 + price_leg_3);
    }

    #[test]
    fn test_invalid_multiple_service_type() {
        // let query1 = PricingRequest {
        //     service_type: ServiceType::Cargo as i32,
        //     weight_kg: 100.0,
        //     distance_km: 100.0,
        // };
        // let query2 = PricingRequest {
        //     service_type: ServiceType::Rideshare as i32,
        //     weight_kg: 100.0,
        //     distance_km: 100.0,
        // };
        // let pricing_requests = vec![query1, query2];
        // let query = PricingRequests {
        //     requests: pricing_requests,
        // };
        // let prices = get_pricing(query);
        // assert_eq!(prices, Err(PricingError::MultipleServiceTypes));
    }

    #[test]
    fn test_invalid_service_type() {
        let query = PricingRequest {
            service_type: 3,
            weight_kg: 100.0,
            distance_km: 100.0,
        };
        let prices = get_pricing(PricingRequests {
            requests: vec![query],
        });
        assert_eq!(prices, Err(PricingError::UnknownServiceType));
    }

    #[test]
    fn test_invalid_weight() {
        let query = PricingRequest {
            service_type: ServiceType::Cargo as i32,
            weight_kg: -1.0,
            distance_km: 100.0,
        };
        let prices = get_pricing(PricingRequests {
            requests: vec![query],
        });
        assert_eq!(prices, Err(PricingError::NegativeWeight));
    }

    #[test]
    fn test_invalid_distance() {
        let query = PricingRequest {
            service_type: ServiceType::Cargo as i32,
            weight_kg: 100.0,
            distance_km: -1.0,
        };
        let prices = get_pricing(PricingRequests {
            requests: vec![query],
        });
        assert_eq!(prices, Err(PricingError::NegativeDistance));
    }

    #[test]
    fn test_pricing_request_empty_requests() {
        let empty_query = PricingRequests { requests: vec![] };
        let prices = get_pricing(empty_query);
        assert_eq!(prices, Err(PricingError::NoRequests));
    }
}