sift_stream 0.8.2

A robust Sift telemetry streaming library
Documentation
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
use crate::SiftChannel;
use hyper_util::rt::TokioIo;
use pbjson_types::Timestamp;
use sift_connect::grpc::interceptor::AuthInterceptor;
use sift_rs::assets::v1::asset_service_server::{AssetService, AssetServiceServer};
use sift_rs::assets::v1::{
    ArchiveAssetRequest, ArchiveAssetResponse, Asset, DeleteAssetRequest, DeleteAssetResponse,
    GetAssetRequest, GetAssetResponse, ListAssetsRequest, ListAssetsResponse, UpdateAssetRequest,
    UpdateAssetResponse,
};
use sift_rs::common::r#type::v1::ChannelDataType;
use sift_rs::ingest::v1::IngestWithConfigDataStreamRequest;
use sift_rs::ingest::v1::{
    IngestWithConfigDataStreamResponse,
    ingest_service_server::{IngestService, IngestServiceServer},
};
use sift_rs::ingestion_configs::v2::ingestion_config_service_server::{
    IngestionConfigService, IngestionConfigServiceServer,
};
use sift_rs::ingestion_configs::v2::{
    ChannelConfig, CreateIngestionConfigFlowsRequest, CreateIngestionConfigFlowsResponse,
    CreateIngestionConfigRequest, CreateIngestionConfigResponse, FlowConfig,
    GetIngestionConfigRequest, GetIngestionConfigResponse, IngestionConfig,
    ListIngestionConfigFlowsRequest, ListIngestionConfigFlowsResponse, ListIngestionConfigsRequest,
    ListIngestionConfigsResponse,
};
use sift_rs::ping::v1::ping_service_server::{PingService, PingServiceServer};
use sift_rs::ping::v1::{PingRequest, PingResponse};
use sift_rs::runs::v2::run_service_server::{RunService, RunServiceServer};
use sift_rs::runs::v2::{
    CreateAdhocRunRequest, CreateAdhocRunResponse, CreateAutomaticRunAssociationForAssetsRequest,
    CreateAutomaticRunAssociationForAssetsResponse, CreateRunRequest, CreateRunResponse,
    DeleteRunRequest, DeleteRunResponse, GetRunRequest, GetRunResponse, ListRunsRequest,
    ListRunsResponse, Run, StopRunRequest, StopRunResponse, UpdateRunRequest, UpdateRunResponse,
};
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::{Arc, Mutex};
use tonic::transport::{Endpoint, Server, Uri};
use tonic::{Request, Response, Status};
use tower::{ServiceBuilder, service_fn};
use uuid::Uuid;

pub(crate) struct MockPingService;

#[tonic::async_trait]
impl PingService for MockPingService {
    async fn ping(&self, _: Request<PingRequest>) -> Result<Response<PingResponse>, Status> {
        Ok(Response::new(PingResponse {
            response: "Hello from a sift test!".to_string(),
        }))
    }
}

pub(crate) struct MockIngestionConfigService {
    existing_flows: Arc<Mutex<Vec<FlowConfig>>>,
    existing_ingestion_configs: Arc<Mutex<Vec<IngestionConfig>>>,
}

impl Default for MockIngestionConfigService {
    fn default() -> Self {
        let existing_flow = FlowConfig {
            name: "already_exists_flow".to_string(),
            channels: vec![ChannelConfig {
                name: "channel1".to_string(),
                data_type: ChannelDataType::Double.into(),
                ..Default::default()
            }],
        };
        let existing_ingestion_config = IngestionConfig {
            ingestion_config_id: Uuid::new_v4().to_string(),
            asset_id: "already_exists_asset".to_string(),
            client_key: "already_exists_client_key".to_string(),
        };
        Self {
            existing_flows: Arc::new(Mutex::new(vec![existing_flow])),
            existing_ingestion_configs: Arc::new(Mutex::new(vec![existing_ingestion_config])),
        }
    }
}

