1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
use super::*;

#[derive(Clone, PartialEq, Eq, Hash, Debug, Default, serde::Deserialize)]
#[serde(rename_all = "kebab-case", default)]
pub struct Options {
    pub allow: String,
    pub capture_directory: String,
    pub capture_on_error: StringBool,
    pub capture_packets: StringBool,
    pub capture_requests: StringBool,
    pub capture_responses: StringBool,
    pub capture_sockets: StringBool,
    pub cause: String,
    pub certificate_file: String,
    pub checkpoint: StringInt,
    pub child: StringBool,
    pub client_subtype: String,
    pub client_threads: StringInt,
    pub client_type: String,
    pub command_address: String,
    pub command_allow_no_pass: String,
    pub deny: String,
    pub command_deny_no_pass: String,
    pub command_enable: StringBool,
    pub command_port: StringInt,
    pub config_rotate: StringBool,
    pub config_rotate_dir: String,
    pub config_rotate_max: StringInt,
    pub connection_timeout: StringInt,
    pub core_priority: String,
    pub cpu_species: String,
    pub cpu_type: String,
    pub cpu_usage: StringInt,
    pub cpus: StringInt,
    pub crl_file: String,
    pub cuda_index: String,
    pub cycle_rate: StringInt,
    pub cycles: StringInt,
    pub daemon: StringBool,
    pub debug_sockets: StringBool,
    pub disable_sleep_when_active: StringBool,
    pub disable_viz: StringBool,
    pub dump_after_deadline: StringBool,
    pub exception_locations: StringBool,
    pub exit_when_done: StringBool,
    pub extra_core_args: String,
    pub fold_anon: String,
    pub gpu: String,
    pub gpu_index: String,
    pub gpu_usage: StringInt,
    pub gui_enabled: String,
    pub http_addresses: String,
    pub https_addresses: String,
    pub idle: StringBool,
    pub log: String,
    pub log_color: StringBool,
    pub log_crlf: StringBool,
    pub log_date: StringBool,
    pub log_date_periodically: StringInt,
    pub log_domain: StringBool,
    pub log_domain_levels: String,
    pub log_header: StringBool,
    pub log_level: StringBool,
    pub log_no_info_header: StringBool,
    pub log_redirect: StringBool,
    pub log_rotate: StringBool,
    pub log_rotate_dir: String,
    pub log_rotate_max: StringInt,
    pub log_short_level: StringBool,
    pub log_simple_domains: StringBool,
    pub log_thread_id: StringBool,
    pub log_thread_prefix: StringBool,
    pub log_time: StringBool,
    pub log_to_screen: StringBool,
    pub log_truncate: StringBool,
    pub machine_id: StringInt,
    pub max_connect_time: StringInt,
    pub max_connections: StringInt,
    pub max_packet_size: String,
    pub max_queue: StringInt,
    pub max_request_length: StringInt,
    pub max_shutdown_wait: StringInt,
    pub max_slot_errors: StringInt,
    pub max_unit_errors: StringInt,
    pub max_units: StringInt,
    pub memory: String,
    pub min_connect_time: StringInt,
    pub next_unit_percentage: StringInt,
    pub priority: String,
    pub no_assembly: StringBool,
    pub open_web_control: StringBool,
    pub opencl_index: String,
    pub os_species: String,
    pub os_type: String,
    pub passkey: String,
    pub password: String,
    pub pause_on_battery: StringBool,
    pub pause_on_start: StringBool,
    pub paused: StringBool,
    pub pid: StringBool,
    pub pid_file: String,
    pub power: Power,
    pub private_key_file: String,
    pub project_key: StringInt,
    pub proxy: String,
    pub proxy_enable: StringBool,
    pub proxy_pass: String,
    pub proxy_user: String,
    pub respawn: StringBool,
    pub service: StringBool,
    pub service_description: String,
    pub service_restart: StringBool,
    pub service_restart_delay: StringInt,
    pub session_cookie: String,
    pub session_lifetime: StringInt,
    pub session_timeout: StringInt,
    pub smp: StringBool,
    pub stack_traces: StringBool,
    pub stall_detection_enabled: StringBool,
    pub stall_percent: StringInt,
    pub stall_timeout: StringInt,
    pub team: StringInt,
    pub user: String,
    pub verbosity: StringInt,
    pub web_allow: String,
    pub web_deny: String,
    pub web_enable: StringBool,
}

#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Default)]
pub struct StringBool(pub bool);

impl From<bool> for StringBool {
    fn from(b: bool) -> Self {
        Self(b)
    }
}

impl std::fmt::Display for StringBool {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.0)
    }
}

impl<'de> serde::de::Deserialize<'de> for StringBool {
    fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        match serde_json::from_str(serde::de::Deserialize::deserialize(deserializer)?) {
            Ok(result) => Ok(Self(result)),
            Err(e) => Err(serde::de::Error::custom(e.to_string())),
        }
    }
}

#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Default)]
pub struct StringInt(pub i64);

impl From<i64> for StringInt {
    fn from(n: i64) -> Self {
        Self(n)
    }
}

impl std::fmt::Display for StringInt {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.0)
    }
}

impl<'de> serde::de::Deserialize<'de> for StringInt {
    fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        match serde_json::from_str(serde::de::Deserialize::deserialize(deserializer)?) {
            Ok(n) => Ok(Self(n)),
            Err(e) => Err(serde::de::Error::custom(e.to_string())),
        }
    }
}

#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
pub enum Power {
    PowerNull,
    PowerLight,
    PowerMedium,
    PowerFull,
}

