Skip to content

Commit 939359a

Browse files
authored
Merge pull request #1539 from rust-lang/site-template
Add template rendering engine to site
2 parents 44c5d9c + e4e2df4 commit 939359a

File tree

13 files changed

+631
-52
lines changed

13 files changed

+631
-52
lines changed

Cargo.lock

Lines changed: 482 additions & 30 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

collector/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ impl Bound {
4040
Bound::Commit(sha) => commit.sha == **sha,
4141
Bound::Date(date) => commit.is_master() && commit.date.0.naive_utc().date() >= *date,
4242
Bound::None => {
43-
let last_month = chrono::Utc::now().date().naive_utc() - chrono::Duration::days(30);
43+
let last_month = chrono::Utc::now().date_naive() - chrono::Duration::days(30);
4444
commit.is_master() && last_month <= commit.date.0.naive_utc().date()
4545
}
4646
}
@@ -50,7 +50,7 @@ impl Bound {
5050
pub fn right_match(&self, commit: &Commit) -> bool {
5151
match self {
5252
Bound::Commit(sha) => commit.sha == **sha,
53-
Bound::Date(date) => commit.is_master() && commit.date.0.date().naive_utc() <= *date,
53+
Bound::Date(date) => commit.is_master() && commit.date.0.date_naive() <= *date,
5454
Bound::None => commit.is_master(),
5555
}
5656
}

database/src/bin/sqlite-to-postgres.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ impl Table for Artifact {
6767
.serialize(ArtifactRow {
6868
id: row.get(0).unwrap(),
6969
name: row.get_ref(1).unwrap().as_str().unwrap(),
70-
date: Nullable(date.map(|seconds| Utc.timestamp(seconds, 0))),
70+
date: Nullable(date.map(|seconds| Utc.timestamp_opt(seconds, 0).unwrap())),
7171
typ: row.get_ref(3).unwrap().as_str().unwrap(),
7272
})
7373
.unwrap();
@@ -102,7 +102,7 @@ impl Table for ArtifactCollectionDuration {
102102
writer
103103
.serialize(ArtifactCollectionDurationRow {
104104
aid: row.get(0).unwrap(),
105-
date_recorded: Utc.timestamp(date_recorded, 0),
105+
date_recorded: Utc.timestamp_opt(date_recorded, 0).unwrap(),
106106
duration: row.get(2).unwrap(),
107107
})
108108
.unwrap();
@@ -200,8 +200,8 @@ impl Table for CollectorProgress {
200200
fn write_postgres_csv_row<W: Write>(writer: &mut csv::Writer<W>, row: &rusqlite::Row) {
201201
let start: Option<i64> = row.get(2).unwrap();
202202
let end: Option<i64> = row.get(3).unwrap();
203-
let start_time = Nullable(start.map(|seconds| Utc.timestamp(seconds, 0)));
204-
let end_time = Nullable(end.map(|seconds| Utc.timestamp(seconds, 0)));
203+
let start_time = Nullable(start.map(|seconds| Utc.timestamp_opt(seconds, 0).unwrap()));
204+
let end_time = Nullable(end.map(|seconds| Utc.timestamp_opt(seconds, 0).unwrap()));
205205

206206
writer
207207
.serialize(CollectorProgressRow {
@@ -386,7 +386,9 @@ impl Table for PullRequestBuild {
386386
pr: row.get(1).unwrap(),
387387
parent_sha: row.get_ref(2).unwrap().try_into().unwrap(),
388388
complete: row.get(3).unwrap(),
389-
requested: Nullable(requested.map(|seconds| Utc.timestamp(seconds, 0))),
389+
requested: Nullable(
390+
requested.map(|seconds| Utc.timestamp_opt(seconds, 0).unwrap()),
391+
),
390392
include: row.get_ref(5).unwrap().try_into().unwrap(),
391393
exclude: row.get_ref(6).unwrap().try_into().unwrap(),
392394
runs: row.get(7).unwrap(),

database/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ impl Date {
6666
}
6767

6868
pub fn ymd_hms(year: i32, month: u32, day: u32, h: u32, m: u32, s: u32) -> Date {
69-
Date(Utc.ymd(year, month, day).and_hms(h, m, s))
69+
Date(Utc.with_ymd_and_hms(year, month, day, h, m, s).unwrap())
7070
}
7171

7272
pub fn empty() -> Date {

database/src/pool/postgres.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,7 @@ where
498498
let timestamp: Option<DateTime<Utc>> = row.get(2);
499499
match timestamp {
500500
Some(t) => Date(t),
501-
None => Date(Utc.ymd(2001, 01, 01).and_hms(0, 0, 0)),
501+
None => Date(Utc.with_ymd_and_hms(2001, 01, 01, 0, 0, 0).unwrap()),
502502
}
503503
},
504504
r#type: CommitType::from_str(&row.get::<_, String>(3)).unwrap()

database/src/pool/sqlite.rs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -438,8 +438,8 @@ impl Connection for SqliteConnection {
438438
date: {
439439
let timestamp: Option<i64> = row.get(2)?;
440440
match timestamp {
441-
Some(t) => Date(Utc.timestamp(t, 0)),
442-
None => Date(Utc.ymd(2001, 01, 01).and_hms(0, 0, 0)),
441+
Some(t) => Date(Utc.timestamp_opt(t, 0).unwrap()),
442+
None => Date(Utc.with_ymd_and_hms(2001, 01, 01, 0, 0, 0).unwrap()),
443443
}
444444
},
445445
r#type: CommitType::from_str(&row.get::<_, String>(3)?).unwrap(),
@@ -648,7 +648,7 @@ impl Connection for SqliteConnection {
648648
include: row.get(3).unwrap(),
649649
exclude: row.get(4).unwrap(),
650650
runs: row.get(5).unwrap(),
651-
commit_date: row.get::<_, Option<i64>>(6).unwrap().map(|timestamp| Date(DateTime::from_utc(NaiveDateTime::from_timestamp(timestamp, 0), Utc)))
651+
commit_date: row.get::<_, Option<i64>>(6).unwrap().map(|timestamp| Date(DateTime::from_utc(NaiveDateTime::from_timestamp_opt(timestamp, 0).unwrap(), Utc)))
652652
})
653653
})
654654
.collect::<Result<Vec<_>, _>>()
@@ -679,7 +679,7 @@ impl Connection for SqliteConnection {
679679
include: row.get(3).unwrap(),
680680
exclude: row.get(4).unwrap(),
681681
runs: row.get(5).unwrap(),
682-
commit_date: row.get::<_, Option<i64>>(6).unwrap().map(|timestamp| Date(DateTime::from_utc(NaiveDateTime::from_timestamp(timestamp, 0), Utc)))
682+
commit_date: row.get::<_, Option<i64>>(6).unwrap().map(|timestamp| Date(DateTime::from_utc(NaiveDateTime::from_timestamp_opt(timestamp, 0).unwrap(), Utc)))
683683
})
684684
},
685685
)
@@ -955,7 +955,7 @@ impl Connection for SqliteConnection {
955955
"try" | "master" => ArtifactId::Commit(Commit {
956956
sha: name,
957957
date: date
958-
.map(|d| Utc.timestamp(d, 0))
958+
.map(|d| Utc.timestamp_opt(d, 0).unwrap())
959959
.map(Date)
960960
.unwrap_or_else(|| Date::ymd_hms(2001, 01, 01, 0, 0, 0)),
961961
r#type: CommitType::from_str(&ty).unwrap(),
@@ -1011,7 +1011,7 @@ impl Connection for SqliteConnection {
10111011
order by date_recorded desc \
10121012
limit 1;",
10131013
params![],
1014-
|r| Ok(Utc.timestamp(r.get(0)?, 0)),
1014+
|r| Ok(Utc.timestamp_opt(r.get(0)?, 0).unwrap()),
10151015
)
10161016
.optional()
10171017
.unwrap()
@@ -1155,13 +1155,16 @@ impl Connection for SqliteConnection {
11551155
match ty.as_str() {
11561156
"master" => Some(ArtifactId::Commit(Commit {
11571157
sha: artifact.to_owned(),
1158-
date: Date(Utc.timestamp(date.expect("master has date"), 0)),
1158+
date: Date(
1159+
Utc.timestamp_opt(date.expect("master has date"), 0)
1160+
.unwrap(),
1161+
),
11591162
r#type: CommitType::Master,
11601163
})),
11611164
"try" => Some(ArtifactId::Commit(Commit {
11621165
sha: artifact.to_owned(),
11631166
date: date
1164-
.map(|d| Date(Utc.timestamp(d, 0)))
1167+
.map(|d| Date(Utc.timestamp_opt(d, 0).unwrap()))
11651168
.unwrap_or_else(|| Date::ymd_hms(2000, 1, 1, 0, 0, 0)),
11661169
r#type: CommitType::Try,
11671170
})),

site/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ inferno = { version="0.10", default-features = false }
4545
mime = "0.3"
4646
prometheus = "0.12"
4747
uuid = { version = "0.8.2", features = ["v4"] }
48+
tera = "1.18"
4849

4950
[target.'cfg(unix)'.dependencies]
5051
jemallocator = "0.3"

site/src/comparison.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1429,7 +1429,7 @@ Revision range: [{first_commit}..{last_commit}](https://perf.rust-lang.org/?star
14291429
TODO: Nags
14301430
14311431
"#####,
1432-
date = chrono::Utc::today().format("%Y-%m-%d"),
1432+
date = chrono::Utc::now().format("%Y-%m-%d"),
14331433
first_commit = start,
14341434
last_commit = end,
14351435
num_comparisons = num_comparisons,

site/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,4 @@ mod interpolate;
2121
mod request_handlers;
2222
mod selector;
2323
mod self_profile;
24+
mod templates;

site/src/server.rs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use brotli::enc::BrotliEncoderParams;
22
use brotli::BrotliCompress;
33
use std::collections::HashMap;
44
use std::net::SocketAddr;
5-
use std::path::Path;
5+
use std::path::{Path, PathBuf};
66
use std::str::FromStr;
77
use std::sync::atomic::{AtomicBool, Ordering as AtomicOrdering};
88
use std::sync::Arc;
@@ -28,6 +28,7 @@ pub use crate::api::{
2828
use crate::db::{self, ArtifactId};
2929
use crate::load::{Config, SiteCtxt};
3030
use crate::request_handlers;
31+
use crate::templates::TemplateRenderer;
3132

3233
pub type Request = http::Request<hyper::Body>;
3334
pub type Response = http::Response<hyper::Body>;
@@ -336,7 +337,7 @@ async fn serve_req(server: Server, req: Request) -> Result<Response, ServerError
336337
None
337338
};
338339

339-
if let Some(response) = handle_fs_path(&req, path) {
340+
if let Some(response) = handle_fs_path(&req, path).await {
340341
return Ok(response);
341342
}
342343

@@ -565,10 +566,14 @@ where
565566

566567
lazy_static::lazy_static! {
567568
static ref VERSION_UUID: Uuid = Uuid::new_v4(); // random UUID used as ETag for cache revalidation
569+
static ref TEMPLATES: TemplateRenderer = {
570+
let livereload = std::env::var("LIVERELOAD").is_ok();
571+
TemplateRenderer::new(PathBuf::from("site/templates"), livereload).unwrap()
572+
};
568573
}
569574

570575
/// Handle the case where the path is to a static file
571-
fn handle_fs_path(req: &Request, path: &str) -> Option<http::Response<hyper::Body>> {
576+
async fn handle_fs_path(req: &Request, path: &str) -> Option<http::Response<hyper::Body>> {
572577
let fs_path = format!(
573578
"site/static{}",
574579
match path {
@@ -597,7 +602,6 @@ fn handle_fs_path(req: &Request, path: &str) -> Option<http::Response<hyper::Bod
597602
}
598603
}
599604

600-
let source = fs::read(&fs_path).unwrap();
601605
let p = Path::new(&fs_path);
602606
match p.extension().and_then(|x| x.to_str()) {
603607
Some("html") => response = response.header_typed(ContentType::html()),
@@ -608,6 +612,14 @@ fn handle_fs_path(req: &Request, path: &str) -> Option<http::Response<hyper::Bod
608612
Some("js") => response = response.header("Content-Type", "application/javascript"),
609613
_ => (),
610614
}
615+
616+
let start = Instant::now();
617+
let source = match path {
618+
"/help.html" => TEMPLATES.render("help.html").await.unwrap().into_bytes(),
619+
_ => fs::read(&fs_path).unwrap(),
620+
};
621+
println!("{:?}", start.elapsed());
622+
611623
Some(response.body(hyper::Body::from(source)).unwrap())
612624
}
613625

0 commit comments

Comments
 (0)