Skip to content

Commit 70bb5e7

Browse files
authored
Merge pull request #1119 from nebocco/migrate
change RPS Ranking API response
2 parents e2b5005 + faf62dd commit 70bb5e7

File tree

7 files changed

+94
-102
lines changed

7 files changed

+94
-102
lines changed

atcoder-problems-backend/src/server/ranking/language.rs

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,35 @@
11
use super::{
2-
LanguageRankingRequest, LanguageUserRankResponse, RankingRequestFormat, RankingResponse,
3-
RankingSelector, UserRankRequest, UserRankSelector,
2+
RankingRequestFormat, RankingResponse, RankingSelector, UserRankRequest,
3+
UserRankResponseFormat, UserRankSelector,
44
};
55

66
use actix_web::{error, web, Result};
77
use async_trait::async_trait;
8+
use serde::{Deserialize, Serialize};
89
use sql_client::{language_count::LanguageCountClient, PgPool};
10+
use std::ops::Range;
11+
12+
#[derive(Deserialize)]
13+
pub(crate) struct LanguageRankingRequest {
14+
from: usize,
15+
to: usize,
16+
language: String,
17+
}
18+
19+
impl RankingRequestFormat for LanguageRankingRequest {
20+
fn range(&self) -> Range<usize> {
21+
(self.from)..(self.to)
22+
}
23+
}
24+
25+
#[derive(Debug, Serialize)]
26+
pub(crate) struct LanguageUserRankResponse {
27+
language: String,
28+
count: i64,
29+
rank: i64,
30+
}
31+
32+
impl UserRankResponseFormat for LanguageUserRankResponse {}
933

1034
pub(crate) struct LanguageRanking;
1135

atcoder-problems-backend/src/server/ranking/mod.rs

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use actix_web::{web, HttpRequest, HttpResponse, Result};
22
use async_trait::async_trait;
33
use serde::{de::DeserializeOwned, Deserialize, Serialize};
4-
use sql_client::{models::UserSum, PgPool};
4+
use sql_client::PgPool;
55
use std::ops::Range;
66

77
pub(crate) mod ac_count;
@@ -70,7 +70,6 @@ pub(crate) trait UserRankSelector {
7070
}
7171
}
7272

73-
// ranking requests
7473
#[derive(Deserialize)]
7574
pub(crate) struct RankingRequest {
7675
from: usize,
@@ -83,20 +82,6 @@ impl RankingRequestFormat for RankingRequest {
8382
}
8483
}
8584

86-
#[derive(Deserialize)]
87-
pub(crate) struct LanguageRankingRequest {
88-
from: usize,
89-
to: usize,
90-
language: String,
91-
}
92-
93-
impl RankingRequestFormat for LanguageRankingRequest {
94-
fn range(&self) -> Range<usize> {
95-
(self.from)..(self.to)
96-
}
97-
}
98-
99-
// ranking responses
10085
#[derive(Serialize)]
10186
pub(crate) struct RankingResponse {
10287
user_id: String,
@@ -105,30 +90,17 @@ pub(crate) struct RankingResponse {
10590

10691
impl RankingResponseFormat for RankingResponse {}
10792

108-
impl RankingResponseFormat for UserSum {}
109-
110-
// user rank requests
11193
#[derive(Deserialize)]
11294
pub(crate) struct UserRankRequest {
11395
user: String,
11496
}
11597

11698
impl UserRankRequestFormat for UserRankRequest {}
11799

118-
// user rank responses
119100
#[derive(Serialize)]
120101
pub(crate) struct UserRankResponse {
121102
count: i64,
122103
rank: i64,
123104
}
124105

125106
impl UserRankResponseFormat for UserRankResponse {}
126-
127-
#[derive(Debug, Serialize)]
128-
pub(crate) struct LanguageUserRankResponse {
129-
language: String,
130-
count: i64,
131-
rank: i64,
132-
}
133-
134-
impl UserRankResponseFormat for LanguageUserRankResponse {}

atcoder-problems-backend/src/server/ranking/rated_point_sum.rs

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,44 @@
11
use super::{
2-
RankingRequest, RankingRequestFormat, RankingSelector, UserRankRequest, UserRankResponse,
3-
UserRankSelector, UserSum,
2+
RankingRequest, RankingRequestFormat, RankingResponseFormat, RankingSelector, UserRankRequest,
3+
UserRankResponse, UserRankSelector,
44
};
55

66
use actix_web::{error, web, Result};
77
use async_trait::async_trait;
8+
use serde::Serialize;
89
use sql_client::{rated_point_sum::RatedPointSumClient, PgPool};
910

11+
#[deprecated(
12+
note = "this special Response type is deprecated and will be replaced with super::RankingResponse"
13+
)]
14+
#[derive(Debug, Serialize)]
15+
pub(crate) struct RPSRankingResponse {
16+
user_id: String,
17+
count: i64,
18+
point_sum: i64,
19+
}
20+
21+
impl RankingResponseFormat for RPSRankingResponse {}
22+
1023
pub(crate) struct RatedPointSumRanking;
1124

