diff --git a/scripts/campaign.py b/scripts/campaign.py index fc03cdf..2f35c73 100755 --- a/scripts/campaign.py +++ b/scripts/campaign.py @@ -16,13 +16,13 @@ import requests import json -from creds import COOKIE +from creds import COOKIE, URL def add_campaign(): """Add campaign""" - # url = "http://localhost:7000/admin/api/v1/campaign/add" - url = "https://survey.mcaptcha.org/admin/api/v1/campaign/add" + + url = URL payload = json.dumps( { "name": "test_1", diff --git a/src/api/v1/bench.rs b/src/api/v1/bench.rs index 0e82cce..59b4a8b 100644 --- a/src/api/v1/bench.rs +++ b/src/api/v1/bench.rs @@ -292,7 +292,7 @@ pub struct BenchConfig { pub difficulties: Vec, } -#[my_codegen::post( +#[my_codegen::get( path = "crate::V1_API_ROUTES.benches.fetch", wrap = "get_check_login()" )] diff --git a/src/tests.rs b/src/tests.rs index 60da5ff..4dd469a 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -249,9 +249,14 @@ pub async fn get_campaign_config( let route = V1_API_ROUTES .benches .fetch_routes(&campaign.campaign_id.to_string()); - let new_resp = - test::call_service(&app, post_request!(&route).cookie(cookies).to_request()) - .await; + let new_resp = test::call_service( + &app, + test::TestRequest::get() + .uri(&route) + .cookie(cookies) + .to_request(), + ) + .await; assert_eq!(new_resp.status(), StatusCode::OK); test::read_body_json(new_resp).await } diff --git a/templates/api/v1/routes.ts b/templates/api/v1/routes.ts index 8789e5e..56e8c0e 100644 --- a/templates/api/v1/routes.ts +++ b/templates/api/v1/routes.ts @@ -18,6 +18,7 @@ const ROUTES = { register: "/survey/api/v1/benches/register", submitBench: (key: string): string => `/survey/api/v1/benches/${key}/submit`, + fetchConfig: (key: string): string => `/survey/api/v1/benches/${key}/fetch`, }; export default ROUTES; diff --git a/templates/bench/index.ts b/templates/bench/index.ts index b8a0d43..1d0b727 100644 --- a/templates/bench/index.ts +++ b/templates/bench/index.ts @@ -14,14 +14,13 @@ * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ -import { Bench, Submission, SubmissionProof } from "./types"; +import { Bench, BenchConfig, Submission, SubmissionProof } from "./types"; import ROUTES from "../api/v1/routes"; import genJsonPaylod from "../utils/genJsonPayload"; import isBlankString from "../utils/isBlankString"; +import createError from "../components/error/"; export const index = () => { - const FACTOR = 500000; - const initSession = async () => { fetch(ROUTES.register); }; @@ -114,6 +113,18 @@ export const index = () => { s.innerHTML = "Benchmark finished"; }; + const getConfig = async (): Promise => { + const resp = await fetch(ROUTES.fetchConfig(CAMPAIGN_ID)); + if (resp.status != 200) { + const msg = "Something went wrong while fetching survey"; + createError(msg); + throw new Error(msg); + } + + const config: BenchConfig = await resp.json(); + return config; + }; + const run = async (e: Event) => { e.preventDefault(); const deveceNameElement = document.getElementById("name"); @@ -128,7 +139,9 @@ export const index = () => { document.getElementById("pre-bench").style.display = "none"; document.getElementById("bench").style.display = "flex"; - const iterations = 9; + const config = await getConfig(); + + const iterations = config.difficulties.length; const counterElement = document.getElementById("counter"); counterElement.innerText = `${iterations} more to go`; @@ -144,10 +157,9 @@ export const index = () => { } }; - for (let i = 1; i <= iterations; i++) { - const difficulty_factor = i * FACTOR; - worker.postMessage(difficulty_factor); - } + config.difficulties.forEach((difficulty_factor) => + worker.postMessage(difficulty_factor) + ); addDeviceInfo(); }; diff --git a/templates/bench/types.ts b/templates/bench/types.ts index 6ba43d5..f587d8f 100644 --- a/templates/bench/types.ts +++ b/templates/bench/types.ts @@ -31,3 +31,7 @@ export type SubmissionProof = { token: String; proof: String; }; + +export type BenchConfig = { + difficulties: Array; +};