[Fix]Connection Stability
This commit is contained in:
parent
fb4facb8c4
commit
7a457e97d8
9 changed files with 258 additions and 164 deletions
|
|
@ -1,7 +1,7 @@
|
|||
use crate::load_keyring;
|
||||
use crate::sql::sql;
|
||||
use crate::sql::sql::{get_by_user_id, get_iota_by_id, get_omikron_by_id};
|
||||
use crate::sql::user_online_tracker::get_iota_primary_omikron_connection;
|
||||
use crate::sql::user_online_tracker::{get_all_connections, get_iota_primary_omikron_connection};
|
||||
use crate::transport::omikron_manager::get_random_omikron;
|
||||
use crate::util::file_util::get_directory;
|
||||
use actix_web::HttpResponse;
|
||||
|
|
@ -151,6 +151,31 @@ pub async fn handle(path: &str, body_string: Option<String>) -> HttpResponse {
|
|||
}
|
||||
}
|
||||
|
||||
["api", "get", "connections"] => {
|
||||
if let Ok(connections) = get_all_connections().await {
|
||||
let mut res = JsonValue::new_object();
|
||||
res["status"] = "success".into();
|
||||
|
||||
for (omikron_id, iota_map) in connections {
|
||||
let mut omikron_obj = JsonValue::new_object();
|
||||
for (iota_id, user_ids) in iota_map {
|
||||
let mut user_arr = JsonValue::new_array();
|
||||
for user_id in user_ids {
|
||||
let _ = user_arr.push(user_id);
|
||||
}
|
||||
omikron_obj[&iota_id.to_string()] = user_arr;
|
||||
}
|
||||
res[&omikron_id.to_string()] = omikron_obj;
|
||||
}
|
||||
|
||||
(StatusCode::OK, res.dump())
|
||||
} else {
|
||||
let mut res = JsonValue::new_object();
|
||||
res["status"] = "error".into();
|
||||
(StatusCode::INTERNAL_SERVER_ERROR, res.dump())
|
||||
}
|
||||
}
|
||||
|
||||
// ==================================================
|
||||
// GET IOTA BY ID
|
||||
// ==================================================
|
||||
|
|
|
|||
|
|
@ -67,6 +67,46 @@ pub fn get_iota_omikron_connections(iota_id: i64) -> Option<Vec<i64>> {
|
|||
IOTA_OMIKRON_CONNECTIONS.get(&iota_id).map(|v| v.clone())
|
||||
}
|
||||
|
||||
pub async fn get_all_connections()
|
||||
-> Result<std::collections::HashMap<i64, std::collections::HashMap<i64, Vec<i64>>>, ()> {
|
||||
let mut result: std::collections::HashMap<i64, std::collections::HashMap<i64, Vec<i64>>> =
|
||||
std::collections::HashMap::new();
|
||||
|
||||
for entry in IOTA_OMIKRON_CONNECTIONS.iter() {
|
||||
let iota_id = *entry.key();
|
||||
for omikron_id in entry.value().iter() {
|
||||
result
|
||||
.entry(*omikron_id)
|
||||
.or_insert_with(std::collections::HashMap::new)
|
||||
.entry(iota_id)
|
||||
.or_insert_with(Vec::new);
|
||||
}
|
||||
}
|
||||
|
||||
for entry in IOTA_OMIKRON_CONNECTIONS.iter() {
|
||||
let iota_id = *entry.key();
|
||||
let omikron_ids = entry.value().clone();
|
||||
|
||||
if let Ok(users) = sql::sql::get_users_by_iota_id(iota_id.try_into().unwrap()).await {
|
||||
for user in users {
|
||||
let user_id = user.0 as i64;
|
||||
if let Some(conn) = USER_STATUS_MAP.get(&user_id) {
|
||||
let user_omikron_id = conn.omikron_id;
|
||||
if omikron_ids.contains(&user_omikron_id) {
|
||||
if let Some(iota_map) = result.get_mut(&user_omikron_id) {
|
||||
if let Some(user_vec) = iota_map.get_mut(&iota_id) {
|
||||
user_vec.push(user_id);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
pub fn track_user_status(user_id: i64, status: UserStatus, omikron_id: i64) {
|
||||
USER_STATUS_MAP.insert(
|
||||
user_id,
|
||||
|
|
|
|||
|
|
@ -304,6 +304,7 @@ impl OmikronConnection {
|
|||
|
||||
let response = CommunicationValue::new(CommunicationType::IotaUserData)
|
||||
.with_id(cv.get_id())
|
||||
.add_typed_default(DataType::IotaId, DataValue::SignedNumber(iota_id.into()))
|
||||
.add_typed_default(DataType::UserIds, DataValue::Array(user_ids));
|
||||
|
||||
let _ = self.send(&response).await;
|
||||
|
|
|
|||
Loading…
Reference in a new issue