-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathurl_discovery_service.rs
More file actions
137 lines (124 loc) · 4.09 KB
/
url_discovery_service.rs
File metadata and controls
137 lines (124 loc) · 4.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
use crate::{
config::Config,
provider_endpoints,
repository::DealRepository,
services::deal_service,
types::{
ClientAddress, ClientId, DiscoveryType, ErrorCode, ProviderAddress, ProviderId, ResultCode,
},
url_tester,
};
use tracing::{debug, error};
use uuid::Uuid;
#[derive(Debug, Clone)]
pub struct UrlDiscoveryResult {
pub id: Uuid,
pub provider_id: ProviderId,
pub client_id: Option<ClientId>,
pub result_type: DiscoveryType,
pub working_url: Option<String>,
pub retrievability_percent: f64,
pub result_code: ResultCode,
pub error_code: Option<ErrorCode>,
}
impl UrlDiscoveryResult {
pub fn new_provider_only(provider_id: ProviderId) -> Self {
Self {
id: Uuid::new_v4(),
provider_id,
client_id: None,
result_type: DiscoveryType::Provider,
working_url: None,
retrievability_percent: 0.0,
result_code: ResultCode::Error,
error_code: None,
}
}
pub fn new_provider_client(provider_id: ProviderId, client_id: ClientId) -> Self {
Self {
id: Uuid::new_v4(),
provider_id,
client_id: Some(client_id),
result_type: DiscoveryType::ProviderClient,
working_url: None,
retrievability_percent: 0.0,
result_code: ResultCode::Error,
error_code: None,
}
}
}
pub async fn discover_url(
config: &Config,
provider_address: &ProviderAddress,
client_address: Option<ClientAddress>,
deal_repo: &DealRepository,
) -> UrlDiscoveryResult {
let provider_id: ProviderId = provider_address.clone().into();
let client_id: Option<ClientId> = client_address.clone().map(|c| c.into());
tracing::info!(
"discover_url called for provider={}, client={:?}",
provider_address,
client_address
);
let mut result = match &client_id {
Some(c) => UrlDiscoveryResult::new_provider_client(provider_id.clone(), c.clone()),
None => UrlDiscoveryResult::new_provider_only(provider_id.clone()),
};
let (result_code, endpoints) =
match provider_endpoints::get_provider_endpoints(config, provider_address).await {
Ok((code, eps)) => (code, eps),
Err(e) => {
error!(
"Failed to get provider endpoints for {}: {:?}",
provider_address, e
);
result.result_code = ResultCode::Error;
result.error_code = Some(e);
return result;
}
};
let Some(endpoints) = endpoints else {
result.result_code = result_code;
return result;
};
let piece_ids =
match deal_service::get_piece_ids_by_provider(deal_repo, &provider_id, client_id.as_ref())
.await
{
Ok(ids) => ids,
Err(e) => {
error!(
"Failed to get piece ids for {} {:?}: {:?}",
provider_id, client_id, e
);
result.result_code = ResultCode::Error;
result.error_code = Some(ErrorCode::FailedToGetDeals);
return result;
}
};
if piece_ids.is_empty() {
result.result_code = ResultCode::NoDealsFound;
return result;
}
let urls = deal_service::get_piece_url(endpoints.clone(), piece_ids).await;
debug!(
"Built {} URLs to test from endpoints: {:?}",
urls.len(),
endpoints
);
debug!("Testing URLs: {:?}", urls);
let (working_url, retrievability_percent) =
url_tester::check_retrievability_with_get(config, urls, true).await;
debug!(
"URL test result - working_url: {:?}, retrievability: {:?}",
working_url, retrievability_percent
);
result.working_url = working_url.clone();
result.retrievability_percent = retrievability_percent.unwrap_or(0.0);
result.result_code = if working_url.is_some() {
ResultCode::Success
} else {
ResultCode::FailedToGetWorkingUrl
};
result
}