#[tonic::async_trait]
impl IngestionConfigService for MockIngestionConfigService {
    async fn get_ingestion_config(
        &self,
        request: Request<GetIngestionConfigRequest>,
    ) -> Result<Response<GetIngestionConfigResponse>, Status> {
        let get_ingestion_config = request.into_inner();
        let existing_ingestion_configs = self.existing_ingestion_configs.lock().unwrap();
        let ingestion_config = existing_ingestion_configs
            .iter()
            .find(|ic| ic.ingestion_config_id == get_ingestion_config.ingestion_config_id);
        if let Some(ingestion_config) = ingestion_config {
            return Ok(Response::new(GetIngestionConfigResponse {
                ingestion_config: Some(ingestion_config.clone()),
            }));
        }

        Err(Status::not_found("ingestion config not found"))
    }
    async fn create_ingestion_config(
        &self,
        request: Request<CreateIngestionConfigRequest>,
    ) -> Result<Response<CreateIngestionConfigResponse>, Status> {
        let create_ingestion_config = request.into_inner();

        let mut existing_ingestion_configs = self.existing_ingestion_configs.lock().unwrap();
        for config in existing_ingestion_configs.iter() {
            if config.client_key == create_ingestion_config.client_key {
                return Err(Status::already_exists("ingestion config already exists"));
            }
        }

        let new_config = IngestionConfig {
            ingestion_config_id: Uuid::new_v4().to_string(),
            asset_id: create_ingestion_config.asset_name.clone(),
            client_key: create_ingestion_config.client_key.clone(),
        };

        existing_ingestion_configs.push(new_config.clone());

        Ok(Response::new(CreateIngestionConfigResponse {
            ingestion_config: Some(new_config),
        }))
    }
    async fn list_ingestion_configs(
        &self,
        request: Request<ListIngestionConfigsRequest>,
    ) -> Result<Response<ListIngestionConfigsResponse>, Status> {
        let list_configs: ListIngestionConfigsRequest = request.into_inner();

        let existing_ingestion_configs = self.existing_ingestion_configs.lock().unwrap();

        let mut ingestion_configs = Vec::new();
        for config in existing_ingestion_configs.iter() {
            if list_configs.filter.is_empty() || list_configs.filter.contains(&config.client_key) {
                ingestion_configs.push(config.clone());
            }
        }

        Ok(Response::new(ListIngestionConfigsResponse {
            ingestion_configs: ingestion_configs,
            next_page_token: "".to_string(),
        }))
    }
    async fn create_ingestion_config_flows(
        &self,
        request: Request<CreateIngestionConfigFlowsRequest>,
    ) -> Result<Response<CreateIngestionConfigFlowsResponse>, Status> {
        let create_flows = request.into_inner();

        let mut existing_flows = self.existing_flows.lock().unwrap();
        for flow in create_flows.flows.iter() {
            if existing_flows.iter().any(|f| f.name == flow.name) {
                return Err(Status::already_exists("flow already exists"));
            }

            existing_flows.push(flow.clone());
        }

        Ok(Response::new(CreateIngestionConfigFlowsResponse {}))
    }
    async fn list_ingestion_config_flows(
        &self,
        request: Request<ListIngestionConfigFlowsRequest>,
    ) -> Result<Response<ListIngestionConfigFlowsResponse>, Status> {
        let list_flows: ListIngestionConfigFlowsRequest = request.into_inner();
        let existing_flows = self.existing_flows.lock().unwrap();

        // If the filter contains "already_exists_flow" or is empty, return the existing flow.
        let mut flows = Vec::new();
        for flow in existing_flows.iter() {
            if list_flows.filter.is_empty() || list_flows.filter.contains(&flow.name) {
                flows.push(flow.clone());
            }
        }

        Ok(Response::new(ListIngestionConfigFlowsResponse {
            flows,
            next_page_token: "".to_string(),
        }))
    }
}

pub(crate) struct MockAssetService;

