gitlab-time-report 1.3.0

Library to generate statistics and charts from GitLab time tracking data.
Documentation
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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
//! This module contains methods to validate [`TimeLog`]s and report possible problems.
//! They are implemented via the validator chain pattern.

use crate::model::{TimeLog, TrackableItem};
use chrono::{Local, NaiveDate};
use std::collections::HashSet;

/// Possible problems in [`TimeLog`] that can be found during validation.
#[derive(Debug)]
pub enum ValidationProblem {
    /// The time spent exceeds the maximum allowed.
    ExcessiveHours { max_hours: u16 },
    /// No summary was entered.
    MissingSummary,
    /// Entered date is in the future.
    FutureDate,
    /// Duplicate entry has been found (same user, date, trackable item, time spent and summary)
    DuplicateEntry,
    /// `TimeLog` is before the configured project start date.
    BeforeStartDate { start_date: NaiveDate },
}

/// Stores the result of a validation run.
pub struct ValidationResult<'a> {
    /// The time log that was validated.
    pub time_log: &'a TimeLog,
    /// The problems found in this time log.
    pub problems: Vec<ValidationProblem>,
}

impl ValidationResult<'_> {
    /// Returns true if there are no problems found in this time log.
    #[must_use]
    pub fn is_valid(&self) -> bool {
        self.problems.is_empty()
    }

    /// Returns true if there is at least one problem of the given type.
    #[must_use]
    pub fn has_problems(&self, problem_type: &ValidationProblem) -> bool {
        self.problems
            .iter()
            .any(|i| std::mem::discriminant(i) == std::mem::discriminant(problem_type))
    }
}

/// Trait functions that a validator must implement.
pub trait Validator {
    /// Validate each [`TimeLog`] on its own. Returns a Vec containing the problems found
    /// or an empty Vec if there are none.
    fn validate_single(&mut self, time_log: &TimeLog) -> Vec<ValidationProblem>;
}

/// The main validator that runs all validators.  Add the other validators to this one with
/// [`TimeLogValidator::with_validator`], then call [`TimeLogValidator::validate`] to run the validation.
/// # Example
/// ```
/// # use gitlab_time_report::validation::{TimeLogValidator, ValidationProblem, ExcessiveHoursValidator, HasSummaryValidator};
/// # use gitlab_time_report::model::TimeLog;
/// # use chrono::Local;
/// let time_logs = [
///     TimeLog{ summary: None, ..Default::default() },
///     TimeLog{ summary: Some("Code Review".to_string()), ..Default::default() }
/// ];
///
/// let mut validator = TimeLogValidator::new()
///     .with_validator(ExcessiveHoursValidator::new(10))
///     .with_validator(HasSummaryValidator);
/// let results = validator.validate(&time_logs);
///
/// // Assertions to check the results, you don't need to do this in your code
/// assert!(results[0].has_problems(&ValidationProblem::MissingSummary));
/// assert!(!results[0].has_problems(&ValidationProblem::ExcessiveHours{ max_hours: 10 }));
/// assert!(results[1].is_valid());
///
/// for result in results {
///     if result.is_valid() { continue; }
///     for problem in &result.problems {
///         match problem {
///             ValidationProblem::ExcessiveHours { max_hours } => println!("Time spent exceeds maximum of {max_hours} hours"),
///             ValidationProblem::MissingSummary => println!("No summary was entered"),
///             _ => {}
///         }
///     }
/// }
/// ```
pub struct TimeLogValidator {
    validators: Vec<Box<dyn Validator>>,
}

/// Added for <https://rust-lang.github.io/rust-clippy/master/index.html#new_without_default>
impl Default for TimeLogValidator {
    fn default() -> Self {
        Self::new()
    }
}

impl TimeLogValidator {
    /// Create a new [`TimeLogValidator`].
    #[must_use]
    pub fn new() -> Self {
        Self {
            validators: Vec::new(),
        }
    }

    #[must_use]
    /// Add a new validator to the chain. See [`Validator`] for more information.
    /// # Example
    /// ```
    /// # use gitlab_time_report::validation::{TimeLogValidator, ValidationProblem, ExcessiveHoursValidator, HasSummaryValidator};
    /// # use gitlab_time_report::model::TimeLog;
    /// # use chrono::Local;
    /// let mut validator = TimeLogValidator::new()
    ///     .with_validator(ExcessiveHoursValidator::new(10))
    ///     .with_validator(HasSummaryValidator);
    /// ```
    pub fn with_validator(mut self, validator: impl Validator + 'static) -> Self {
        self.validators.push(Box::new(validator));
        self
    }

    /// Run all validators on the given time logs.
    pub fn validate<'a>(&mut self, time_logs: &'a [TimeLog]) -> Vec<ValidationResult<'a>> {
        let validation_results: Vec<ValidationResult<'a>> = time_logs
            .iter()
            .map(|time_log| {
                let mut problems = Vec::new();

                // Run all validators on this time log
                for validator in &mut self.validators {
                    // Append the problems Vec with the problems found by this validator
                    problems.extend(validator.validate_single(time_log));
                }

                ValidationResult { time_log, problems }
            })
            .collect();

        validation_results
    }
}

