Rust · 2522 bytes Raw Blame History
1 //! Resource Scheduler
2 //!
3 //! Schedules resource allocation based on contribution priorities and system capacity
4
5 use anyhow::Result;
6 use serde::{Deserialize, Serialize};
7 use std::collections::HashMap;
8 use chrono::{DateTime, Utc, Duration};
9
10 /// Resource scheduler for contribution-based allocation
11 #[derive(Debug, Clone, Serialize, Deserialize)]
12 pub struct ResourceScheduler {
13 pub scheduled_allocations: Vec<ScheduledAllocation>,
14 pub allocation_schedule: AllocationSchedule,
15 pub scheduling_policies: Vec<SchedulingPolicy>,
16 pub resource_reservations: HashMap<String, ResourceReservation>,
17 }
18
19 #[derive(Debug, Clone, Serialize, Deserialize)]
20 pub struct ScheduledAllocation {
21 pub allocation_id: String,
22 pub user_id: String,
23 pub scheduled_for: DateTime<Utc>,
24 pub duration: Option<Duration>,
25 pub priority: u32,
26 pub resource_requirements: HashMap<String, u64>,
27 }
28
29 #[derive(Debug, Clone, Serialize, Deserialize)]
30 pub struct AllocationSchedule {
31 pub schedule_id: String,
32 pub time_slots: Vec<TimeSlot>,
33 pub capacity_limits: HashMap<String, u64>,
34 pub last_updated: DateTime<Utc>,
35 }
36
37 #[derive(Debug, Clone, Serialize, Deserialize)]
38 pub struct TimeSlot {
39 pub start_time: DateTime<Utc>,
40 pub end_time: DateTime<Utc>,
41 pub available_capacity: HashMap<String, u64>,
42 pub scheduled_allocations: Vec<String>,
43 }
44
45 #[derive(Debug, Clone, Serialize, Deserialize)]
46 pub struct SchedulingPolicy {
47 pub policy_name: String,
48 pub description: String,
49 pub priority: u32,
50 pub enabled: bool,
51 }
52
53 #[derive(Debug, Clone, Serialize, Deserialize)]
54 pub struct ResourceReservation {
55 pub reservation_id: String,
56 pub user_id: String,
57 pub resource_type: String,
58 pub amount: u64,
59 pub reserved_until: DateTime<Utc>,
60 pub status: ReservationStatus,
61 }
62
63 #[derive(Debug, Clone, Serialize, Deserialize)]
64 pub enum ReservationStatus {
65 Active,
66 Pending,
67 Expired,
68 Cancelled,
69 }
70
71 impl ResourceScheduler {
72 pub fn new() -> Self {
73 Self {
74 scheduled_allocations: Vec::new(),
75 allocation_schedule: AllocationSchedule {
76 schedule_id: "default".to_string(),
77 time_slots: Vec::new(),
78 capacity_limits: HashMap::new(),
79 last_updated: Utc::now(),
80 },
81 scheduling_policies: Vec::new(),
82 resource_reservations: HashMap::new(),
83 }
84 }
85 }
86
87 impl Default for ResourceScheduler {
88 fn default() -> Self {
89 Self::new()
90 }
91 }