Skip to content

Commit

Permalink
HLM-6225_added time out according to data
Browse files Browse the repository at this point in the history
  • Loading branch information
Bhavya-egov committed Jun 12, 2024
1 parent b3a0b33 commit 45a1c42
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ const UploadData = ({ formData, onSelect, ...props }) => {
const { data: Schemas, isLoading: isThisLoading } = Digit.Hooks.useCustomMDMS(tenantId, "HCM-ADMIN-CONSOLE", [{ name: "adminSchema" }]);

const { data: readMe } = Digit.Hooks.useCustomMDMS(tenantId, "HCM-ADMIN-CONSOLE", [{ name: "ReadMeConfig" }]);
const { data: baseTimeOut } = Digit.Hooks.useCustomMDMS(tenantId, "HCM-ADMIN-CONSOLE", [{ name: "baseTimeOut" }]);
const [sheetHeaders, setSheetHeaders] = useState({});
const [translatedSchema, setTranslatedSchema] = useState({});
const [readMeInfo, setReadMeInfo] = useState({});
Expand Down Expand Up @@ -711,13 +712,14 @@ const UploadData = ({ formData, onSelect, ...props }) => {
setIsError(true);

try {
const temp = await Digit.Hooks.campaign.useResourceData(uploadedFile, params?.hierarchyType, type, tenantId, id);
const temp = await Digit.Hooks.campaign.useResourceData(uploadedFile, params?.hierarchyType, type, tenantId, id , baseTimeOut?.["HCM-ADMIN-CONSOLE"]?.baseTimeOut?.[0]?.baseTimeOut);
if (temp?.isError) {
setIsValidation(false);
const errorMessage = temp?.error.replaceAll(":", "-");
setShowToast({ key: "error", label: errorMessage, transitionTime: 5000000 });
setIsError(true);
setApiError(errorMessage);
setIsValidation(false);

return;
}
if (temp?.status === "completed") {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const baseTimeOut =
{
baseTimeOut: 100
};

Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
export const useResourceData = async (data, hierarchyType, type, tenantId, id) => {
import axios from 'axios';
import * as XLSX from 'xlsx';
import { baseTimeOut } from '../configs/baseTimeOut';
export const useResourceData = async (data, hierarchyType, type, tenantId, id , baseTimeOut) => {


let Type;
let jsonDataLength;
let Error = {
isError: false,
error: {},
Expand All @@ -13,6 +19,29 @@ export const useResourceData = async (data, hierarchyType, type, tenantId, id) =
Type = "boundaryWithTarget";
}
try {
if(data){
axios
.get("/filestore/v1/files/id", {
responseType: "arraybuffer",
headers: {
"Content-Type": "application/json",
Accept: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
"auth-token": Digit.UserService.getUser()?.["access_token"],
},
params: {
tenantId: Digit.ULBService.getCurrentTenantId(),
fileStoreId: data?.[0]?.filestoreId,
},
})
.then(async (res) => {
const fileData = res.data;
const workbook = XLSX.read(fileData, { type: 'buffer' });
const sheetName = workbook.SheetNames[1];
const worksheet = workbook.Sheets[sheetName];
const jsonData = XLSX.utils.sheet_to_json(worksheet);
jsonDataLength = jsonData.length;
});
}
const responseTemp = await Digit.CustomService.getResponse({
url: "/project-factory/v1/data/_create",
body: {
Expand All @@ -27,7 +56,9 @@ export const useResourceData = async (data, hierarchyType, type, tenantId, id) =
},
},
});

response = responseTemp;

} catch (error) {
if (error?.response && error?.response?.data) {
const errorMessage = error?.response?.data?.Errors?.[0]?.message;
Expand All @@ -46,6 +77,10 @@ export const useResourceData = async (data, hierarchyType, type, tenantId, id) =

let searchResponse;
let status = "validation-started";
const baseDelay = baseTimeOut;
let retryInterval = baseDelay * jsonDataLength;

await new Promise((resolve) => setTimeout(resolve, retryInterval));

// Retry until a response is received
while (status !== "failed" && status !== "invalid" && status !== "completed") {
Expand All @@ -61,7 +96,8 @@ export const useResourceData = async (data, hierarchyType, type, tenantId, id) =
});
status = searchResponse?.ResourceDetails?.[0]?.status;
if (status !== "failed" && status !== "invalid" && status !== "completed") {
await new Promise((resolve) => setTimeout(resolve, 5000));
retryInterval *= 2;
await new Promise((resolve) => setTimeout(resolve, retryInterval));
}
}
if (Error.isError) {
Expand Down

0 comments on commit 45a1c42

Please sign in to comment.