1225
#[async_trait(?Send)]
1326
impl RankingSelector for RatedPointSumRanking {
1427
type Request = RankingRequest;
15-
type Response = UserSum;
28+
type Response = RPSRankingResponse;
1629
async fn fetch(pool: web::Data<PgPool>, query: Self::Request) -> Result<Vec<Self::Response>> {
1730
let ranking = pool
1831
.load_rated_point_sum_in_range(query.range())
1932
.await
2033
.map_err(error::ErrorInternalServerError)?;
21-
Ok(ranking)
34+
Ok(ranking
35+
.into_iter()
36+
.map(|entry| RPSRankingResponse {
37+
user_id: entry.user_id,
38+
count: entry.point_sum,
39+
point_sum: entry.point_sum,
40+
})
41+
.collect())
2242
}
2343
}
2444

atcoder-problems-backend/tests/test_server_e2e_rated_point_sum_ranking.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ async fn test_rated_point_sum_ranking() {
3434
assert_eq!(
3535
response,
3636
json!([
37-
{"user_id":"u2","point_sum":2},
38-
{"user_id":"u1","point_sum":1},
39-
{"user_id":"u3","point_sum":1}
37+
{"user_id":"u2","point_sum":2,"count":2},
38+
{"user_id":"u1","point_sum":1,"count":1},
39+
{"user_id":"u3","point_sum":1,"count":1}
4040
])
4141
);
4242

@@ -48,8 +48,8 @@ async fn test_rated_point_sum_ranking() {
4848
assert_eq!(
4949
response,
5050
json!([
51-
{"user_id":"u1","point_sum":1},
52-
{"user_id":"u3","point_sum":1}
51+
{"user_id":"u1","point_sum":1,"count":1},
52+
{"user_id":"u3","point_sum":1,"count":1}
5353
])
5454
);
5555

@@ -61,7 +61,7 @@ async fn test_rated_point_sum_ranking() {
6161
assert_eq!(
6262
response,
6363
json!([
64-
{"user_id":"u2","point_sum":2}
64+
{"user_id":"u2","point_sum":2,"count":2}
6565
])
6666
);
6767

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,44 @@
11
[
2-
{
3-
"id": "ukuku09",
4-
"start_epoch_second": 1530939600,
5-
"duration_second": 10800,
6-
"title": "ウクーニャたんお誕生日コンテスト",
2+
{
3+
"id": "ukuku09",
4+
"start_epoch_second": 1530939600,
5+
"duration_second": 10800,
6+
"title": "ウクーニャたんお誕生日コンテスト",
77
"rate_change": "-"
8-
},
9-
{
10-
"id": "summerfes2018-div1",
11-
"start_epoch_second": 1535175000,
12-
"duration_second": 7200,
13-
"title": "Summer Festival Contest 2018 (Division 1)",
8+
},
9+
{
10+
"id": "summerfes2018-div1",
11+
"start_epoch_second": 1535175000,
12+
"duration_second": 7200,
13+
"title": "Summer Festival Contest 2018 (Division 1)",
1414
"rate_change": "-"
15-
},
16-
{
17-
"id": "summerfes2018-div2",
18-
"start_epoch_second": 1535175000,
19-
"duration_second": 7200,
20-
"title": "Summer Festival Contest 2018 (Division 2)",
15+
},
16+
{
17+
"id": "summerfes2018-div2",
18+
"start_epoch_second": 1535175000,
19+
"duration_second": 7200,
20+
"title": "Summer Festival Contest 2018 (Division 2)",
2121
"rate_change": "-"
22-
},
23-
{
24-
"id": "monamieHB2021",
25-
"start_epoch_second": 1618920000,
26-
"duration_second": 7200,
27-
"title": "えびまのお誕生日コンテスト 2021 Day 1",
22+
},
23+
{
24+
"id": "monamieHB2021",
25+
"start_epoch_second": 1618920000,
26+
"duration_second": 7200,
27+
"title": "えびまのお誕生日コンテスト 2021 Day 1",
2828
"rate_change": "-"
29-
},
30-
{
31-
"id": "tkppc6-1",
32-
"start_epoch_second": 1629604800,
33-
"duration_second": 10800,
34-
"title": "技術室奥プログラミングコンテスト#6 Day1",
29+
},
30+
{
31+
"id": "tkppc6-1",
32+
"start_epoch_second": 1629604800,
33+
"duration_second": 10800,
34+
"title": "技術室奥プログラミングコンテスト#6 Day1",
3535
"rate_change": "-"
36-
},
37-
{
38-
"id": "genocon2021",
39-
"start_epoch_second": 1629720000,
40-
"duration_second": 2429940,
41-
"title": "ゲノコン2021 ー DNA配列解析チャレンジ",
36+
},
37+
{
38+
"id": "genocon2021",
39+
"start_epoch_second": 1629720000,
40+
"duration_second": 2429940,
41+
"title": "ゲノコン2021 ー DNA配列解析チャレンジ",
4242
"rate_change": "-"
4343
}
4444
]

atcoder-problems-frontend/src/api/APIClient.ts

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,7 @@ import { isContestParticipation } from "../interfaces/ContestParticipation";
44
import MergedProblem, { isMergedProblem } from "../interfaces/MergedProblem";
55
import Problem, { isProblem } from "../interfaces/Problem";
66
import ProblemModel, { isProblemModel } from "../interfaces/ProblemModel";
7-
import {
8-
isRankingEntry,
9-
isSumRankingEntry,
10-
RankingEntry,
11-
SumRankingEntry,
12-
} from "../interfaces/RankingEntry";
7+
import { isRankingEntry, RankingEntry } from "../interfaces/RankingEntry";
138
import { ContestId, ProblemId, UserId } from "../interfaces/Status";
149
import { isSubmission } from "../interfaces/Submission";
1510
import { isUserRankEntry, UserRankEntry } from "../interfaces/UserRankEntry";
@@ -99,18 +94,8 @@ export const useUserStreakRank = (user: string) => {
9994
};
10095

10196
export const useSumRanking = (from: number, to: number) => {
102-
const fetcher = async (url: string) => {
103-
const ranking = await fetchTypedArray<SumRankingEntry>(
104-
url,
105-
isSumRankingEntry
106-
);
107-
return ranking.map((entry) => ({
108-
count: entry.point_sum,
109-
user_id: entry.user_id,
110-
}));
111-
};
11297
const url = `${ATCODER_API_URL}/v3/rated_point_sum_ranking?from=${from}&to=${to}`;
113-
return useSWRData(url, fetcher);
98+
return useRankingV3(url);
11499
};
115100

116101
export const useUserSumRank = (user: string) => {

atcoder-problems-frontend/src/interfaces/RankingEntry.ts

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,6 @@ export const isRankingEntry = (obj: unknown): obj is RankingEntry =>
99
hasPropertyAsType(obj, "count", isNumber) &&
1010
hasPropertyAsType(obj, "user_id", isString);
1111

12-
export interface SumRankingEntry {
13-
readonly user_id: string;
14-
readonly point_sum: number;
15-
}
16-
17-
export const isSumRankingEntry = (obj: unknown): obj is SumRankingEntry =>
18-
hasPropertyAsType(obj, "user_id", isString) &&
19-
hasPropertyAsType(obj, "point_sum", isNumber);
20-
2112
export interface LangRankingEntry {
2213
user_id: string;
2314
count: number;

0 commit comments

Comments
 (0)