#[tonic::async_trait]
impl AssetService for MockAssetService {
    async fn get_asset(
        &self,
        request: Request<GetAssetRequest>,
    ) -> Result<Response<GetAssetResponse>, Status> {
        let get_asset = request.into_inner();
        let asset_id = get_asset.asset_id;

        Ok(Response::new(GetAssetResponse {
            asset: Some(Asset {
                asset_id: asset_id.clone(),
                name: asset_id.clone(),
                organization_id: "test".to_string(),
                ..Default::default()
            }),
        }))
    }
    async fn update_asset(
        &self,
        _: Request<UpdateAssetRequest>,
    ) -> Result<Response<UpdateAssetResponse>, Status> {
        Err(Status::unimplemented("Not implemented for test"))
    }
    async fn delete_asset(
        &self,
        _: Request<DeleteAssetRequest>,
    ) -> Result<Response<DeleteAssetResponse>, Status> {
        Err(Status::unimplemented("Not implemented for test"))
    }
    async fn list_assets(
        &self,
        _: Request<ListAssetsRequest>,
    ) -> Result<Response<ListAssetsResponse>, Status> {
        Err(Status::unimplemented("Not implemented for test"))
    }
    async fn archive_asset(
        &self,
        _: Request<ArchiveAssetRequest>,
    ) -> Result<Response<ArchiveAssetResponse>, Status> {
        Err(Status::unimplemented("Not implemented for test"))
    }
}

pub(crate) struct MockRunService;

impl MockRunService {
    fn run() -> Run {
        Run {
            run_id: "123".to_string(),
            name: "test_run".to_string(),
            description: "test_run".to_string(),
            created_date: Some(Timestamp {
                seconds: 1,
                nanos: 0,
            }),
            modified_date: Some(Timestamp {
                seconds: 1,
                nanos: 0,
            }),
            created_by_user_id: "test".to_string(),
            modified_by_user_id: "test".to_string(),
            organization_id: "test".to_string(),
            start_time: None,
            stop_time: None,
            is_pinned: false,
            tags: vec!["test".to_string()],
            default_report_id: "".to_string(),
            client_key: None,
            metadata: vec![],
            asset_ids: vec!["123".to_string()],
            archived_date: None,
            is_adhoc: false,
            is_archived: false,
            duration: None,
        }
    }
}

#[tonic::async_trait]
impl RunService for MockRunService {
    async fn get_run(&self, _: Request<GetRunRequest>) -> Result<Response<GetRunResponse>, Status> {
        Ok(Response::new(GetRunResponse {
            run: Some(Self::run()),
        }))
    }
    async fn list_runs(
        &self,
        _: Request<ListRunsRequest>,
    ) -> Result<Response<ListRunsResponse>, Status> {
        Ok(Response::new(ListRunsResponse {
            runs: vec![Self::run()],
            next_page_token: "".to_string(),
        }))
    }
    async fn create_run(
        &self,
        _: Request<CreateRunRequest>,
    ) -> Result<Response<CreateRunResponse>, Status> {
        Ok(Response::new(CreateRunResponse {
            run: Some(Self::run()),
        }))
    }

    async fn create_adhoc_run(
        &self,
        _: Request<CreateAdhocRunRequest>,
    ) -> Result<Response<CreateAdhocRunResponse>, Status> {
        Err(Status::unimplemented("Not implemented for test"))
    }
    async fn update_run(
        &self,
        _: Request<UpdateRunRequest>,
    ) -> Result<Response<UpdateRunResponse>, Status> {
        Err(Status::unimplemented("Not implemented for test"))
    }
    async fn delete_run(
        &self,
        _: Request<DeleteRunRequest>,
    ) -> Result<Response<DeleteRunResponse>, Status> {
        Err(Status::unimplemented("Not implemented for test"))
    }
    async fn stop_run(
        &self,
        _: Request<StopRunRequest>,
    ) -> Result<Response<StopRunResponse>, Status> {
        Err(Status::unimplemented("Not implemented for test"))
    }
    async fn create_automatic_run_association_for_assets(
        &self,
        _: Request<CreateAutomaticRunAssociationForAssetsRequest>,
    ) -> Result<Response<CreateAutomaticRunAssociationForAssetsResponse>, Status> {
        Err(Status::unimplemented("Not implemented for test"))
    }
}

