mirror of
https://github.com/Start9Labs/start-os.git
synced 2026-03-30 20:14:49 +00:00
Feature/remove postgres (#2570)
* wip: move postgres data to patchdb * wip * wip * wip * complete notifications and clean up warnings * fill in user agent * move os tor bindings to single call
This commit is contained in:
@@ -1,24 +1,23 @@
|
||||
use std::collections::HashMap;
|
||||
use std::collections::BTreeMap;
|
||||
use std::fmt;
|
||||
use std::str::FromStr;
|
||||
|
||||
use chrono::{DateTime, TimeZone, Utc};
|
||||
use chrono::{DateTime, Utc};
|
||||
use clap::builder::ValueParserFactory;
|
||||
use clap::Parser;
|
||||
use color_eyre::eyre::eyre;
|
||||
use imbl_value::InternedString;
|
||||
use models::PackageId;
|
||||
use rpc_toolkit::{command, from_fn_async, HandlerExt, ParentHandler};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use sqlx::PgPool;
|
||||
use tokio::sync::Mutex;
|
||||
use tracing::instrument;
|
||||
|
||||
use crate::backup::BackupReport;
|
||||
use crate::context::{CliContext, RpcContext};
|
||||
use crate::db::model::DatabaseModel;
|
||||
use crate::prelude::*;
|
||||
use crate::util::clap::FromStrParser;
|
||||
use crate::util::serde::HandlerExtSerde;
|
||||
use crate::{Error, ErrorKind, ResultExt};
|
||||
|
||||
// #[command(subcommands(list, delete, delete_before, create))]
|
||||
pub fn notification() -> ParentHandler {
|
||||
@@ -53,132 +52,102 @@ pub fn notification() -> ParentHandler {
|
||||
#[serde(rename_all = "kebab-case")]
|
||||
#[command(rename_all = "kebab-case")]
|
||||
pub struct ListParams {
|
||||
before: Option<i32>,
|
||||
|
||||
limit: Option<u32>,
|
||||
before: Option<u32>,
|
||||
limit: Option<usize>,
|
||||
}
|
||||
// #[command(display(display_serializable))]
|
||||
#[instrument(skip_all)]
|
||||
pub async fn list(
|
||||
ctx: RpcContext,
|
||||
ListParams { before, limit }: ListParams,
|
||||
) -> Result<Vec<Notification>, Error> {
|
||||
let limit = limit.unwrap_or(40);
|
||||
match before {
|
||||
None => {
|
||||
let records = sqlx::query!(
|
||||
"SELECT id, package_id, created_at, code, level, title, message, data FROM notifications ORDER BY id DESC LIMIT $1",
|
||||
limit as i64
|
||||
).fetch_all(&ctx.secret_store).await?;
|
||||
let notifs = records
|
||||
.into_iter()
|
||||
.map(|r| {
|
||||
Ok(Notification {
|
||||
id: r.id as u32,
|
||||
package_id: r.package_id.and_then(|p| p.parse().ok()),
|
||||
created_at: Utc.from_utc_datetime(&r.created_at),
|
||||
code: r.code as u32,
|
||||
level: match r.level.parse::<NotificationLevel>() {
|
||||
Ok(a) => a,
|
||||
Err(e) => return Err(e.into()),
|
||||
},
|
||||
title: r.title,
|
||||
message: r.message,
|
||||
data: match r.data {
|
||||
None => serde_json::Value::Null,
|
||||
Some(v) => match v.parse::<serde_json::Value>() {
|
||||
Ok(a) => a,
|
||||
Err(e) => {
|
||||
return Err(Error::new(
|
||||
eyre!("Invalid Notification Data: {}", e),
|
||||
ErrorKind::ParseDbField,
|
||||
))
|
||||
}
|
||||
},
|
||||
},
|
||||
})
|
||||
})
|
||||
.collect::<Result<Vec<Notification>, Error>>()?;
|
||||
|
||||
ctx.db
|
||||
.mutate(|d| {
|
||||
d.as_public_mut()
|
||||
) -> Result<Vec<NotificationWithId>, Error> {
|
||||
ctx.db
|
||||
.mutate(|db| {
|
||||
let limit = limit.unwrap_or(40);
|
||||
match before {
|
||||
None => {
|
||||
let records = db
|
||||
.as_private()
|
||||
.as_notifications()
|
||||
.as_entries()?
|
||||
.into_iter()
|
||||
.take(limit);
|
||||
let notifs = records
|
||||
.into_iter()
|
||||
.map(|(id, notification)| {
|
||||
Ok(NotificationWithId {
|
||||
id,
|
||||
notification: notification.de()?,
|
||||
})
|
||||
})
|
||||
.collect::<Result<Vec<NotificationWithId>, Error>>()?;
|
||||
db.as_public_mut()
|
||||
.as_server_info_mut()
|
||||
.as_unread_notification_count_mut()
|
||||
.ser(&0)
|
||||
})
|
||||
.await?;
|
||||
Ok(notifs)
|
||||
}
|
||||
Some(before) => {
|
||||
let records = sqlx::query!(
|
||||
"SELECT id, package_id, created_at, code, level, title, message, data FROM notifications WHERE id < $1 ORDER BY id DESC LIMIT $2",
|
||||
before,
|
||||
limit as i64
|
||||
).fetch_all(&ctx.secret_store).await?;
|
||||
let res = records
|
||||
.into_iter()
|
||||
.map(|r| {
|
||||
Ok(Notification {
|
||||
id: r.id as u32,
|
||||
package_id: r.package_id.and_then(|p| p.parse().ok()),
|
||||
created_at: Utc.from_utc_datetime(&r.created_at),
|
||||
code: r.code as u32,
|
||||
level: match r.level.parse::<NotificationLevel>() {
|
||||
Ok(a) => a,
|
||||
Err(e) => return Err(e.into()),
|
||||
},
|
||||
title: r.title,
|
||||
message: r.message,
|
||||
data: match r.data {
|
||||
None => serde_json::Value::Null,
|
||||
Some(v) => match v.parse::<serde_json::Value>() {
|
||||
Ok(a) => a,
|
||||
Err(e) => {
|
||||
return Err(Error::new(
|
||||
eyre!("Invalid Notification Data: {}", e),
|
||||
ErrorKind::ParseDbField,
|
||||
))
|
||||
}
|
||||
},
|
||||
},
|
||||
})
|
||||
})
|
||||
.collect::<Result<Vec<Notification>, Error>>()?;
|
||||
Ok(res)
|
||||
}
|
||||
}
|
||||
.ser(&0)?;
|
||||
Ok(notifs)
|
||||
}
|
||||
Some(before) => {
|
||||
let records = db
|
||||
.as_private()
|
||||
.as_notifications()
|
||||
.as_entries()?
|
||||
.into_iter()
|
||||
.filter(|(id, _)| *id < before)
|
||||
.take(limit);
|
||||
records
|
||||
.into_iter()
|
||||
.map(|(id, notification)| {
|
||||
Ok(NotificationWithId {
|
||||
id,
|
||||
notification: notification.de()?,
|
||||
})
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
}
|
||||
})
|
||||
.await
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Serialize, Parser)]
|
||||
#[serde(rename_all = "kebab-case")]
|
||||
#[command(rename_all = "kebab-case")]
|
||||
pub struct DeleteParams {
|
||||
id: i32,
|
||||
id: u32,
|
||||
}
|
||||
|
||||
pub async fn delete(ctx: RpcContext, DeleteParams { id }: DeleteParams) -> Result<(), Error> {
|
||||
sqlx::query!("DELETE FROM notifications WHERE id = $1", id)
|
||||
.execute(&ctx.secret_store)
|
||||
.await?;
|
||||
Ok(())
|
||||
ctx.db
|
||||
.mutate(|db| {
|
||||
db.as_private_mut().as_notifications_mut().remove(&id)?;
|
||||
Ok(())
|
||||
})
|
||||
.await
|
||||
}
|
||||
#[derive(Deserialize, Serialize, Parser)]
|
||||
#[serde(rename_all = "kebab-case")]
|
||||
#[command(rename_all = "kebab-case")]
|
||||
pub struct DeleteBeforeParams {
|
||||
before: i32,
|
||||
before: u32,
|
||||
}
|
||||
|
||||
pub async fn delete_before(
|
||||
ctx: RpcContext,
|
||||
DeleteBeforeParams { before }: DeleteBeforeParams,
|
||||
) -> Result<(), Error> {
|
||||
sqlx::query!("DELETE FROM notifications WHERE id < $1", before)
|
||||
.execute(&ctx.secret_store)
|
||||
.await?;
|
||||
Ok(())
|
||||
ctx.db
|
||||
.mutate(|db| {
|
||||
for id in db.as_private().as_notifications().keys()? {
|
||||
if id < before {
|
||||
db.as_private_mut().as_notifications_mut().remove(&id)?;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
})
|
||||
.await
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Serialize, Parser)]
|
||||
#[serde(rename_all = "kebab-case")]
|
||||
#[command(rename_all = "kebab-case")]
|
||||
@@ -198,8 +167,8 @@ pub async fn create(
|
||||
message,
|
||||
}: CreateParams,
|
||||
) -> Result<(), Error> {
|
||||
ctx.notification_manager
|
||||
.notify(ctx.db.clone(), package, level, title, message, (), None)
|
||||
ctx.db
|
||||
.mutate(|db| notify(db, package, level, title, message, ()))
|
||||
.await
|
||||
}
|
||||
|
||||
@@ -254,120 +223,95 @@ impl fmt::Display for InvalidNotificationLevel {
|
||||
write!(f, "Invalid Notification Level: {}", self.0)
|
||||
}
|
||||
}
|
||||
#[derive(Debug, serde::Serialize, serde::Deserialize)]
|
||||
|
||||
#[derive(Debug, Default, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "kebab-case")]
|
||||
pub struct Notifications(pub BTreeMap<u32, Notification>);
|
||||
impl Notifications {
|
||||
pub fn new() -> Self {
|
||||
Self(BTreeMap::new())
|
||||
}
|
||||
}
|
||||
impl Map for Notifications {
|
||||
type Key = u32;
|
||||
type Value = Notification;
|
||||
fn key_str(key: &Self::Key) -> Result<impl AsRef<str>, Error> {
|
||||
Self::key_string(key)
|
||||
}
|
||||
fn key_string(key: &Self::Key) -> Result<InternedString, Error> {
|
||||
Ok(InternedString::from_display(key))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "kebab-case")]
|
||||
pub struct Notification {
|
||||
id: u32,
|
||||
package_id: Option<PackageId>,
|
||||
created_at: DateTime<Utc>,
|
||||
code: u32,
|
||||
level: NotificationLevel,
|
||||
title: String,
|
||||
message: String,
|
||||
data: serde_json::Value,
|
||||
data: Value,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "kebab-case")]
|
||||
pub struct NotificationWithId {
|
||||
id: u32,
|
||||
#[serde(flatten)]
|
||||
notification: Notification,
|
||||
}
|
||||
|
||||
pub trait NotificationType:
|
||||
serde::Serialize + for<'de> serde::Deserialize<'de> + std::fmt::Debug
|
||||
{
|
||||
const CODE: i32;
|
||||
const CODE: u32;
|
||||
}
|
||||
|
||||
impl NotificationType for () {
|
||||
const CODE: i32 = 0;
|
||||
const CODE: u32 = 0;
|
||||
}
|
||||
impl NotificationType for BackupReport {
|
||||
const CODE: i32 = 1;
|
||||
const CODE: u32 = 1;
|
||||
}
|
||||
|
||||
pub struct NotificationManager {
|
||||
sqlite: PgPool,
|
||||
cache: Mutex<HashMap<(Option<PackageId>, NotificationLevel, String), i64>>,
|
||||
}
|
||||
impl NotificationManager {
|
||||
pub fn new(sqlite: PgPool) -> Self {
|
||||
NotificationManager {
|
||||
sqlite,
|
||||
cache: Mutex::new(HashMap::new()),
|
||||
}
|
||||
}
|
||||
#[instrument(skip(db, subtype, self))]
|
||||
pub async fn notify<T: NotificationType>(
|
||||
&self,
|
||||
db: PatchDb,
|
||||
package_id: Option<PackageId>,
|
||||
level: NotificationLevel,
|
||||
title: String,
|
||||
message: String,
|
||||
subtype: T,
|
||||
debounce_interval: Option<u32>,
|
||||
) -> Result<(), Error> {
|
||||
let peek = db.peek().await;
|
||||
if !self
|
||||
.should_notify(&package_id, &level, &title, debounce_interval)
|
||||
.await
|
||||
{
|
||||
return Ok(());
|
||||
}
|
||||
let mut count = peek
|
||||
.as_public()
|
||||
.as_server_info()
|
||||
.as_unread_notification_count()
|
||||
.de()?;
|
||||
let sql_package_id = package_id.as_ref().map(|p| &**p);
|
||||
let sql_code = T::CODE;
|
||||
let sql_level = format!("{}", level);
|
||||
let sql_data =
|
||||
serde_json::to_string(&subtype).with_kind(crate::ErrorKind::Serialization)?;
|
||||
sqlx::query!(
|
||||
"INSERT INTO notifications (package_id, code, level, title, message, data) VALUES ($1, $2, $3, $4, $5, $6)",
|
||||
sql_package_id,
|
||||
sql_code as i32,
|
||||
sql_level,
|
||||
title,
|
||||
message,
|
||||
sql_data
|
||||
).execute(&self.sqlite).await?;
|
||||
count += 1;
|
||||
db.mutate(|db| {
|
||||
db.as_public_mut()
|
||||
.as_server_info_mut()
|
||||
.as_unread_notification_count_mut()
|
||||
.ser(&count)
|
||||
})
|
||||
.await
|
||||
}
|
||||
async fn should_notify(
|
||||
&self,
|
||||
package_id: &Option<PackageId>,
|
||||
level: &NotificationLevel,
|
||||
title: &String,
|
||||
debounce_interval: Option<u32>,
|
||||
) -> bool {
|
||||
let mut guard = self.cache.lock().await;
|
||||
let k = (package_id.clone(), level.clone(), title.clone());
|
||||
let v = (*guard).get(&k);
|
||||
match v {
|
||||
None => {
|
||||
(*guard).insert(k, Utc::now().timestamp());
|
||||
true
|
||||
}
|
||||
Some(last_issued) => match debounce_interval {
|
||||
None => {
|
||||
(*guard).insert(k, Utc::now().timestamp());
|
||||
true
|
||||
}
|
||||
Some(interval) => {
|
||||
if last_issued + interval as i64 > Utc::now().timestamp() {
|
||||
false
|
||||
} else {
|
||||
(*guard).insert(k, Utc::now().timestamp());
|
||||
true
|
||||
}
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
#[instrument(skip(subtype, db))]
|
||||
pub fn notify<T: NotificationType>(
|
||||
db: &mut DatabaseModel,
|
||||
package_id: Option<PackageId>,
|
||||
level: NotificationLevel,
|
||||
title: String,
|
||||
message: String,
|
||||
subtype: T,
|
||||
) -> Result<(), Error> {
|
||||
let data = to_value(&subtype)?;
|
||||
db.as_public_mut()
|
||||
.as_server_info_mut()
|
||||
.as_unread_notification_count_mut()
|
||||
.mutate(|c| {
|
||||
*c += 1;
|
||||
Ok(())
|
||||
})?;
|
||||
let id = db
|
||||
.as_private()
|
||||
.as_notifications()
|
||||
.keys()?
|
||||
.into_iter()
|
||||
.max()
|
||||
.map_or(0, |id| id + 1);
|
||||
db.as_private_mut().as_notifications_mut().insert(
|
||||
&id,
|
||||
&Notification {
|
||||
package_id,
|
||||
created_at: Utc::now(),
|
||||
code: T::CODE,
|
||||
level,
|
||||
title,
|
||||
message,
|
||||
data,
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
Reference in New Issue
Block a user