/// Validates that a single time log does not exceed the given maximum hours.
pub struct ExcessiveHoursValidator {
    max_hours: u16,
}

impl ExcessiveHoursValidator {
    /// Set the maximum hours allowed.
    #[must_use]
    pub fn new(max_hours: u16) -> Self {
        Self { max_hours }
    }
}

impl Validator for ExcessiveHoursValidator {
    fn validate_single(&mut self, time_log: &TimeLog) -> Vec<ValidationProblem> {
        let hours = time_log.time_spent.num_hours();

        match hours > i64::from(self.max_hours) {
            true => vec![ValidationProblem::ExcessiveHours {
                max_hours: self.max_hours,
            }],
            false => Vec::new(),
        }
    }
}

/// Validates that a time log has a summary.
pub struct HasSummaryValidator;

impl Validator for HasSummaryValidator {
    fn validate_single(&mut self, time_log: &TimeLog) -> Vec<ValidationProblem> {
        match time_log.summary.is_none() {
            true => vec![ValidationProblem::MissingSummary],
            false => Vec::new(),
        }
    }
}

/// Validates that a time log has no date in the future.
pub struct NoFutureDateValidator;

impl Validator for NoFutureDateValidator {
    fn validate_single(&mut self, time_log: &TimeLog) -> Vec<ValidationProblem> {
        if time_log.spent_at > Local::now() {
            return vec![ValidationProblem::FutureDate];
        }
        Vec::new()
    }
}

/// Validates that a time log does not contain duplicates (same user, date, trackable item, time spent and summary)
pub struct DuplicatesValidator {
    seen: HashSet<(String, NaiveDate, i64, Option<String>, TrackableItem)>,
}

/// Added for <https://rust-lang.github.io/rust-clippy/master/index.html#new_without_default>
impl Default for DuplicatesValidator {
    fn default() -> Self {
        Self::new()
    }
}

impl DuplicatesValidator {
    #[must_use]
    pub fn new() -> Self {
        Self {
            seen: HashSet::new(),
        }
    }
}

impl Validator for DuplicatesValidator {
    fn validate_single(&mut self, time_log: &TimeLog) -> Vec<ValidationProblem> {
        let key = (
            time_log.user.name.clone(),
            time_log.spent_at.date_naive(),
            time_log.time_spent.num_seconds(),
            time_log.summary.clone(),
            time_log.trackable_item.clone(),
        );

        if self.seen.insert(key) {
            Vec::new()
        } else {
            vec![ValidationProblem::DuplicateEntry]
        }
    }
}

/// Validates that a time log date is not before the project start date.
pub struct BeforeStartDateValidator {
    start_date: NaiveDate,
}

impl BeforeStartDateValidator {
    #[must_use]
    pub fn new(start_date: NaiveDate) -> Self {
        Self { start_date }
    }
}

