aboutsummaryrefslogtreecommitdiffstats
path: root/crates/turtle/src/atuin_server/database/db
diff options
context:
space:
mode:
Diffstat (limited to 'crates/turtle/src/atuin_server/database/db')
-rw-r--r--crates/turtle/src/atuin_server/database/db/mod.rs265
-rw-r--r--crates/turtle/src/atuin_server/database/db/wrappers.rs22
2 files changed, 3 insertions, 284 deletions
diff --git a/crates/turtle/src/atuin_server/database/db/mod.rs b/crates/turtle/src/atuin_server/database/db/mod.rs
index 22d69d3c..e0c6b736 100644
--- a/crates/turtle/src/atuin_server/database/db/mod.rs
+++ b/crates/turtle/src/atuin_server/database/db/mod.rs
@@ -1,5 +1,4 @@
use std::collections::HashMap;
-use std::ops::Range;
use rand::Rng;
@@ -7,19 +6,14 @@ use crate::{
atuin_common::record::{EncryptedData, HostId, Record, RecordIdx, RecordStatus},
atuin_server::database::{
DbError, DbResult, DbSettings,
- calendar::{TimePeriod, TimePeriodInfo},
- into_utc,
- models::{History, NewHistory, NewSession, NewUser, Session, User},
+ models::{NewSession, NewUser, Session, User},
},
};
-use futures_util::TryStreamExt;
-use sqlx::Row;
use sqlx::postgres::PgPoolOptions;
-use time::{Date, Duration, Month, OffsetDateTime, Time, UtcOffset};
use tracing::instrument;
use uuid::Uuid;
-use wrappers::{DbHistory, DbRecord, DbSession, DbUser};
+use wrappers::{DbRecord, DbSession, DbUser};
mod wrappers;
@@ -102,94 +96,6 @@ impl Database {
}
#[instrument(skip_all)]
- pub(crate) async fn calendar(
- &self,
- user: &User,
- period: TimePeriod,
- tz: UtcOffset,
- ) -> DbResult<HashMap<u64, TimePeriodInfo>> {
- let mut ret = HashMap::new();
- let iter: Box<dyn Iterator<Item = DbResult<(u64, Range<Date>)>> + Send> = match period {
- TimePeriod::Year => {
- // First we need to work out how far back to calculate. Get the
- // oldest history item
- let oldest = self
- .oldest_history(user)
- .await?
- .timestamp
- .to_offset(tz)
- .year();
- let current_year = OffsetDateTime::now_utc().to_offset(tz).year();
-
- // All the years we need to get data for
- // The upper bound is exclusive, so include current +1
- let years = oldest..current_year + 1;
-
- Box::new(years.map(|year| {
- let start = Date::from_calendar_date(year, time::Month::January, 1)?;
- let end = Date::from_calendar_date(year + 1, time::Month::January, 1)?;
-
- Ok((year as u64, start..end))
- }))
- }
-
- TimePeriod::Month { year } => {
- let months =
- std::iter::successors(Some(Month::January), |m| Some(m.next())).take(12);
-
- Box::new(months.map(move |month| {
- let start = Date::from_calendar_date(year, month, 1)?;
- let days = start.month().length(year);
- let end = start + Duration::days(days as i64);
-
- Ok((month as u64, start..end))
- }))
- }
-
- TimePeriod::Day { year, month } => {
- let days = 1..month.length(year);
- Box::new(days.map(move |day| {
- let start = Date::from_calendar_date(year, month, day)?;
- let end = start
- .next_day()
- .ok_or_else(|| DbError::Other(eyre::eyre!("no next day?")))?;
-
- Ok((day as u64, start..end))
- }))
- }
- };
-
- for x in iter {
- let (index, range) = x?;
-
- let start = range.start.with_time(Time::MIDNIGHT).assume_offset(tz);
- let end = range.end.with_time(Time::MIDNIGHT).assume_offset(tz);
-
- let count = self.count_history_range(user, start..end).await?;
-
- ret.insert(
- index,
- TimePeriodInfo {
- count: count as u64,
- hash: "".to_string(),
- },
- );
- }
-
- Ok(ret)
- }
-
- #[instrument(skip_all)]
- pub(crate) async fn get_session(&self, token: &str) -> DbResult<Session> {
- sqlx::query_as("select id, user_id, token from sessions where token = $1")
- .bind(token)
- .fetch_one(self.read_pool())
- .await
- .map_err(Into::into)
- .map(|DbSession(session)| session)
- }
-
- #[instrument(skip_all)]
pub(crate) async fn get_user(&self, username: &str) -> DbResult<User> {
sqlx::query_as("select id, username, email, password from users where username = $1")
.bind(username)
@@ -214,36 +120,6 @@ impl Database {
.map(|DbUser(user)| user)
}
- #[instrument(skip_all)]
- pub(crate) async fn count_history(&self, user: &User) -> DbResult<i64> {
- // The cache is new, and the user might not yet have a cache value.
- // They will have one as soon as they post up some new history, but handle that
- // edge case.
-
- let res: (i64,) = sqlx::query_as(
- "select count(1) from history
- where user_id = $1",
- )
- .bind(user.id)
- .fetch_one(self.read_pool())
- .await?;
-
- Ok(res.0)
- }
-
- #[instrument(skip_all)]
- pub(crate) async fn count_history_cached(&self, user: &User) -> DbResult<i64> {
- let res: (i32,) = sqlx::query_as(
- "select total from total_history_count_user
- where user_id = $1",
- )
- .bind(user.id)
- .fetch_one(self.read_pool())
- .await?;
-
- Ok(res.0 as i64)
- }
-
pub(crate) async fn delete_store(&self, user: &User) -> DbResult<()> {
let mut tx = self.pool.begin().await?;
@@ -268,128 +144,6 @@ impl Database {
Ok(())
}
- pub(crate) async fn delete_history(&self, user: &User, id: String) -> DbResult<()> {
- sqlx::query(
- "update history
- set deleted_at = $3
- where user_id = $1
- and client_id = $2
- and deleted_at is null", // don't just keep setting it
- )
- .bind(user.id)
- .bind(id)
- .bind(OffsetDateTime::now_utc())
- .fetch_all(&self.pool)
- .await?;
-
- Ok(())
- }
-
- #[instrument(skip_all)]
- pub(crate) async fn deleted_history(&self, user: &User) -> DbResult<Vec<String>> {
- // The cache is new, and the user might not yet have a cache value.
- // They will have one as soon as they post up some new history, but handle that
- // edge case.
-
- let res = sqlx::query(
- "select client_id from history
- where user_id = $1
- and deleted_at is not null",
- )
- .bind(user.id)
- .fetch_all(self.read_pool())
- .await?;
-
- let res = res
- .iter()
- .map(|row| row.get::<String, _>("client_id"))
- .collect();
-
- Ok(res)
- }
-
- #[instrument(skip_all)]
- pub(crate) async fn count_history_range(
- &self,
- user: &User,
- range: Range<OffsetDateTime>,
- ) -> DbResult<i64> {
- let res: (i64,) = sqlx::query_as(
- "select count(1) from history
- where user_id = $1
- and timestamp >= $2::date
- and timestamp < $3::date",
- )
- .bind(user.id)
- .bind(into_utc(range.start))
- .bind(into_utc(range.end))
- .fetch_one(self.read_pool())
- .await?;
-
- Ok(res.0)
- }
-
- #[instrument(skip_all)]
- pub(crate) async fn list_history(
- &self,
- user: &User,
- created_after: OffsetDateTime,
- since: OffsetDateTime,
- host: &str,
- page_size: i64,
- ) -> DbResult<Vec<History>> {
- let res = sqlx::query_as(
- "select id, client_id, user_id, hostname, timestamp, data, created_at from history
- where user_id = $1
- and hostname != $2
- and created_at >= $3
- and timestamp >= $4
- order by timestamp asc
- limit $5",
- )
- .bind(user.id)
- .bind(host)
- .bind(into_utc(created_after))
- .bind(into_utc(since))
- .bind(page_size)
- .fetch(self.read_pool())
- .map_ok(|DbHistory(h)| h)
- .try_collect()
- .await?;
-
- Ok(res)
- }
-
- #[instrument(skip_all)]
- pub(crate) async fn add_history(&self, history: &[NewHistory]) -> DbResult<()> {
- let mut tx = self.pool.begin().await?;
-
- for i in history {
- let client_id: &str = &i.client_id;
- let hostname: &str = &i.hostname;
- let data: &str = &i.data;
-
- sqlx::query(
- "insert into history
- (client_id, user_id, hostname, timestamp, data)
- values ($1, $2, $3, $4, $5)
- on conflict do nothing
- ",
- )
- .bind(client_id)
- .bind(i.user_id)
- .bind(hostname)
- .bind(i.timestamp)
- .bind(data)
- .execute(&mut *tx)
- .await?;
- }
-
- tx.commit().await?;
-
- Ok(())
- }
-
#[instrument(skip_all)]
pub(crate) async fn delete_user(&self, u: &User) -> DbResult<()> {
sqlx::query("delete from sessions where user_id = $1")
@@ -484,21 +238,6 @@ impl Database {
}
#[instrument(skip_all)]
- pub(crate) async fn oldest_history(&self, user: &User) -> DbResult<History> {
- sqlx::query_as(
- "select id, client_id, user_id, hostname, timestamp, data, created_at from history
- where user_id = $1
- order by timestamp asc
- limit 1",
- )
- .bind(user.id)
- .fetch_one(self.read_pool())
- .await
- .map_err(Into::into)
- .map(|DbHistory(h)| h)
- }
-
- #[instrument(skip_all)]
pub(crate) async fn add_records(
&self,
user: &User,
diff --git a/crates/turtle/src/atuin_server/database/db/wrappers.rs b/crates/turtle/src/atuin_server/database/db/wrappers.rs
index de4c5814..c0633202 100644
--- a/crates/turtle/src/atuin_server/database/db/wrappers.rs
+++ b/crates/turtle/src/atuin_server/database/db/wrappers.rs
@@ -1,14 +1,12 @@
use crate::{
atuin_common::record::{EncryptedData, Host, Record},
- atuin_server::database::models::{History, Session, User},
+ atuin_server::database::models::{Session, User},
};
use ::sqlx::{FromRow, Result};
use sqlx::{Row, postgres::PgRow};
-use time::PrimitiveDateTime;
pub struct DbUser(pub User);
pub struct DbSession(pub Session);
-pub struct DbHistory(pub History);
pub struct DbRecord(pub Record<EncryptedData>);
impl<'a> FromRow<'a, PgRow> for DbUser {
@@ -32,24 +30,6 @@ impl<'a> ::sqlx::FromRow<'a, PgRow> for DbSession {
}
}
-impl<'a> ::sqlx::FromRow<'a, PgRow> for DbHistory {
- fn from_row(row: &'a PgRow) -> ::sqlx::Result<Self> {
- Ok(Self(History {
- id: row.try_get("id")?,
- client_id: row.try_get("client_id")?,
- user_id: row.try_get("user_id")?,
- hostname: row.try_get("hostname")?,
- timestamp: row
- .try_get::<PrimitiveDateTime, _>("timestamp")?
- .assume_utc(),
- data: row.try_get("data")?,
- created_at: row
- .try_get::<PrimitiveDateTime, _>("created_at")?
- .assume_utc(),
- }))
- }
-}
-
impl<'a> ::sqlx::FromRow<'a, PgRow> for DbRecord {
fn from_row(row: &'a PgRow) -> ::sqlx::Result<Self> {
let timestamp: i64 = row.try_get("timestamp")?;