• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

kdash-rs / kdash / 3750513782

pending completion
3750513782

push

github

Deepu
fix tests

982 of 4441 branches covered (22.11%)

Branch coverage included in aggregate %.

2731 of 4724 relevant lines covered (57.81%)

8.14 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

88.13
/src/app/utils.rs
1
use k8s_openapi::{
2
  apimachinery::pkg::apis::meta::v1::Time,
3
  chrono::{DateTime, Duration, Utc},
4
};
5

6
use kube::{Resource, ResourceExt};
7

8
pub fn sanitize_obj<K: Resource>(mut obj: K) -> K {
55✔
9
  obj.managed_fields_mut().clear();
55✔
10
  obj
55✔
11
}
55✔
12

13
pub static UNKNOWN: &str = "Unknown";
14

15
pub fn to_age(timestamp: Option<&Time>, against: DateTime<Utc>) -> String {
116✔
16
  match timestamp {
116!
17
    Some(time) => {
116✔
18
      let time = time.0;
116✔
19
      let duration = against.signed_duration_since(time);
116✔
20

21
      duration_to_age(duration, false)
116✔
22
    }
23
    None => String::default(),
×
24
  }
25
}
116✔
26

27
pub fn to_age_secs(timestamp: Option<&Time>, against: DateTime<Utc>) -> String {
19✔
28
  match timestamp {
19!
29
    Some(time) => {
19✔
30
      let time = time.0;
19✔
31
      let duration = against.signed_duration_since(time);
19✔
32

33
      duration_to_age(duration, true)
19✔
34
    }
35
    None => String::default(),
×
36
  }
37
}
19✔
38

39
pub fn duration_to_age(duration: Duration, with_secs: bool) -> String {
138✔
40
  let mut out = String::new();
138✔
41
  if duration.num_weeks() != 0 {
138✔
42
    out.push_str(format!("{}w", duration.num_weeks()).as_str());
109✔
43
  }
44
  let days = duration.num_days() - (duration.num_weeks() * 7);
138✔
45
  if days != 0 {
138✔
46
    out.push_str(format!("{}d", days).as_str());
115✔
47
  }
48
  let hrs = duration.num_hours() - (duration.num_days() * 24);
138✔
49
  if hrs != 0 {
138✔
50
    out.push_str(format!("{}h", hrs).as_str());
111✔
51
  }
52
  let mins = duration.num_minutes() - (duration.num_hours() * 60);
138✔
53
  if mins != 0 && days == 0 && duration.num_weeks() == 0 {
138✔
54
    out.push_str(format!("{}m", mins).as_str());
14✔
55
  }
56
  if with_secs {
138✔
57
    let secs = duration.num_seconds() - (duration.num_minutes() * 60);
22✔
58
    if secs != 0 && hrs == 0 && days == 0 && duration.num_weeks() == 0 {
22✔
59
      out.push_str(format!("{}s", secs).as_str());
6✔
60
    }
61
  }
62
  if out.is_empty() && with_secs {
138✔
63
    "0s".into()
1✔
64
  } else if out.is_empty() {
137✔
65
    "0m".into()
1✔
66
  } else {
67
    out
136✔
68
  }
69
}
138✔
70

71
pub fn mem_to_mi(mem: String) -> String {
8✔
72
  if mem.ends_with("Ki") {
13✔
73
    let mem = mem.trim_end_matches("Ki").parse::<i64>().unwrap_or(0);
5✔
74
    format!("{}Mi", mem / 1024)
5!
75
  } else if mem.ends_with("Gi") {
3✔
76
    let mem = mem.trim_end_matches("Gi").parse::<i64>().unwrap_or(0);
1✔
77
    format!("{}Mi", mem * 1024)
1✔
78
  } else {
79
    mem
2✔
80
  }
81
}
8✔
82

83
pub fn cpu_to_milli(cpu: String) -> String {
8✔
84
  if cpu.ends_with('m') {
8✔
85
    cpu
1✔
86
  } else if cpu.ends_with('n') {
7✔
87
    format!(
3✔
88
      "{}m",
89
      (convert_to_f64(cpu.trim_end_matches('n')) / 1000000f64).floor()
3✔
90
    )
91
  } else {
92
    format!("{}m", (convert_to_f64(&cpu) * 1000f64).floor())
4✔
93
  }
94
}
8✔
95

