From 8d47a7537713759c65f424e28eee3eb8786d5d52 Mon Sep 17 00:00:00 2001 From: Justin Chang Date: Tue, 8 Oct 2024 19:45:31 -0700 Subject: [PATCH] edits --- .../down.sql | 1 + .../up.sql | 3 +- .../src/asset_uploader/api/get_status.rs | 28 ++++------ .../src/asset_uploader/api/mod.rs | 4 +- .../models/asset_uploader_request_statuses.rs | 51 ++----------------- .../asset_uploader_request_statuses_query.rs | 2 +- ecosystem/nft-metadata-crawler/src/schema.rs | 2 +- 7 files changed, 20 insertions(+), 71 deletions(-) diff --git a/ecosystem/nft-metadata-crawler/migrations/2024-10-02-062003_add_asset_uploader_requests_table/down.sql b/ecosystem/nft-metadata-crawler/migrations/2024-10-02-062003_add_asset_uploader_requests_table/down.sql index 76697328d7b60..8c4d8349ff9da 100644 --- a/ecosystem/nft-metadata-crawler/migrations/2024-10-02-062003_add_asset_uploader_requests_table/down.sql +++ b/ecosystem/nft-metadata-crawler/migrations/2024-10-02-062003_add_asset_uploader_requests_table/down.sql @@ -1 +1,2 @@ +DROP INDEX IF EXISTS asset_uploader_request_status_code; DROP TABLE IF EXISTS nft_metadata_crawler.asset_uploader_request_statuses; diff --git a/ecosystem/nft-metadata-crawler/migrations/2024-10-02-062003_add_asset_uploader_requests_table/up.sql b/ecosystem/nft-metadata-crawler/migrations/2024-10-02-062003_add_asset_uploader_requests_table/up.sql index 4a32d4df17490..1766b6b6fdd70 100644 --- a/ecosystem/nft-metadata-crawler/migrations/2024-10-02-062003_add_asset_uploader_requests_table/up.sql +++ b/ecosystem/nft-metadata-crawler/migrations/2024-10-02-062003_add_asset_uploader_requests_table/up.sql @@ -2,10 +2,11 @@ CREATE TABLE nft_metadata_crawler.asset_uploader_request_statuses ( request_id UUID NOT NULL, asset_uri VARCHAR NOT NULL, application_id UUID NOT NULL, - status_code BIGINT, + status_code BIGINT NOT NULL DEFAULT 202, error_message VARCHAR, cdn_image_uri VARCHAR, num_failures BIGINT NOT NULL DEFAULT 0, inserted_at TIMESTAMP NOT NULL DEFAULT NOW(), PRIMARY KEY (request_id, asset_uri) ); +CREATE INDEX IF NOT EXISTS asset_uploader_request_status_code ON nft_metadata_crawler.asset_uploader_request_statuses (status_code); diff --git a/ecosystem/nft-metadata-crawler/src/asset_uploader/api/get_status.rs b/ecosystem/nft-metadata-crawler/src/asset_uploader/api/get_status.rs index 8e75358a7c089..badb30c22d66c 100644 --- a/ecosystem/nft-metadata-crawler/src/asset_uploader/api/get_status.rs +++ b/ecosystem/nft-metadata-crawler/src/asset_uploader/api/get_status.rs @@ -6,6 +6,7 @@ use crate::{ models::asset_uploader_request_statuses_query::AssetUploaderRequestStatusesQuery, schema, }; use ahash::AHashMap; +use axum::http::StatusCode; use diesel::{ r2d2::{ConnectionManager, Pool, PooledConnection}, ExpressionMethods, PgConnection, QueryDsl, RunQueryDsl, @@ -23,28 +24,17 @@ pub fn get_status( let mut status_response = AHashMap::new(); let rows = query_status(&mut conn, &request_id)?; for row in rows { - let status_code = row.status_code.map(|x| x as u16); - - let response = if let Some(status_code) = status_code { - if status_code == 200 { - GetStatusResponseSuccess::Success { - status_code: Some(status_code), - cdn_image_uri: row.cdn_image_uri.unwrap_or_default(), - } - } else { - GetStatusResponseSuccess::Error { - status_code: Some(status_code), - error_message: row.error_message, - } - } + if row.status_code == StatusCode::OK.as_u16() as i64 { + status_response.insert(row.asset_uri, GetStatusResponseSuccess::Success { + status_code: StatusCode::OK.as_u16(), + cdn_image_uri: row.cdn_image_uri.unwrap_or_default(), + }); } else { - GetStatusResponseSuccess::Error { - status_code, + status_response.insert(row.asset_uri, GetStatusResponseSuccess::Error { + status_code: row.status_code as u16, error_message: row.error_message, - } + }); }; - - status_response.insert(row.asset_uri, response); } Ok(status_response) diff --git a/ecosystem/nft-metadata-crawler/src/asset_uploader/api/mod.rs b/ecosystem/nft-metadata-crawler/src/asset_uploader/api/mod.rs index 85fa9e748288c..df6b16d5808b7 100644 --- a/ecosystem/nft-metadata-crawler/src/asset_uploader/api/mod.rs +++ b/ecosystem/nft-metadata-crawler/src/asset_uploader/api/mod.rs @@ -45,11 +45,11 @@ enum BatchUploadResponse { #[serde(untagged)] pub enum GetStatusResponseSuccess { Success { - status_code: Option, + status_code: u16, cdn_image_uri: String, }, Error { - status_code: Option, + status_code: u16, error_message: Option, }, } diff --git a/ecosystem/nft-metadata-crawler/src/models/asset_uploader_request_statuses.rs b/ecosystem/nft-metadata-crawler/src/models/asset_uploader_request_statuses.rs index 94e2521853545..fd190ac41f25c 100644 --- a/ecosystem/nft-metadata-crawler/src/models/asset_uploader_request_statuses.rs +++ b/ecosystem/nft-metadata-crawler/src/models/asset_uploader_request_statuses.rs @@ -2,6 +2,7 @@ // SPDX-License-Identifier: Apache-2.0 use crate::schema::nft_metadata_crawler::asset_uploader_request_statuses; +use axum::http::StatusCode; use diesel::prelude::*; use field_count::FieldCount; use serde::{Deserialize, Serialize}; @@ -14,7 +15,7 @@ pub struct AssetUploaderRequestStatuses { request_id: Uuid, asset_uri: String, application_id: Uuid, - status_code: Option, + status_code: i64, error_message: Option, cdn_image_uri: Option, num_failures: i64, @@ -26,7 +27,7 @@ impl AssetUploaderRequestStatuses { request_id, asset_uri: asset_uri.to_string(), application_id, - status_code: None, + status_code: StatusCode::ACCEPTED.as_u16() as i64, error_message: None, cdn_image_uri: None, num_failures: 0, @@ -43,54 +44,10 @@ impl AssetUploaderRequestStatuses { request_id, asset_uri: asset_uri.to_string(), application_id, - status_code: Some(200), + status_code: StatusCode::OK.as_u16() as i64, error_message: None, cdn_image_uri: Some(cdn_image_uri.to_string()), num_failures: 0, } } - - pub fn get_request_id(&self) -> Uuid { - self.request_id - } - - pub fn get_asset_uri(&self) -> String { - self.asset_uri.clone() - } - - pub fn get_application_id(&self) -> Uuid { - self.application_id - } - - pub fn get_status_code(&self) -> Option { - self.status_code - } - - pub fn set_status_code(&mut self, status_code: Option) { - self.status_code = status_code; - } - - pub fn get_error_message(&self) -> Option { - self.error_message.clone() - } - - pub fn set_error_message(&mut self, error_message: Option) { - self.error_message = error_message; - } - - pub fn get_cdn_image_uri(&self) -> Option { - self.cdn_image_uri.clone() - } - - pub fn set_cdn_image_uri(&mut self, cdn_image_uri: Option) { - self.cdn_image_uri = cdn_image_uri; - } - - pub fn get_num_failures(&self) -> i64 { - self.num_failures - } - - pub fn increment_num_failures(&mut self) { - self.num_failures += 1; - } } diff --git a/ecosystem/nft-metadata-crawler/src/models/asset_uploader_request_statuses_query.rs b/ecosystem/nft-metadata-crawler/src/models/asset_uploader_request_statuses_query.rs index a52bb14a3d7e5..7ad868422ec73 100644 --- a/ecosystem/nft-metadata-crawler/src/models/asset_uploader_request_statuses_query.rs +++ b/ecosystem/nft-metadata-crawler/src/models/asset_uploader_request_statuses_query.rs @@ -14,7 +14,7 @@ pub struct AssetUploaderRequestStatusesQuery { pub request_id: Uuid, pub asset_uri: String, pub application_id: Uuid, - pub status_code: Option, + pub status_code: i64, pub error_message: Option, pub cdn_image_uri: Option, pub num_failures: i64, diff --git a/ecosystem/nft-metadata-crawler/src/schema.rs b/ecosystem/nft-metadata-crawler/src/schema.rs index d228340752221..39a543ab7b139 100644 --- a/ecosystem/nft-metadata-crawler/src/schema.rs +++ b/ecosystem/nft-metadata-crawler/src/schema.rs @@ -9,7 +9,7 @@ pub mod nft_metadata_crawler { request_id -> Uuid, asset_uri -> Varchar, application_id -> Uuid, - status_code -> Nullable, + status_code -> Int8, error_message -> Nullable, cdn_image_uri -> Nullable, num_failures -> Int8,