fetch survey config

This commit is contained in:
Aravinth Manivannan 2021-10-14 22:35:38 +05:30
parent 1b06c2c5be
commit eccb87fd99
Signed by: realaravinth
GPG key ID: AD9F0F08E855ED88
6 changed files with 37 additions and 15 deletions

View file

@ -16,13 +16,13 @@
import requests import requests
import json import json
from creds import COOKIE from creds import COOKIE, URL
def add_campaign(): def add_campaign():
"""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( payload = json.dumps(
{ {
"name": "test_1", "name": "test_1",

View file

@ -292,7 +292,7 @@ pub struct BenchConfig {
pub difficulties: Vec<i32>, pub difficulties: Vec<i32>,
} }
#[my_codegen::post( #[my_codegen::get(
path = "crate::V1_API_ROUTES.benches.fetch", path = "crate::V1_API_ROUTES.benches.fetch",
wrap = "get_check_login()" wrap = "get_check_login()"
)] )]

View file

@ -249,8 +249,13 @@ pub async fn get_campaign_config(
let route = V1_API_ROUTES let route = V1_API_ROUTES
.benches .benches
.fetch_routes(&campaign.campaign_id.to_string()); .fetch_routes(&campaign.campaign_id.to_string());
let new_resp = let new_resp = test::call_service(
test::call_service(&app, post_request!(&route).cookie(cookies).to_request()) &app,
test::TestRequest::get()
.uri(&route)
.cookie(cookies)
.to_request(),
)
.await; .await;
assert_eq!(new_resp.status(), StatusCode::OK); assert_eq!(new_resp.status(), StatusCode::OK);
test::read_body_json(new_resp).await test::read_body_json(new_resp).await

View file

@ -18,6 +18,7 @@
const ROUTES = { const ROUTES = {
register: "/survey/api/v1/benches/register", register: "/survey/api/v1/benches/register",
submitBench: (key: string): string => `/survey/api/v1/benches/${key}/submit`, submitBench: (key: string): string => `/survey/api/v1/benches/${key}/submit`,
fetchConfig: (key: string): string => `/survey/api/v1/benches/${key}/fetch`,
}; };
export default ROUTES; export default ROUTES;

View file

@ -14,14 +14,13 @@
* You should have received a copy of the GNU Affero General Public License * 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/>. * along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
import { Bench, Submission, SubmissionProof } from "./types"; import { Bench, BenchConfig, Submission, SubmissionProof } from "./types";
import ROUTES from "../api/v1/routes"; import ROUTES from "../api/v1/routes";
import genJsonPaylod from "../utils/genJsonPayload"; import genJsonPaylod from "../utils/genJsonPayload";
import isBlankString from "../utils/isBlankString"; import isBlankString from "../utils/isBlankString";
import createError from "../components/error/";
export const index = () => { export const index = () => {
const FACTOR = 500000;
const initSession = async () => { const initSession = async () => {
fetch(ROUTES.register); fetch(ROUTES.register);
}; };
@ -114,6 +113,18 @@ export const index = () => {
s.innerHTML = "Benchmark finished"; s.innerHTML = "Benchmark finished";
}; };
const getConfig = async (): Promise<BenchConfig> => {
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) => { const run = async (e: Event) => {
e.preventDefault(); e.preventDefault();
const deveceNameElement = <HTMLInputElement>document.getElementById("name"); const deveceNameElement = <HTMLInputElement>document.getElementById("name");
@ -128,7 +139,9 @@ export const index = () => {
document.getElementById("pre-bench").style.display = "none"; document.getElementById("pre-bench").style.display = "none";
document.getElementById("bench").style.display = "flex"; document.getElementById("bench").style.display = "flex";
const iterations = 9; const config = await getConfig();
const iterations = config.difficulties.length;
const counterElement = document.getElementById("counter"); const counterElement = document.getElementById("counter");
counterElement.innerText = `${iterations} more to go`; counterElement.innerText = `${iterations} more to go`;
@ -144,10 +157,9 @@ export const index = () => {
} }
}; };
for (let i = 1; i <= iterations; i++) { config.difficulties.forEach((difficulty_factor) =>
const difficulty_factor = i * FACTOR; worker.postMessage(difficulty_factor)
worker.postMessage(difficulty_factor); );
}
addDeviceInfo(); addDeviceInfo();
}; };

View file

@ -31,3 +31,7 @@ export type SubmissionProof = {
token: String; token: String;
proof: String; proof: String;
}; };
export type BenchConfig = {
difficulties: Array<number>;
};