Skip to content

Commit

Permalink
ref issues
Browse files Browse the repository at this point in the history
  • Loading branch information
githubering182 committed Jun 24, 2024
1 parent 1f407bd commit 8cc8cc1
Show file tree
Hide file tree
Showing 9 changed files with 89 additions and 44 deletions.
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ prod-stop:
docker compose down

prod-restart:
make stop && make start
make prod-stop && make prod

dev:
docker compose -f docker-compose.yml -f docker-compose.dev.yml up -d
Expand All @@ -20,7 +20,7 @@ dev-stop:
docker compose -f docker-compose.yml -f docker-compose.dev.yml down

dev-restart:
make stop-dev && make start-dev
make dev-stop && make dev

test:
docker compose -f docker-compose.test.yml up -d --build
Expand All @@ -29,7 +29,7 @@ test-stop:
docker compose -f docker-compose.test.yml down

test-restart:
make stop-test && make start-test
make test-stop && make test

appdb-dump-schema:
docker exec iss-main-db pg_dump -U postgres -d iss_app_db --schema-only > app_dump_schema
Expand Down
20 changes: 11 additions & 9 deletions backend-app/file/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,15 +147,17 @@ def _get_files(

paginator: Paginator = Paginator(files, per_page)

return {
"data": FileSerializer(
paginator.page(page).object_list,
many=True
).data,
"page": page,
"per_page": paginator.per_page,
"total_pages": paginator.num_pages
}, HTTP_200_OK
try:
return {
"data": FileSerializer(
paginator.page(page).object_list,
many=True
).data,
"page": page,
"per_page": paginator.per_page,
"total_pages": paginator.num_pages
}, HTTP_200_OK
except Exception: return {}, HTTP_404_NOT_FOUND

def _create_file(
self,
Expand Down
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ services:
ports:
- "${FRONTEND_PORT:-3000}:${FRONTEND_PORT:-3000}"
command: >
sh -c "npm run build && serve -s build"
sh -c "REACT_APP_STORAGE_PORT=${STORAGE_PORT:-9000} REACT_APP_BACKEND_PORT=${BACKEND_PORT:-8000} npm run build && serve -s build"
networks:
- iss_network

Expand Down
3 changes: 2 additions & 1 deletion frontend-app/.eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
"react-hooks/exhaustive-deps": "warn",
"no-empty-function": "off",
"@typescript-eslint/no-empty-function": "off",
"@typescript-eslint/no-unused-vars": "error"
"@typescript-eslint/no-unused-vars": "error",
"no-constant-condition": "off"
}
}
2 changes: 1 addition & 1 deletion frontend-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"scripts": {
"start": "export REACT_APP_STORAGE_PORT=$STORAGE_PORT && export REACT_APP_BACKEND_PORT=$BACKEND_PORT && export REACT_APP_CASE=$CASE && react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"test": "react-scripts REACT_APP_STORAGE_PORT=$STORAGE_PORT && export REACT_APP_BACKEND_PORT=$BACKEND_PORT test",
"eject": "react-scripts eject",
"lint": "eslint src/*",
"compile": "tsc"
Expand Down
5 changes: 2 additions & 3 deletions frontend-app/src/config/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,9 @@ const origin = process.env.REACT_APP_CASE === 'test'
/** @type {string} */
const storageOrigin = getOriginDomain();

// TODO: fix env variables
/** @type {Api} */
export const api = axios.create({
baseURL: `${origin}:${backendPort || 8000}`,
baseURL: `${origin}:${backendPort}`,
headers: { 'Content-Type': 'application/json' },
withCredentials: true,
xsrfCookieName: "csrftoken",
Expand All @@ -36,7 +35,7 @@ export const api = axios.create({

/** @type {Api} */
export const fileApi = axios.create({
baseURL: `${storageOrigin}:${storagePort || 9000}`,
baseURL: `${storageOrigin}:${storagePort}`,
headers: { 'Content-Type': 'application/json' },
withCredentials: true,
xsrfCookieName: "csrftoken",
Expand Down
67 changes: 43 additions & 24 deletions frontend-app/src/hooks/fileUploader.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useState } from "react";
import { api, fileApi } from '../config/api';
import { sleep } from "../utils";

/**
* @param {number} projectID
Expand All @@ -13,7 +14,7 @@ export default function useFileUploader(projectID) {
* @param {object} file
* @returns {Promise<string>}
*/
async function _writeFile(file) {
const _writeFile = async (file) => {
var data = {
file: file.file,
file_meta: JSON.stringify({
Expand Down Expand Up @@ -45,14 +46,14 @@ export default function useFileUploader(projectID) {
setFiles((prev) => [...prev]);

return response.data.result;
}
};

/**
* @param {object} file
* @param {string} fileID
* @returns {Promise<{data: object}>}
*/
async function _writeObject(file, fileID) {
const _writeObject = async (file, fileID) => {
var formData = new FormData();
var { name, extension, type, atrsGroups } = file;

Expand All @@ -69,24 +70,24 @@ export default function useFileUploader(projectID) {
"Authorization": "Bearer " + localStorage.getItem("dtcAccess"),
}
});
}
};

/**
* @param {string} fileID
* @returns {Promise<object>}
*/
async function _deleteFile(fileID) {
const _deleteFile = async (fileID) => {
await fileApi.delete(
`/api/storage/project_${project}/${fileID}/`,
{ headers: { "Authorization": "Bearer " + localStorage.getItem("dtcAccess") } }
);
}
};

/**
* @param {object} file
* @returns {Promise<undefined>}
* @returns {Promise<void>}
*/
async function _proceedFile(file) {
const _proceedFile = async (file) => {
try {
var fileID = await _writeFile(file);
await _writeObject(file, fileID);
Expand All @@ -103,24 +104,42 @@ export default function useFileUploader(projectID) {
file.error = response.data?.result || message;
setFiles((prev) => [...prev]);
}
}
};

// TODO: rework: rewrite pool
/**
* @returns {Promise<undefined>}
*/
async function proceedUpload() {
const atOnce = 5;
var proceedFiles = files.filter(({ status }) => status !== "a");

for (var i = 0; i < Math.ceil(proceedFiles.length / atOnce); i++) {
await Promise.all(
proceedFiles
.slice(atOnce * i, atOnce * (i + 1))
.map((file) => _proceedFile(file))
);
// /** @returns {Promise<undefined>} */
// async function proceedUpload() {
// const atOnce = 5;
// var proceedFiles = files.filter(({ status }) => status !== "a");

// for (var i = 0; i < Math.ceil(proceedFiles.length / atOnce); i++) {
// await Promise.all(
// proceedFiles
// .slice(atOnce * i, atOnce * (i + 1))
// .map((file) => _proceedFile(file))
// );
// }
// }

/** @returns {Promise<void>} */
const proceedUpload = async () => {
let pool = 20;

const proceedFiles = files.filter(({ status }) => status !== "a");

while (true) {
if (pool === 0) {
await sleep(100);
continue;
}

let file = proceedFiles.pop();

if (!file) break;

pool -= 1;
_proceedFile(file).finally(() => (pool += 1));
}
}
};

return { files, setFiles, proceedUpload };
}
11 changes: 9 additions & 2 deletions frontend-app/src/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,10 +169,17 @@ export function traverseWithReplace(
* @param {number} parentOrder
* @returns {void}
*/
export function drawPaths(items, parentOrder=0) {
export const drawPaths = (items, parentOrder=0) => {
items.forEach((item, index) => {
var { children } = item;
item.order = (parentOrder * 10) + (index + 1);
if (children) drawPaths(item.children, item.order);
});
}
};


/**
* @param {number} ms
* @returns {Promise<void>}
*/
export const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
17 changes: 17 additions & 0 deletions frontend-app/src/utils/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
formUID,
traverseWithReplace,
drawPaths,
sleep
} from ".";
import {
object_with_inner_list,
Expand Down Expand Up @@ -446,3 +447,19 @@ test("draw path test", () => {
},
]);
});

test("sleep util test", async () => {
let val = 0;
sleep(100).then(() => {val += 1; expect(val).toBe(2);});
expect(val).toBe(0);
val += 1;
expect(val).toBe(1);
});

test("sleep util test", async () => {
let val = 0;
await sleep(100).then(() => val = 1);
expect(val).toBe(1);
val = 2;
expect(val).toBe(2);
});

0 comments on commit 8cc8cc1

Please sign in to comment.