Skip to content

Commit

Permalink
chore: fully switch to time, update deps
Browse files Browse the repository at this point in the history
Signed-off-by: Henry Gressmann <mail@henrygressmann.de>
  • Loading branch information
explodingcamera committed Sep 12, 2024
1 parent ee765fd commit 23fe322
Show file tree
Hide file tree
Showing 14 changed files with 81 additions and 85 deletions.
52 changes: 24 additions & 28 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 18 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,26 +27,33 @@ serde_json={version="1.0"}
md-5="0.10"

# utils
cached="0.53"
argh={version="0.1"}
eyre={version="0.6"}
rand={version="0.8"}
chrono={version="0.4", features=["serde"]}
time={version="0.3"} # need both chrono and time for compatibility with dependencies
time={version="0.3"}
itertools={version="0.13"}
colored={version="2.1"}
figment={version="0.10", features=["toml", "env"]}
sha3={version="0.10"}
argon2={version="0.5"}
crossbeam="0.8"
cached="0.53"
tracing="0.1"
tracing-subscriber={version="0.3", features=["env-filter"]}
async-compression="0.4.12"
tokio-tar={package="krata-tokio-tar", version="0.4.0"}

# web
poem={version="3.0", features=["embed", "session", "cookie", "compression", "tower-compat"]}
poem-openapi={version="5.0", features=["chrono"]}
poem={version="3.1", default-features=false, features=[
"time",
"embed",
"session",
"cookie",
"compression",
"tower-compat",
]}

poem-openapi={version="5.0", default-features=false, features=["time"]}
tower={version="0.5", default-features=false, features=["limit"]}
uaparser="0.6"
mime_guess={version="2.0"}
Expand All @@ -59,8 +66,12 @@ reqwest={version="0.12", default-features=false, features=[
]}

# database
duckdb={version="1.0", features=["bundled", "chrono", "r2d2"]}
rusqlite={version="0.32", features=["bundled", "chrono"]}
duckdb={version="1.0", git="https://github.com/explodingcamera/duckdb-rs", features=[
"bundled",
"time",
"r2d2",
]}
rusqlite={version="0.32", features=["bundled", "time"]}
r2d2={version="0.8"}
r2d2_sqlite="0.25.0"
refinery={version="0.8"}
Expand Down
9 changes: 5 additions & 4 deletions src/app/core/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::sync::Arc;

use crossbeam::{channel::Receiver, sync::ShardedLock};
use eyre::{bail, Result};
use time::OffsetDateTime;

