Skip to content

Commit

Permalink
feat: use NativeTime instead of unix timestamp to have more accurate …
Browse files Browse the repository at this point in the history
…price timestamps
  • Loading branch information
keyvankhademi committed May 12, 2024
1 parent 646951c commit 3fbabf0
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 21 deletions.
6 changes: 1 addition & 5 deletions src/agent/dashboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,7 @@ impl MetricsServer {
};

let last_local_update_string = if let Some(local_data) = price_data.local_data {
if let Some(datetime) = DateTime::from_timestamp(local_data.timestamp, 0) {
datetime.format("%Y-%m-%d %H:%M:%S").to_string()
} else {
format!("Invalid timestamp {}", local_data.timestamp)
}
local_data.timestamp.format("%Y-%m-%d %H:%M:%S").to_string()
} else {
"no data".to_string()
};
Expand Down
6 changes: 2 additions & 4 deletions src/agent/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,7 @@ use {
},
warp::{
hyper::StatusCode,
reply::{
self,
},
reply,
Filter,
Rejection,
Reply,
Expand Down Expand Up @@ -428,7 +426,7 @@ impl PriceLocalMetrics {
.get_or_create(&PriceLocalLabels {
pubkey: price_key.to_string(),
})
.set(price_info.timestamp);
.set(price_info.timestamp.and_utc().timestamp());
update_count
.get_or_create(&PriceLocalLabels {
pubkey: price_key.to_string(),
Expand Down
2 changes: 1 addition & 1 deletion src/agent/pythd/adapter/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ impl AdapterApi for Adapter {
status: Adapter::map_status(&status)?,
price,
conf,
timestamp: Utc::now().timestamp(),
timestamp: Utc::now().naive_utc(),
},
})
.await
Expand Down
19 changes: 10 additions & 9 deletions src/agent/solana/exporter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ impl Exporter {
let publish_keypair = self.get_publish_keypair().await?;
self.update_our_prices(&publish_keypair.pubkey());

let now = Utc::now();
let now = Utc::now().naive_utc();

debug!(self.logger, "Exporter: filtering prices permissioned to us";
"our_prices" => format!("{:?}", self.our_prices.keys()),
Expand All @@ -450,14 +450,14 @@ impl Exporter {
.into_iter()
.filter(|(_identifier, info)| {
// Filter out timestamps that are old
(now.timestamp() - info.timestamp) < self.config.staleness_threshold.as_secs() as i64
now < info.timestamp + self.config.staleness_threshold
})
.filter(|(identifier, info)| {
// Filter out unchanged price data if the max delay wasn't reached

if let Some(last_info) = self.last_published_state.get(identifier) {
if info.timestamp.saturating_sub(last_info.timestamp)
> self.config.unchanged_publish_threshold.as_secs() as i64
if info.timestamp
> last_info.timestamp + self.config.unchanged_publish_threshold
{
true // max delay since last published state reached, we publish anyway
} else {
Expand All @@ -470,13 +470,14 @@ impl Exporter {
.filter(|(id, _data)| {
let key_from_id = Pubkey::from((*id).clone().to_bytes());
if let Some(publisher_permission) = self.our_prices.get(&key_from_id) {
let ret = publisher_permission.schedule.can_publish_at(&now);
let now_utc = Utc::now();
let ret = publisher_permission.schedule.can_publish_at(&now_utc);

if !ret {
debug!(self.logger, "Exporter: Attempted to publish price outside market hours";
"price_account" => key_from_id.to_string(),
"schedule" => format!("{:?}", publisher_permission.schedule),
"utc_time" => now.format("%c").to_string(),
"utc_time" => now_utc.format("%c").to_string(),
);
}

Expand Down Expand Up @@ -512,7 +513,7 @@ impl Exporter {
let publisher_permission = publisher_permisssion.unwrap();

if let Some(publish_interval) = publisher_permission.publish_interval {
if info.timestamp.saturating_sub(last_info.timestamp) < publish_interval.as_secs() as i64 {
if info.timestamp < last_info.timestamp + publish_interval {
// Updating the price too soon after the last update, skipping
return false;
}
Expand Down Expand Up @@ -641,9 +642,9 @@ impl Exporter {
let network_state = *self.network_state_rx.borrow();
for (identifier, price_info_result) in refreshed_batch {
let price_info = price_info_result?;
let now = Utc::now().naive_utc();

let stale_price = (Utc::now().timestamp() - price_info.timestamp)
> self.config.staleness_threshold.as_secs() as i64;
let stale_price = now > price_info.timestamp + self.config.staleness_threshold;
if stale_price {
continue;
}
Expand Down
4 changes: 2 additions & 2 deletions src/agent/store/local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use {
anyhow,
Result,
},
pyth_sdk::UnixTimestamp,
chrono::NaiveDateTime,
pyth_sdk_solana::state::PriceStatus,
slog::Logger,
solana_sdk::bs58,
Expand All @@ -30,7 +30,7 @@ pub struct PriceInfo {
pub status: PriceStatus,
pub price: i64,
pub conf: u64,
pub timestamp: UnixTimestamp,
pub timestamp: NaiveDateTime,
}

impl PriceInfo {
Expand Down

0 comments on commit 3fbabf0

Please sign in to comment.