#[derive(Debug, Clone)]
pub(crate) struct MockIngestService {
    captured_data: Arc<Mutex<Vec<IngestWithConfigDataStreamRequest>>>,
    num_errors_to_return: Arc<AtomicUsize>,
}

impl MockIngestService {
    pub(crate) fn new() -> Self {
        Self {
            captured_data: Arc::new(Mutex::new(Vec::new())),
            num_errors_to_return: Arc::new(AtomicUsize::new(0)),
        }
    }

    pub(crate) fn get_captured_data(&self) -> Vec<IngestWithConfigDataStreamRequest> {
        self.captured_data.lock().unwrap().clone()
    }

    pub(crate) fn set_num_errors_to_return(&self, num_errors_to_return: usize) {
        self.num_errors_to_return
            .store(num_errors_to_return, Ordering::Relaxed);
    }
}

#[tonic::async_trait]
impl IngestService for MockIngestService {
    async fn ingest_with_config_data_stream(
        &self,
        request: Request<tonic::Streaming<IngestWithConfigDataStreamRequest>>,
    ) -> Result<Response<IngestWithConfigDataStreamResponse>, Status> {
        if self.num_errors_to_return.load(Ordering::Relaxed) > 0 {
            self.num_errors_to_return.fetch_sub(1, Ordering::Relaxed);
            return Err(Status::internal("test error"));
        }

        let mut stream = request.into_inner();

        while let Some(data) = stream.message().await? {
            self.captured_data.lock().unwrap().push(data);
        }

        Ok(Response::new(IngestWithConfigDataStreamResponse {}))
    }

    async fn ingest_arbitrary_protobuf_data_stream(
        &self,
        _request: Request<
            tonic::Streaming<sift_rs::ingest::v1::IngestArbitraryProtobufDataStreamRequest>,
        >,
    ) -> Result<Response<sift_rs::ingest::v1::IngestArbitraryProtobufDataStreamResponse>, Status>
    {
        Err(Status::unimplemented("Not implemented for test"))
    }
}

pub(crate) async fn create_mock_grpc_channel_with_service() -> (SiftChannel, MockIngestService) {
    // Create a mock ingest service.
    let mock_ingest_service = MockIngestService::new();
    let mock_ingest_service_clone = mock_ingest_service.clone();

    let (client, server) = tokio::io::duplex(1024);

    tokio::spawn(async move {
        Server::builder()
            .add_service(IngestServiceServer::new(mock_ingest_service_clone))
            .add_service(PingServiceServer::new(MockPingService))
            .add_service(IngestionConfigServiceServer::new(
                MockIngestionConfigService::default(),
            ))
            .add_service(AssetServiceServer::new(MockAssetService))
            .add_service(RunServiceServer::new(MockRunService))
            .serve_with_incoming(tokio_stream::once(Ok::<_, std::io::Error>(server)))
            .await
            .unwrap();
    });

    let mut client = Some(client);
    let channel = Endpoint::try_from("http://[::]:50051")
        .unwrap()
        .connect_with_connector(service_fn(move |_: Uri| {
            let client = client.take();

            async move {
                if let Some(client) = client {
                    Ok(TokioIo::new(client))
                } else {
                    Err(std::io::Error::other("Client already taken"))
                }
            }
        }))
        .await
        .unwrap();

    let sift_channel = ServiceBuilder::new()
        .layer(tonic::service::interceptor::InterceptorLayer::new(
            AuthInterceptor {
                apikey: "test-api-key".to_string(),
            },
        ))
        .service(channel);

    (sift_channel, mock_ingest_service)
}