use crate::{
app::{
Expand All @@ -15,12 +16,12 @@ use crate::{
pub struct LiwanEvents {
duckdb: DuckDBPool,
sqlite: SqlitePool,
daily_salt: Arc<ShardedLock<(String, chrono::DateTime<chrono::Utc>)>>,
daily_salt: Arc<ShardedLock<(String, OffsetDateTime)>>,
}

impl LiwanEvents {
pub fn try_new(duckdb: DuckDBPool, sqlite: SqlitePool) -> Result<Self> {
let daily_salt: (String, chrono::DateTime<chrono::Utc>) = {
let daily_salt: (String, OffsetDateTime) = {
tracing::debug!("Loading daily salt");
sqlite.get()?.query_row("select salt, updated_at from salts where id = 1", [], |row| {
Ok((row.get(0)?, row.get(1)?))
Expand All @@ -37,10 +38,10 @@ impl LiwanEvents {
};

// if the salt is older than 24 hours, replace it with a new one (utils::generate_salt)
if chrono::Utc::now() - updated_at > chrono::Duration::hours(24) {
if (OffsetDateTime::now_utc() - updated_at) > time::Duration::hours(24) {
tracing::debug!("Daily salt expired, generating a new one");
let new_salt = generate_salt();
let now = chrono::Utc::now();
let now = OffsetDateTime::now_utc();
let conn = self.sqlite.get()?;
conn.execute("update salts set salt = ?, updated_at = ? where id = 1", rusqlite::params![&new_salt, now])?;

Expand Down
16 changes: 8 additions & 8 deletions src/app/core/reports.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use std::collections::BTreeMap;
use std::fmt::{Debug, Display};

use crate::utils::{validate, TimeExt};
use crate::{app::DuckDBConn, utils::Timestamp};
use crate::app::DuckDBConn;
use crate::utils::validate;
use duckdb::params;
use eyre::Result;
use itertools::Itertools;
use poem_openapi::{Enum, Object};
use time::OffsetDateTime;

// TODO: more fine-grained caching (e.g. don't cache for short durations/ending in now)
// use cached::proc_macro::cached;
Expand All @@ -17,8 +18,8 @@ use poem_openapi::{Enum, Object};

#[derive(Object)]
pub struct DateRange {
pub start: Timestamp,
pub end: Timestamp,
pub start: OffsetDateTime,
pub end: OffsetDateTime,
}

impl Display for DateRange {
Expand Down Expand Up @@ -204,8 +205,7 @@ pub fn overall_report(
");

let mut stmt = conn.prepare_cached(&query)?;
let params =
params![range.start.to_time(), range.end.to_time(), data_points, data_points, event, range.end.to_time()];
let params = params![range.start, range.end, data_points, data_points, event, range.end];

match metric {
Metric::Views | Metric::UniqueVisitors | Metric::Sessions => {
Expand Down Expand Up @@ -280,7 +280,7 @@ pub fn overall_stats(
");

let mut stmt = conn.prepare_cached(&query)?;
let params = params![range.start.to_time(), range.end.to_time(), event];
let params = params![range.start, range.end, event];

let result = stmt.query_row(duckdb::params_from_iter(params), |row| {
Ok(ReportStats {
Expand Down Expand Up @@ -365,7 +365,7 @@ pub fn dimension_report(
);

let mut stmt = conn.prepare_cached(&query)?;
let params = params![range.start.to_time(), range.end.to_time(), event];
let params = params![range.start, range.end, event];

match metric {
Metric::Views | Metric::UniqueVisitors | Metric::Sessions => {
Expand Down
9 changes: 5 additions & 4 deletions src/app/core/sessions.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::app::SqlitePool;
use eyre::Result;
use time::OffsetDateTime;

#[derive(Clone)]
pub struct LiwanSessions {
Expand All @@ -12,7 +13,7 @@ impl LiwanSessions {
}

/// Create a new session
pub fn create(&self, session_id: &str, username: &str, expires_at: chrono::DateTime<chrono::Utc>) -> Result<()> {
pub fn create(&self, session_id: &str, username: &str, expires_at: OffsetDateTime) -> Result<()> {
let conn = self.pool.get()?;
let mut stmt = conn.prepare_cached("insert into sessions (id, username, expires_at) values (?, ?, ?)")?;
stmt.execute(rusqlite::params![session_id, username, expires_at])?;
Expand All @@ -24,9 +25,9 @@ impl LiwanSessions {
pub fn get(&self, session_id: &str) -> Result<Option<String>> {
let conn = self.pool.get()?;
let mut stmt = conn.prepare_cached("select username, expires_at from sessions where id = ?")?;
let (username, expires_at): (String, chrono::DateTime<chrono::Utc>) =
let (username, expires_at): (String, OffsetDateTime) =
stmt.query_row([session_id], |row| Ok((row.get("username")?, row.get("expires_at")?)))?;
if expires_at < chrono::Utc::now() {
if expires_at < OffsetDateTime::now_utc() {
return Ok(None);
}
Ok(Some(username))
Expand All @@ -36,7 +37,7 @@ impl LiwanSessions {
pub fn delete(&self, session_id: &str) -> Result<()> {
let conn = self.pool.get()?;
let mut stmt = conn.prepare_cached("update sessions set expires_at = ? where id = ?")?;
stmt.execute(rusqlite::params![chrono::Utc::now(), session_id])?;
stmt.execute(rusqlite::params![OffsetDateTime::now_utc(), session_id])?;
Ok(())
}
}
5 changes: 3 additions & 2 deletions src/app/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use duckdb::DuckdbConnectionManager;
use eyre::Result;
use r2d2_sqlite::SqliteConnectionManager;
use std::sync::Arc;
use time::OffsetDateTime;

pub type DuckDBConn = r2d2::PooledConnection<DuckdbConnectionManager>;
pub type DuckDBPool = r2d2::Pool<DuckdbConnectionManager>;
Expand Down Expand Up @@ -132,8 +133,8 @@ impl Liwan {
)?;
}

let start = chrono::Utc::now().checked_sub_signed(chrono::Duration::days(365)).unwrap();
let end = chrono::Utc::now();
let start = OffsetDateTime::now_utc().checked_sub(time::Duration::days(365)).unwrap();
let end = OffsetDateTime::now_utc();
for (entity_id, display_name, fqdn, project_ids) in entities.iter() {
self.entities.create(
&models::Entity { id: entity_id.to_string(), display_name: display_name.to_string() },
Expand Down
3 changes: 2 additions & 1 deletion src/app/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ pub struct Event {
pub entity_id: String,
pub visitor_id: String,
pub event: String,
pub created_at: chrono::DateTime<chrono::Utc>,
pub created_at: OffsetDateTime,
pub fqdn: Option<String>,
pub path: Option<String>,
pub referrer: Option<String>,
Expand Down Expand Up @@ -90,3 +90,4 @@ macro_rules! event_params {
pub use event_params;
use poem_openapi::Enum;
use serde::{Deserialize, Serialize};
use time::OffsetDateTime;
Loading

0 comments on commit 23fe322

Please sign in to comment.