|
| 1 | +use std::collections::HashMap; |
| 2 | + |
| 3 | +use aw_client_rust::AwClient; |
| 4 | +use chrono::{DateTime, Utc}; |
| 5 | +use reqwest::StatusCode; |
| 6 | + |
| 7 | +use aw_datastore::{Datastore, DatastoreError}; |
| 8 | +use aw_models::{Bucket, Event}; |
| 9 | + |
| 10 | +// This trait should be implemented by both AwClient and Datastore, unifying them under a single API |
| 11 | +pub trait AccessMethod: std::fmt::Debug { |
| 12 | + fn get_buckets(&self) -> Result<HashMap<String, Bucket>, String>; |
| 13 | + fn get_bucket(&self, bucket_id: &str) -> Result<Bucket, DatastoreError>; |
| 14 | + fn create_bucket(&self, bucket: &Bucket) -> Result<(), DatastoreError>; |
| 15 | + fn get_events( |
| 16 | + &self, |
| 17 | + bucket_id: &str, |
| 18 | + start: Option<DateTime<Utc>>, |
| 19 | + end: Option<DateTime<Utc>>, |
| 20 | + limit: Option<u64>, |
| 21 | + ) -> Result<Vec<Event>, String>; |
| 22 | + fn insert_events(&self, bucket_id: &str, events: Vec<Event>) -> Result<Vec<Event>, String>; |
| 23 | + fn get_event_count(&self, bucket_id: &str) -> Result<i64, String>; |
| 24 | + fn heartbeat(&self, bucket_id: &str, event: Event, duration: f64) -> Result<(), String>; |
| 25 | +} |
| 26 | + |
| 27 | +impl AccessMethod for Datastore { |
| 28 | + fn get_buckets(&self) -> Result<HashMap<String, Bucket>, String> { |
| 29 | + Ok(self.get_buckets().unwrap()) |
| 30 | + } |
| 31 | + fn get_bucket(&self, bucket_id: &str) -> Result<Bucket, DatastoreError> { |
| 32 | + self.get_bucket(bucket_id) |
| 33 | + } |
| 34 | + fn create_bucket(&self, bucket: &Bucket) -> Result<(), DatastoreError> { |
| 35 | + self.create_bucket(bucket)?; |
| 36 | + self.force_commit().unwrap(); |
| 37 | + Ok(()) |
| 38 | + } |
| 39 | + fn get_events( |
| 40 | + &self, |
| 41 | + bucket_id: &str, |
| 42 | + start: Option<DateTime<Utc>>, |
| 43 | + end: Option<DateTime<Utc>>, |
| 44 | + limit: Option<u64>, |
| 45 | + ) -> Result<Vec<Event>, String> { |
| 46 | + Ok(self.get_events(bucket_id, start, end, limit).unwrap()) |
| 47 | + } |
| 48 | + fn heartbeat(&self, bucket_id: &str, event: Event, duration: f64) -> Result<(), String> { |
| 49 | + self.heartbeat(bucket_id, event, duration).unwrap(); |
| 50 | + self.force_commit().unwrap(); |
| 51 | + Ok(()) |
| 52 | + } |
| 53 | + fn insert_events(&self, bucket_id: &str, events: Vec<Event>) -> Result<Vec<Event>, String> { |
| 54 | + let res = self.insert_events(bucket_id, &events[..]).unwrap(); |
| 55 | + self.force_commit().unwrap(); |
| 56 | + Ok(res) |
| 57 | + } |
| 58 | + fn get_event_count(&self, bucket_id: &str) -> Result<i64, String> { |
| 59 | + Ok(self.get_event_count(bucket_id, None, None).unwrap()) |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +impl AccessMethod for AwClient { |
| 64 | + fn get_buckets(&self) -> Result<HashMap<String, Bucket>, String> { |
| 65 | + Ok(self.get_buckets().unwrap()) |
| 66 | + } |
| 67 | + fn get_bucket(&self, bucket_id: &str) -> Result<Bucket, DatastoreError> { |
| 68 | + let bucket = self.get_bucket(bucket_id); |
| 69 | + match bucket { |
| 70 | + Ok(bucket) => Ok(bucket), |
| 71 | + Err(e) => { |
| 72 | + warn!("{:?}", e); |
| 73 | + let code = e.status().unwrap(); |
| 74 | + if code == StatusCode::NOT_FOUND { |
| 75 | + Err(DatastoreError::NoSuchBucket(bucket_id.into())) |
| 76 | + } else { |
| 77 | + panic!("Unexpected error"); |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + fn get_events( |
| 83 | + &self, |
| 84 | + bucket_id: &str, |
| 85 | + start: Option<DateTime<Utc>>, |
| 86 | + end: Option<DateTime<Utc>>, |
| 87 | + limit: Option<u64>, |
| 88 | + ) -> Result<Vec<Event>, String> { |
| 89 | + Ok(self.get_events(bucket_id, start, end, limit).unwrap()) |
| 90 | + } |
| 91 | + fn insert_events(&self, _bucket_id: &str, _events: Vec<Event>) -> Result<Vec<Event>, String> { |
| 92 | + //Ok(self.insert_events(bucket_id, &events[..]).unwrap()) |
| 93 | + Err("Not implemented".to_string()) |
| 94 | + } |
| 95 | + fn get_event_count(&self, bucket_id: &str) -> Result<i64, String> { |
| 96 | + Ok(self.get_event_count(bucket_id).unwrap()) |
| 97 | + } |
| 98 | + fn create_bucket(&self, bucket: &Bucket) -> Result<(), DatastoreError> { |
| 99 | + self.create_bucket(bucket.id.as_str(), bucket._type.as_str()) |
| 100 | + .unwrap(); |
| 101 | + Ok(()) |
| 102 | + //Err(DatastoreError::InternalError("Not implemented".to_string())) |
| 103 | + } |
| 104 | + fn heartbeat(&self, bucket_id: &str, event: Event, duration: f64) -> Result<(), String> { |
| 105 | + self.heartbeat(bucket_id, &event, duration) |
| 106 | + .map_err(|e| format!("{:?}", e)) |
| 107 | + } |
| 108 | +} |
0 commit comments