Revert to storing history as nanos
This commit is contained in:
parent
156893d774
commit
4f16e8411e
3 changed files with 41 additions and 18 deletions
|
@ -1,7 +1,7 @@
|
||||||
-- Add migration script here
|
-- Add migration script here
|
||||||
create table if not exists history (
|
create table if not exists history (
|
||||||
id text primary key,
|
id text primary key,
|
||||||
timestamp text not null,
|
timestamp integer not null,
|
||||||
duration integer not null,
|
duration integer not null,
|
||||||
exit integer not null,
|
exit integer not null,
|
||||||
command text not null,
|
command text not null,
|
||||||
|
|
|
@ -2,11 +2,15 @@ use std::path::Path;
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
|
|
||||||
use async_trait::async_trait;
|
use async_trait::async_trait;
|
||||||
|
use chrono::prelude::*;
|
||||||
use chrono::Utc;
|
use chrono::Utc;
|
||||||
|
|
||||||
use eyre::Result;
|
use eyre::Result;
|
||||||
|
|
||||||
use sqlx::sqlite::{SqliteConnectOptions, SqliteJournalMode, SqlitePool, SqlitePoolOptions};
|
use sqlx::sqlite::{
|
||||||
|
SqliteConnectOptions, SqliteJournalMode, SqlitePool, SqlitePoolOptions, SqliteRow,
|
||||||
|
};
|
||||||
|
use sqlx::Row;
|
||||||
|
|
||||||
use super::history::History;
|
use super::history::History;
|
||||||
|
|
||||||
|
@ -78,7 +82,7 @@ impl Sqlite {
|
||||||
values(?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8)",
|
values(?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8)",
|
||||||
)
|
)
|
||||||
.bind(h.id.as_str())
|
.bind(h.id.as_str())
|
||||||
.bind(h.timestamp.to_rfc3339())
|
.bind(h.timestamp.timestamp_nanos())
|
||||||
.bind(h.duration)
|
.bind(h.duration)
|
||||||
.bind(h.exit)
|
.bind(h.exit)
|
||||||
.bind(h.command.as_str())
|
.bind(h.command.as_str())
|
||||||
|
@ -90,6 +94,19 @@ impl Sqlite {
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn query_history(row: SqliteRow) -> History {
|
||||||
|
History {
|
||||||
|
id: row.get("id"),
|
||||||
|
timestamp: Utc.timestamp_nanos(row.get("timestamp")),
|
||||||
|
duration: row.get("duration"),
|
||||||
|
exit: row.get("exit"),
|
||||||
|
command: row.get("command"),
|
||||||
|
cwd: row.get("cwd"),
|
||||||
|
session: row.get("session"),
|
||||||
|
hostname: row.get("hostname"),
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[async_trait]
|
#[async_trait]
|
||||||
|
@ -121,8 +138,9 @@ impl Database for Sqlite {
|
||||||
async fn load(&self, id: &str) -> Result<History> {
|
async fn load(&self, id: &str) -> Result<History> {
|
||||||
debug!("loading history item {}", id);
|
debug!("loading history item {}", id);
|
||||||
|
|
||||||
let res = sqlx::query_as::<_, History>("select * from history where id = ?1")
|
let res = sqlx::query("select * from history where id = ?1")
|
||||||
.bind(id)
|
.bind(id)
|
||||||
|
.map(Self::query_history)
|
||||||
.fetch_one(&self.pool)
|
.fetch_one(&self.pool)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
|
@ -138,7 +156,7 @@ impl Database for Sqlite {
|
||||||
where id = ?1",
|
where id = ?1",
|
||||||
)
|
)
|
||||||
.bind(h.id.as_str())
|
.bind(h.id.as_str())
|
||||||
.bind(h.timestamp.to_rfc3339())
|
.bind(h.timestamp.timestamp_nanos())
|
||||||
.bind(h.duration)
|
.bind(h.duration)
|
||||||
.bind(h.exit)
|
.bind(h.exit)
|
||||||
.bind(h.command.as_str())
|
.bind(h.command.as_str())
|
||||||
|
@ -181,7 +199,8 @@ impl Database for Sqlite {
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
let res = sqlx::query_as::<_, History>(query.as_str())
|
let res = sqlx::query(query.as_str())
|
||||||
|
.map(Self::query_history)
|
||||||
.fetch_all(&self.pool)
|
.fetch_all(&self.pool)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
|
@ -195,11 +214,12 @@ impl Database for Sqlite {
|
||||||
) -> Result<Vec<History>> {
|
) -> Result<Vec<History>> {
|
||||||
debug!("listing history from {:?} to {:?}", from, to);
|
debug!("listing history from {:?} to {:?}", from, to);
|
||||||
|
|
||||||
let res = sqlx::query_as::<_, History>(
|
let res = sqlx::query(
|
||||||
"select * from history where timestamp >= ?1 and timestamp <= ?2 order by timestamp asc",
|
"select * from history where timestamp >= ?1 and timestamp <= ?2 order by timestamp asc",
|
||||||
)
|
)
|
||||||
.bind(from)
|
.bind(from)
|
||||||
.bind(to)
|
.bind(to)
|
||||||
|
.map(Self::query_history)
|
||||||
.fetch_all(&self.pool)
|
.fetch_all(&self.pool)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
|
@ -207,19 +227,20 @@ impl Database for Sqlite {
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn first(&self) -> Result<History> {
|
async fn first(&self) -> Result<History> {
|
||||||
let res = sqlx::query_as::<_, History>(
|
let res =
|
||||||
"select * from history where duration >= 0 order by timestamp asc limit 1",
|
sqlx::query("select * from history where duration >= 0 order by timestamp asc limit 1")
|
||||||
)
|
.map(Self::query_history)
|
||||||
.fetch_one(&self.pool)
|
.fetch_one(&self.pool)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
Ok(res)
|
Ok(res)
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn last(&self) -> Result<History> {
|
async fn last(&self) -> Result<History> {
|
||||||
let res = sqlx::query_as::<_, History>(
|
let res = sqlx::query(
|
||||||
"select * from history where duration >= 0 order by timestamp desc limit 1",
|
"select * from history where duration >= 0 order by timestamp desc limit 1",
|
||||||
)
|
)
|
||||||
|
.map(Self::query_history)
|
||||||
.fetch_one(&self.pool)
|
.fetch_one(&self.pool)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
|
@ -227,11 +248,12 @@ impl Database for Sqlite {
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn before(&self, timestamp: chrono::DateTime<Utc>, count: i64) -> Result<Vec<History>> {
|
async fn before(&self, timestamp: chrono::DateTime<Utc>, count: i64) -> Result<Vec<History>> {
|
||||||
let res = sqlx::query_as::<_, History>(
|
let res = sqlx::query(
|
||||||
"select * from history where timestamp < ?1 order by timestamp desc limit ?2",
|
"select * from history where timestamp < ?1 order by timestamp desc limit ?2",
|
||||||
)
|
)
|
||||||
.bind(timestamp)
|
.bind(timestamp.timestamp_nanos())
|
||||||
.bind(count)
|
.bind(count)
|
||||||
|
.map(Self::query_history)
|
||||||
.fetch_all(&self.pool)
|
.fetch_all(&self.pool)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
|
@ -250,7 +272,7 @@ impl Database for Sqlite {
|
||||||
let query = query.to_string().replace("*", "%"); // allow wildcard char
|
let query = query.to_string().replace("*", "%"); // allow wildcard char
|
||||||
let limit = limit.map_or("".to_owned(), |l| format!("limit {}", l));
|
let limit = limit.map_or("".to_owned(), |l| format!("limit {}", l));
|
||||||
|
|
||||||
let res = sqlx::query_as::<_, History>(
|
let res = sqlx::query(
|
||||||
format!(
|
format!(
|
||||||
"select * from history
|
"select * from history
|
||||||
where command like ?1 || '%'
|
where command like ?1 || '%'
|
||||||
|
@ -260,6 +282,7 @@ impl Database for Sqlite {
|
||||||
.as_str(),
|
.as_str(),
|
||||||
)
|
)
|
||||||
.bind(query)
|
.bind(query)
|
||||||
|
.map(Self::query_history)
|
||||||
.fetch_all(&self.pool)
|
.fetch_all(&self.pool)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
|
@ -267,7 +290,8 @@ impl Database for Sqlite {
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn query_history(&self, query: &str) -> Result<Vec<History>> {
|
async fn query_history(&self, query: &str) -> Result<Vec<History>> {
|
||||||
let res = sqlx::query_as::<_, History>(query)
|
let res = sqlx::query(query)
|
||||||
|
.map(Self::query_history)
|
||||||
.fetch_all(&self.pool)
|
.fetch_all(&self.pool)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
|
|
|
@ -122,7 +122,6 @@ async fn sync_upload(
|
||||||
// anything left over outside of the 100 block size
|
// anything left over outside of the 100 block size
|
||||||
client.post_history(&buffer).await?;
|
client.post_history(&buffer).await?;
|
||||||
cursor = buffer.last().unwrap().timestamp;
|
cursor = buffer.last().unwrap().timestamp;
|
||||||
|
|
||||||
remote_count = client.count().await?;
|
remote_count = client.count().await?;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue