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
|
//! This module contains specific filters that operate on log files.
//!
//! This is the "base unit", as each file corresponds to one log. Filters on other items (such as
//! players) have to be lifted into log filters first.
use super::{
super::{EarlyLogResult, FightOutcome, LogResult},
Filter, Inclusion,
};
use std::{collections::HashSet, ffi::OsStr};
use evtclib::Boss;
use chrono::{DateTime, Datelike, Local, TimeZone, Utc, Weekday};
use num_traits::FromPrimitive as _;
use once_cell::sync::Lazy;
use regex::Regex;
/// The regular expression used to extract datetimes from filenames.
static DATE_REGEX: Lazy<Regex> = Lazy::new(|| Regex::new(r"\d{8}-\d{6}").unwrap());
/// Filter trait used for filters that operate on complete logs.
pub trait LogFilter = Filter<EarlyLogResult, LogResult>;
#[derive(Debug, Clone)]
struct BossFilter(HashSet<Boss>);
impl Filter<EarlyLogResult, LogResult> for BossFilter {
fn filter_early(&self, early_log: &EarlyLogResult) -> Inclusion {
let boss = Boss::from_u16(early_log.evtc.header.combat_id);
boss.map(|b| self.0.contains(&b).into())
.unwrap_or(Inclusion::Exclude)
}
fn filter(&self, log: &LogResult) -> bool {
let boss = Boss::from_u16(log.boss_id);
boss.map(|b| self.0.contains(&b)).unwrap_or(false)
}
}
/// A `LogFilter` that only accepts logs with one of the given bosses.
pub fn boss(bosses: HashSet<Boss>) -> Box<dyn LogFilter> {
Box::new(BossFilter(bosses))
}
#[derive(Debug, Clone)]
struct OutcomeFilter(HashSet<FightOutcome>);
impl Filter<EarlyLogResult, LogResult> for OutcomeFilter {
fn filter(&self, log: &LogResult) -> bool {
self.0.contains(&log.outcome)
}
}
/// A `LogFilter` that only accepts logs with one of the given outcomes.
///
/// See also [`success`][success] and [`wipe`][wipe].
pub fn outcome(outcomes: HashSet<FightOutcome>) -> Box<dyn LogFilter> {
Box::new(OutcomeFilter(outcomes))
}
/// A `LogFilter` that only accepts successful logs.
///
/// See also [`outcome`][outcome] and [`wipe`][wipe].
pub fn success() -> Box<dyn LogFilter> {
let mut outcomes = HashSet::new();
outcomes.insert(FightOutcome::Success);
outcome(outcomes)
}
/// A `LogFilter` that only accepts failed logs.
///
/// See also [`outcome`][outcome] and [`success`][wipe].
pub fn wipe() -> Box<dyn LogFilter> {
let mut outcomes = HashSet::new();
outcomes.insert(FightOutcome::Wipe);
outcome(outcomes)
}
#[derive(Debug, Clone)]
struct WeekdayFilter(HashSet<Weekday>);
impl Filter<EarlyLogResult, LogResult> for WeekdayFilter {
fn filter(&self, log: &LogResult) -> bool {
self.0.contains(&log.time.weekday())
}
}
/// A `LogFilter` that only accepts logs if they were done on one of the given weekdays.
pub fn weekday(weekdays: HashSet<Weekday>) -> Box<dyn LogFilter> {
Box::new(WeekdayFilter(weekdays))
}
#[derive(Debug, Clone)]
struct TimeFilter(Option<DateTime<Utc>>, Option<DateTime<Utc>>, bool);
impl Filter<EarlyLogResult, LogResult> for TimeFilter {
fn filter_early(&self, early_log: &EarlyLogResult) -> Inclusion {
// Ignore the filename heuristic if the user wishes so.
if !self.2 {
return Inclusion::Unknown;
}
early_log
.log_file
.file_name()
.and_then(datetime_from_filename)
.map(|d| time_is_between(d, self.0, self.1))
.map(Into::into)
.unwrap_or(Inclusion::Unknown)
}
fn filter(&self, log: &LogResult) -> bool {
time_is_between(log.time, self.0, self.1)
}
}
/// Check if the given time is after `after` but before `before`.
///
/// If one of the bounds is `None`, the time is always in bounds w.r.t. that bound.
fn time_is_between(
time: DateTime<Utc>,
after: Option<DateTime<Utc>>,
before: Option<DateTime<Utc>>,
) -> bool {
let after_ok = match after {
Some(after) => after <= time,
None => true,
};
let before_ok = match before {
Some(before) => before >= time,
None => true,
};
after_ok && before_ok
}
/// Try to extract the log time from the filename.
///
/// This expects the filename to have the datetime in the pattern `YYYYmmdd-HHMMSS` somewhere in
/// it.
fn datetime_from_filename(name: &OsStr) -> Option<DateTime<Utc>> {
let date_match = DATE_REGEX.find(name.to_str()?)?;
let local_time = Local
.datetime_from_str(date_match.as_str(), "%Y%m%d-%H%M%S")
.ok()?;
Some(local_time.with_timezone(&Utc))
}
/// A `LogFilter` that only accepts logs in the given time frame.
///
/// If a bound is not given, -Infinity is assumed for the lower bound, and Infinity for the upper
/// bound.
pub fn time(lower: Option<DateTime<Utc>>, upper: Option<DateTime<Utc>>) -> Box<dyn LogFilter> {
Box::new(TimeFilter(lower, upper, true))
}
/// A `LogFilter` that only accepts logs after the given date.
///
/// Also see [`time`][time] and [`before`][before].
pub fn after(when: DateTime<Utc>) -> Box<dyn LogFilter> {
time(Some(when), None)
}
/// A `LogFilter` that only accepts logs before the given date.
///
/// Also see [`time`][time] and [`after`][after].
pub fn before(when: DateTime<Utc>) -> Box<dyn LogFilter> {
time(None, Some(when))
}
/// A `LogFilter` that only accepts logs in the given time frame.
///
/// Compared to [`time`][time], this filter ignores the file name. This can result in more accurate
/// results if you renamed logs, but if also leads to a worse runtime.
pub fn log_time(lower: Option<DateTime<Utc>>, upper: Option<DateTime<Utc>>) -> Box<dyn LogFilter> {
Box::new(TimeFilter(lower, upper, false))
}
/// Like [`after`][after], but ignores the file name for date calculations.
pub fn log_after(when: DateTime<Utc>) -> Box<dyn LogFilter> {
log_time(Some(when), None)
}
/// Like [`before`][before], but ignores the file name for date calculations.
pub fn log_before(when: DateTime<Utc>) -> Box<dyn LogFilter> {
log_time(None, Some(when))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_time_is_between() {
assert!(time_is_between(
Utc.ymd(1955, 11, 5).and_hms(6, 15, 0),
None,
None,
));
assert!(time_is_between(
Utc.ymd(1955, 11, 5).and_hms(6, 15, 0),
Some(Utc.ymd(1955, 11, 5).and_hms(5, 0, 0)),
None,
));
assert!(time_is_between(
Utc.ymd(1955, 11, 5).and_hms(6, 15, 0),
None,
Some(Utc.ymd(1955, 11, 5).and_hms(7, 0, 0)),
));
assert!(time_is_between(
Utc.ymd(1955, 11, 5).and_hms(6, 15, 0),
Some(Utc.ymd(1955, 11, 5).and_hms(5, 0, 0)),
Some(Utc.ymd(1955, 11, 5).and_hms(7, 0, 0)),
));
assert!(!time_is_between(
Utc.ymd(1955, 11, 5).and_hms(6, 15, 0),
Some(Utc.ymd(1955, 11, 5).and_hms(7, 0, 0)),
None,
));
assert!(!time_is_between(
Utc.ymd(1955, 11, 5).and_hms(6, 15, 0),
None,
Some(Utc.ymd(1955, 11, 5).and_hms(5, 0, 0)),
));
assert!(!time_is_between(
Utc.ymd(1955, 11, 5).and_hms(6, 15, 0),
Some(Utc.ymd(1955, 11, 5).and_hms(5, 0, 0)),
Some(Utc.ymd(1955, 11, 5).and_hms(6, 0, 0)),
));
}
}
|