feat: port benchmark page to tera

This commit is contained in:
Aravinth Manivannan 2023-01-24 23:29:17 +05:30
parent 82d293a159
commit e5bf7feebe
Signed by: realaravinth
GPG key ID: AD9F0F08E855ED88
2 changed files with 165 additions and 154 deletions

View file

@ -14,46 +14,55 @@
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
use std::str::FromStr;
use std::cell::RefCell;
use actix_web::http::header::ContentType;
use actix_web::{web, HttpResponse, Responder};
use lazy_static::lazy_static;
use my_codegen::get;
use sailfish::TemplateOnce;
use tera::Context;
use uuid::Uuid;
use crate::errors::*;
use crate::pages::errors::ErrorPage;
use crate::AppData;
use crate::PAGES;
#[derive(TemplateOnce)]
#[template(path = "bench/index.html")]
struct Bench<'a> {
error: Option<ErrorPage<'a>>,
}
const PAGE: &str = "Survey";
pub use super::*;
impl<'a> Default for Bench<'a> {
fn default() -> Self {
Bench { error: None }
pub struct Bench {
ctx: RefCell<Context>,
}
pub const BENCH: TemplateFile = TemplateFile::new("new_campaign", "bench/index.html");
impl CtxError for Bench {
fn with_error(&self, e: &ReadableError) -> String {
self.ctx.borrow_mut().insert(ERROR_KEY, e);
self.render()
}
}
lazy_static! {
static ref BENCH: String = Bench::default().render_once().unwrap();
impl Bench {
pub fn new(settings: &Settings) -> Self {
let ctx = RefCell::new(context(settings, "Login"));
Self { ctx }
}
pub fn render(&self) -> String {
TEMPLATES.render(BENCH.name, &self.ctx.borrow()).unwrap()
}
}
#[get(
#[actix_web_codegen_const_routes::get(
path = "PAGES.panel.campaigns.bench",
wrap = "crate::api::v1::bench::get_check_login()"
)]
pub async fn bench(path: web::Path<String>) -> PageResult<impl Responder> {
let path = path.into_inner();
match Uuid::from_str(&path) {
Err(_) => Err(PageError::PageDoesntExist),
Ok(_) => Ok(HttpResponse::Ok()
.content_type("text/html; charset=utf-8")
.body(&*BENCH.as_str())),
}
pub async fn bench(
data: AppData,
_path: web::Path<Uuid>,
) -> PageResult<impl Responder, Bench> {
let bench = Bench::new(&data.settings).render();
let html = ContentType::html();
Ok(HttpResponse::Ok().content_type(html).body(bench))
}
pub fn services(cfg: &mut actix_web::web::ServiceConfig) {
cfg.service(bench);
}

View file

@ -1,143 +1,145 @@
<. include!("../components/base/top.html"); .>
<body class="survey__body">
<main class="survey__container">
<. include!("../components/error/index.html"); .>
<div id="pre-bench">
<h1>Click to Start Benchmark</h1>
<form class="new-campaign__form" accept-charset="utf-8">
<label class="form__label" for="name">
Device name
<input
class="form__input"
name="name"
required
id="name"
type="text"
/>
</label>
{% extends 'base' %}
{% block body %}
<body class="survey__body">
<main class="survey__container">
{% include "error_comp" %}
<!-- <button id="start" type="submit">Start</button> -->
<button id="start" class="form__submit" type="submit">Start</button>
</form>
</div>
<div id="pre-bench">
<h1>Click to Start Benchmark</h1>
<form class="new-campaign__form" accept-charset="utf-8">
<label class="form__label" for="name">
Device name
<input
class="form__input"
name="name"
required
id="name"
type="text"
/>
</label>
<noscript>
<b>
This is a WASM benchmark, please enable JavScript to run it. We don't
collect any data and
<a href="https://github.com/mCaptcha/survey"
>source code is available here</a
>
</b>
</noscript>
<div id="bench">
<h1 id="status" class="name">Running benchmark</h1>
<div id="counter"></div>
<button class="btn" id="advance">Show Advanced Log</button>
<div id="winner-instructions">
<h2>Winner Announcement</h2>
<p>
The winners will be announed on the organisation website
<a href="https://mcaptcha.org/blog/survey" target="_blank">here</a> on
December 1, 2021. The winning submission IDs will be published and the
winners expected to provide the submission proof to claim their
rewards.
</p>
<!-- <button id="start" type="submit">Start</button> -->
<button id="start" class="form__submit" type="submit">Start</button>
</form>
</div>
<div id="submission-proof"></div>
<div class="advance-log" id="device-info"></div>
<noscript>
<b>
This is a WASM benchmark, please enable JavScript to run it. We don't
collect any data and
<a href="https://github.com/mCaptcha/survey"
>source code is available here</a
>
</b>
</noscript>
<table class="advance-log">
<thead>
<tr>
<th>Difficulty factor</th>
<th>Duration(ms)</th>
</tr>
</thead>
<tbody id="stats"></tbody>
</table>
</div>
</main>
<. include!("../components/footer/index.html"); .>
</body>
<style>
body {
width: 100%;
display: flex;
flex-direction: column;
padding: 0;
display: hidden;
align-items: center;
}
<div id="bench">
<h1 id="status" class="name">Running benchmark</h1>
<div id="counter"></div>
<button class="btn" id="advance">Show Advanced Log</button>
#pre-bench,
#bench {
width: 100%;
display: flex;
margin: 40px auto;
align-items: center;
flex-direction: column;
}
<div id="winner-instructions">
<h2>Winner Announcement</h2>
<p>
The winners will be announed on the organisation website
<a href="https://mcaptcha.org/blog/survey" target="_blank">here</a> on
December 1, 2021. The winning submission IDs will be published and the
winners expected to provide the submission proof to claim their
rewards.
</p>
</div>
<div id="submission-proof"></div>
#bench {
display: none;
}
<div class="advance-log" id="device-info"></div>
.survey__container {
flex: 2;
margin: 0 auto;
}
<table class="advance-log">
<thead>
<tr>
<th>Difficulty factor</th>
<th>Duration(ms)</th>
</tr>
</thead>
<tbody id="stats"></tbody>
</table>
</div>
</main>
</body>
<style>
body {
width: 100%;
display: flex;
flex-direction: column;
padding: 0;
display: hidden;
align-items: center;
}
#status {
max-width: 80%;
margin: auto;
}
#pre-bench,
#bench {
width: 100%;
display: flex;
margin: 40px auto;
align-items: center;
flex-direction: column;
}
table {
border-collapse: collapse;
margin: 40px auto;
width: 40%;
}
#bench {
display: none;
}
td,
th {
border: 1px solid #999;
padding: 0.5rem;
text-align: left;
}
.survey__container {
flex: 2;
margin: 0 auto;
}
th {
color: green;
}
#status {
max-width: 80%;
margin: auto;
}
#device-info {
margin: auto;
font-size: 0.8rem;
}
table {
border-collapse: collapse;
margin: 40px auto;
width: 40%;
}
.advance-log > b,
div {
font-size: 0.8rem;
}
.advance-log {
display: none;
}
#winner-instructions {
display: none;
}
td,
th {
border: 1px solid #999;
padding: 0.5rem;
text-align: left;
}
#submission-proof {
margin: 15px auto;
}
th {
color: green;
}
#submission-proof > div,
b {
font-size: 1.2rem;
font-family: monospace, monospace;
}
</style>
<script src="<.= &*crate::GLUE .>"></script>
<. include!("../components/base/bottom.html"); .>
#device-info {
margin: auto;
font-size: 0.8rem;
}
.advance-log > b,
div {
font-size: 0.8rem;
}
.advance-log {
display: none;
}
#winner-instructions {
display: none;
}
#submission-proof {
margin: 15px auto;
}
#submission-proof > div,
b {
font-size: 1.2rem;
font-family: monospace, monospace;
}
</style>
<script src="{{ assets.glue }}"></script>
{% endblock body %}