impl Power {
    fn new(s: &str) -> Result<Self> {
        Ok(match s.to_uppercase().as_str() {
            "" => Power::PowerNull,
            "LIGHT" => Power::PowerLight,
            "MEDIUM" => Power::PowerMedium,
            "FULL" => Power::PowerFull,
            _ => return Err(format!("s is invalid: {}", s).into()),
        })
    }
}

impl std::default::Default for Power {
    fn default() -> Self {
        Power::PowerNull
    }
}

impl From<&str> for Power {
    /// Panics if s is invalid.
    fn from(s: &str) -> Self {
        Self::new(s).unwrap()
    }
}

impl std::fmt::Display for Power {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let s = match self {
            Power::PowerNull => "",
            Power::PowerLight => "LIGHT",
            Power::PowerMedium => "MEDIUM",
            Power::PowerFull => "FULL",
        };
        write!(f, "{}", s)
    }
}

impl<'de> serde::de::Deserialize<'de> for Power {
    fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        Self::new(serde::de::Deserialize::deserialize(deserializer)?)
            .map_err(|e| serde::de::Error::custom(e.to_string()))
    }
}

#[derive(Clone, PartialEq, Eq, Hash, Debug, Default, serde::Deserialize)]
#[serde(rename_all = "kebab-case", default)]
pub struct SlotQueueInfo {
    pub id: String,
    pub state: String,
    pub error: String,
    pub project: i64,
    pub run: i64,
    pub clone: i64,
    pub gen: i64,
    pub core: String,
    pub unit: String,
    pub percent_done: String,
    pub eta: FAHDuration,
    pub ppd: StringInt,
    pub credit_estimate: StringInt,
    pub waiting_on: String,
    pub next_attempt: FAHDuration,
    pub time_remaining: FAHDuration,
    pub total_frames: i64,
    pub frames_done: i64,
    pub assigned: FAHTime,
    pub timeout: FAHTime,
    pub deadline: FAHTime,
    pub ws: String,
    pub cs: String,
    pub attempts: i64,
    pub slot: String,
    pub tpf: FAHDuration,
    pub base_credit: StringInt,
}

/// None means invalid time.
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Default)]
pub struct FAHTime(pub Option<chrono::DateTime<chrono::offset::Utc>>);

impl From<Option<chrono::DateTime<chrono::offset::Utc>>> for FAHTime {
    fn from(t: Option<chrono::DateTime<chrono::offset::Utc>>) -> Self {
        Self(t)
    }
}

impl From<chrono::DateTime<chrono::offset::Utc>> for FAHTime {
    fn from(t: chrono::DateTime<chrono::offset::Utc>) -> Self {
        Self(Some(t))
    }
}

const INVALID_TIME: &str = "<invalid>";

impl std::fmt::Display for FAHTime {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self.0 {
            Some(t) => write!(f, "{}", t),
            None => write!(f, "{}", INVALID_TIME),
        }
    }
}

impl<'de> serde::de::Deserialize<'de> for FAHTime {
    fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        let s = serde::de::Deserialize::deserialize(deserializer)?;
        if s == INVALID_TIME {
            return Ok(None.into());
        }

        match chrono::DateTime::parse_from_rfc3339(s) {
            Ok(t) => Ok(t.with_timezone(&chrono::offset::Utc).into()),
            Err(e) => Err(serde::de::Error::custom(e.to_string())),
        }
    }
}

/// None means unknown duration.
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Default)]
pub struct FAHDuration(pub Option<chrono::Duration>);

impl From<Option<chrono::Duration>> for FAHDuration {
    fn from(d: Option<chrono::Duration>) -> Self {
        Self(d)
    }
}

impl From<chrono::Duration> for FAHDuration {
    fn from(d: chrono::Duration) -> Self {
        Self(Some(d))
    }
}

const UNKNOWN_TIME: &str = "unknowntime";

impl std::fmt::Display for FAHDuration {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self.0 {
            Some(d) => write!(f, "{}", d),
            None => write!(f, "{}", UNKNOWN_TIME),
        }
    }
}

impl<'de> serde::de::Deserialize<'de> for FAHDuration {
    fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        let s = serde::de::Deserialize::deserialize(deserializer)?;
        if s == UNKNOWN_TIME {
            return Ok(None.into());
        }

        // TODO don't rely on parse_duration since it has a terrible worst-case performance
        match parse_duration::parse(s) {
            Ok(d) => match chrono::Duration::from_std(d) {
                Ok(d) => Ok(d.into()),
                Err(e) => Err(serde::de::Error::custom(e.to_string())),
            },
            Err(e) => Err(serde::de::Error::custom(e.to_string())),
        }
    }
}

#[derive(Clone, PartialEq, Debug, Default, serde::Deserialize)]
#[serde(rename_all = "kebab-case", default)]
pub struct SimulationInfo {
    pub user: String,
    pub team: String,
    pub project: i64,
    pub run: i64,
    pub clone: i64,
    pub gen: i64,
    pub core_type: i64,
    pub core: String,
    pub total_iterations: i64,
    pub iterations_done: i64,
    pub energy: i64,
    pub temperature: i64,
    pub start_time: FAHTime,
    pub timeout: i64,
    pub deadline: i64,
    pub eta: i64,
    pub progress: f64,
    pub slot: i64,
}

#[derive(Clone, PartialEq, Eq, Debug, Default, serde::Deserialize)]
#[serde(rename_all = "kebab-case", default)]
pub struct SlotInfo {
    pub id: String,
    pub status: String,
    pub description: String,
    pub options: serde_json::map::Map<String, serde_json::Value>,
    pub reason: String,
    pub idle: bool,
}

#[derive(Clone, PartialEq, Eq, Debug, Default, serde::Deserialize)]
#[serde(rename_all = "kebab-case", default)]
pub struct SlotOptions {
    pub machine_id: String,
    pub paused: StringBool,
}