optionchain_simulator 0.0.3

OptionChain-Simulator is a lightweight REST API service that simulates an evolving option chain with every request. It is designed for developers building or testing trading systems, backtesters, and visual tools that depend on option data streams but want to avoid relying on live data feeds.
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
use crate::infrastructure::telemetry::collector::MetricsCollector;
use actix_web::{
    Error,
    dev::{Service, ServiceRequest, ServiceResponse, Transform, forward_ready},
};
use futures::future::{LocalBoxFuture, Ready, ok};
use std::sync::Arc;
use std::time::Instant;

/// `MetricsMiddleware` is a struct that acts as middleware for collecting
/// metrics in an application. It facilitates the integration of a `MetricsCollector`
/// into the middleware pipeline by allowing for concurrent access and usage.
///
/// # Fields
/// - `metrics`:
///   An atomically reference-counted pointer (`Arc`) to a `MetricsCollector`.
///   The `MetricsCollector` is responsible for collecting and storing metrics
///   such as request counts, response times, or error rates.
///
/// # Usage
/// This middleware is typically used in server applications where tracking
/// application performance and health are critical. By using an `Arc`,
/// the `MetricsCollector` can be shared safely across multiple threads.
///
/// The `MetricsMiddleware` should be configured to intercept requests and responses,
/// allowing it to record relevant metrics automatically.
///
pub struct MetricsMiddleware {
    metrics: Arc<MetricsCollector>,
}

impl MetricsMiddleware {
    /// Creates a new instance of the struct with the provided `MetricsCollector`.
    ///
    /// # Arguments
    ///
    /// * `metrics` - An `Arc`-wrapped `MetricsCollector` instance to be used
    ///   by the struct for collecting and managing metrics.
    ///
    /// # Returns
    ///
    /// A new instance of the struct containing the provided `MetricsCollector`.
    ///
    pub fn new(metrics: Arc<MetricsCollector>) -> Self {
        Self { metrics }
    }
}

impl<S, B> Transform<S, ServiceRequest> for MetricsMiddleware
where
    S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
    S::Future: 'static,
    B: 'static,
{
    type Response = ServiceResponse<B>;
    type Error = Error;
    type Transform = MetricsMiddlewareService<S>;
    type InitError = ();
    type Future = Ready<Result<Self::Transform, Self::InitError>>;

    fn new_transform(&self, service: S) -> Self::Future {
        ok(MetricsMiddlewareService {
            service,
            metrics: self.metrics.clone(),
        })
    }
}

pub struct MetricsMiddlewareService<S> {
    service: S,
    metrics: Arc<MetricsCollector>,
}

