Skip to content

Commit 38a454a

Browse files
committed
Optimization, Little refactor and routes updates
1 parent 39beffa commit 38a454a

File tree

6 files changed

+70
-59
lines changed

6 files changed

+70
-59
lines changed

src/epsilon/server/instances/instance_provider.rs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,29 +53,37 @@ impl InstanceProvider {
5353

5454
pub async fn get_instances(
5555
&self,
56-
instance_type: &InstanceType,
56+
instance_type: InstanceType,
5757
template_option: Option<&str>,
58-
state_option: Option<&EpsilonState>,
58+
state_option: Option<EpsilonState>,
5959
) -> Result<Vec<Arc<EpsilonInstance>>, EpsilonError> {
6060
let instances = self.epsilon_controller.get_epsilon_instance_store().state();
6161

6262
for instance in &instances {
63-
instance.status.as_ref().ok_or(EpsilonError::RetrieveInstanceError)?;
63+
instance
64+
.status
65+
.as_ref()
66+
.ok_or(EpsilonError::RetrieveInstanceError)?;
6467
}
6568

66-
Ok(instances.into_iter()
69+
Ok(instances
70+
.into_iter()
6771
.filter(|instance| {
6872
let status = instance.status.as_ref().unwrap();
6973

70-
let condition1 = status.t == *instance_type;
74+
let condition1 = status.t == instance_type;
7175

7276
let condition2 = if let Some(template_name) = template_option {
7377
instance.spec.template == template_name
74-
} else { true };
78+
} else {
79+
true
80+
};
7581

7682
let condition3 = if let Some(state) = state_option {
77-
status.state == *state
78-
} else { true };
83+
status.state == state
84+
} else {
85+
true
86+
};
7987

8088
condition1 && condition2 && condition3
8189
})

src/epsilon/server/instances/routes.rs

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -59,22 +59,29 @@ pub async fn in_game(instance: &str, context: &State<Arc<Context>>) -> Result<()
5959
Ok(())
6060
}
6161

62-
#[rocket::get("/get/<template>")]
63-
pub async fn get(template: &str, context: &State<Arc<Context>>) -> Result<String, EpsilonError> {
62+
#[rocket::get("/get/<instance_name>")]
63+
pub async fn get(
64+
instance_name: &str,
65+
context: &State<Arc<Context>>,
66+
) -> Result<String, EpsilonError> {
67+
let instance_provider = context.get_instance_provider();
68+
let instance = instance_provider.get_instance(instance_name).await?;
69+
70+
Ok(serde_json::to_string(&instance.to_json().await?)
71+
.map_err(|_| EpsilonError::ParseJsonError)?)
72+
}
73+
74+
#[rocket::get("/get_all")]
75+
pub async fn get_all(context: &State<Arc<Context>>) -> Result<String, EpsilonError> {
6476
let instance_provider = context.get_instance_provider();
6577

6678
let instances = instance_provider
67-
.get_instances(&InstanceType::Server, Some(template), None)
79+
.get_instances(InstanceType::Server, None, None)
6880
.await
69-
.map_err(|_| {
70-
EpsilonError::ApiServerError(format!(
71-
"Failed to get instance from template {}",
72-
template
73-
))
74-
})?
81+
.map_err(|_| EpsilonError::ApiServerError("Failed to get every instance".to_string()))?
7582
.into_iter();
7683

77-
let mut json_array: Vec<InstanceJson> = Vec::with_capacity(instances.len());
84+
let mut json_array: Vec<InstanceJson> = Vec::new();
7885

7986
for instance in instances {
8087
let json = instance.to_json().await?;
@@ -84,29 +91,25 @@ pub async fn get(template: &str, context: &State<Arc<Context>>) -> Result<String
8491
Ok(json!({ "instances": json_array }).to_string())
8592
}
8693

87-
#[rocket::get("/get_from_name/<instance_name>")]
88-
pub async fn get_from_name(
89-
instance_name: &str,
94+
#[rocket::get("/get_from_template/<template>")]
95+
pub async fn get_from_template(
96+
template: &str,
9097
context: &State<Arc<Context>>,
9198
) -> Result<String, EpsilonError> {
9299
let instance_provider = context.get_instance_provider();
93-
let instance = instance_provider.get_instance(instance_name).await?;
94-
95-
Ok(serde_json::to_string(&instance.to_json().await?)
96-
.map_err(|_| EpsilonError::ParseJsonError)?)
97-
}
98-
99-
#[rocket::get("/get_all")]
100-
pub async fn get_all(context: &State<Arc<Context>>) -> Result<String, EpsilonError> {
101-
let instance_provider = context.get_instance_provider();
102100

103101
let instances = instance_provider
104-
.get_instances(&InstanceType::Server, None, None)
102+
.get_instances(InstanceType::Server, Some(template), None)
105103
.await
106-
.map_err(|_| EpsilonError::ApiServerError("Failed to get every instance".to_string()))?
104+
.map_err(|_| {
105+
EpsilonError::ApiServerError(format!(
106+
"Failed to get instance from template {}",
107+
template
108+
))
109+
})?
107110
.into_iter();
108111

109-
let mut json_array: Vec<InstanceJson> = Vec::new();
112+
let mut json_array: Vec<InstanceJson> = Vec::with_capacity(instances.len());
110113

111114
for instance in instances {
112115
let json = instance.to_json().await?;

src/main.rs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
#[macro_use]
22
extern crate log;
33

4-
use std::{env, fs};
54
use std::io::Write;
65
use std::sync::Arc;
76
use std::time::Duration;
7+
use std::{env, fs};
88

99
use env_logger::fmt::Color;
1010
use k8s_openapi::chrono::Local;
@@ -19,8 +19,8 @@ use crate::controller::definitions::epsilon_queue::EpsilonQueue;
1919
use crate::controller::epsilon_controller::EpsilonController;
2020
use crate::epsilon::api::epsilon_api::EpsilonApi;
2121
use crate::epsilon::queue::queue_provider::QueueProvider;
22-
use crate::epsilon::server::instances::EResult;
2322
use crate::epsilon::server::instances::instance_provider::InstanceProvider;
23+
use crate::epsilon::server::instances::EResult;
2424
use crate::epsilon::server::templates::template_provider::TemplateProvider;
2525
use crate::tasks::hub_task::HubTask;
2626
use crate::tasks::proxy_task::ProxyTask;
@@ -46,13 +46,13 @@ async fn main() -> EResult<()> {
4646
format!("{}/{}", path_name, "epsilon_instance-definition.yaml"),
4747
serde_yaml::to_string(&EpsilonInstance::crd()).unwrap(),
4848
)
49-
.unwrap();
49+
.unwrap();
5050

5151
fs::write(
5252
format!("{}/{}", path_name, "epsilon_queue-definition.yaml"),
5353
serde_yaml::to_string(&EpsilonQueue::crd()).unwrap(),
5454
)
55-
.unwrap();
55+
.unwrap();
5656

5757
std::env::set_var(
5858
"RUST_LOG",
@@ -83,17 +83,17 @@ async fn main() -> EResult<()> {
8383
.init();
8484

8585
let epsilon = concat!(
86-
"┌──────────────────────────────────────┐\n",
87-
"│ _____ _ _ │\n",
88-
"│ | ___| (_) | │\n",
89-
"│ | |__ _ __ ___ _| | ___ _ __ │\n",
90-
"│ | __| '_ \\/ __| | |/ _ \\| '_ \\\n",
91-
"│ | |__| |_) \\__ \\ | | (_) | | | | │\n",
92-
"│ \\____/ .__/|___/_|_|\\___/|_| |_| │\n",
93-
"│ | | │\n",
94-
"│ |_| │\n",
95-
"├──────────────────────────────────────┤\n",
96-
"────────────────────────────────────────\n",
86+
"┌──────────────────────────────────────┐\n",
87+
"│ _____ _ _ │\n",
88+
"│ | ___| (_) | │\n",
89+
"│ | |__ _ __ ___ _| | ___ _ __ │\n",
90+
"│ | __| '_ \\/ __| | |/ _ \\| '_ \\\n",
91+
"│ | |__| |_) \\__ \\ | | (_) | | | | │\n",
92+
"│ \\____/ .__/|___/_|_|\\___/|_| |_| │\n",
93+
"│ | | │\n",
94+
"│ |_| │\n",
95+
"├──────────────────────────────────────┤\n",
96+
"────────────────────────────────────────\n",
9797
);
9898

9999
println!("{}", epsilon);
@@ -151,7 +151,7 @@ async fn main() -> EResult<()> {
151151
epsilon::server::instances::routes::in_game,
152152
epsilon::server::instances::routes::get,
153153
epsilon::server::instances::routes::get_all,
154-
epsilon::server::instances::routes::get_from_name
154+
epsilon::server::instances::routes::get_from_template
155155
],
156156
)
157157
.launch()

src/tasks/hub_task.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,26 +36,26 @@ impl Task for HubTask {
3636
let template_name = &self.hub_template.name;
3737

3838
let proxies = instance_provider
39-
.get_instances(&InstanceType::Proxy, None, Some(&EpsilonState::Running))
39+
.get_instances(InstanceType::Proxy, None, Some(EpsilonState::Running))
4040
.await?;
4141

4242
let proxy_number = proxies.len();
4343

4444
if proxy_number > 0 {
4545
let hubs_starting = instance_provider
4646
.get_instances(
47-
&InstanceType::Server,
47+
InstanceType::Server,
4848
Some(template_name),
49-
Some(&EpsilonState::Starting),
49+
Some(EpsilonState::Starting),
5050
)
5151
.await?;
5252

5353
if hubs_starting.is_empty() {
5454
let hubs_ready = instance_provider
5555
.get_instances(
56-
&InstanceType::Server,
56+
InstanceType::Server,
5757
Some(template_name),
58-
Some(&EpsilonState::Running),
58+
Some(EpsilonState::Running),
5959
)
6060
.await?;
6161

src/tasks/proxy_task.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ impl Task for ProxyTask {
2828
let template_name = &self.proxy_template.name;
2929

3030
let proxies = instance_provider
31-
.get_instances(&InstanceType::Proxy, None, None)
31+
.get_instances(InstanceType::Proxy, None, None)
3232
.await?;
3333

3434
if proxies.is_empty() {

src/tasks/queue_task.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,17 @@ impl Task for QueueTask {
2828
if !queue.read().await.is_empty() {
2929
let instances_starting = instance_provider
3030
.get_instances(
31-
&InstanceType::Server,
31+
InstanceType::Server,
3232
Some(template_name),
33-
Some(&EpsilonState::Starting),
33+
Some(EpsilonState::Starting),
3434
)
3535
.await?;
3636

3737
let instances_ready = instance_provider
3838
.get_instances(
39-
&InstanceType::Server,
39+
InstanceType::Server,
4040
Some(template_name),
41-
Some(&EpsilonState::Running),
41+
Some(EpsilonState::Running),
4242
)
4343
.await?;
4444

0 commit comments

Comments
 (0)