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 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",

View file

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

View file

@ -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
}

View file

@ -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;

View file

@ -14,14 +14,13 @@
* 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/>.
*/
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<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) => {
e.preventDefault();
const deveceNameElement = <HTMLInputElement>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();
};

View file

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