-
Notifications
You must be signed in to change notification settings - Fork 83
/
main.rs
1237 lines (1106 loc) · 40.4 KB
/
main.rs
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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
use actix_web::{web, HttpResponse};
use chrono::DurationRound as _;
use chrono::Offset as _;
use chrono::TimeZone as _;
use chrono::{DateTime, NaiveDateTime};
use futures::StreamExt as _;
use futures::TryStreamExt as _;
use std::collections::{HashMap, HashSet};
const SESSION_NAME: &str = "isucondition_rust";
const CONDITION_LIMIT: usize = 20;
const FRONTEND_CONTENTS_PATH: &str = "../public";
const JIA_JWT_SIGNING_KEY_PATH: &str = "../ec256-public.pem";
const DEFAULT_ICON_FILE_PATH: &str = "../NoImage.jpg";
const DEFAULT_JIA_SERVICE_URL: &str = "http://localhost:5000";
const MYSQL_ERR_NUM_DUPLICATE_ENTRY: u16 = 1062;
const CONDITION_LEVEL_INFO: &str = "info";
const CONDITION_LEVEL_WARNING: &str = "warning";
const CONDITION_LEVEL_CRITICAL: &str = "critical";
const SCORE_CONDITION_LEVEL_INFO: i64 = 3;
const SCORE_CONDITION_LEVEL_WARNING: i64 = 2;
const SCORE_CONDITION_LEVEL_CRITICAL: i64 = 1;
lazy_static::lazy_static! {
static ref JIA_JWT_SIGNING_KEY_PEM: Vec<u8> = std::fs::read(JIA_JWT_SIGNING_KEY_PATH).expect("failed to read JIA JWT signing key file");
static ref JIA_JWT_SIGNING_KEY: jsonwebtoken::DecodingKey<'static> = jsonwebtoken::DecodingKey::from_ec_pem(&JIA_JWT_SIGNING_KEY_PEM).expect("failed to parse JIA JWT signing key");
// JIAへのactivate時に登録する,ISUがconditionを送る先のURL
static ref POST_ISUCONDITION_TARGET_BASE_URL: String = std::env::var("POST_ISUCONDITION_TARGET_BASE_URL").expect("missing: POST_ISUCONDITION_TARGET_BASE_URL");
static ref JST_OFFSET: chrono::FixedOffset = chrono::FixedOffset::east(9 * 60 * 60);
}
#[derive(Debug, sqlx::FromRow)]
struct Config {
name: String,
url: String,
}
#[derive(Debug, serde::Serialize)]
struct Isu {
id: i64,
jia_isu_uuid: String,
name: String,
#[serde(skip)]
image: Vec<u8>,
character: String,
#[serde(skip)]
jia_user_id: String,
#[serde(skip)]
created_at: DateTime<chrono::FixedOffset>,
#[serde(skip)]
updated_at: DateTime<chrono::FixedOffset>,
}
impl sqlx::FromRow<'_, sqlx::mysql::MySqlRow> for Isu {
fn from_row(row: &sqlx::mysql::MySqlRow) -> sqlx::Result<Self> {
use sqlx::Row as _;
let created_at: NaiveDateTime = row.try_get("created_at")?;
let updated_at: NaiveDateTime = row.try_get("updated_at")?;
// DB の datetime 型は JST として解釈する
let created_at = JST_OFFSET.from_local_datetime(&created_at).unwrap();
let updated_at = JST_OFFSET.from_local_datetime(&updated_at).unwrap();
Ok(Self {
id: row.try_get("id")?,
jia_isu_uuid: row.try_get("jia_isu_uuid")?,
name: row.try_get("name")?,
image: row.try_get("image")?,
character: row.try_get("character")?,
jia_user_id: row.try_get("jia_user_id")?,
created_at,
updated_at,
})
}
}
#[derive(Debug, serde::Deserialize)]
struct IsuFromJIA {
character: String,
}
#[derive(Debug, serde::Serialize)]
struct GetIsuListResponse {
id: i64,
jia_isu_uuid: String,
name: String,
character: String,
latest_isu_condition: Option<GetIsuConditionResponse>,
}
#[derive(Debug)]
struct IsuCondition {
id: i64,
jia_isu_uuid: String,
timestamp: DateTime<chrono::FixedOffset>,
is_sitting: bool,
condition: String,
message: String,
created_at: DateTime<chrono::FixedOffset>,
}
impl sqlx::FromRow<'_, sqlx::mysql::MySqlRow> for IsuCondition {
fn from_row(row: &sqlx::mysql::MySqlRow) -> sqlx::Result<Self> {
use sqlx::Row as _;
let timestamp: NaiveDateTime = row.try_get("timestamp")?;
let created_at: NaiveDateTime = row.try_get("created_at")?;
// DB の datetime 型は JST として解釈する
let timestamp = JST_OFFSET.from_local_datetime(×tamp).unwrap();
let created_at = JST_OFFSET.from_local_datetime(&created_at).unwrap();
Ok(Self {
id: row.try_get("id")?,
jia_isu_uuid: row.try_get("jia_isu_uuid")?,
timestamp,
is_sitting: row.try_get("is_sitting")?,
condition: row.try_get("condition")?,
message: row.try_get("message")?,
created_at,
})
}
}
#[derive(Debug)]
struct MySQLConnectionEnv {
host: String,
port: u16,
user: String,
db_name: String,
password: String,
}
impl Default for MySQLConnectionEnv {
fn default() -> Self {
let port = if let Ok(port) = std::env::var("MYSQL_PORT") {
port.parse().unwrap_or(3306)
} else {
3306
};
Self {
host: std::env::var("MYSQL_HOST").unwrap_or_else(|_| "127.0.0.1".to_owned()),
port,
user: std::env::var("MYSQL_USER").unwrap_or_else(|_| "isucon".to_owned()),
db_name: std::env::var("MYSQL_DBNAME").unwrap_or_else(|_| "isucondition".to_owned()),
password: std::env::var("MYSQL_PASS").unwrap_or_else(|_| "isucon".to_owned()),
}
}
}
#[derive(Debug, serde::Deserialize)]
struct InitializeRequest {
jia_service_url: String,
}
#[derive(Debug, serde::Serialize)]
struct InitializeResponse {
language: String,
}
#[derive(Debug, serde::Serialize)]
struct GetMeResponse {
jia_user_id: String,
}
#[derive(Debug, serde::Serialize)]
struct GraphResponse {
start_at: i64,
end_at: i64,
data: Option<GraphDataPoint>,
condition_timestamps: Vec<i64>,
}
#[derive(Debug, serde::Serialize)]
struct GraphDataPoint {
score: i64,
percentage: ConditionsPercentage,
}
#[derive(Debug, serde::Serialize)]
struct ConditionsPercentage {
sitting: i64,
is_broken: i64,
is_dirty: i64,
is_overweight: i64,
}
#[derive(Debug)]
struct GraphDataPointWithInfo {
jia_isu_uuid: String,
start_at: DateTime<chrono::FixedOffset>,
data: GraphDataPoint,
condition_timestamps: Vec<i64>,
}
#[derive(Debug, serde::Serialize)]
struct GetIsuConditionResponse {
jia_isu_uuid: String,
isu_name: String,
timestamp: i64,
is_sitting: bool,
condition: String,
condition_level: &'static str,
message: String,
}
#[derive(Debug, serde::Serialize)]
struct TrendResponse {
character: String,
info: Vec<TrendCondition>,
warning: Vec<TrendCondition>,
critical: Vec<TrendCondition>,
}
#[derive(Debug, serde::Serialize)]
struct TrendCondition {
#[serde(rename = "isu_id")]
id: i64,
timestamp: i64,
}
#[derive(Debug, serde::Deserialize)]
struct PostIsuConditionRequest {
is_sitting: bool,
condition: String,
message: String,
timestamp: i64,
}
#[derive(Debug, serde::Serialize)]
struct JIAServiceRequest<'a> {
target_base_url: &'a str,
isu_uuid: &'a str,
}
#[actix_web::main]
async fn main() -> std::io::Result<()> {
env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("info,sqlx=warn"))
.init();
let mysql_connection_env = MySQLConnectionEnv::default();
let pool = sqlx::mysql::MySqlPoolOptions::new()
.max_connections(10)
.after_connect(|conn| {
Box::pin(async move {
use sqlx::Executor as _;
// DB のタイムゾーンを JST に強制する
conn.execute("set time_zone = '+09:00'").await?;
Ok(())
})
})
.connect_with(
sqlx::mysql::MySqlConnectOptions::new()
.host(&mysql_connection_env.host)
.port(mysql_connection_env.port)
.database(&mysql_connection_env.db_name)
.username(&mysql_connection_env.user)
.password(&mysql_connection_env.password),
)
.await
.expect("failed to connect db");
let mut session_key = std::env::var("SESSION_KEY")
.map(|k| k.into_bytes())
.unwrap_or_else(|_| b"isucondition".to_vec());
if session_key.len() < 32 {
session_key.resize(32, 0);
}
let server = actix_web::HttpServer::new(move || {
actix_web::App::new()
.app_data(web::JsonConfig::default().error_handler(|err, _| {
if matches!(err, actix_web::error::JsonPayloadError::Deserialize(_)) {
actix_web::error::ErrorBadRequest("bad request body")
} else {
actix_web::error::ErrorBadRequest(err)
}
}))
.app_data(web::Data::new(pool.clone()))
.wrap(actix_web::middleware::Logger::default())
.wrap(
actix_session::CookieSession::signed(&session_key)
.secure(false)
.name(SESSION_NAME)
.max_age(2592000),
)
.service(post_initialize)
.service(post_authentication)
.service(post_signout)
.service(get_me)
.service(get_isu_list)
.service(post_isu)
.service(get_isu_id)
.service(get_isu_icon)
.service(get_isu_graph)
.service(get_isu_conditions)
.service(get_trend)
.service(post_isu_condition)
.route("/", web::get().to(get_index))
.route("/isu/{jia_isu_uuid}", web::get().to(get_index))
.route("/isu/{jia_isu_uuid}/condition", web::get().to(get_index))
.route("/isu/{jia_isu_uuid}/graph", web::get().to(get_index))
.route("/register", web::get().to(get_index))
.service(actix_files::Files::new(
"/assets",
std::path::Path::new(FRONTEND_CONTENTS_PATH).join("assets"),
))
});
let server = if let Some(l) = listenfd::ListenFd::from_env().take_tcp_listener(0)? {
server.listen(l)?
} else {
server.bind((
"0.0.0.0",
std::env::var("SERVER_APP_PORT")
.map(|port_str| port_str.parse().expect("Failed to parse SERVER_APP_PORT"))
.unwrap_or(3000),
))?
};
server.run().await
}
#[derive(Debug)]
struct SqlxError(sqlx::Error);
impl std::fmt::Display for SqlxError {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
self.0.fmt(f)
}
}
impl actix_web::ResponseError for SqlxError {
fn error_response(&self) -> HttpResponse {
log::error!("db error: {}", self.0);
HttpResponse::InternalServerError()
.content_type(mime::TEXT_PLAIN)
.body(format!("SQLx error: {:?}", self.0))
}
}
#[derive(Debug)]
struct ReqwestError(reqwest::Error);
impl std::fmt::Display for ReqwestError {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
self.0.fmt(f)
}
}
impl actix_web::ResponseError for ReqwestError {
fn error_response(&self) -> HttpResponse {
HttpResponse::InternalServerError()
.content_type(mime::TEXT_PLAIN)
.body(format!("reqwest error: {:?}", self.0))
}
}
async fn require_signed_in<'e, 'c, E>(
executor: E,
session: actix_session::Session,
) -> actix_web::Result<String>
where
'c: 'e,
E: 'e + sqlx::Executor<'c, Database = sqlx::MySql>,
{
if let Some(jia_user_id) = session.get("jia_user_id")? {
let count: i64 = sqlx::query_scalar("SELECT COUNT(*) FROM `user` WHERE `jia_user_id` = ?")
.bind(&jia_user_id)
.fetch_one(executor)
.await
.map_err(SqlxError)?;
if count == 0 {
Err(actix_web::error::ErrorUnauthorized("not found: user"))
} else {
Ok(jia_user_id)
}
} else {
Err(actix_web::error::ErrorUnauthorized("you are not signed in"))
}
}
async fn get_jia_service_url<'e, 'c, E>(executor: E) -> sqlx::Result<String>
where
'c: 'e,
E: 'e + sqlx::Executor<'c, Database = sqlx::MySql>,
{
let config: Option<Config> =
sqlx::query_as("SELECT * FROM `isu_association_config` WHERE `name` = ?")
.bind("jia_service_url")
.fetch_optional(executor)
.await?;
Ok(config
.map(|c| c.url)
.unwrap_or_else(|| DEFAULT_JIA_SERVICE_URL.to_owned()))
}
// サービスを初期化
#[actix_web::post("/initialize")]
async fn post_initialize(
pool: web::Data<sqlx::MySqlPool>,
request: web::Json<InitializeRequest>,
) -> actix_web::Result<HttpResponse> {
let status = tokio::process::Command::new("../sql/init.sh")
.status()
.await
.map_err(|e| {
log::error!("exec init.sh error: {}", e);
e
})?;
if !status.success() {
log::error!("exec init.sh failed with exit code {:?}", status.code());
return Err(actix_web::error::ErrorInternalServerError(""));
}
sqlx::query(
"INSERT INTO `isu_association_config` (`name`, `url`) VALUES (?, ?) ON DUPLICATE KEY UPDATE `url` = VALUES(`url`)",
)
.bind("jia_service_url")
.bind(&request.jia_service_url)
.execute(pool.as_ref())
.await
.map_err(SqlxError)?;
Ok(HttpResponse::Ok().json(InitializeResponse {
language: "rust".to_owned(),
}))
}
#[derive(Debug, serde::Deserialize)]
struct Claims {
jia_user_id: String,
}
// サインアップ・サインイン
#[actix_web::post("/api/auth")]
async fn post_authentication(
pool: web::Data<sqlx::MySqlPool>,
request: actix_web::HttpRequest,
session: actix_session::Session,
) -> actix_web::Result<HttpResponse> {
let req_jwt = request
.headers()
.get("Authorization")
.map(|value| value.to_str().unwrap_or_default())
.unwrap_or_default()
.trim_start_matches("Bearer ");
let validation = jsonwebtoken::Validation::new(jsonwebtoken::Algorithm::ES256);
let token = match jsonwebtoken::decode(req_jwt, &JIA_JWT_SIGNING_KEY, &validation) {
Ok(token) => token,
Err(e) => {
if matches!(e.kind(), jsonwebtoken::errors::ErrorKind::Json(_)) {
return Err(actix_web::error::ErrorBadRequest("invalid JWT payload"));
} else {
return Err(actix_web::error::ErrorForbidden("forbidden"));
}
}
};
let claims: Claims = token.claims;
let jia_user_id = claims.jia_user_id;
sqlx::query("INSERT IGNORE INTO user (`jia_user_id`) VALUES (?)")
.bind(&jia_user_id)
.execute(pool.as_ref())
.await
.map_err(SqlxError)?;
session.insert("jia_user_id", jia_user_id).map_err(|e| {
log::error!("failed to set cookie: {}", e);
e
})?;
Ok(HttpResponse::Ok().finish())
}
// サインアウト
#[actix_web::post("/api/signout")]
async fn post_signout(session: actix_session::Session) -> actix_web::Result<HttpResponse> {
if session.remove("jia_user_id").is_some() {
Ok(HttpResponse::Ok().finish())
} else {
Err(actix_web::error::ErrorUnauthorized("you are not signed in"))
}
}
// サインインしている自分自身の情報を取得
#[actix_web::get("/api/user/me")]
async fn get_me(
pool: web::Data<sqlx::MySqlPool>,
session: actix_session::Session,
) -> actix_web::Result<HttpResponse> {
let jia_user_id = require_signed_in(pool.as_ref(), session).await?;
Ok(HttpResponse::Ok().json(GetMeResponse { jia_user_id }))
}
// ISUの一覧を取得
#[actix_web::get("/api/isu")]
async fn get_isu_list(
pool: web::Data<sqlx::MySqlPool>,
session: actix_session::Session,
) -> actix_web::Result<HttpResponse> {
let jia_user_id = require_signed_in(pool.as_ref(), session).await?;
let mut tx = pool.begin().await.map_err(SqlxError)?;
let isu_list: Vec<Isu> =
sqlx::query_as("SELECT * FROM `isu` WHERE `jia_user_id` = ? ORDER BY `id` DESC")
.bind(&jia_user_id)
.fetch_all(&mut tx)
.await
.map_err(SqlxError)?;
let mut response_list = Vec::new();
for isu in isu_list {
let last_condition: Option<IsuCondition> = sqlx::query_as(
"SELECT * FROM `isu_condition` WHERE `jia_isu_uuid` = ? ORDER BY `timestamp` DESC LIMIT 1"
)
.bind(&isu.jia_isu_uuid)
.fetch_optional(&mut tx)
.await
.map_err(SqlxError)?;
let formatted_condition = if let Some(last_condition) = last_condition {
let condition_level = calculate_condition_level(&last_condition.condition);
if condition_level.is_none() {
log::error!("unexpected warn count");
return Err(actix_web::error::ErrorInternalServerError(""));
}
let condition_level = condition_level.unwrap();
Some(GetIsuConditionResponse {
jia_isu_uuid: last_condition.jia_isu_uuid,
isu_name: isu.name.clone(),
timestamp: last_condition.timestamp.timestamp(),
is_sitting: last_condition.is_sitting,
condition: last_condition.condition,
condition_level,
message: last_condition.message,
})
} else {
None
};
response_list.push(GetIsuListResponse {
id: isu.id,
jia_isu_uuid: isu.jia_isu_uuid,
name: isu.name,
character: isu.character,
latest_isu_condition: formatted_condition,
});
}
tx.commit().await.map_err(SqlxError)?;
Ok(HttpResponse::Ok().json(response_list))
}
// ISUを登録
#[actix_web::post("/api/isu")]
async fn post_isu(
pool: web::Data<sqlx::MySqlPool>,
session: actix_session::Session,
mut payload: actix_multipart::Multipart,
) -> actix_web::Result<HttpResponse> {
let jia_user_id = require_signed_in(pool.as_ref(), session).await?;
let mut jia_isu_uuid = None;
let mut isu_name = None;
let mut image = None;
while let Some(field) = payload.next().await {
let field = field.map_err(|_| actix_web::error::ErrorBadRequest("bad format: icon"))?;
let content_disposition = field.content_disposition().unwrap();
let content = field
.map_ok(|chunk| bytes::BytesMut::from(&chunk[..]))
.try_concat()
.await
.map_err(|_| actix_web::error::ErrorBadRequest("bad format: icon"))?
.freeze();
match content_disposition.get_name().unwrap() {
"jia_isu_uuid" => {
jia_isu_uuid = Some(String::from_utf8_lossy(&content).into_owned());
}
"isu_name" => {
isu_name = Some(String::from_utf8_lossy(&content).into_owned());
}
"image" => {
image = Some(content);
}
_ => {}
}
}
let jia_isu_uuid: String = jia_isu_uuid.unwrap_or_default();
let isu_name: String = isu_name.unwrap_or_default();
let image = match image {
Some(image) => image,
None => {
let content = tokio::fs::read(DEFAULT_ICON_FILE_PATH).await.map_err(|e| {
log::error!("{}", e);
e
})?;
bytes::Bytes::from(content)
}
};
let mut tx = pool.begin().await.map_err(SqlxError)?;
let result = sqlx::query(
"INSERT INTO `isu` (`jia_isu_uuid`, `name`, `image`, `jia_user_id`) VALUES (?, ?, ?, ?)",
)
.bind(&jia_isu_uuid)
.bind(&isu_name)
.bind(image.as_ref())
.bind(&jia_user_id)
.execute(&mut tx)
.await;
if let Err(sqlx::Error::Database(ref db_error)) = result {
if let Some(mysql_error) = db_error.try_downcast_ref::<sqlx::mysql::MySqlDatabaseError>() {
if mysql_error.number() == MYSQL_ERR_NUM_DUPLICATE_ENTRY {
return Err(actix_web::error::ErrorConflict("duplicated: isu"));
}
}
}
result.map_err(SqlxError)?;
let target_url = format!(
"{}/api/activate",
get_jia_service_url(&mut tx).await.map_err(SqlxError)?
);
let body = JIAServiceRequest {
target_base_url: &POST_ISUCONDITION_TARGET_BASE_URL,
isu_uuid: &jia_isu_uuid,
};
let resp = reqwest::Client::new()
.post(&target_url)
.json(&body)
.send()
.await
.map_err(|e| {
log::error!("failed to request to JIAService: {}", e);
ReqwestError(e)
})?;
let status = resp.status();
if status != reqwest::StatusCode::ACCEPTED {
let body = resp.text().await.map_err(|e| {
log::error!("{}", e);
ReqwestError(e)
})?;
log::error!(
"JIAService returned error: status code {}, message: {}",
status,
body
);
return Err(
actix_web::error::InternalError::new("JIAService returned error", status).into(),
);
}
let isu_from_jia: IsuFromJIA = resp.json().await.map_err(|e| {
log::error!("error occured while reading JIA response: {}", e);
ReqwestError(e)
})?;
sqlx::query("UPDATE `isu` SET `character` = ? WHERE `jia_isu_uuid` = ?")
.bind(&isu_from_jia.character)
.bind(&jia_isu_uuid)
.execute(&mut tx)
.await
.map_err(SqlxError)?;
let isu: Isu =
sqlx::query_as("SELECT * FROM `isu` WHERE `jia_user_id` = ? AND `jia_isu_uuid` = ?")
.bind(&jia_user_id)
.bind(&jia_isu_uuid)
.fetch_one(&mut tx)
.await
.map_err(SqlxError)?;
tx.commit().await.map_err(SqlxError)?;
Ok(HttpResponse::Created().json(isu))
}
// ISUの情報を取得
#[actix_web::get("/api/isu/{jia_isu_uuid}")]
async fn get_isu_id(
pool: web::Data<sqlx::MySqlPool>,
session: actix_session::Session,
jia_isu_uuid: web::Path<String>,
) -> actix_web::Result<HttpResponse> {
let jia_user_id = require_signed_in(pool.as_ref(), session).await?;
let isu: Option<Isu> =
sqlx::query_as("SELECT * FROM `isu` WHERE `jia_user_id` = ? AND `jia_isu_uuid` = ?")
.bind(&jia_user_id)
.bind(jia_isu_uuid.as_ref())
.fetch_optional(pool.as_ref())
.await
.map_err(SqlxError)?;
if isu.is_none() {
return Err(actix_web::error::ErrorNotFound("not found: isu"));
}
let isu = isu.unwrap();
Ok(HttpResponse::Ok().json(isu))
}
// ISUのアイコンを取得
#[actix_web::get("/api/isu/{jia_isu_uuid}/icon")]
async fn get_isu_icon(
pool: web::Data<sqlx::MySqlPool>,
session: actix_session::Session,
jia_isu_uuid: web::Path<String>,
) -> actix_web::Result<HttpResponse> {
let jia_user_id = require_signed_in(pool.as_ref(), session).await?;
let image: Option<Vec<u8>> = sqlx::query_scalar(
"SELECT `image` FROM `isu` WHERE `jia_user_id` = ? AND `jia_isu_uuid` = ?",
)
.bind(&jia_user_id)
.bind(jia_isu_uuid.as_ref())
.fetch_optional(pool.as_ref())
.await
.map_err(SqlxError)?;
if let Some(image) = image {
Ok(HttpResponse::Ok().body(image))
} else {
Err(actix_web::error::ErrorNotFound("not found: isu"))
}
}
#[derive(Debug, serde::Deserialize)]
struct GetIsuGraphQuery {
datetime: Option<String>,
}
// ISUのコンディショングラフ描画のための情報を取得
#[actix_web::get("/api/isu/{jia_isu_uuid}/graph")]
async fn get_isu_graph(
pool: web::Data<sqlx::MySqlPool>,
session: actix_session::Session,
jia_isu_uuid: web::Path<String>,
query: web::Query<GetIsuGraphQuery>,
) -> actix_web::Result<HttpResponse> {
let jia_user_id = require_signed_in(pool.as_ref(), session).await?;
let date = match &query.datetime {
Some(datetime_str) => match datetime_str.parse() {
Ok(datetime) => {
DateTime::from_utc(NaiveDateTime::from_timestamp(datetime, 0), JST_OFFSET.fix())
.duration_trunc(chrono::Duration::hours(1))
.unwrap()
}
Err(_) => {
return Err(actix_web::error::ErrorBadRequest("bad format: datetime"));
}
},
None => {
return Err(actix_web::error::ErrorBadRequest("missing: datetime"));
}
};
let mut tx = pool.begin().await.map_err(SqlxError)?;
let count: i64 = sqlx::query_scalar(
"SELECT COUNT(*) FROM `isu` WHERE `jia_user_id` = ? AND `jia_isu_uuid` = ?",
)
.bind(&jia_user_id)
.bind(jia_isu_uuid.as_ref())
.fetch_one(&mut tx)
.await
.map_err(SqlxError)?;
if count == 0 {
return Err(actix_web::error::ErrorNotFound("not found: isu"));
}
let res = generate_isu_graph_response(&mut tx, &jia_isu_uuid, date).await?;
tx.commit().await.map_err(SqlxError)?;
Ok(HttpResponse::Ok().json(res))
}
// グラフのデータ点を一日分生成
async fn generate_isu_graph_response(
tx: &mut sqlx::Transaction<'_, sqlx::MySql>,
jia_isu_uuid: &str,
graph_date: DateTime<chrono::FixedOffset>,
) -> actix_web::Result<Vec<GraphResponse>> {
let mut data_points = Vec::new();
let mut conditions_in_this_hour = Vec::new();
let mut timestamps_in_this_hour = Vec::new();
let mut start_time_in_this_hour =
DateTime::from_utc(NaiveDateTime::from_timestamp(0, 0), JST_OFFSET.fix());
let mut rows = sqlx::query_as(
"SELECT * FROM `isu_condition` WHERE `jia_isu_uuid` = ? ORDER BY `timestamp` ASC",
)
.bind(jia_isu_uuid)
.fetch(tx);
while let Some(row) = rows.next().await {
let condition: IsuCondition = row.map_err(SqlxError)?;
let truncated_condition_time = condition
.timestamp
.duration_trunc(chrono::Duration::hours(1))
.unwrap();
if truncated_condition_time != start_time_in_this_hour {
if !conditions_in_this_hour.is_empty() {
let data = calculate_graph_data_point(&conditions_in_this_hour)?;
data_points.push(GraphDataPointWithInfo {
jia_isu_uuid: jia_isu_uuid.to_owned(),
start_at: start_time_in_this_hour,
data,
condition_timestamps: timestamps_in_this_hour,
});
}
start_time_in_this_hour = truncated_condition_time;
conditions_in_this_hour = Vec::new();
timestamps_in_this_hour = Vec::new();
}
timestamps_in_this_hour.push(condition.timestamp.timestamp());
conditions_in_this_hour.push(condition);
}
if !conditions_in_this_hour.is_empty() {
let data = calculate_graph_data_point(&conditions_in_this_hour)?;
data_points.push(GraphDataPointWithInfo {
jia_isu_uuid: jia_isu_uuid.to_owned(),
start_at: start_time_in_this_hour,
data,
condition_timestamps: timestamps_in_this_hour,
});
}
let end_time = graph_date + chrono::Duration::hours(24);
let mut filtered_data_points = data_points
.into_iter()
.skip_while(|graph| graph.start_at < graph_date)
.take_while(|graph| graph.start_at < end_time)
.peekable();
let mut response_list = Vec::new();
let mut this_time = graph_date;
while this_time < end_time {
let (data, timestamps) = filtered_data_points
.next_if(|data_with_info| data_with_info.start_at == this_time)
.map(|data_with_info| {
(
Some(data_with_info.data),
data_with_info.condition_timestamps,
)
})
.unwrap_or_else(|| (None, Vec::new()));
let resp = GraphResponse {
start_at: this_time.timestamp(),
end_at: (this_time + chrono::Duration::hours(1)).timestamp(),
data,
condition_timestamps: timestamps,
};
response_list.push(resp);
this_time = this_time + chrono::Duration::hours(1);
}
Ok(response_list)
}
// 複数のISUのコンディションからグラフの一つのデータ点を計算
fn calculate_graph_data_point(
isu_conditions: &[IsuCondition],
) -> actix_web::Result<GraphDataPoint> {
use std::iter::FromIterator as _;
let mut conditions_count: HashMap<&str, i64> =
HashMap::from_iter([("is_broken", 0), ("is_dirty", 0), ("is_overweight", 0)]);
let mut raw_score = 0;
for condition in isu_conditions {
if !is_valid_condition_format(&condition.condition) {
return Err(actix_web::error::ErrorInternalServerError(
"invalid condition format",
));
}
let conditions = condition
.condition
.split(',')
.map(|cond_str| {
let mut key_value = cond_str.split('=');
(key_value.next().unwrap(), key_value.next().unwrap())
})
.filter(|(_, value)| *value == "true")
.map(|(condition_name, _)| condition_name);
let mut bad_conditions_count = 0;
for condition_name in conditions {
bad_conditions_count += 1;
*conditions_count.get_mut(&condition_name).unwrap() += 1;
}
if bad_conditions_count >= 3 {
raw_score += SCORE_CONDITION_LEVEL_CRITICAL;
} else if bad_conditions_count >= 1 {
raw_score += SCORE_CONDITION_LEVEL_WARNING;
} else {
raw_score += SCORE_CONDITION_LEVEL_INFO;
}
}
let sitting_count = isu_conditions
.iter()
.filter(|condition| condition.is_sitting)
.count() as i64;
let isu_conditions_length = isu_conditions.len() as i64;
let score = raw_score * 100 / 3 / isu_conditions_length;
let sitting_percentage = sitting_count * 100 / isu_conditions_length;
let is_broken_percentage =
conditions_count.get("is_broken").unwrap() * 100 / isu_conditions_length;
let is_overweight_percentage =
conditions_count.get("is_overweight").unwrap() * 100 / isu_conditions_length;
let is_dirty_percentage =
conditions_count.get("is_dirty").unwrap() * 100 / isu_conditions_length;
Ok(GraphDataPoint {
score,
percentage: ConditionsPercentage {
sitting: sitting_percentage,
is_broken: is_broken_percentage,
is_overweight: is_overweight_percentage,
is_dirty: is_dirty_percentage,
},
})
}
#[derive(Debug, serde::Deserialize)]
struct GetIsuConditionsQuery {
end_time: Option<String>,
condition_level: Option<String>,
start_time: Option<String>,
}
// ISUのコンディションを取得
#[actix_web::get("/api/condition/{jia_isu_uuid}")]
async fn get_isu_conditions(
pool: web::Data<sqlx::MySqlPool>,
session: actix_session::Session,
jia_isu_uuid: web::Path<String>,
query: web::Query<GetIsuConditionsQuery>,
) -> actix_web::Result<HttpResponse> {
let jia_user_id = require_signed_in(pool.as_ref(), session).await?;
if jia_isu_uuid.is_empty() {
return Err(actix_web::error::ErrorBadRequest("missing: jia_isu_uuid"));
}
let end_time = match &query.end_time {
Some(end_time_str) => match end_time_str.parse() {
Ok(end_time) => {
DateTime::from_utc(NaiveDateTime::from_timestamp(end_time, 0), JST_OFFSET.fix())
}
Err(_) => {
return Err(actix_web::error::ErrorBadRequest("bad format: end_time"));
}
},
None => {
return Err(actix_web::error::ErrorBadRequest("bad format: end_time"));
}
};
if query.condition_level.is_none() {
return Err(actix_web::error::ErrorBadRequest(
"missing: condition_level",
));
}
let mut condition_level = HashSet::new();
for level in query.condition_level.as_ref().unwrap().split(',') {
condition_level.insert(level);
}
let start_time = match &query.start_time {
Some(start_time_str) => match start_time_str.parse() {
Ok(start_time) => Some(DateTime::from_utc(
NaiveDateTime::from_timestamp(start_time, 0),
JST_OFFSET.fix(),
)),
Err(_) => {
return Err(actix_web::error::ErrorBadRequest("bad format: start_time"));
}
},
None => None,
};
let isu_name: Option<String> =
sqlx::query_scalar("SELECT name FROM `isu` WHERE `jia_isu_uuid` = ? AND `jia_user_id` = ?")
.bind(jia_isu_uuid.as_ref())
.bind(&jia_user_id)
.fetch_optional(pool.as_ref())
.await
.map_err(SqlxError)?;
if isu_name.is_none() {
log::error!("isu not found");
return Err(actix_web::error::ErrorNotFound("not found: isu"));
}
let isu_name = isu_name.unwrap();
let conditions_response = get_isu_conditions_from_db(