Skip to content

Commit

Permalink
src: Use our timestamp functions instead of chrono
Browse files Browse the repository at this point in the history
  • Loading branch information
joaoantoniocardoso committed Oct 4, 2024
1 parent f0413b5 commit 8f3983f
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 14 deletions.
6 changes: 4 additions & 2 deletions src/drivers/rest/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ use lazy_static::lazy_static;
use mavlink::{self, Message};
use serde::{Deserialize, Serialize};

use crate::protocol::timestamp_micros;

lazy_static! {
static ref DATA: Data = Data {
messages: Arc::new(Mutex::new(MAVLinkVehiclesData::default())),
Expand Down Expand Up @@ -135,7 +137,7 @@ struct Temporal {

impl Default for Temporal {
fn default() -> Self {
let now_us = chrono::Local::now().timestamp_micros() as u64;
let now_us = timestamp_micros();
Self {
first_update_us: now_us,
last_update_us: now_us,
Expand All @@ -147,7 +149,7 @@ impl Default for Temporal {

impl Temporal {
fn update(&mut self) {
self.last_update_us = chrono::Local::now().timestamp_micros() as u64;
self.last_update_us = timestamp_micros();
self.counter = self.counter.wrapping_add(1);
self.frequency =
(10e6 * self.counter as f32) / ((self.last_update_us - self.first_update_us) as f32);
Expand Down
5 changes: 2 additions & 3 deletions src/drivers/tlog/reader.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
use std::{path::PathBuf, sync::Arc};

use anyhow::{Context, Result};
use chrono::DateTime;
use mavlink::ardupilotmega::MavMessage;
use mavlink_server::callbacks::{Callbacks, MessageCallback};
use tokio::sync::{broadcast, RwLock};
use tracing::*;

use crate::{
drivers::{Driver, DriverInfo},
protocol::Protocol,
protocol::{check_timestamp_us, Protocol},
stats::{
accumulated::driver::{AccumulatedDriverStats, AccumulatedDriverStatsProvider},
driver::DriverUuid,
Expand Down Expand Up @@ -85,7 +84,7 @@ impl TlogReader {
u64::from_be_bytes(timestamp_bytes)
};

if DateTime::from_timestamp_micros(us_since_epoch as i64).is_none() {
if !check_timestamp_us(us_since_epoch) {
warn!("Failed to convert unix time: {us_since_epoch:?}");

reader.consume(1);
Expand Down
4 changes: 2 additions & 2 deletions src/drivers/tlog/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use tracing::*;

use crate::{
drivers::{Driver, DriverInfo},
protocol::Protocol,
protocol::{timestamp_micros, Protocol},
stats::{
accumulated::driver::{AccumulatedDriverStats, AccumulatedDriverStatsProvider},
driver::DriverUuid,
Expand Down Expand Up @@ -70,7 +70,7 @@ impl TlogWriter {
loop {
match hub_receiver.recv().await {
Ok(message) => {
let timestamp = chrono::Utc::now().timestamp_micros() as u64;
let timestamp = timestamp_micros();

self.stats.write().await.stats.update_output(&message);

Expand Down
4 changes: 2 additions & 2 deletions src/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ impl Protocol {
pub fn new(origin: &str, message: MAVLinkV2MessageRaw) -> Self {
Self {
origin: origin.to_string(),
timestamp: chrono::Utc::now().timestamp_micros() as u64,
timestamp: timestamp_micros(),
message,
}
}
Expand Down Expand Up @@ -83,7 +83,7 @@ impl DerefMut for Protocol {
}

#[inline(always)]
pub fn generate_timestamp_us() -> u64 {
pub fn timestamp_micros() -> u64 {
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.map(|duration| duration.as_micros() as u64)
Expand Down
6 changes: 3 additions & 3 deletions src/stats/accumulated/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::sync::Arc;

use serde::Serialize;

use crate::protocol::Protocol;
use crate::protocol::{timestamp_micros, Protocol};

#[derive(Clone, Debug, Serialize)]
pub struct AccumulatedStatsInner {
Expand All @@ -20,7 +20,7 @@ impl Default for AccumulatedStatsInner {
fn default() -> Self {
Self {
last_message: None,
last_update_us: chrono::Utc::now().timestamp_micros() as u64,
last_update_us: timestamp_micros(),
messages: 0,
bytes: 0,
delay: 0,
Expand All @@ -31,7 +31,7 @@ impl Default for AccumulatedStatsInner {
impl AccumulatedStatsInner {
pub fn update(&mut self, message: &Arc<Protocol>) {
self.last_message = Some(message.clone());
self.last_update_us = chrono::Utc::now().timestamp_micros() as u64;
self.last_update_us = timestamp_micros();
self.bytes = self.bytes.wrapping_add(message.raw_bytes().len() as u64);
self.messages = self.messages.wrapping_add(1);
self.delay = self
Expand Down
5 changes: 3 additions & 2 deletions src/stats/actor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use tracing::*;

use crate::{
hub,
protocol::timestamp_micros,
stats::{
accumulated::{
driver::AccumulatedDriversStats, messages::AccumulatedHubMessagesStats,
Expand Down Expand Up @@ -128,7 +129,7 @@ impl StatsActor {
let last_accumulated_hub_messages_stats =
Arc::new(Mutex::new(AccumulatedHubMessagesStats::default()));
let hub_messages_stats = Arc::new(RwLock::new(HubMessagesStats::default()));
let start_time = Arc::new(RwLock::new(chrono::Utc::now().timestamp_micros() as u64));
let start_time = Arc::new(RwLock::new(timestamp_micros()));

Self {
start_time,
Expand Down Expand Up @@ -180,7 +181,7 @@ impl StatsActor {
if let Err(error) = hub::reset_all_stats().await {
error!("Failed resetting stats: {error:?}");
}
*self.start_time.write().await = chrono::Utc::now().timestamp_micros() as u64;
*self.start_time.write().await = timestamp_micros();

self.last_accumulated_drivers_stats.lock().await.clear();
driver_stats.clear();
Expand Down

0 comments on commit 8f3983f

Please sign in to comment.