impl Validator for BeforeStartDateValidator {
    fn validate_single(&mut self, time_log: &TimeLog) -> Vec<ValidationProblem> {
        let log_date = time_log.spent_at.date_naive();
        if log_date < self.start_date {
            return vec![ValidationProblem::BeforeStartDate {
                start_date: self.start_date,
            }];
        }
        Vec::new()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::model::{MergeRequest, TimeLog, TrackableItemFields, TrackableItemKind};
    use chrono::{Duration, Local, TimeDelta};

    const NUMBER_OF_LOGS: usize = 7;
    fn get_time_logs() -> [TimeLog; NUMBER_OF_LOGS] {
        [
            TimeLog {
                summary: Some("Valid Time log".to_string()),
                spent_at: Local::now() - TimeDelta::days(1),
                time_spent: Duration::hours(4),
                trackable_item: TrackableItem {
                    common: TrackableItemFields {
                        title: "test".to_string(),
                        ..Default::default()
                    },
                    kind: TrackableItemKind::MergeRequest(MergeRequest::default()),
                },
                ..Default::default()
            },
            TimeLog {
                summary: Some("Excessive Hours".to_string()),
                spent_at: Local::now() - TimeDelta::days(1),
                time_spent: Duration::hours(12),
                ..Default::default()
            },
            TimeLog {
                summary: None,
                spent_at: Local::now() - TimeDelta::days(1),
                time_spent: Duration::hours(5) + Duration::minutes(30),
                ..Default::default()
            },
            TimeLog {
                summary: Some("Future Date".to_string()),
                spent_at: Local::now() + TimeDelta::hours(1),
                time_spent: Duration::hours(3),
                ..Default::default()
            },
            TimeLog {
                // Duplicate entry
                summary: Some("Valid Time log".to_string()),
                spent_at: Local::now() - TimeDelta::days(1),
                time_spent: Duration::hours(4),
                trackable_item: TrackableItem {
                    common: TrackableItemFields {
                        title: "test".to_string(),
                        ..Default::default()
                    },
                    kind: TrackableItemKind::MergeRequest(MergeRequest::default()),
                },
                ..Default::default()
            },
            TimeLog {
                // No Summary, Future Date and Excessive Hours
                summary: None,
                spent_at: Local::now() + TimeDelta::days(1),
                time_spent: Duration::hours(15),
                ..Default::default()
            },
            TimeLog {
                // Should not trigger a duplicate entry problem
                summary: Some("Same time spent & spent_at as 'Summary missing' timelog, but different summary".to_string()),
                spent_at: Local::now() - TimeDelta::days(1),
                time_spent: Duration::hours(5) + Duration::minutes(30),
                ..Default::default()
            }
        ]
    }

    #[test]
    fn test_excessive_hours_validator() {
        const EXCESSIVE_HOURS_LIMIT: u16 = 10;

        let time_logs = get_time_logs();
        let expected_problem = ValidationProblem::ExcessiveHours {
            max_hours: EXCESSIVE_HOURS_LIMIT,
        };

        let mut validator = TimeLogValidator::new()
            .with_validator(ExcessiveHoursValidator::new(EXCESSIVE_HOURS_LIMIT));

        let results = validator.validate(&time_logs);
        assert_eq!(results.len(), NUMBER_OF_LOGS);
        for (i, result) in results.iter().enumerate() {
            match i {
                1 | 5 => assert!(result.has_problems(&expected_problem)),
                _ => assert!(result.is_valid()),
            }
        }
    }

    #[test]
    fn test_has_summary_validator() {
        let time_logs = get_time_logs();
        let expected_problem = ValidationProblem::MissingSummary;

        let mut validator = TimeLogValidator::new().with_validator(HasSummaryValidator);

        let results = validator.validate(&time_logs);
        assert_eq!(results.len(), NUMBER_OF_LOGS);
        for (i, result) in results.iter().enumerate() {
            match i {
                2 | 5 => assert!(result.has_problems(&expected_problem)),
                _ => assert!(result.is_valid()),
            }
        }
    }

    #[test]
    fn test_future_date_validator() {
        let time_logs = get_time_logs();
        let expected_problem = ValidationProblem::FutureDate;

        let mut validator = TimeLogValidator::new().with_validator(NoFutureDateValidator);

        let results = validator.validate(&time_logs);
        assert_eq!(results.len(), NUMBER_OF_LOGS);
        for (i, result) in results.iter().enumerate() {
            match i {
                3 | 5 => assert!(result.has_problems(&expected_problem)),
                _ => assert!(result.is_valid()),
            }
        }
    }

    #[test]
    fn test_duplicates_validator() {
        let time_logs = get_time_logs();
        let expected_problem = ValidationProblem::DuplicateEntry;

        let mut validator = TimeLogValidator::new().with_validator(DuplicatesValidator::new());

        let results = validator.validate(&time_logs);
        assert_eq!(results.len(), NUMBER_OF_LOGS);
        for (i, result) in results.iter().enumerate() {
            match i {
                4 => assert!(result.has_problems(&expected_problem)),
                _ => assert!(result.is_valid()),
            }
        }
    }

    #[test]
    fn test_before_start_date_validator() {
        let time_logs = get_time_logs();
        let start_date = Local::now().date_naive();

        let expected_problem = ValidationProblem::BeforeStartDate { start_date };

        let mut validator =
            TimeLogValidator::new().with_validator(BeforeStartDateValidator::new(start_date));

        let results = validator.validate(&time_logs);
        assert_eq!(results.len(), NUMBER_OF_LOGS);
        for (i, result) in results.iter().enumerate() {
            match i {
                0 | 1 | 2 | 4 | 6 => assert!(result.has_problems(&expected_problem)),
                _ => assert!(result.is_valid()),
            }
        }
    }

    #[test]
    fn test_all_validators() {
        const EXCESSIVE_HOURS_LIMIT: u16 = 10;

        let time_logs = get_time_logs();
        let mut validator = TimeLogValidator::new()
            .with_validator(ExcessiveHoursValidator::new(EXCESSIVE_HOURS_LIMIT))
            .with_validator(HasSummaryValidator)
            .with_validator(NoFutureDateValidator)
            .with_validator(DuplicatesValidator::new());

        let excessive_hours_validator = ValidationProblem::ExcessiveHours {
            max_hours: EXCESSIVE_HOURS_LIMIT,
        };

        let results = validator.validate(&time_logs);

        assert_eq!(results.len(), NUMBER_OF_LOGS);
        assert!(results[0].is_valid());

        assert_eq!(results[1].problems.len(), 1);
        assert!(results[1].has_problems(&excessive_hours_validator));

        assert_eq!(results[2].problems.len(), 1);
        assert!(results[2].has_problems(&ValidationProblem::MissingSummary));

        assert_eq!(results[3].problems.len(), 1);
        assert!(results[3].has_problems(&ValidationProblem::FutureDate));

        assert_eq!(results[4].problems.len(), 1);
        assert!(results[4].has_problems(&ValidationProblem::DuplicateEntry));

        assert_eq!(results[5].problems.len(), 3);
        assert!(results[5].has_problems(&excessive_hours_validator));
        assert!(results[5].has_problems(&ValidationProblem::MissingSummary));
        assert!(results[5].has_problems(&ValidationProblem::FutureDate));

        assert!(results[6].is_valid());
    }
}