Skip to content

Commit

Permalink
Merge pull request NextDotID#168 from ZhongFuze/upstream/basenames
Browse files Browse the repository at this point in the history
Adjust async online tasks and fetch_all
  • Loading branch information
nykma authored Sep 4, 2024
2 parents d199cf1 + 5fbb74d commit 82a43ac
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 53 deletions.
90 changes: 88 additions & 2 deletions src/controller/tigergraphql/identity_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ use crate::{
},
},
upstream::{fetch_all, Chain, ContractCategory, DataSource, Platform, Target},
util::make_http_client,
util::{make_http_client, naive_now},
};
use async_graphql::{Context, Object};
use chrono::Duration;
use dataloader::non_cached::Loader;
use tracing::{event, Level};
use uuid::Uuid;
Expand Down Expand Up @@ -339,7 +340,92 @@ impl ExpandIdentityRecord {
)
.await?)
}
Some(identity_graph) => Ok(Some(identity_graph)),
Some(identity_graph) => {
// filter out dataSource == "basenames" edges
let filter_edges: Vec<IdentityConnection> = identity_graph
.edges
.clone()
.into_iter()
.filter(|e| e.data_source != DataSource::Basenames)
.collect();

if filter_edges.len() == 0 {
// only have basenames edges
let basenames_vertex: Vec<ExpandIdentityRecord> = identity_graph
.vertices
.clone()
.into_iter()
.filter(|v| {
v.record.platform == Platform::Basenames
&& v.record.identity == self.identity
})
.collect();

if basenames_vertex.len() > 0 {
let updated_at = basenames_vertex.first().cloned().unwrap().updated_at;
let current_time = naive_now();
let duration_since_update = current_time.signed_duration_since(updated_at);
// Check if the difference is greater than 2 hours
if duration_since_update > Duration::hours(2) {
let basename_address = basenames_vertex.first().cloned().unwrap();
let resolved_address = match basename_address.resolve_address {
Some(addr_list) => {
if addr_list.len() > 0 {
addr_list.first().cloned().unwrap().address
} else {
match basename_address.owner_address {
Some(owner_addr_list) => {
if owner_addr_list.len() > 0 {
owner_addr_list
.first()
.cloned()
.unwrap()
.address
} else {
"".to_string()
}
}
None => "".to_string(),
}
}
}
None => match basename_address.owner_address {
Some(owner_addr_list) => {
if owner_addr_list.len() > 0 {
owner_addr_list.first().cloned().unwrap().address
} else {
"".to_string()
}
}
None => "".to_string(),
},
};
if resolved_address != "".to_string() {
tracing::trace!("Basenames refetching {} ...", resolved_address);
let target = Target::Identity(Platform::Ethereum, resolved_address);
let fetch_result = fetch_all(vec![target], Some(3)).await;
if fetch_result.is_err() {
event!(
Level::WARN,
?self.platform,
self.identity,
err = fetch_result.unwrap_err().to_string(),
"Failed to fetch_all"
);
}
return Ok(IdentityGraph::find_graph_by_platform_identity(
&client,
&self.platform,
&self.identity,
reverse,
)
.await?);
}
}
}
}
Ok(Some(identity_graph))
}
}
}
}
Expand Down
57 changes: 45 additions & 12 deletions src/controller/tigergraphql/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::{
fetch_all, fetch_domains, trim_name, Chain, ContractCategory, DataFetcher, DataSource,
DomainNameSystem, DomainStatus, Platform, Target,
},
util::make_http_client,
util::{make_http_client, naive_now},
};
use async_graphql::{Context, Object};
use strum::IntoEnumIterator;
Expand Down Expand Up @@ -188,20 +188,53 @@ impl ResolveQuery {
}
}
Some(found) => {
if found.collection.is_outdated() {
event!(
Level::DEBUG,
process_name,
"Outdated. Delete and Refetching all available domains."
);
tokio::spawn(async move {
// Delete and Refetch in the background
sleep(Duration::from_secs(10)).await;
// filter out dataSource == "basenames" edges
let filter_edges: Vec<AvailableDomain> = found
.domains
.clone()
.into_iter()
.filter(|e| e.platform != Platform::Basenames && e.availability == false)
.collect();

if filter_edges.len() == 0 {
// only have basenames edges
let updated_at = found.collection.updated_at.clone();
let current_time = naive_now();
let duration_since_update = current_time.signed_duration_since(updated_at);
// Check if the difference is greater than 2 hours
if duration_since_update > chrono::Duration::hours(2) {
event!(
Level::DEBUG,
process_name,
"Outdated. Delete and Refetching all available domains."
);
delete_domain_collection(&client, &process_name).await?;
fetch_domains(&name).await?;
Ok::<_, Error>(())
});
match DomainCollection::domain_available_search(&client, &process_name)
.await?
{
None => return Ok(None),
Some(result) => return Ok(Some(result.domains)),
}
}
} else {
if found.collection.is_outdated() {
event!(
Level::DEBUG,
process_name,
"Outdated. Delete and Refetching all available domains."
);
let client_clone = client.clone();
tokio::spawn(async move {
// Delete and Refetch in the background
sleep(Duration::from_secs(10)).await;
delete_domain_collection(&client_clone, &process_name).await?;
fetch_domains(&name).await?;
Ok::<_, Error>(())
});
}
}

Ok(Some(found.domains))
}
}
Expand Down
40 changes: 1 addition & 39 deletions src/tigergraph/vertex/identity_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@ use crate::{
Attribute, BaseResponse, Graph, OpCode, Transfer,
},
upstream::{Chain, DataSource, Platform},
util::{naive_now, parse_body},
util::parse_body,
};
use async_trait::async_trait;
use chrono::Duration;
use http::uri::InvalidUri;
use hyper::{client::HttpConnector, Body, Client, Method};
use serde::de::{self, MapAccess, Visitor};
Expand Down Expand Up @@ -322,43 +321,6 @@ impl IdentityGraph {
if result.vertices.len() > 1 {
return Ok(None); // If vertices=1, it's isolated vertex
}
} else {
// filter out dataSource == "basenames" edges
let filter_edges: Vec<IdentityConnection> = result
.edges
.clone()
.into_iter()
.filter(|e| e.data_source != DataSource::Basenames)
.collect();

if filter_edges.len() == 0 {
// only have basenames edges
let basenames_vertex: Vec<ExpandIdentityRecord> = result
.vertices
.clone()
.into_iter()
.filter(|v| v.record.platform == Platform::Ethereum)
.collect();

if basenames_vertex.len() > 0 {
let updated_at =
basenames_vertex.first().cloned().unwrap().updated_at;
let current_time = naive_now();
let duration_since_update =
current_time.signed_duration_since(updated_at);
// Check if the difference is greater than 2 hours
tracing::trace!(
"updated_at {}, duration_since_update {}, current_time {}",
updated_at,
duration_since_update,
current_time
);
if duration_since_update > Duration::hours(2) {
tracing::info!("Basenames refetching...");
return Ok(None);
}
}
}
}
return Ok(Some(result));
}
Expand Down
2 changes: 2 additions & 0 deletions src/upstream/types/contract_category.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ impl ContractCategory {
ENS => Some("0x57f1887a8BF19b14fC0dF6Fd9B2acc9Af147eA85".to_lowercase()),
SNS => Some("4gj2A7SSgWUGfHTm2iG4NeH3kpySmGd54bj78TM4d7Fg".to_string()), // Solana Name Service
GNS => Some("0x5dc881dda4e4a8d312be3544ad13118d1a04cb17".to_string()), // Gnosis Name Service
Basenames => Some("0x4ccb0bb02fcaba27e82a56646e81d8c5bc4119a5".to_string()), // Basenames
_ => None,
}
}
Expand All @@ -81,6 +82,7 @@ impl ContractCategory {
POAP => Some(Chain::Ethereum),
SNS => Some(Chain::Solana),
GNS => Some(Chain::Gnosis),
Basenames => Some(Chain::Base),
_ => None,
}
}
Expand Down

0 comments on commit 82a43ac

Please sign in to comment.