Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(api): respond w/ task.status #286

Merged
merged 8 commits into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 0 additions & 19 deletions .github/workflows/js-css.yml

This file was deleted.

18 changes: 18 additions & 0 deletions .github/workflows/pre-commit.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: pre-commit

on:
pull_request:
push:
branches: [main]

jobs:
pre-commit:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: 18
- uses: actions/setup-python@v3
- run: npm install
- uses: pre-commit/[email protected]
10 changes: 0 additions & 10 deletions .github/workflows/python.yml

This file was deleted.

4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ repos:
- id: ruff
# Javascript
- repo: https://github.com/pre-commit/mirrors-eslint
rev: "v8.24.0"
rev: "v8.51.0"
hooks:
- id: eslint
additional_dependencies: ['eslint@8.24.0', 'eslint-plugin-import@2.26.0', '[email protected]']
additional_dependencies: ['eslint@8.51.0', 'eslint-plugin-import@2.28.1', '[email protected]']
2 changes: 1 addition & 1 deletion .stylelintrc.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"extends": "stylelint-config-standard",
"rules": {
"indentation": 4
"declaration-property-value-no-unknown": true
}
}
2 changes: 1 addition & 1 deletion client-src/base/base.css
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
--card-sectionning-background-color: #e6e6e6 !important;
}

