Skip to content

Commit

Permalink
Merge pull request #980 from block-mesh/more-db-opt
Browse files Browse the repository at this point in the history
More db opt
  • Loading branch information
ohaddahan authored Feb 4, 2025
2 parents dd7efcd + 0ee3df7 commit d2e185e
Show file tree
Hide file tree
Showing 26 changed files with 559 additions and 277 deletions.
104 changes: 52 additions & 52 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ dead_code = "forbid"

[workspace.package]
authors = ["Ohad Dahan <ohaddahan@gmail.com>"]
version = "0.0.470"
version = "0.0.471"

[patch.crates-io]
## aes-gcm-siv 0.10.3 and curve25519-dalek 3.x pin zeroize to <1.4
Expand Down
9 changes: 8 additions & 1 deletion libs/block-mesh-common/src/interfaces/server_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ pub struct DashboardResponse {
pub calls_to_action: Vec<CallToActionUI>,
pub referral_summary: ReferralSummary,
pub verified_email: bool,
pub user_ips: Vec<UserIpInfo>,
// pub user_ips: Vec<UserIpInfo>,
pub wallet_address: Option<String>,
}

Expand Down Expand Up @@ -719,3 +719,10 @@ pub struct GetTwitterProfileDetails {
pub code: String,
pub username: String,
}

#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct DebugEndpoint {
pub code: String,
pub method: String,
pub user_id: Uuid,
}
2 changes: 2 additions & 0 deletions libs/block-mesh-common/src/routes_enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::fmt::{Display, Formatter};

#[allow(non_camel_case_types)]
pub enum RoutesEnum {
Static_Admin_Debug,
Static_Admin_Referral,
Static_UnAuth_Map,
Static_UnAuth_AuthStatus,
Expand Down Expand Up @@ -56,6 +57,7 @@ pub enum RoutesEnum {
impl Display for RoutesEnum {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match &self {
RoutesEnum::Static_Admin_Debug => write!(f, "/admin_debug"),
RoutesEnum::Static_Admin_Referral => write!(f, "/admin_referral"),
RoutesEnum::Api_ReferralBonus => write!(f, "/referral_bonus"),
RoutesEnum::Static_Auth_Proof_Of_Humanity => write!(f, "/proof_of_humanity"),
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ pub fn init_rand() -> i32 {
random_number
}

#[tracing::instrument(name = "bulk_get_or_create_aggregate_by_user_and_name", skip_all)]
pub async fn bulk_get_or_create_aggregate_by_user_and_name(
#[tracing::instrument(name = "bulk_get_or_create_aggregate_by_user_and_name_old", skip_all)]
pub async fn bulk_get_or_create_aggregate_by_user_and_name_old(
transaction: &mut Transaction<'_, Postgres>,
user_id: &Uuid,
) -> anyhow::Result<Vec<Aggregate>> {
Expand Down Expand Up @@ -62,11 +62,68 @@ inserted AS (
updated_at
)
-- Combine results from inserted and existing records
SELECT id, created_at, user_id, name, value, updated_at
FROM inserted
SELECT id, created_at, user_id, name, value, updated_at FROM inserted
UNION ALL
SELECT id, created_at, user_id, name, value, updated_at FROM extant;
"#,
user_id,
value,
upload,
download,
latency
)
.fetch_all(&mut **transaction)
.await?;
let aggregates = aggregates
.into_iter()
.map(|aggregate| Aggregate {
id: aggregate.id.expect("MISSING ID"),
user_id: aggregate.user_id.expect("MISSING USER ID"),
name: aggregate.name.expect("MISSING NAME").into(),
value: aggregate.value.expect("MISSING VALUE"),
created_at: aggregate.created_at.expect("MISSING TIMESTAMP CREATED_AT"),
updated_at: aggregate.updated_at.expect("MISSING TIMESTAMP UPDATED_AT"),
})
.collect();
Ok(aggregates)
}

#[tracing::instrument(name = "bulk_get_or_create_aggregate_by_user_and_name", skip_all)]
pub async fn bulk_get_or_create_aggregate_by_user_and_name(
transaction: &mut Transaction<'_, Postgres>,
user_id: &Uuid,
) -> anyhow::Result<Vec<Aggregate>> {
let upload = Value::from(init_rand());
let download = Value::from(init_rand());
let latency = Value::from(init_rand());
let value = Value::Null;
let aggregates: Vec<AggregateTmp> = sqlx::query_as!(
AggregateTmp,
r#"
WITH input_data(id, created_at, updated_at, user_id, name, value) AS (
VALUES
(gen_random_uuid(), now(), now(), $1::uuid, 'Uptime', $2::jsonb),
(gen_random_uuid(), now(), now(), $1::uuid, 'Download', $4::jsonb),
(gen_random_uuid(), now(), now(), $1::uuid, 'Upload', $3::jsonb),
(gen_random_uuid(), now(), now(), $1::uuid, 'Latency', $5::jsonb),
(gen_random_uuid(), now(), now(), $1::uuid, 'Tasks', $2::jsonb)
),
upsert AS (
INSERT INTO aggregates (id, created_at, user_id, name, value, updated_at, dummy_updated_at)
SELECT id, created_at, user_id, name, value, created_at, created_at
FROM input_data
ON CONFLICT (user_id, name) DO NOTHING
RETURNING id, created_at, user_id, name, value, updated_at
)
SELECT id, created_at, user_id, name, value, updated_at FROM upsert
UNION ALL
SELECT id, created_at, user_id, name, value, updated_at
FROM extant;
SELECT a.id, a.created_at, a.user_id, a.name, a.value, a.updated_at FROM aggregates a
JOIN input_data i USING (user_id, name)
WHERE NOT EXISTS (
SELECT 1
FROM upsert u
WHERE u.user_id = i.user_id AND u.name = i.name
);
"#,
user_id,
value,
Expand Down

This file was deleted.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

This file was deleted.

Loading

0 comments on commit d2e185e

Please sign in to comment.