Skip to content

Commit d3b4331

Browse files
author
ThePhoenixPixel
committed
add &update Installer "InstallAllDesc"
1 parent 6b622b9 commit d3b4331

File tree

4 files changed

+76
-31
lines changed

4 files changed

+76
-31
lines changed

src/core/installer.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,30 @@ use serde::{Deserialize, Serialize};
33
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
44
pub enum Installer {
55
InstallAll,
6+
InstallAllDesc,
67
InstallRandom,
78
InstallRandomWithPriority,
89
}
910

10-
// Implementiere die From-Trait, um Installer in Task zu konvertieren
1111
impl Installer {
1212
pub fn from(s: &str) -> Self {
1313
match s {
14-
"InstallAll" => Installer::InstallAll,
15-
"InstallRandom" => Installer::InstallRandom,
16-
"InstallRandomWithPriority" => Installer::InstallRandomWithPriority,
17-
_ => Installer::InstallAll, // Fallback-Wert, wenn der übergebene String nicht erkannt wird
14+
"InstallAll" => Installer::InstallAll,
15+
"InstallAllDesc" => Installer::InstallAllDesc,
16+
"InstallRandom" => Installer::InstallRandom,
17+
"InstallRandomWithPriority" => Installer::InstallRandomWithPriority,
18+
_ => Installer::InstallAll,
1819
}
1920
}
2021
}
2122

22-
// Implementiere die Into-Trait, um Task in Installer zu konvertieren
2323
impl Into<&str> for Installer {
2424
fn into(self) -> &'static str {
2525
match self {
26-
Installer::InstallAll => "InstallAll",
27-
Installer::InstallRandom => "InstallRandom",
28-
Installer::InstallRandomWithPriority => "InstallRandomWithPriority",
26+
Installer::InstallAll => "InstallAll",
27+
Installer::InstallAllDesc => "InstallAllDesc",
28+
Installer::InstallRandom => "InstallRandom",
29+
Installer::InstallRandomWithPriority => "InstallRandomWithPriority",
2930
}
3031
}
3132
}

src/core/service.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ impl Service {
5858
&task.get_start_port(),
5959
)) {
6060
Some(port ) => port,
61-
None => return Err(error!(CloudErrorKind::NextFreePortNotFound)),
61+
None => return Err(error!(NextFreePortNotFound)),
6262
};
6363
let server_address = Address::new(&CloudConfig::get().get_server_host(), &port);
6464
let service_path = task.prepared_to_service()?;

src/core/task.rs

Lines changed: 65 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::fs::File;
55
use std::io::{Read, Write};
66
use std::path::PathBuf;
77
use std::{fs, io};
8-
8+
use rand::seq::IndexedRandom;
99
use crate::core::installer::Installer;
1010
use crate::core::software::Software;
1111
use crate::core::template::Template;
@@ -454,14 +454,73 @@ impl Task {
454454
fs::remove_file(task_path).expect("Error bei removen der task datei");
455455
}
456456

457+
pub fn get_templates_sorted_by_priority(&self) -> Vec<Template> {
458+
let mut templates = self.get_templates();
459+
templates.sort_by(|a, b| a.priority.cmp(&b.priority));
460+
templates
461+
}
462+
463+
pub fn get_templates_sorted_by_priority_desc(&self) -> Vec<Template> {
464+
let mut templates = self.get_templates();
465+
templates.sort_by(|a, b| b.priority.cmp(&a.priority));
466+
templates
467+
}
468+
469+
pub fn get_template_rng(&self) -> Option<&Template> {
470+
let mut rng = rand::rng();
471+
self.templates.choose(&mut rng)
472+
}
473+
474+
// Select Template based on Priority (higher priority = higher chance)
475+
pub fn get_template_rng_based_on_priority(&self) -> Option<&Template> {
476+
if self.templates.is_empty() {
477+
return None;
478+
}
479+
480+
let total_weight: u32 = self.templates.iter().map(|t| t.priority).sum();
481+
482+
if total_weight == 0 {
483+
return self.get_template_rng();
484+
}
485+
486+
let mut rng = rand::thread_rng();
487+
let mut random_value = rng.gen_range(0..total_weight);
488+
489+
for template in &self.templates {
490+
if random_value < template.priority {
491+
return Some(template);
492+
}
493+
random_value -= template.priority;
494+
}
495+
496+
// fallback
497+
self.templates.last()
498+
}
499+
457500
pub fn prepared_to_service(&self) -> Result<PathBuf, CloudError> {
458501
// create the next free service folder with the template
459502
let target_path = self.create_next_free_service_folder()?;
460-
let templates = &self.get_templates();
461-
let template = select_template_with_priority(&templates)?;
503+
let mut templates: Vec<Template> = Vec::new();
504+
match self.get_installer() {
505+
Installer::InstallAll => templates = self.get_templates_sorted_by_priority(),
506+
Installer::InstallAllDesc => templates = self.get_templates_sorted_by_priority_desc(),
507+
Installer::InstallRandom => {
508+
match self.get_template_rng() {
509+
Some(template) => templates.push(template.clone()),
510+
None => return Err(error!(TemplateNotFound)),
511+
}
512+
}
513+
Installer::InstallRandomWithPriority => {
514+
match self.get_template_rng_based_on_priority() {
515+
Some(template) => templates.push(template.clone()),
516+
None => return Err(error!(TemplateNotFound)),
517+
}
518+
}
519+
}
462520

463-
// copy the template in the new service folder
464-
Directory::copy_folder_contents(&template.get_path(), &target_path).map_err(|e| error!(CantCopyTemplateToNewServiceFolder, e))?;
521+
for template in templates {
522+
Directory::copy_folder_contents(&template.get_path(), &target_path).map_err(|e| error!(CantCopyTemplateToNewServiceFolder, e))?;
523+
}
465524
Ok(target_path)
466525
}
467526

@@ -470,6 +529,7 @@ impl Task {
470529
let mut folder_index: u32 = 1;
471530
let target_base_path = self.get_service_path();
472531
let mut target_service_folder_path =
532+
// TODO: vllt hier noch den split aus task eig. benutzen
473533
target_base_path.join(format!("{}-{}", &self.get_name(), folder_index));
474534

475535
while target_service_folder_path.exists() {
@@ -525,17 +585,3 @@ impl Task {
525585
log_info!("-----------------------------");
526586
}
527587
}
528-
529-
fn select_template_with_priority(templates: &[Template]) -> Result<&Template, CloudError> {
530-
let mut rng = rand::rng();
531-
let total_priority: u32 = templates.iter().map(|t| t.priority).sum();
532-
let mut rand_value = rng.random_range(1..=total_priority);
533-
534-
for template in templates {
535-
if rand_value <= template.priority {
536-
return Ok(template);
537-
}
538-
rand_value -= template.priority;
539-
}
540-
Err(error!(CloudErrorKind::TemplateWithPriorityNotFound))
541-
}

src/utils/error_kind.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ pub enum CloudErrorKind {
2121

2222
/// 22.xxx Template
2323
TemplateNotFound,
24-
TemplateWithPriorityNotFound,
2524

2625
/// 3x.xxx Service
2726
/// 31.xxx Not Found
@@ -72,7 +71,6 @@ impl CloudErrorKind {
7271
// 22.xxxx Template
7372
// 22.1.xx NotFound
7473
CloudErrorKind::TemplateNotFound => 221000,
75-
CloudErrorKind::TemplateWithPriorityNotFound => 221001,
7674

7775
// 3x.xxx Service
7876
// 30.1xx NotFound

0 commit comments

Comments
 (0)