Skip to content

Commit

Permalink
Handle min_refreshed_at argument
Browse files Browse the repository at this point in the history
  • Loading branch information
kurotych committed Nov 12, 2024
1 parent 6691728 commit 31ea478
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions mobile_config/src/gateway_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,14 @@ pub(crate) mod db {
join key_to_assets kta on infos.asset = kta.asset
"#;
const BATCH_SQL_WHERE_SNIPPET: &str = " where kta.entity_key = any($1::bytea[]) ";
const DEVICE_TYPES_WHERE_SNIPPET: &str = " where device_type::text = any($1) ";
const DEVICE_TYPES_AND_SNIPPET: &str = " and device_type::text = any($2) ";

lazy_static::lazy_static! {
static ref BATCH_METADATA_SQL: String = format!("{GET_METADATA_SQL} {BATCH_SQL_WHERE_SNIPPET}");
static ref DEVICE_TYPES_METADATA_SQL: String = format!("{GET_METADATA_SQL} {DEVICE_TYPES_WHERE_SNIPPET}");
static ref GET_METADATA_SQL_REFRESHED_AT: String = format!("{GET_METADATA_SQL} where infos.refreshed_at > $1");

static ref DEVICE_TYPES_METADATA_SQL: String = format!("{} {}", *GET_METADATA_SQL_REFRESHED_AT, DEVICE_TYPES_AND_SNIPPET);

}

pub async fn get_info(
Expand Down Expand Up @@ -180,14 +183,19 @@ pub(crate) mod db {
pub fn all_info_stream<'a>(
db: impl PgExecutor<'a> + 'a,
device_types: &'a [DeviceType],
_min_refreshed_at: i64, // TODO
min_refreshed_at: i64,
) -> impl Stream<Item = GatewayInfo> + 'a {
// TODO fix unwrap
let min_refreshed_at = DateTime::<Utc>::from_timestamp(min_refreshed_at, 0).unwrap();

match device_types.is_empty() {
true => sqlx::query_as::<_, GatewayInfo>(GET_METADATA_SQL)
true => sqlx::query_as::<_, GatewayInfo>(&GET_METADATA_SQL_REFRESHED_AT)
.bind(min_refreshed_at)
.fetch(db)
.filter_map(|metadata| async move { metadata.ok() })
.boxed(),
false => sqlx::query_as::<_, GatewayInfo>(&DEVICE_TYPES_METADATA_SQL)
.bind(min_refreshed_at)
.bind(
device_types
.iter()
Expand Down

0 comments on commit 31ea478

Please sign in to comment.