Skip to content

Commit

Permalink
288: Uniform datetime precision
Browse files Browse the repository at this point in the history
  • Loading branch information
m-milek committed Dec 23, 2024
1 parent ce8f6dd commit 396a3dd
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 12 deletions.
5 changes: 3 additions & 2 deletions backend/src/api/report/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use crate::nlp::nlp_request::NlpRequest;
use crate::report::report::{new_report_id, Report, ReportAnalysesMap, ReportMetadata};
use crate::report::report_request::ReportRequest;
use crate::report::report_status::ReportStatus;
use crate::util::get_utc_timestamp;
use crate::validation::validated::Validated;
use crate::websocket::SystemMessage;
use crate::websocket::SystemMessage::ReportError;
Expand Down Expand Up @@ -46,8 +47,8 @@ pub async fn nlp_analysis<T: RedditFeedData>(
is_public: true,
status: ReportStatus::InProgress,
metadata: ReportMetadata {
created_at: Utc::now(),
updated_at: Utc::now(),
created_at: get_utc_timestamp(),
updated_at: get_utc_timestamp(),
},
analyses_map: ReportAnalysesMap { analyses },
};
Expand Down
5 changes: 3 additions & 2 deletions backend/src/auth/jwt.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use crate::auth::error::AuthError;
use crate::auth::google::JwtUserInfo;
use crate::auth::user::User;
use chrono::{Duration, Utc};
use crate::util::get_utc_timestamp;
use chrono::Duration;
use jsonwebtoken::{decode, DecodingKey, Header, TokenData, Validation};
use log_derive::logfn;
use serde::{Deserialize, Serialize};
Expand All @@ -21,7 +22,7 @@ pub fn create_jwt(user_info: User) -> String {
let secret = dotenvy::var("JWT_SECRET").expect("JWT_SECRET should be set");

let claim = {
let now = Utc::now();
let now = get_utc_timestamp();
let duration = Duration::days(30);
let iat = now.timestamp() as usize;
let exp = (now + duration).timestamp() as usize;
Expand Down
11 changes: 6 additions & 5 deletions backend/src/db/impls/report_repository.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::db::model::DbReport;
use crate::db::pagination::DbPagination;
use crate::nlp::analysis::NlpAnalysisKind;
use crate::report::report::Report;
use crate::util::get_utc_timestamp;
use axum::async_trait;
use chrono::{DateTime, NaiveDateTime, Utc};
use serde::Deserialize;
Expand Down Expand Up @@ -150,7 +151,7 @@ impl ReportQuery {
let start_date = self
.start_date
.unwrap_or(NaiveDateTime::UNIX_EPOCH.and_utc());
let end_date = self.end_date.unwrap_or(Utc::now());
let end_date = self.end_date.unwrap_or(get_utc_timestamp());
let title_pattern = self.title_pattern.unwrap_or("".to_string());

ReportQueryBindArgs {
Expand Down Expand Up @@ -298,7 +299,7 @@ mod tests {

#[test]
fn test_into_where_clauses_date_range() {
let now = Utc::now();
let now = get_utc_timestamp();
let query = ReportQuery {
start_date: Some(now - chrono::Duration::days(1)),
end_date: Some(now),
Expand All @@ -312,7 +313,7 @@ mod tests {

#[test]
fn test_into_where_clauses_date_range_empty() {
let now = Utc::now();
let now = get_utc_timestamp();
let query = ReportQuery::default();
let args = query.into_bind_args();

Expand Down Expand Up @@ -377,8 +378,8 @@ mod tests {
NlpAnalysisKind::Sentiment,
NlpAnalysisKind::HateSpeech,
],
start_date: Some(Utc::now() - chrono::Duration::days(3)),
end_date: Some(Utc::now()),
start_date: Some(get_utc_timestamp() - chrono::Duration::days(3)),
end_date: Some(get_utc_timestamp()),
title_pattern: Some("test".to_string()),
include_my_reports: true,
pagination: DbPagination::new(0, 10),
Expand Down
6 changes: 3 additions & 3 deletions backend/src/db/impls/test_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use crate::auth::user::{GoogleId, User};
use crate::report::report::{Report, ReportAnalysesMap, ReportMetadata};
use crate::report::report_status::ReportStatus;
use chrono::Utc;
use crate::util::get_utc_timestamp;
use std::collections::HashMap;

pub(super) fn get_test_user() -> User {
Expand All @@ -29,8 +29,8 @@ pub(super) fn get_test_report(title: String, user_id: GoogleId) -> Report {
is_public: false,
status: ReportStatus::InProgress,
metadata: ReportMetadata {
created_at: Utc::now(),
updated_at: Utc::now(),
created_at: get_utc_timestamp(),
updated_at: get_utc_timestamp(),
},
analyses_map: ReportAnalysesMap {
analyses: HashMap::new(),
Expand Down
1 change: 1 addition & 0 deletions backend/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ mod nlp;
mod open_api;
mod report;
mod startup;
mod util;
mod validation;
mod websocket;

Expand Down
9 changes: 9 additions & 0 deletions backend/src/util/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use chrono::{DateTime, SubsecRound, Utc};

/// Returns the UTC DateTime timestamp rounded to 6 subsecond digits.
///
/// This is due to how Postgres handles timestamps. It returns them with 6 digits,
/// as opposed to [chrono::Utc]'s 9 digits.
pub fn get_utc_timestamp() -> DateTime<Utc> {
Utc::now().round_subsecs(6)
}

0 comments on commit 396a3dd

Please sign in to comment.