@@ -5,7 +5,7 @@ use std::fs::File;
55use std:: io:: { Read , Write } ;
66use std:: path:: PathBuf ;
77use std:: { fs, io} ;
8-
8+ use rand :: seq :: IndexedRandom ;
99use crate :: core:: installer:: Installer ;
1010use crate :: core:: software:: Software ;
1111use 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- }
0 commit comments