Skip to content

Commit 06e196d

Browse files
committed
Merge branch 'master' of github.com:mdcpp/mdoj
2 parents f50b3c8 + afd084f commit 06e196d

File tree

12 files changed

+46
-57
lines changed

12 files changed

+46
-57
lines changed

backend/migration/src/m20231207_000001_create_table.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ enum Submit {
104104
Status,
105105
Accept,
106106
Score,
107-
Public
107+
Public,
108108
}
109109
#[derive(Iden)]
110110
enum Testcase {

backend/src/controller/judger/route/mod.rs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,23 @@ use std::{
1414
use crossbeam_queue::SegQueue;
1515
use dashmap::{DashMap, DashSet};
1616
use tonic::{service::Interceptor, *};
17-
use tracing::{debug_span, instrument, Instrument};
17+
use tracing::{debug_span, instrument, Instrument, Span};
1818
use uuid::Uuid;
1919

20-
use crate::config::{self, Judger as JudgerConfig};
20+
use crate::config::{self, Judger as JudgeConfig};
2121
use grpc::judger::{judger_client::*, *};
2222

2323
// TODO: add tracing
2424

2525
// about health score:
2626
//
2727
// health score is a number in range [-1,HEALTH_MAX_SCORE)
28-
// Upstream with negitive health score is consider unhealthy, and should disconnect immediately
28+
// Upstream with negative health score is consider unhealthy, and should disconnect immediately
2929

3030
/// Max score a health Upstream can reach
3131
const HEALTH_MAX_SCORE: isize = 100;
3232

33-
/// Judger Client intercepted by BasicAuthInterceptor
33+
/// Judge Client intercepted by BasicAuthInterceptor
3434
type AuthJudgerClient = JudgerClient<
3535
service::interceptor::InterceptedService<transport::Channel, BasicAuthInterceptor>,
3636
>;
@@ -54,12 +54,12 @@ impl Interceptor for BasicAuthInterceptor {
5454
}
5555
}
5656

57-
#[derive(Clone)]
57+
#[derive(Clone, Debug)]
5858
/// Info necessary to create connection, implement reuse logic
5959
pub struct ConnectionDetail {
6060
pub uri: String,
6161
pub secret: Option<String>,
62-
// TODO: reuse logic shouldn't be binded with connection creation logic
62+
// TODO: reuse logic shouldn't be bound with connection creation logic
6363
pub reuse: bool,
6464
}
6565

@@ -128,9 +128,10 @@ impl Drop for ConnGuard {
128128
/// occupy future, should generally be spawn in a green thread
129129
#[instrument(skip(router), level = "info")]
130130
async fn discover<I: Routable + Send>(
131-
config: JudgerConfig,
131+
config: JudgeConfig,
132132
router: Weak<Router>,
133133
) -> Result<(), Error> {
134+
let parent = Span::current();
134135
let mut instance = I::new(config.clone())?;
135136
loop {
136137
match instance.discover().in_current_span().await {
@@ -141,7 +142,7 @@ async fn discover<I: Routable + Send>(
141142
};
142143
let uri = detail.uri.clone();
143144
let (upstream, langs) = Upstream::new(detail).in_current_span().await?;
144-
let _ = debug_span!("connected", uri = uri).entered();
145+
let _ = debug_span!(parent: parent.clone(), "connected", uri = uri).entered();
145146
for (uuid, lang) in langs.into_iter() {
146147
router.langs.insert(lang);
147148
loop {
@@ -175,8 +176,8 @@ pub struct Router {
175176

176177
impl Router {
177178
// skip because config contain basic auth secret
178-
#[instrument(name = "router_construct", level = "info", skip_all)]
179-
pub fn new(config: Vec<JudgerConfig>) -> Result<Arc<Self>, Error> {
179+
#[instrument(name = "router_construct", level = "debug", skip_all)]
180+
pub fn new(config: Vec<JudgeConfig>) -> Result<Arc<Self>, Error> {
180181
let self_ = Arc::new(Self {
181182
routing_table: DashMap::default(),
182183
langs: DashSet::default(),
@@ -235,6 +236,7 @@ pub struct Upstream {
235236

236237
impl Upstream {
237238
/// create new Upstream
239+
#[instrument(name = "connecting_upstream", err, level = "info")]
238240
async fn new(detail: ConnectionDetail) -> Result<(Arc<Self>, Vec<(Uuid, LangInfo)>), Error> {
239241
let mut client = detail.connect().await?;
240242
let info = client.judger_info(()).await?;
@@ -245,7 +247,7 @@ impl Upstream {
245247
let uuid = match Uuid::parse_str(&lang.lang_uid) {
246248
Ok(x) => x,
247249
Err(err) => {
248-
log::warn!("invalid lang_uid from judger: {}", err);
250+
log::warn!("invalid lang_uid from judge: {}", err);
249251
continue;
250252
}
251253
};
@@ -304,7 +306,7 @@ where
304306
// return new connection when available, will immediately retry true is returned
305307
async fn route(&mut self) -> Result<RouteStatus, Error>;
306308
/// create from config
307-
fn new(config: JudgerConfig) -> Result<Self, Error>;
309+
fn new(config: JudgeConfig) -> Result<Self, Error>;
308310
}
309311

310312
/// wrapper for Routable(Error handling)

backend/src/endpoint/announcement.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ impl<'a> From<WithAuth<'a, Model>> for AnnouncementFullInfo {
1717
id: model.id,
1818
title: model.title,
1919
update_date: into_prost(model.update_at),
20+
create_date: into_prost(model.create_at),
2021
},
2122
author_id: model.user_id,
2223
content: model.content,
@@ -34,6 +35,7 @@ impl From<Model> for AnnouncementInfo {
3435
id: value.id,
3536
title: value.title,
3637
update_date: into_prost(value.update_at),
38+
create_date: into_prost(value.create_at),
3739
}
3840
}
3941
}
@@ -44,6 +46,7 @@ impl From<PartialModel> for AnnouncementInfo {
4446
id: value.id,
4547
title: value.title,
4648
update_date: into_prost(value.update_at),
49+
create_date: into_prost(value.create_at),
4750
}
4851
}
4952
}

backend/src/endpoint/contest.rs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,6 @@ impl<'a> From<WithAuth<'a, Model>> for ContestFullInfo {
2424

2525
impl WithAuthTrait for Model {}
2626

27-
impl From<user_contest::Model> for UserRank {
28-
fn from(value: user_contest::Model) -> Self {
29-
UserRank {
30-
user_id: value.user_id,
31-
score: value.score,
32-
}
33-
}
34-
}
35-
3627
impl From<Model> for ContestInfo {
3728
fn from(value: Model) -> Self {
3829
ContestInfo {
@@ -41,6 +32,7 @@ impl From<Model> for ContestInfo {
4132
begin: value.begin.map(into_prost),
4233
end: value.end.map(into_prost),
4334
need_password: value.password.is_some(),
35+
public: value.public,
4436
}
4537
}
4638
}
@@ -53,6 +45,7 @@ impl From<PartialModel> for ContestInfo {
5345
begin: value.begin.map(into_prost),
5446
end: value.end.map(into_prost),
5547
need_password: value.password.is_some(),
48+
public: value.public,
5649
}
5750
}
5851
}

backend/src/endpoint/problem.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ impl<'a> From<WithAuth<'a, Model>> for ProblemFullInfo {
2121
submit_count: model.submit_count,
2222
ac_rate: model.ac_rate,
2323
difficulty: model.difficulty,
24+
create_at: into_prost(model.create_at),
25+
update_at: into_prost(model.update_at),
26+
public: model.public,
2427
},
2528
author: model.user_id,
2629
writable,
@@ -38,6 +41,9 @@ impl From<PartialModel> for ProblemInfo {
3841
submit_count: value.submit_count,
3942
ac_rate: value.ac_rate,
4043
difficulty: value.difficulty,
44+
create_at: into_prost(value.create_at),
45+
update_at: into_prost(value.update_at),
46+
public: value.public,
4147
}
4248
}
4349
}

backend/src/endpoint/submit.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,11 @@ use tokio_stream::wrappers::ReceiverStream;
1313

1414
impl From<Model> for SubmitInfo {
1515
fn from(value: Model) -> Self {
16-
// TODO: solve devation and uncommitted submit!
17-
let db_code: Code = value.status.unwrap().try_into().unwrap();
16+
let db_code: Code = value
17+
.status
18+
.map(|x| x.try_into().ok())
19+
.flatten()
20+
.unwrap_or(Code::Unknown);
1821
SubmitInfo {
1922
id: value.id,
2023
upload_time: into_prost(value.upload_at),

backend/src/endpoint/user.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,19 @@ impl<'a> From<WithAuth<'a, Model>> for UserFullInfo {
1919
}
2020
}
2121

22-
impl WithAuthTrait for Model {}
23-
2422
impl From<Model> for UserInfo {
2523
fn from(value: Model) -> Self {
26-
UserInfo {
24+
Self {
2725
username: value.username,
28-
// FIXME: capture Error(database corruption?) instead!
29-
score: value.score.try_into().unwrap_or_default(),
26+
score: value.score as u64,
3027
id: value.id,
28+
create_at: into_prost(value.create_at),
3129
}
3230
}
3331
}
3432

33+
impl WithAuthTrait for Model {}
34+
3535
#[async_trait]
3636
impl User for ArcServer {
3737
#[instrument(

backend/src/entity/announcement.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,6 @@ impl SortSource<PartialModel> for ColPagerTrait {
211211
match data.0 {
212212
Sort::UpdateDate => Column::UpdateAt,
213213
Sort::CreateDate => Column::CreateAt,
214-
Sort::Public => Column::Public,
215214
}
216215
}
217216
fn get_val(data: &Self::Data) -> impl Into<Value> + Clone + Send {
@@ -221,7 +220,6 @@ impl SortSource<PartialModel> for ColPagerTrait {
221220
data.1 = match data.0 {
222221
Sort::UpdateDate => model.update_at.to_string(),
223222
Sort::CreateDate => model.create_at.to_string(),
224-
Sort::Public => model.public.to_string(),
225223
}
226224
}
227225
}

backend/src/entity/contest.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -252,23 +252,17 @@ impl Source for ColPagerTrait {
252252
impl SortSource<PartialModel> for ColPagerTrait {
253253
fn sort_col(data: &Self::Data) -> impl ColumnTrait {
254254
match data.0 {
255-
Sort::UpdateDate => Column::UpdateAt,
256-
Sort::CreateDate => Column::CreateAt,
257255
Sort::Begin => Column::Begin,
258256
Sort::End => Column::End,
259-
Sort::Public => Column::Public,
260257
}
261258
}
262259
fn get_val(data: &Self::Data) -> impl Into<sea_orm::Value> + Clone + Send {
263260
&data.1
264261
}
265262
fn save_val(data: &mut Self::Data, model: &PartialModel) {
266263
data.1 = match data.0 {
267-
Sort::UpdateDate => model.update_at.to_string(),
268-
Sort::CreateDate => model.create_at.to_string(),
269264
Sort::Begin => model.begin.unwrap().to_string(),
270265
Sort::End => model.end.unwrap().to_string(),
271-
Sort::Public => model.public.to_string(),
272266
}
273267
}
274268
}
@@ -294,7 +288,7 @@ impl Paginator {
294288
))
295289
}
296290
pub fn new(start_from_end: bool) -> Self {
297-
Self::new_sort(Sort::CreateDate, start_from_end)
291+
Self::new_sort(Sort::Begin, start_from_end)
298292
}
299293
}
300294

backend/src/entity/problem.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,6 @@ impl SortSource<PartialModel> for ColPagerTrait {
403403
Sort::SubmitCount => Column::SubmitCount,
404404
Sort::Difficulty => Column::Difficulty,
405405
Sort::Order => Column::Order,
406-
Sort::Public => Column::Public,
407406
}
408407
}
409408
fn get_val(data: &Self::Data) -> impl Into<sea_orm::Value> + Clone + Send {
@@ -417,7 +416,6 @@ impl SortSource<PartialModel> for ColPagerTrait {
417416
Sort::SubmitCount => model.submit_count.to_string(),
418417
Sort::Difficulty => model.difficulty.to_string(),
419418
Sort::Order => model.order.to_string(),
420-
Sort::Public => model.public.to_string(),
421419
}
422420
}
423421
}

backend/src/util/code.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use grpc::backend::StateCode as BackendCode;
22
use grpc::judger::JudgerCode;
33

4-
/// Stablized JudgeResponse Code, store in database
4+
/// Stabilized JudgeResponse Code, store in database
55
#[derive(Clone, Copy, PartialEq, Eq)]
66
#[repr(C)]
77
pub enum Code {

grpc/proto/backend.proto

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,9 @@ message ProblemInfo {
147147
// 3000 - inf geek
148148
required uint32 difficulty = 5;
149149
required float ac_rate = 4;
150+
required google.protobuf.Timestamp update_at = 6;
151+
required google.protobuf.Timestamp create_at = 7;
152+
required bool public = 8;
150153
}
151154

152155
message ProblemFullInfo {
@@ -177,7 +180,7 @@ message ListProblemRequest {
177180
SORT_SUBMIT_COUNT = 3;
178181
SORT_DIFFICULTY = 4;
179182
SORT_ORDER = 5;
180-
SORT_PUBLIC = 6;
183+
// 6 is used
181184
}
182185
message Query {
183186
optional int32 contest_id = 1;
@@ -299,6 +302,7 @@ message AnnouncementInfo {
299302
required int32 id = 1;
300303
required string title = 2;
301304
required google.protobuf.Timestamp update_date = 3;
305+
required google.protobuf.Timestamp create_date = 4;
302306
}
303307

304308
message AnnouncementFullInfo {
@@ -358,7 +362,6 @@ message ListAnnouncementRequest {
358362
enum Sort {
359363
SORT_UPDATE_DATE = 0;
360364
SORT_CREATE_DATE = 1;
361-
SORT_PUBLIC = 2;
362365
}
363366
message Query {
364367
optional Sort sort_by = 1;
@@ -591,6 +594,7 @@ message ContestInfo {
591594
optional google.protobuf.Timestamp begin = 4;
592595
optional google.protobuf.Timestamp end = 5;
593596
required bool need_password = 6;
597+
required bool public = 7;
594598
}
595599

596600
message ContestFullInfo {
@@ -645,17 +649,6 @@ message UpdateContestRequest {
645649
optional string request_id = 3;
646650
}
647651

648-
message UserRank {
649-
required int32 user_id = 1;
650-
required uint32 score = 2;
651-
}
652-
653-
message ListRankResponse {
654-
repeated UserRank list = 1;
655-
required string paginator = 2;
656-
required uint64 remain = 3;
657-
}
658-
659652
message JoinContestRequest {
660653
required int32 id = 1;
661654
optional string password = 2;
@@ -667,11 +660,9 @@ message JoinContestRequest {
667660

668661
message ListContestRequest {
669662
enum Sort {
670-
SORT_CREATE_DATE = 0;
671-
SORT_UPDATE_DATE = 1;
672663
SORT_BEGIN = 2;
673664
SORT_END = 3;
674-
SORT_PUBLIC = 4;
665+
// leave 4, it's used
675666
}
676667
message Query {
677668
optional Sort sort_by = 1;
@@ -720,6 +711,7 @@ message UserInfo {
720711
required string username = 1;
721712
required uint64 score = 4;
722713
required int32 id = 3;
714+
required google.protobuf.Timestamp create_at = 5;
723715
}
724716

725717
message UserFullInfo {

0 commit comments

Comments
 (0)