Skip to content

Commit dd37640

Browse files
committed
fix: optional query params
1 parent 82b745c commit dd37640

File tree

3 files changed

+33
-21
lines changed

3 files changed

+33
-21
lines changed

pragma-node/src/handlers/entries/get_entry.rs

+20-10
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use axum::extract::{Query, State};
22
use axum::Json;
33
use bigdecimal::num_bigint::ToBigInt;
44

5-
use crate::handlers::entries::GetEntryResponse;
5+
use crate::handlers::entries::{GetEntryResponse, Interval};
66
use crate::infra::repositories::entry_repository::{self, MedianEntry};
77
use crate::utils::PathExtractor;
88
use crate::AppState;
@@ -31,8 +31,22 @@ pub async fn get_entry(
3131
// Construct pair id
3232
let pair_id = currency_pair_to_pair_id(&pair.1, &pair.0);
3333

34+
let now = chrono::Utc::now().naive_utc().timestamp_millis() as u64;
35+
36+
let timestamp = if let Some(timestamp) = params.timestamp {
37+
timestamp
38+
} else {
39+
now
40+
};
41+
42+
let interval = if let Some(interval) = params.interval {
43+
interval
44+
} else {
45+
Interval::OneMinute
46+
};
47+
3448
// Validate given timestamp
35-
if params.timestamp > chrono::Utc::now().naive_utc() {
49+
if timestamp > now {
3650
return Err(EntryError::InvalidTimestamp);
3751
}
3852

@@ -47,14 +61,10 @@ pub async fn get_entry(
4761
}));
4862
}
4963

50-
let entry = entry_repository::get_median_price(
51-
&state.pool,
52-
pair_id.clone(),
53-
params.interval,
54-
params.timestamp,
55-
)
56-
.await
57-
.map_err(|db_error| to_entry_error(db_error, &pair_id))?;
64+
let entry =
65+
entry_repository::get_median_price(&state.pool, pair_id.clone(), interval, timestamp)
66+
.await
67+
.map_err(|db_error| to_entry_error(db_error, &pair_id))?;
5868

5969
let decimals = entry_repository::get_decimals(&state.pool, &pair_id)
6070
.await

pragma-node/src/handlers/entries/mod.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use chrono::NaiveDateTime;
21
use serde::{Deserialize, Serialize};
32

43
use starknet::core::types::FieldElement;
@@ -72,15 +71,15 @@ pub enum Interval {
7271

7372
#[derive(Debug, Deserialize)]
7473
pub struct GetEntryParams {
75-
pub timestamp: NaiveDateTime,
76-
pub interval: Interval,
74+
pub timestamp: Option<u64>,
75+
pub interval: Option<Interval>,
7776
}
7877

7978
impl Default for GetEntryParams {
8079
fn default() -> Self {
8180
Self {
82-
timestamp: chrono::Utc::now().naive_utc(),
83-
interval: Interval::default(),
81+
timestamp: Some(chrono::Utc::now().timestamp_millis() as u64),
82+
interval: Some(Interval::default()),
8483
}
8584
}
8685
}

pragma-node/src/infra/repositories/entry_repository.rs

+9-6
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ pub async fn get_median_price(
8585
pool: &deadpool_diesel::postgres::Pool,
8686
pair_id: String,
8787
interval: Interval,
88-
time: NaiveDateTime,
88+
time: u64,
8989
) -> Result<MedianEntry, InfraError> {
9090
let conn = pool.get().await.map_err(adapt_infra_error)?;
9191

@@ -102,7 +102,7 @@ pub async fn get_median_price(
102102
WHERE
103103
pair_id = $1
104104
AND
105-
time <= $2
105+
bucket <= $2
106106
ORDER BY
107107
time DESC
108108
LIMIT 1;
@@ -120,7 +120,7 @@ pub async fn get_median_price(
120120
WHERE
121121
pair_id = $1
122122
AND
123-
time <= $2
123+
bucket <= $2
124124
ORDER BY
125125
time DESC
126126
LIMIT 1;
@@ -134,23 +134,26 @@ pub async fn get_median_price(
134134
median_price,
135135
num_sources
136136
FROM
137-
price_1_hour_agg
137+
price_1_h_agg
138138
WHERE
139139
pair_id = $1
140140
AND
141-
time <= $2
141+
bucket <= $2
142142
ORDER BY
143143
time DESC
144144
LIMIT 1;
145145
"#
146146
}
147147
};
148148

149+
let date_time =
150+
NaiveDateTime::from_timestamp_millis(time as i64).ok_or(InfraError::InvalidTimeStamp)?;
151+
149152
let raw_entry = conn
150153
.interact(move |conn| {
151154
diesel::sql_query(raw_sql)
152155
.bind::<diesel::sql_types::Text, _>(pair_id)
153-
.bind::<diesel::sql_types::Timestamptz, _>(time)
156+
.bind::<diesel::sql_types::Timestamptz, _>(date_time)
154157
.load::<MedianEntryRaw>(conn)
155158
})
156159
.await

0 commit comments

Comments
 (0)