Verified Commit 98473e0e authored by Mattia Righetti's avatar Mattia Righetti
Browse files

refactor: impl query to get both publ and totals relays/bridges

parent 397bc0b4
Loading
Loading
Loading
Loading
+39 −0
Original line number Diff line number Diff line
@@ -67,3 +67,42 @@ pub async fn bridges_published(pg: &PgPool) -> Result<String, String> {

    Ok(row.to_string())
}

pub async fn published(pg: &PgPool) -> Result<(String, String), String> {
    let row = sqlx::query(
        r"WITH ss AS (
            SELECT DISTINCT ON (fingerprint)
                *
            FROM server_status
            where published >= current_timestamp - INTERVAL '1 week'
            ORDER BY fingerprint, published DESC, is_bridge asc
        )
        SELECT MAX(published) FROM ss WHERE is_bridge = true union
        SELECT MAX(published) FROM ss WHERE is_bridge = false;",
    )
    .map(|row: PgRow| row.get::<NaiveDateTime, _>(0).to_string())
    .fetch_all(pg)
    .await
    .unwrap();

    Ok((row[0].clone(), row[1].clone()))
}

pub async fn totals(pg: &PgPool) -> Result<(i64, i64), String> {
    let row = sqlx::query(
        r"WITH ss AS (
            SELECT DISTINCT ON (fingerprint) *
            FROM server_status
            WHERE published >= current_timestamp - INTERVAL '1 week'
            ORDER BY fingerprint, published DESC, is_bridge
        )
        SELECT COUNT(*) FROM ss WHERE is_bridge = true UNION
        SELECT COUNT(*) FROM ss WHERE is_bridge = false;",
    )
    .map(|row: PgRow| row.get::<i64, _>(0))
    .fetch_all(pg)
    .await
    .unwrap();

    Ok((row[0], row[1]))
}
+5 −6
Original line number Diff line number Diff line
@@ -5,13 +5,12 @@ pub fn get_truncated(
    limit: Option<PositiveOrZeroValue>,
    offset: Option<PositiveOrZeroValue>,
) -> i32 {
    if limit.is_none() {
        return 0;
    }

    let limit: i32 = limit.unwrap().into();
    let limit: i32 = match limit {
        Some(limit) => limit.into(),
        None => 0,
    };

    let offset: i32 = match offset {
    let offset = match offset {
        Some(offset) => offset.into(),
        None => 0,
    };