diff --git a/server/src/storage/mod.rs b/server/src/storage/mod.rs index 5bd1dd6..85035d0 100644 --- a/server/src/storage/mod.rs +++ b/server/src/storage/mod.rs @@ -44,19 +44,31 @@ impl TryFrom<&Row<'_>> for UptimeStorageModel { type Error = rusqlite::Error; fn try_from(row: &Row) -> Result { - let first_seen: DateTime = row.get::<_, String>(1)?.parse().map_err(|e| { - rusqlite::Error::FromSqlConversionFailure(1, rusqlite::types::Type::Text, Box::new(e)) - })?; + let first_seen = DateTime::parse_from_rfc3339(&row.get::<_, String>(2)?) + .map(|dt| dt.with_timezone(&Utc)) + .map_err(|e| { + rusqlite::Error::FromSqlConversionFailure( + 1, + rusqlite::types::Type::Text, + Box::new(e), + ) + })?; - let last_seen: DateTime = row.get::<_, String>(2)?.parse().map_err(|e| { - rusqlite::Error::FromSqlConversionFailure(2, rusqlite::types::Type::Text, Box::new(e)) - })?; + let last_seen = DateTime::parse_from_rfc3339(&row.get::<_, String>(2)?) + .map(|dt| dt.with_timezone(&Utc)) + .map_err(|e| { + rusqlite::Error::FromSqlConversionFailure( + 2, + rusqlite::types::Type::Text, + Box::new(e), + ) + })?; Ok(UptimeStorageModel { id: row.get(0)?, first_seen, last_seen, - message_count: row.get(1)?, + message_count: row.get(3)?, }) } } diff --git a/server/src/storage/sqlite.rs b/server/src/storage/sqlite.rs index 5b06dad..44d8cb9 100644 --- a/server/src/storage/sqlite.rs +++ b/server/src/storage/sqlite.rs @@ -29,11 +29,11 @@ impl SQLiteRepository { message_count INTEGER NOT NULL DEFAULT 0 ) STRICT; - CREATE TABLE IF NOT EXISTS agents ( - id TEXT PRIMARY KEY AUTO_INCREMENT, + CREATE TABLE IF NOT EXISTS messages ( + id INTEGER PRIMARY KEY AUTOINCREMENT, agent_id TEXT NOT NULL, message TEXT NOT NULL, - timestamp TEXT NOT NULL + timestamp TEXT NOT NULL, FOREIGN KEY (agent_id) REFERENCES agents(id) ON DELETE CASCADE ) STRICT; @@ -101,7 +101,7 @@ impl StorageRepository for SQLiteRepository { let (sql, params) = if let Some(duration) = duration { ( - "SELECT agent_id, message, timestamp FROM messages WHERE agent_id = ? AND timestamp <= ?", + "SELECT agent_id, message, timestamp FROM messages WHERE agent_id = ? AND timestamp >= ?", vec![agent.to_string(), duration.to_rfc3339()], ) } else {