2021-04-13 20:14:07 +02:00
|
|
|
use std::convert::TryInto;
|
|
|
|
|
|
|
|
use chrono::prelude::*;
|
|
|
|
use eyre::Result;
|
|
|
|
|
2021-04-20 22:53:07 +02:00
|
|
|
use atuin_common::{api::AddHistoryRequest, utils::hash_str};
|
|
|
|
|
|
|
|
use crate::api_client;
|
|
|
|
use crate::database::Database;
|
2021-04-21 19:13:51 +02:00
|
|
|
use crate::encryption::{encrypt, load_encoded_key, load_key};
|
2021-04-20 22:53:07 +02:00
|
|
|
use crate::settings::{Settings, HISTORY_PAGE_SIZE};
|
2021-04-13 20:14:07 +02:00
|
|
|
|
|
|
|
// Currently sync is kinda naive, and basically just pages backwards through
|
|
|
|
// history. This means newly added stuff shows up properly! We also just use
|
|
|
|
// the total count in each database to indicate whether a sync is needed.
|
|
|
|
// I think this could be massively improved! If we had a way of easily
|
|
|
|
// indicating count per time period (hour, day, week, year, etc) then we can
|
|
|
|
// easily pinpoint where we are missing data and what needs downloading. Start
|
|
|
|
// with year, then find the week, then the day, then the hour, then download it
|
|
|
|
// all! The current naive approach will do for now.
|
|
|
|
|
|
|
|
// Check if remote has things we don't, and if so, download them.
|
|
|
|
// Returns (num downloaded, total local)
|
2021-04-20 18:07:11 +02:00
|
|
|
async fn sync_download(
|
2021-04-13 20:14:07 +02:00
|
|
|
force: bool,
|
2021-04-20 18:07:11 +02:00
|
|
|
client: &api_client::Client<'_>,
|
|
|
|
db: &mut (impl Database + Send),
|
2021-04-13 20:14:07 +02:00
|
|
|
) -> Result<(i64, i64)> {
|
2021-04-21 19:13:51 +02:00
|
|
|
debug!("starting sync download");
|
|
|
|
|
2021-04-20 18:07:11 +02:00
|
|
|
let remote_count = client.count().await?;
|
2021-04-13 20:14:07 +02:00
|
|
|
|
2021-04-25 19:21:52 +02:00
|
|
|
let initial_local = db.history_count().await?;
|
2021-04-13 20:14:07 +02:00
|
|
|
let mut local_count = initial_local;
|
|
|
|
|
|
|
|
let mut last_sync = if force {
|
|
|
|
Utc.timestamp_millis(0)
|
|
|
|
} else {
|
2021-04-20 22:53:07 +02:00
|
|
|
Settings::last_sync()?
|
2021-04-13 20:14:07 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
let mut last_timestamp = Utc.timestamp_millis(0);
|
|
|
|
|
|
|
|
let host = if force { Some(String::from("")) } else { None };
|
|
|
|
|
|
|
|
while remote_count > local_count {
|
2021-04-20 18:07:11 +02:00
|
|
|
let page = client
|
|
|
|
.get_history(last_sync, last_timestamp, host.clone())
|
|
|
|
.await?;
|
2021-04-13 20:14:07 +02:00
|
|
|
|
2021-04-25 19:21:52 +02:00
|
|
|
db.save_bulk(&page).await?;
|
2021-04-13 20:14:07 +02:00
|
|
|
|
2021-04-25 19:21:52 +02:00
|
|
|
local_count = db.history_count().await?;
|
2021-04-13 20:14:07 +02:00
|
|
|
|
2021-04-21 19:13:51 +02:00
|
|
|
if page.len() < HISTORY_PAGE_SIZE.try_into().unwrap() {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2021-04-13 20:14:07 +02:00
|
|
|
let page_last = page
|
|
|
|
.last()
|
|
|
|
.expect("could not get last element of page")
|
|
|
|
.timestamp;
|
|
|
|
|
|
|
|
// in the case of a small sync frequency, it's possible for history to
|
|
|
|
// be "lost" between syncs. In this case we need to rewind the sync
|
|
|
|
// timestamps
|
|
|
|
if page_last == last_timestamp {
|
|
|
|
last_timestamp = Utc.timestamp_millis(0);
|
|
|
|
last_sync = last_sync - chrono::Duration::hours(1);
|
|
|
|
} else {
|
|
|
|
last_timestamp = page_last;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok((local_count - initial_local, local_count))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if we have things remote doesn't, and if so, upload them
|
2021-04-20 18:07:11 +02:00
|
|
|
async fn sync_upload(
|
2021-04-13 20:14:07 +02:00
|
|
|
settings: &Settings,
|
|
|
|
_force: bool,
|
2021-04-20 18:07:11 +02:00
|
|
|
client: &api_client::Client<'_>,
|
|
|
|
db: &mut (impl Database + Send),
|
2021-04-13 20:14:07 +02:00
|
|
|
) -> Result<()> {
|
2021-04-21 19:13:51 +02:00
|
|
|
debug!("starting sync upload");
|
|
|
|
|
2021-04-20 18:07:11 +02:00
|
|
|
let initial_remote_count = client.count().await?;
|
2021-04-13 20:14:07 +02:00
|
|
|
let mut remote_count = initial_remote_count;
|
|
|
|
|
2021-04-25 19:21:52 +02:00
|
|
|
let local_count = db.history_count().await?;
|
2021-04-13 20:14:07 +02:00
|
|
|
|
2021-04-21 19:13:51 +02:00
|
|
|
debug!("remote has {}, we have {}", remote_count, local_count);
|
|
|
|
|
2021-04-13 20:14:07 +02:00
|
|
|
let key = load_key(settings)?; // encryption key
|
|
|
|
|
|
|
|
// first just try the most recent set
|
|
|
|
|
|
|
|
let mut cursor = Utc::now();
|
|
|
|
|
|
|
|
while local_count > remote_count {
|
2021-04-25 19:21:52 +02:00
|
|
|
let last = db.before(cursor, HISTORY_PAGE_SIZE).await?;
|
2021-04-13 20:14:07 +02:00
|
|
|
let mut buffer = Vec::<AddHistoryRequest>::new();
|
|
|
|
|
|
|
|
if last.is_empty() {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
for i in last {
|
|
|
|
let data = encrypt(&i, &key)?;
|
|
|
|
let data = serde_json::to_string(&data)?;
|
|
|
|
|
|
|
|
let add_hist = AddHistoryRequest {
|
|
|
|
id: i.id,
|
|
|
|
timestamp: i.timestamp,
|
|
|
|
data,
|
|
|
|
hostname: hash_str(i.hostname.as_str()),
|
|
|
|
};
|
|
|
|
|
|
|
|
buffer.push(add_hist);
|
|
|
|
}
|
|
|
|
|
|
|
|
// anything left over outside of the 100 block size
|
2021-04-20 18:07:11 +02:00
|
|
|
client.post_history(&buffer).await?;
|
2021-04-13 20:14:07 +02:00
|
|
|
cursor = buffer.last().unwrap().timestamp;
|
2021-04-20 18:07:11 +02:00
|
|
|
remote_count = client.count().await?;
|
2021-04-26 12:50:31 +02:00
|
|
|
|
|
|
|
debug!("upload cursor: {:?}", cursor);
|
2021-04-13 20:14:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-04-20 18:07:11 +02:00
|
|
|
pub async fn sync(settings: &Settings, force: bool, db: &mut (impl Database + Send)) -> Result<()> {
|
|
|
|
let client = api_client::Client::new(
|
2021-04-20 22:53:07 +02:00
|
|
|
settings.sync_address.as_str(),
|
|
|
|
settings.session_token.as_str(),
|
2021-04-21 19:13:51 +02:00
|
|
|
load_encoded_key(settings)?,
|
|
|
|
)?;
|
2021-04-13 20:14:07 +02:00
|
|
|
|
2021-04-20 18:07:11 +02:00
|
|
|
sync_upload(settings, force, &client, db).await?;
|
2021-04-13 20:14:07 +02:00
|
|
|
|
2021-04-20 18:07:11 +02:00
|
|
|
let download = sync_download(force, &client, db).await?;
|
2021-04-13 20:14:07 +02:00
|
|
|
|
|
|
|
debug!("sync downloaded {}", download.0);
|
|
|
|
|
2021-04-20 22:53:07 +02:00
|
|
|
Settings::save_sync_time()?;
|
2021-04-13 20:14:07 +02:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|