Skip to content

Commit

Permalink
fix: minor progress on sync
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikBjare committed May 15, 2021
1 parent 4323765 commit 257be7c
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 17 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions aw-client-rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ impl AwClient {

pub fn get_bucket(&self, bucketname: &str) -> Result<Bucket, reqwest::Error> {
let url = format!("{}/api/0/buckets/{}", self.baseurl, bucketname);
let bucket: Bucket = self.client.get(&url).send()?.json()?;
let bucket = self.client.get(&url).send()?.error_for_status()?.json()?;
Ok(bucket)
}

Expand Down Expand Up @@ -128,8 +128,8 @@ impl AwClient {

pub fn get_event_count(&self, bucketname: &str) -> Result<i64, reqwest::Error> {
let url = format!("{}/api/0/buckets/{}/events/count", self.baseurl, bucketname);
let res = self.client.get(&url).send()?.text()?;
let count: i64 = match res.parse() {
let res = self.client.get(&url).send()?.error_for_status()?.text()?;
let count: i64 = match res.trim().parse() {
Ok(count) => count,
Err(err) => panic!("could not parse get_event_count response: {:?}", err),
};
Expand Down
1 change: 1 addition & 0 deletions aw-sync/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ log = "0.4"
chrono = { version = "0.4", features = ["serde"] }
serde = "1.0"
serde_json = "1.0"
reqwest = { version = "0.11", features = ["json", "blocking"] }
aw-server = { path = "../aw-server" }
aw-models = { path = "../aw-models" }
aw-datastore = { path = "../aw-datastore" }
Expand Down
41 changes: 27 additions & 14 deletions aw-sync/src/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
/// It manages a sync-folder by syncing the aw-server datastore with a copy/staging datastore in the folder (one for each host).
/// The sync folder is then synced with remotes using Syncthing/Dropbox/whatever.
extern crate chrono;
extern crate reqwest;
extern crate serde_json;

use std::collections::HashMap;
Expand All @@ -14,6 +15,7 @@ use std::path::Path;

use aw_client_rust::AwClient;
use chrono::{DateTime, Duration, Utc};
use reqwest::StatusCode;

use aw_datastore::{Datastore, DatastoreError};
use aw_models::{Bucket, Event};
Expand Down Expand Up @@ -76,7 +78,19 @@ impl AccessMethod for AwClient {
Ok(self.get_buckets().unwrap())
}
fn get_bucket(&self, bucket_id: &str) -> Result<Bucket, DatastoreError> {
Ok(self.get_bucket(bucket_id).unwrap())
let bucket = self.get_bucket(bucket_id);
match bucket {
Ok(bucket) => Ok(bucket),
Err(e) => {
warn!("{:?}", e);
let code = e.status().unwrap();
if code == StatusCode::NOT_FOUND {
Err(DatastoreError::NoSuchBucket(bucket_id.into()))
} else {
panic!("Unexpected error");
}
}
}
}
fn get_events(
&self,
Expand All @@ -91,9 +105,8 @@ impl AccessMethod for AwClient {
//Ok(self.insert_events(bucket_id, &events[..]).unwrap())
Err("Not implemented".to_string())
}
fn get_event_count(&self, _bucket_id: &str) -> Result<i64, String> {
//Ok(self.get_event_count(bucket_id, None, None).unwrap())
Err("Not implemented".to_string())
fn get_event_count(&self, bucket_id: &str) -> Result<i64, String> {
Ok(self.get_event_count(bucket_id).unwrap())
}
fn create_bucket(&self, bucket: &Bucket) -> Result<(), DatastoreError> {
self.create_bucket(bucket.id.as_str(), bucket._type.as_str())
Expand All @@ -113,14 +126,15 @@ pub fn sync_run() {
fs::create_dir_all(sync_directory).unwrap();

// TODO: Use the local datastore here, preferably passed from main
let ds_local = Datastore::new(
sync_directory
.join("test-local.db")
.into_os_string()
.into_string()
.unwrap(),
false,
);
let ds_local = AwClient::new("127.0.0.1", "5666", "aw-sync-rust");
//let ds_local = Datastore::new(
// sync_directory
// .join("test-local.db")
// .into_os_string()
// .into_string()
// .unwrap(),
// false,
//);
info!("Set up local datastore");
//log_buckets(&ds_local)?;

Expand Down Expand Up @@ -185,8 +199,7 @@ fn setup_test(sync_directory: &Path) -> std::io::Result<Vec<Datastore>> {
timestamp.to_rfc3339(),
i
);
let event = serde_json::from_str(&event_jsonstr).unwrap();
event
serde_json::from_str(&event_jsonstr).unwrap()
})
.collect::<Vec<Event>>();

Expand Down

0 comments on commit 257be7c

Please sign in to comment.