Skip to content

Commit 82b745c

Browse files
committed
feat: query by timestamp
1 parent 7efcf12 commit 82b745c

File tree

2 files changed

+18
-10
lines changed

2 files changed

+18
-10
lines changed

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

+10-10
Original file line numberDiff line numberDiff line change
@@ -47,18 +47,18 @@ pub async fn get_entry(
4747
}));
4848
}
4949

50-
// Get entries from database with given pair id (only the latest one grouped by publisher)
51-
let entry = entry_repository::get_median_price(&state.pool, pair_id.clone(), params.interval)
52-
.await
53-
.map_err(|db_error| to_entry_error(db_error, pair_id.clone()))?;
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))?;
5458

5559
let decimals = entry_repository::get_decimals(&state.pool, &pair_id)
5660
.await
57-
.map_err(|db_error| match db_error {
58-
InfraError::InternalServerError => EntryError::InternalServerError,
59-
InfraError::NotFound => EntryError::NotFound(pair_id.clone()),
60-
InfraError::InvalidTimeStamp => EntryError::InvalidTimestamp,
61-
})?;
61+
.map_err(|db_error| to_entry_error(db_error, &pair_id))?;
6262

6363
Ok(Json(adapt_entry_to_entry_response(
6464
pair_id, &entry, decimals,
@@ -86,7 +86,7 @@ fn adapt_entry_to_entry_response(
8686
}
8787
}
8888

89-
fn to_entry_error(error: InfraError, pair_id: String) -> EntryError {
89+
fn to_entry_error(error: InfraError, pair_id: &String) -> EntryError {
9090
match error {
9191
InfraError::InternalServerError => EntryError::InternalServerError,
9292
InfraError::NotFound => EntryError::NotFound(pair_id.to_string()),

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

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

@@ -100,6 +101,8 @@ pub async fn get_median_price(
100101
price_1_min_agg
101102
WHERE
102103
pair_id = $1
104+
AND
105+
time <= $2
103106
ORDER BY
104107
time DESC
105108
LIMIT 1;
@@ -116,6 +119,8 @@ pub async fn get_median_price(
116119
price_15_min_agg
117120
WHERE
118121
pair_id = $1
122+
AND
123+
time <= $2
119124
ORDER BY
120125
time DESC
121126
LIMIT 1;
@@ -132,6 +137,8 @@ pub async fn get_median_price(
132137
price_1_hour_agg
133138
WHERE
134139
pair_id = $1
140+
AND
141+
time <= $2
135142
ORDER BY
136143
time DESC
137144
LIMIT 1;
@@ -143,6 +150,7 @@ pub async fn get_median_price(
143150
.interact(move |conn| {
144151
diesel::sql_query(raw_sql)
145152
.bind::<diesel::sql_types::Text, _>(pair_id)
153+
.bind::<diesel::sql_types::Timestamptz, _>(time)
146154
.load::<MedianEntryRaw>(conn)
147155
})
148156
.await

0 commit comments

Comments
 (0)