-
-
Notifications
You must be signed in to change notification settings - Fork 150
Expand file tree
/
Copy pathstreak.rs
More file actions
200 lines (181 loc) · 5.87 KB
/
streak.rs
File metadata and controls
200 lines (181 loc) · 5.87 KB
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
use crate::models::{Submission, UserStreak};
use crate::{PgPool, MAX_INSERT_ROWS};
use anyhow::Result;
use async_trait::async_trait;
use chrono::Duration;
use chrono::{DateTime, Datelike, FixedOffset, TimeZone, Utc};
use sqlx::postgres::PgRow;
use sqlx::Row;
use std::cmp;
use std::collections::BTreeMap;
use std::ops::Range;
#[async_trait]
pub trait StreakClient {
async fn load_streak_count_in_range(&self, rank_range: Range<usize>)
-> Result<Vec<UserStreak>>;
async fn get_users_streak_count(&self, user_id: &str) -> Option<i64>;
async fn get_streak_count_rank(&self, streak_count: i64) -> Result<i64>;
async fn update_streak_count(&self, submissions: &[Submission]) -> Result<()>;
}
#[async_trait]
impl StreakClient for PgPool {
async fn load_streak_count_in_range(
&self,
rank_range: Range<usize>,
) -> Result<Vec<UserStreak>> {
let users_streaks = sqlx::query_as(
r"
SELECT user_id, streak FROM max_streaks
ORDER BY streak DESC, user_id ASC
OFFSET $1 LIMIT $2;
",
)
.bind(rank_range.start as i32)
.bind(rank_range.len() as i32)
.fetch_all(self)
.await?;
Ok(users_streaks)
}
async fn get_users_streak_count(&self, user_id: &str) -> Option<i64> {
let count = sqlx::query(
r"
SELECT streak FROM max_streaks
WHERE LOWER(user_id) = LOWER($1)
",
)
.bind(user_id)
.try_map(|row: PgRow| row.try_get::<i64, _>("streak"))
.fetch_one(self)
.await
.ok()?;
Some(count)
}
async fn get_streak_count_rank(&self, streak_count: i64) -> Result<i64> {
let rank = sqlx::query(
r"
SELECT COUNT(*) AS rank
FROM max_streaks
WHERE streak > $1
",
)
.bind(streak_count)
.try_map(|row: PgRow| row.try_get::<i64, _>("rank"))
.fetch_one(self)
.await?;
Ok(rank)
}
async fn update_streak_count(&self, ac_submissions: &[Submission]) -> Result<()> {
let mut submissions = ac_submissions
.iter()
.map(|s| {
(
Utc.timestamp(s.epoch_second, 0),
s.user_id.as_str(),
s.problem_id.as_str(),
)
})
.collect::<Vec<_>>();
submissions.sort_by_key(|&(timestamp, _, _)| timestamp);
let first_ac_map = submissions.into_iter().fold(
BTreeMap::new(),
|mut map, (epoch_second, user_id, problem_id)| {
map.entry(user_id)
.or_insert_with(BTreeMap::new)
.entry(problem_id)
.or_insert(epoch_second);
map
},
);
let user_max_streak = first_ac_map
.into_iter()
.map(|(user_id, m)| {
let max_streak = get_max_streak(m.into_values().collect());
(user_id, max_streak)
})
.collect::<Vec<_>>();
for chunk in user_max_streak.chunks(MAX_INSERT_ROWS) {
let (user_ids, max_streaks): (Vec<&str>, Vec<i64>) = chunk.iter().copied().unzip();
sqlx::query(
r"
INSERT INTO max_streaks (user_id, streak)
VALUES (
UNNEST($1::VARCHAR(255)[]),
UNNEST($2::BIGINT[])
)
ON CONFLICT (user_id)
DO UPDATE SET streak = EXCLUDED.streak
",
)
.bind(user_ids)
.bind(max_streaks)
.execute(self)
.await?;
}
Ok(())
}
}
fn get_max_streak<Tz: TimeZone>(mut v: Vec<DateTime<Tz>>) -> i64 {
v.sort();
let (_, max_streak) = (1..v.len()).fold((1, 1), |(current_streak, max_streak), i| {
if v[i - 1].is_same_day_in_jst(&v[i]) {
(current_streak, max_streak)
} else if (v[i - 1].clone() + Duration::days(1)).is_same_day_in_jst(&v[i]) {
(current_streak + 1, cmp::max(max_streak, current_streak + 1))
} else {
(1, max_streak)
}
});
max_streak
}
trait AsJst {
fn as_jst(&self) -> DateTime<FixedOffset>;
fn is_same_day_in_jst<T: TimeZone>(&self, rhs: &DateTime<T>) -> bool {
let d1 = self.as_jst();
let d2 = rhs.as_jst();
d1.day() == d2.day() && d1.month() == d2.month() && d1.year() == d2.year()
}
}
impl<Tz> AsJst for DateTime<Tz>
where
Tz: TimeZone,
{
fn as_jst(&self) -> DateTime<FixedOffset> {
self.with_timezone(&FixedOffset::east(9 * 3600))
}
}
#[cfg(test)]
mod tests {
use super::*;
use chrono::Datelike;
#[test]
fn test_to_jst() {
let dt_10_04 = Utc.timestamp(1570114800, 0); //2019-10-04T00:00:00+09:00
assert_eq!(dt_10_04.as_jst().day(), 04);
let dt_10_03 = Utc.timestamp(1570114799, 0); //2019-10-03T23:59:59+09:00
assert_eq!(dt_10_03.as_jst().day(), 03);
let tomorrow = dt_10_03 + Duration::days(1);
assert!((tomorrow).is_same_day_in_jst(&dt_10_04));
}
#[test]
fn test_get_max_streak() {
let v = vec![
"2014-11-28T17:00:09+09:00",
"2014-11-28T18:00:09+09:00",
"2014-11-28T19:00:09+09:00",
"2014-11-28T20:00:09+09:00",
"2014-11-28T21:00:09+09:00",
"2014-11-28T22:00:09+09:00",
"2014-11-28T23:00:09+09:00",
"2014-11-28T23:59:59+09:00",
"2014-12-04T23:59:59+09:00",
"2014-12-02T23:59:59+09:00",
"2014-12-03T23:59:59+09:00",
"2014-12-01T23:59:59+09:00",
]
.into_iter()
.map(|s| s.parse::<DateTime<Utc>>().unwrap())
.collect::<Vec<_>>();
let streak = get_max_streak(v);
assert_eq!(streak, 4);
}
}