96
pub fn to_cpu_percent(used: String, total: String) -> f64 {
2✔
97
  // convert from nano cpu to milli cpu
98
  let used = convert_to_f64(used.trim_end_matches('m'));
2✔
99
  let total = convert_to_f64(total.trim_end_matches('m'));
2✔
100

101
  to_percent(used, total)
2✔
102
}
2✔
103

104
pub fn to_mem_percent(used: String, total: String) -> f64 {
2✔
105
  let used = convert_to_f64(used.trim_end_matches("Mi"));
2✔
106
  let total = convert_to_f64(total.trim_end_matches("Mi"));
2✔
107

108
  to_percent(used, total)
2✔
109
}
2✔
110

111
pub fn to_percent(used: f64, total: f64) -> f64 {
4✔
112
  ((used / total) * 100f64).floor()
4✔
113
}
4✔
114

115
pub fn convert_to_f64(s: &str) -> f64 {
15✔
116
  s.parse().unwrap_or(0f64)
15✔
117
}
15✔
118

119
#[cfg(test)]
120
#[allow(clippy::float_cmp)]
121
mod tests {
122
  use k8s_openapi::{
123
    apimachinery::pkg::apis::meta::v1::Time,
124
    chrono::{DateTime, TimeZone, Utc},
125
  };
126

127
  #[test]
128
  fn test_mem_to_mi() {
2✔
129
    use super::mem_to_mi;
130
    assert_eq!(mem_to_mi(String::from("2820Mi")), String::from("2820Mi"));
1!
131
    assert_eq!(mem_to_mi(String::from("2888180Ki")), String::from("2820Mi"));
1!
132
    assert_eq!(mem_to_mi(String::from("5Gi")), String::from("5120Mi"));
1!
133
    assert_eq!(mem_to_mi(String::from("5")), String::from("5"));
1!
134
  }
2✔
135
  #[test]
136
  fn test_to_cpu_percent() {
2✔
137
    use super::to_cpu_percent;
138
    assert_eq!(
1✔
139
      to_cpu_percent(String::from("126m"), String::from("940m")),
1!
140
      13f64
141
    );
142
  }
2✔
143

144
  #[test]
145
  fn test_to_mem_percent() {
2✔
146
    use super::to_mem_percent;
147
    assert_eq!(
1✔
148
      to_mem_percent(String::from("645784Mi"), String::from("2888184Mi")),
1!
149
      22f64
150
    );
151
  }
2✔
152
  #[test]
153
  fn test_cpu_to_milli() {
2✔
154
    use super::cpu_to_milli;
155
    assert_eq!(cpu_to_milli(String::from("645m")), String::from("645m"));
1!
156
    assert_eq!(
1✔
157
      cpu_to_milli(String::from("126632173n")),
1✔
158
      String::from("126m")
1!
159
    );
160
    assert_eq!(cpu_to_milli(String::from("8")), String::from("8000m"));
1!
161
    assert_eq!(cpu_to_milli(String::from("0")), String::from("0m"));
1!
162
  }
2✔
163

164
  fn get_time(s: &str) -> Time {
28✔
165
    Time(to_utc(s))
28✔
166
  }
28✔
167

168
  fn to_utc(s: &str) -> DateTime<Utc> {
58✔
169
    Utc.datetime_from_str(s, "%d-%m-%Y %H:%M:%S").unwrap()
58✔
170
  }
58✔
171

172
  #[test]
173
  fn test_to_age_secs() {
2✔
174
    use std::time::SystemTime;
175

176
    use super::to_age_secs;
177

178
    assert_eq!(
1✔
179
      to_age_secs(Some(&Time(Utc::now())), Utc::now()),
1✔
180
      String::from("0s")
1!
181
    );
182
    assert_eq!(
1✔
183
      to_age_secs(
1✔
184
        Some(&get_time("15-4-2021 14:09:10")),
1✔
185
        to_utc("15-4-2021 14:10:00")
1✔
186
      ),
187
      String::from("50s")
1!
188
    );
189
    assert_eq!(
1✔
190
      to_age_secs(
1✔
191
        Some(&get_time("15-4-2021 14:08:10")),
1✔
192
        to_utc("15-4-2021 14:10:00")
1✔
193
      ),
194
      String::from("1m50s")
1!
195
    );
196
    assert_eq!(
1✔
197
      to_age_secs(
1✔
198
        Some(&get_time("15-4-2021 14:09:00")),
1✔
199
        to_utc("15-4-2021 14:10:00")
1✔
200
      ),
201
      String::from("1m")
1!
202
    );
203
    assert_eq!(
1✔
204
      to_age_secs(
1✔
205
        Some(&get_time("15-4-2021 13:50:00")),
1✔
206
        to_utc("15-4-2021 14:10:00")
1✔
207
      ),
208
      String::from("20m")
1!
209
    );
210
    assert_eq!(
1✔
211
      to_age_secs(
1✔
212
        Some(&get_time("15-4-2021 13:50:10")),
1✔
213
        to_utc("15-4-2021 14:10:0")
1✔
214
      ),
215
      String::from("19m50s")
1!
216
    );
217
    assert_eq!(
1✔
218
      to_age_secs(
1✔
219
        Some(&get_time("15-4-2021 10:50:10")),
1✔
220
        to_utc("15-4-2021 14:10:0")
1✔
221
      ),
222
      String::from("3h19m")
1!
223
    );
224
    assert_eq!(
1✔
225
      to_age_secs(
1✔
226
        Some(&get_time("14-4-2021 15:10:10")),
1✔
227
        to_utc("15-4-2021 14:10:10")
1✔
228
      ),
229
      String::from("23h")
1!
230
    );
231
    assert_eq!(
1✔
232
      to_age_secs(
1✔
233
        Some(&get_time("14-4-2021 14:11:10")),
1✔
234
        to_utc("15-4-2021 14:10:10")
1✔
235
      ),
236
      String::from("23h59m")
1!
237
    );
238
    assert_eq!(
1✔
239
      to_age_secs(
1✔
240
        Some(&get_time("14-4-2021 14:10:10")),
1✔
241
        to_utc("15-4-2021 14:10:10")
1✔
242
      ),
243
      String::from("1d")
1!
244
    );
245
    assert_eq!(
1✔
246
      to_age_secs(
1✔
247
        Some(&get_time("12-4-2021 14:10:10")),
1✔
248
        to_utc("15-4-2021 14:10:10")
1✔
249
      ),
250
      String::from("3d")
1!
251
    );
252
    assert_eq!(
1✔
253
      to_age_secs(
1✔
254
        Some(&get_time("12-4-2021 13:50:10")),
1✔
255
        to_utc("15-4-2021 14:10:10")
1✔
256
      ),
257
      String::from("3d")
1!
258
    );
259
    assert_eq!(
1✔
260
      to_age_secs(
1✔
261
        Some(&get_time("12-4-2021 11:10:10")),
1✔
262
        to_utc("15-4-2021 14:10:10")
1✔
263
      ),
264
      String::from("3d3h")
1!
265
    );
266
    assert_eq!(
1✔
267
      to_age_secs(
1✔
268
        Some(&get_time("12-4-2021 10:50:10")),
1✔
269
        to_utc("15-4-2021 14:10:0")
1✔
270
      ),
271
      String::from("3d3h")
1!
272
    );
273
    assert_eq!(
1✔
274
      to_age_secs(
1✔
275
        Some(&get_time("08-4-2021 14:10:10")),
1✔
276
        to_utc("15-4-2021 14:10:10")
1✔
277
      ),
278
      String::from("1w")
1!
279
    );
280
    assert_eq!(
1✔
281
      to_age_secs(
1✔
282
        Some(&get_time("05-4-2021 12:30:10")),
1✔
283
        to_utc("15-4-2021 14:10:10")
1✔
284
      ),
285
      String::from("1w3d1h")
1!
286
    );
287
    assert_eq!(
1✔
288
      to_age_secs(
1✔
289
        Some(&Time(DateTime::from(SystemTime::UNIX_EPOCH))),
1✔
290
        to_utc("15-4-2021 14:10:0")
1✔
291
      ),
292
      String::from("2676w14h")
1!
293
    );
294
  }
2✔
295
  #[test]
296
  fn test_to_age() {
2✔
297
    use std::time::SystemTime;
298

299
    use super::to_age;
300

301
    assert_eq!(
1✔
302
      to_age(Some(&Time(Utc::now())), Utc::now()),
1✔
303
      String::from("0m")
1!
304
    );
305
    assert_eq!(
1✔
306
      to_age(
1✔
307
        Some(&get_time("15-4-2021 14:09:00")),
1✔
308
        to_utc("15-4-2021 14:10:00")
1✔
309
      ),
310
      String::from("1m")
1!
311
    );
312
    assert_eq!(
1✔
313
      to_age(
1✔
314
        Some(&get_time("15-4-2021 13:50:00")),
1✔
315
        to_utc("15-4-2021 14:10:00")
1✔
316
      ),
317
      String::from("20m")
1!
318
    );
319
    assert_eq!(
1✔
320
      to_age(
1✔
321
        Some(&get_time("15-4-2021 13:50:10")),
1✔
322
        to_utc("15-4-2021 14:10:0")
1✔
323
      ),
324
      String::from("19m")
1!
325
    );
326
    assert_eq!(
1✔
327
      to_age(
1✔
328
        Some(&get_time("15-4-2021 10:50:10")),
1✔
329
        to_utc("15-4-2021 14:10:0")
1✔
330
      ),
331
      String::from("3h19m")
1!
332
    );
333
    assert_eq!(
1✔
334
      to_age(
1✔
335
        Some(&get_time("14-4-2021 15:10:10")),
1✔
336
        to_utc("15-4-2021 14:10:10")
1✔
337
      ),
338
      String::from("23h")
1!
339
    );
340
    assert_eq!(
1✔
341
      to_age(
1✔
342
        Some(&get_time("14-4-2021 14:11:10")),
1✔
343
        to_utc("15-4-2021 14:10:10")
1✔
344
      ),
345
      String::from("23h59m")
1!
346
    );
347
    assert_eq!(
1✔
348
      to_age(
1✔
349
        Some(&get_time("14-4-2021 14:10:10")),
1✔
350
        to_utc("15-4-2021 14:10:10")
1✔
351
      ),
352
      String::from("1d")
1!
353
    );
354
    assert_eq!(
1✔
355
      to_age(
1✔
356
        Some(&get_time("12-4-2021 14:10:10")),
1✔
357
        to_utc("15-4-2021 14:10:10")
1✔
358
      ),
359
      String::from("3d")
1!
360
    );
361
    assert_eq!(
1✔
362
      to_age(
1✔
363
        Some(&get_time("12-4-2021 13:50:10")),
1✔
364
        to_utc("15-4-2021 14:10:10")
1✔
365
      ),
366
      String::from("3d")
1!
367
    );
368
    assert_eq!(
1✔
369
      to_age(
1✔
370
        Some(&get_time("12-4-2021 11:10:10")),
1✔
371
        to_utc("15-4-2021 14:10:10")
1✔
372
      ),
373
      String::from("3d3h")
1!
374
    );
375
    assert_eq!(
1✔
376
      to_age(
1✔
377
        Some(&get_time("12-4-2021 10:50:10")),
1✔
378
        to_utc("15-4-2021 14:10:0")
1✔
379
      ),
380
      String::from("3d3h")
1!
381
    );
382
    assert_eq!(
1✔
383
      to_age(
1✔
384
        Some(&get_time("08-4-2021 14:10:10")),
1✔
385
        to_utc("15-4-2021 14:10:10")
1✔
386
      ),
387
      String::from("1w")
1!
388
    );
389
    assert_eq!(
1✔
390
      to_age(
1✔
391
        Some(&get_time("05-4-2021 12:30:10")),
1✔
392
        to_utc("15-4-2021 14:10:10")
1✔
393
      ),
394
      String::from("1w3d1h")
1!
395
    );
396
    assert_eq!(
1✔
397
      to_age(
1✔
398
        Some(&Time(DateTime::from(SystemTime::UNIX_EPOCH))),
1✔
399
        to_utc("15-4-2021 14:10:0")
1✔
400
      ),
401
      String::from("2676w14h")
1!
402
    );
403
  }
2✔
404
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc