update 2
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
use chrono::Utc;
|
||||
|
||||
use crate::db::{Db, db_error};
|
||||
use crate::error::AppResult;
|
||||
|
||||
/// Soma `bytes` ao total trafegado pelo proxy no mês corrente (UTC), usado
|
||||
/// para cobrar o custo de consumo de dados no orçamento mensal.
|
||||
pub async fn add_bytes(db: &Db, bytes: i64) -> AppResult<()> {
|
||||
if bytes <= 0 {
|
||||
return Ok(());
|
||||
}
|
||||
let client = db.client().await?;
|
||||
let month = Utc::now().format("%Y-%m").to_string();
|
||||
client
|
||||
.execute(
|
||||
"INSERT INTO data_usage_monthly (month, bytes) VALUES ($1, $2)
|
||||
ON CONFLICT (month) DO UPDATE SET bytes = data_usage_monthly.bytes + EXCLUDED.bytes",
|
||||
&[&month, &bytes],
|
||||
)
|
||||
.await
|
||||
.map_err(db_error)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Total de bytes trafegados pelo proxy no mês corrente (UTC).
|
||||
pub async fn monthly_bytes(db: &Db) -> AppResult<i64> {
|
||||
let client = db.client().await?;
|
||||
let month = Utc::now().format("%Y-%m").to_string();
|
||||
let bytes: Option<i64> = client
|
||||
.query_opt(
|
||||
"SELECT bytes FROM data_usage_monthly WHERE month = $1",
|
||||
&[&month],
|
||||
)
|
||||
.await
|
||||
.map_err(db_error)?
|
||||
.map(|row| row.get(0));
|
||||
Ok(bytes.unwrap_or(0))
|
||||
}
|
||||
Reference in New Issue
Block a user