@media (min-width: 576px) {
@media (width >= 576px) {
:root {
--logo-text-size: 2.8rem;
}
Expand Down
4 changes: 4 additions & 0 deletions client-src/base/results.css
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,7 @@
height: 3rem;
font-size: 2rem;
}

.task-status {
font-size: small;
}
2 changes: 1 addition & 1 deletion client-src/create/create.css
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@media screen and (min-width: 992px) {
@media screen and (width >= 992px) {
#main-grid.grid {
grid-template-columns: 2fr 1fr;
}
Expand Down
2 changes: 1 addition & 1 deletion client-src/digitize/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ setDisabled("submitBtn", true);

// ...until files are added
const fileElement = document.querySelector(".filebokz");
fileElement.addEventListener("file-added", (e) => {
fileElement.addEventListener("file-added", () => {
setDisabled("submitBtn", false);
});

Expand Down
4 changes: 2 additions & 2 deletions client-src/help/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ if (anchor) {

// jump inside page
[...document.querySelectorAll("[href^='#']")].forEach((anchorLink) => {
const anchor = anchorLink.getAttribute("href");
const anchorHref = anchorLink.getAttribute("href");
const onClick = () => {
closeAllDetailsElements();
openDetailsParents(document.querySelector(anchor));
openDetailsParents(document.querySelector(anchorHref));
};
anchorLink.addEventListener("click", onClick);
});
Expand Down
4 changes: 2 additions & 2 deletions client-src/index/index.css
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
@media screen and (max-width: 999px) {
@media screen and (width < 1000px) {
.horizontal.step-arrows {
display: none;
}
}

@media screen and (min-width: 1000px) {
@media screen and (width >= 1000px) {
.vertical.step-arrows {
display: none;
}
Expand Down
15 changes: 10 additions & 5 deletions client-src/shared/domHelpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*/
function getUUIDFromURL() {
const UUID_V4_PATTERN = /[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}/i;
// eslint-disable-next-line no-restricted-globals
return location.pathname.match(UUID_V4_PATTERN)[0];
}

Expand All @@ -15,13 +16,12 @@ function getUUIDFromURL() {
*/
function fillSelectOptions(selectElementId, optionsMap) {
const selectElement = document.getElementById(selectElementId);
for (const paperformatKey in optionsMap) {
if (Object.prototype.hasOwnProperty.call(optionsMap, paperformatKey)) {
Object.values(optionsMap)
.forEach((paperformatValue) => {
const option = document.createElement("option");
option.text = optionsMap[paperformatKey];
option.text = paperformatValue;
selectElement.appendChild(option);
}
}
});
}

/**
Expand All @@ -34,6 +34,10 @@ function setIsBusy(elementId, isBusy) {
element.setAttribute("aria-busy", isBusy);
}

function setTaskStatus(elementId, taskStatus) {
const element = document.getElementById(elementId);
element.textContent = taskStatus;
}
/**
* set the disabled HTMLAttribute on the given HTMLElement
* @param elementId
Expand Down Expand Up @@ -99,6 +103,7 @@ function openAllDetailsElements() {
export {
getUUIDFromURL,
fillSelectOptions,
setTaskStatus,
setDisabled,
setDownloadLink,
setIsBusy,
Expand Down
54 changes: 40 additions & 14 deletions client-src/shared/poll/poll.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
import { PollUntilValid } from "./pollUntilValid.js";
import { setDownloadLink, setIsBusy, setDisabled } from "../domHelpers.js";
import {
setDownloadLink,
setIsBusy,
setDisabled,
setTaskStatus,
} from "../domHelpers.js";

/**
* This function polls repeatedly data from a URL as long as it receives
* HTTP 202 Accepted Codes.
* It will stop if it get a 200 OK or any Code outside the range of 200-299.
* Depending on Success(200), Pending(202), or any failure (!200-299 or NetworkError) it
* It will stop if it gets a 200 OK or any Code outside the range of 200-299.
* Depending on Success(200), Pending(202), or any failure (!200-299) it
* will display different messages to the user.
* On NetworkError it will continue polling.
*
* The function assumes the availability of 2 HTML Elements with the following ids:
* @example
* <span id="YOUR_PREFIX_HERE-status"></span>
* <a id="YOUR_PREFIX_HERE-download-button"></a>
*
* @param downloadUrl
* @param prefix
* @param url url to poll from
* @param prefix identifier used to decide where results or progress messages should be displayed
* @returns {Promise<Response>}
*/
async function poll(url, prefix) {
Expand All @@ -25,11 +31,14 @@ async function poll(url, prefix) {

async function onProgress(response) {
// console.log("progress", response);
const result = await response.json();
setTaskStatus(`${prefix}-status`, `Processing ${result.status}`);
}

async function onValid(response) {
const result = await response.json();
const { status, href } = result;
const { href } = result;
setTaskStatus(`${prefix}-status`, "");
setDownloadLink(`${prefix}-download-button`, href);
setIsBusy(`${prefix}-download-button`, false);
setDisabled(`${prefix}-download-button`, false);
Expand All @@ -42,6 +51,7 @@ async function poll(url, prefix) {
/**
* Displays an error message and disappears the download button
* @param _prefix sketch-map | quality-report
* @param errorText text shown in the error message details
*/
function handleError(_prefix, errorText) {
document.querySelectorAll(`#${prefix} :is(.pending, .success)`)
Expand All @@ -60,19 +70,35 @@ async function poll(url, prefix) {
}

async function onError(response) {
const { status: httpStatus } = response;
const resonseJSON = await response.json();
const errorText = resonseJSON.error;
console.log(response.status, response.statusText, errorText, resonseJSON);
handleError(prefix, errorText);
const {
error: errorText,
status: taskStatus,
} = resonseJSON;
// display error
if (httpStatus === 500) {
handleError(prefix, `${new Date().toISOString()} ${taskStatus} <br>
There was an Internal Server Error.`);
} else {
handleError(prefix, `${new Date().toISOString()} ${taskStatus} <br> ${errorText}`);
}
// remove task status
setTaskStatus(`${prefix}-status`, "");
}

try {
return await PollUntilValid.poll(url, validateFn, 1000, onValid, onProgress, onError);
} catch (e) {
// Network Error or other reason why the request could not be completed
console.log(e);
handleError(prefix, e);
return null;
} catch (error) {
if (error instanceof TypeError) {
// Chrome and Firefox use different Error messages, so it's hard to be more
// specific than checking for TypeError
// see: https://developer.mozilla.org/en-US/docs/Web/API/fetch#exceptions
setTaskStatus(`${prefix}-status`, "NetworkError: RETRYING to get task status");
await PollUntilValid.wait(5000);
return poll(url, prefix);
}
throw error;
}
}

Expand Down
22 changes: 3 additions & 19 deletions client-src/shared/poll/pollUntilValid.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,8 @@ class PollUntilValid {
onError = () => {},
) {
do {
let response;
try {
/* eslint-disable no-await-in-loop */
response = await fetch(url);
} catch (error) {
if (error instanceof TypeError) {
// Chrome and Firefox use different Error messages, so it's hard to be more
// specific than checking for TypeError
// see: https://developer.mozilla.org/en-US/docs/Web/API/fetch#exceptions
console.log("NetworkError, continue retrying", error);
await PollUntilValid.wait(5000);
} else {
throw error;
}
}

// eslint-disable-next-line no-continue
if (!response) continue;
/* eslint-disable no-await-in-loop */
const response = await fetch(url);

// if response code is not between 200-299
if (!response.ok) {
Expand All @@ -60,7 +44,7 @@ class PollUntilValid {
return response;
}

/* eslint-enable */
// eslint-disable-next-line no-constant-condition
} while (true);
}

Expand Down
Loading