impl<S, B> Service<ServiceRequest> for MetricsMiddlewareService<S>
where
    S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
    S::Future: 'static,
    B: 'static,
{
    type Response = ServiceResponse<B>;
    type Error = Error;
    type Future = LocalBoxFuture<'static, Result<Self::Response, Self::Error>>;

    forward_ready!(service);

    fn call(&self, req: ServiceRequest) -> Self::Future {
        let metrics = self.metrics.clone();
        let path = req.path().to_string();
        let method = req.method().to_string();
        let start_time = Instant::now();

        let fut = self.service.call(req);

        Box::pin(async move {
            let res = fut.await?;
            let status = res.status().as_u16().to_string();
            let duration = start_time.elapsed();

            metrics.record_request(&path, &method, &status);
            metrics.record_request_duration(&path, &method, duration);

            if !res.status().is_success() {
                metrics.record_error(&path, &status);
            }

            Ok(res)
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use actix_web::{
        App, Error, HttpResponse,
        dev::{Service, ServiceResponse, Transform},
        http::{StatusCode, header::ContentType},
        test::{self, TestRequest},
        web,
    };
    use futures::future::{Ready, ready};
    use std::sync::{Arc, Mutex};
    use std::task::{Context, Poll};
    use std::time::Duration;

    // Tracking structure for test metrics
    #[derive(Default, Clone)]
    struct MetricsCall {
        path: String,
        method: String,
        status: String,
        is_error: bool,
        duration_recorded: bool,
    }

    // Test metrics collector that implements the same methods as the real one
    struct TestMetricsCollector {
        calls: Arc<Mutex<Vec<MetricsCall>>>,
    }

    impl TestMetricsCollector {
        fn new() -> Self {
            Self {
                calls: Arc::new(Mutex::new(Vec::new())),
            }
        }

        fn get_calls(&self) -> Vec<MetricsCall> {
            let calls = self.calls.lock().unwrap();
            calls.clone()
        }

        // Implement the same methods as the real MetricsCollector
        fn record_request(&self, endpoint: &str, method: &str, status: &str) {
            let mut calls = self.calls.lock().unwrap();
            calls.push(MetricsCall {
                path: endpoint.to_string(),
                method: method.to_string(),
                status: status.to_string(),
                is_error: false,
                duration_recorded: false,
            });
        }

        fn record_request_duration(&self, endpoint: &str, method: &str, _duration: Duration) {
            let mut calls = self.calls.lock().unwrap();
            let existing_call = calls
                .iter_mut()
                .find(|call| call.path == endpoint && call.method == method);

            if let Some(call) = existing_call {
                call.duration_recorded = true;
            } else {
                calls.push(MetricsCall {
                    path: endpoint.to_string(),
                    method: method.to_string(),
                    status: String::new(),
                    is_error: false,
                    duration_recorded: true,
                });
            }
        }

        fn record_error(&self, endpoint: &str, error_type: &str) {
            let mut calls = self.calls.lock().unwrap();
            calls.push(MetricsCall {
                path: endpoint.to_string(),
                method: String::new(),
                status: error_type.to_string(),
                is_error: true,
                duration_recorded: false,
            });
        }
    }

    // A simple service that returns a response with the given status code
    struct TestService {
        status_code: StatusCode,
    }

    impl TestService {
        fn new(status: StatusCode) -> Self {
            Self {
                status_code: status,
            }
        }
    }

    impl Service<ServiceRequest> for TestService {
        type Response = ServiceResponse<actix_web::body::BoxBody>;
        type Error = Error;
        type Future = Ready<Result<Self::Response, Self::Error>>;

        fn poll_ready(&self, _: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
            Poll::Ready(Ok(()))
        }

        fn call(&self, req: ServiceRequest) -> Self::Future {
            // Create a new response each time
            let response = HttpResponse::build(self.status_code)
                .content_type(ContentType::plaintext())
                .body("Test response");

            ready(Ok(req.into_response(response)))
        }
    }

    // Define a trait for MetricsCollector interface that our middleware will use
    trait MetricsInterface {
        fn record_request(&self, endpoint: &str, method: &str, status: &str);
        fn record_request_duration(&self, endpoint: &str, method: &str, duration: Duration);
        fn record_error(&self, endpoint: &str, error_type: &str);
    }

    // Implement the trait for both the real MetricsCollector and our test version
    impl MetricsInterface for crate::infrastructure::telemetry::collector::MetricsCollector {
        fn record_request(&self, endpoint: &str, method: &str, status: &str) {
            self.record_request(endpoint, method, status);
        }

        fn record_request_duration(&self, endpoint: &str, method: &str, duration: Duration) {
            self.record_request_duration(endpoint, method, duration);
        }

        fn record_error(&self, endpoint: &str, error_type: &str) {
            self.record_error(endpoint, error_type);
        }
    }

    impl MetricsInterface for TestMetricsCollector {
        fn record_request(&self, endpoint: &str, method: &str, status: &str) {
            self.record_request(endpoint, method, status);
        }

        fn record_request_duration(&self, endpoint: &str, method: &str, duration: Duration) {
            self.record_request_duration(endpoint, method, duration);
        }

        fn record_error(&self, endpoint: &str, error_type: &str) {
            self.record_error(endpoint, error_type);
        }
    }

    // Modify the middleware to use the trait instead of the concrete type
    // This is just for testing purposes
    struct TestMetricsMiddleware<T: MetricsInterface + 'static> {
        metrics: Arc<T>,
    }

    impl<T: MetricsInterface + 'static> TestMetricsMiddleware<T> {
        fn new(metrics: Arc<T>) -> Self {
            Self { metrics }
        }
    }

    impl<S, B, T> Transform<S, ServiceRequest> for TestMetricsMiddleware<T>
    where
        S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
        S::Future: 'static,
        B: 'static,
        T: MetricsInterface + 'static,
    {
        type Response = ServiceResponse<B>;
        type Error = Error;
        type Transform = TestMetricsMiddlewareService<S, T>;
        type InitError = ();
        type Future = Ready<Result<Self::Transform, Self::InitError>>;

        fn new_transform(&self, service: S) -> Self::Future {
            ok(TestMetricsMiddlewareService {
                service,
                metrics: self.metrics.clone(),
            })
        }
    }

    struct TestMetricsMiddlewareService<S, T: MetricsInterface + 'static> {
        service: S,
        metrics: Arc<T>,
    }

    impl<S, B, T> Service<ServiceRequest> for TestMetricsMiddlewareService<S, T>
    where
        S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
        S::Future: 'static,
        B: 'static,
        T: MetricsInterface + 'static,
    {
        type Response = ServiceResponse<B>;
        type Error = Error;
        type Future = LocalBoxFuture<'static, Result<Self::Response, Self::Error>>;

        forward_ready!(service);

        fn call(&self, req: ServiceRequest) -> Self::Future {
            let metrics = self.metrics.clone();
            let path = req.path().to_string();
            let method = req.method().to_string();
            let start_time = Instant::now();

            let fut = self.service.call(req);

            Box::pin(async move {
                let res = fut.await?;
                let status = res.status().as_u16().to_string();
                let duration = start_time.elapsed();

                metrics.record_request(&path, &method, &status);
                metrics.record_request_duration(&path, &method, duration);

                if !res.status().is_success() {
                    metrics.record_error(&path, &status);
                }

                Ok(res)
            })
        }
    }

    // Test for constructor
    #[test]
    fn test_metrics_middleware_new() {
        let collector = Arc::new(TestMetricsCollector::new());
        let middleware = TestMetricsMiddleware::new(collector);

        // Check that we can create the middleware
        assert!(Arc::strong_count(&middleware.metrics) > 0);
    }

    // Test the transformation process
    #[test]
    fn test_transform_creates_service() {
        let collector = Arc::new(TestMetricsCollector::new());
        let middleware = TestMetricsMiddleware::new(collector);

        let test_service = TestService::new(StatusCode::OK);
        let transform_future = middleware.new_transform(test_service);

        let service =
            futures::executor::block_on(transform_future).expect("Transform should succeed");

        // Check that we get a service back from the transform
        assert!(Arc::strong_count(&service.metrics) > 0);
    }

    // Test recording of successful request
    #[test]
    fn test_service_records_successful_request() {
        let collector = Arc::new(TestMetricsCollector::new());

        // Create test service with 200 OK response
        let test_service = TestService::new(StatusCode::OK);

        // Create middleware service
        let middleware_service = TestMetricsMiddlewareService {
            service: test_service,
            metrics: collector.clone(),
        };

        // Create a test request
        let req = TestRequest::get().uri("/test").to_srv_request();

        // Execute the service call
        let response = futures::executor::block_on(middleware_service.call(req))
            .expect("Service call should succeed");

        // Check the response status
        assert_eq!(response.status(), StatusCode::OK);

        // Check that metrics were recorded correctly
        let calls = collector.get_calls();
        assert!(
            !calls.is_empty(),
            "Should have recorded at least one metrics call"
        );

        // Find the request metrics call
        let request_call = calls
            .iter()
            .find(|call| call.path == "/test" && call.method == "GET" && call.status == "200");
        assert!(
            request_call.is_some(),
            "Should have recorded request metrics"
        );

        // Check that duration was recorded
        let duration_call = calls
            .iter()
            .find(|call| call.path == "/test" && call.method == "GET" && call.duration_recorded);
        assert!(
            duration_call.is_some(),
            "Should have recorded duration metrics"
        );

        // Check that no error was recorded
        let error_call = calls.iter().find(|call| call.is_error);
        assert!(
            error_call.is_none(),
            "Should not have recorded error metrics"
        );
    }

    // Test recording of error request
    #[test]
    fn test_service_records_error_request() {
        let collector = Arc::new(TestMetricsCollector::new());

        // Create test service with 404 Not Found response
        let test_service = TestService::new(StatusCode::NOT_FOUND);

        // Create middleware service
        let middleware_service = TestMetricsMiddlewareService {
            service: test_service,
            metrics: collector.clone(),
        };

        // Create a test request
        let req = TestRequest::get().uri("/test").to_srv_request();

        // Execute the service call
        let response = futures::executor::block_on(middleware_service.call(req))
            .expect("Service call should succeed");

        // Check the response status
        assert_eq!(response.status(), StatusCode::NOT_FOUND);

        // Check that metrics were recorded correctly
        let calls = collector.get_calls();

        // Find the request metrics call
        let request_call = calls
            .iter()
            .find(|call| call.path == "/test" && call.method == "GET" && call.status == "404");
        assert!(
            request_call.is_some(),
            "Should have recorded request metrics"
        );

        // Check that duration was recorded
        let duration_call = calls
            .iter()
            .find(|call| call.path == "/test" && call.method == "GET" && call.duration_recorded);
        assert!(
            duration_call.is_some(),
            "Should have recorded duration metrics"
        );

        // Check that error was recorded
        let error_call = calls
            .iter()
            .find(|call| call.is_error && call.status == "404");
        assert!(error_call.is_some(), "Should have recorded error metrics");
    }

    // Integration test using actix-web test utilities
    #[actix_web::test]
    async fn test_middleware_integration() {
        let collector = Arc::new(TestMetricsCollector::new());

        // Define a simple handler
        async fn test_handler() -> HttpResponse {
            HttpResponse::Ok().body("Test response")
        }

        // Create app with middleware
        let app = test::init_service(
            App::new()
                .wrap(TestMetricsMiddleware::new(collector.clone()))
                .route("/test", web::get().to(test_handler)),
        )
        .await;

        // Send a request
        let req = TestRequest::get().uri("/test").to_request();
        let resp = test::call_service(&app, req).await;

        // Verify response
        assert_eq!(resp.status(), StatusCode::OK);

        // Check that metrics were recorded
        let calls = collector.get_calls();
        assert!(!calls.is_empty(), "Should have recorded metrics");
    }

    // Test for error response in middleware
    #[actix_web::test]
    async fn test_middleware_with_error_response() {
        let collector = Arc::new(TestMetricsCollector::new());

        // Define a handler that returns an error
        async fn error_handler() -> HttpResponse {
            HttpResponse::InternalServerError().body("Error occurred")
        }

        // Create app with middleware
        let app = test::init_service(
            App::new()
                .wrap(TestMetricsMiddleware::new(collector.clone()))
                .route("/error", web::get().to(error_handler)),
        )
        .await;

        // Send a request
        let req = TestRequest::get().uri("/error").to_request();
        let resp = test::call_service(&app, req).await;

        // Verify response
        assert_eq!(resp.status(), StatusCode::INTERNAL_SERVER_ERROR);

        // Check that metrics were recorded including error
        let calls = collector.get_calls();

        // Check for error recording
        let error_calls = calls.iter().filter(|call| call.is_error).count();
        assert!(error_calls > 0, "Should have recorded error metrics");
    }
}