Skip to content

Commit

Permalink
Merge branch 'main' into UseHTMLLabelInIDCard
Browse files Browse the repository at this point in the history
  • Loading branch information
TomDijkema authored Oct 30, 2024
2 parents 48fd80a + 2be5075 commit 2e5459c
Show file tree
Hide file tree
Showing 25 changed files with 1,311 additions and 75 deletions.
2 changes: 1 addition & 1 deletion src/api/digitalSpecimen/GetDigitalSpecimen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,6 @@ const GetDigitalSpecimen = async({ handle, version } : { handle: string, version
};

return digitalSpecimen;
}
};

export default GetDigitalSpecimen;
2 changes: 1 addition & 1 deletion src/api/digitalSpecimen/GetDigitalSpecimenAnnotations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const GetDigitalSpecimenAnnotations = async({ handle } : { handle: string }) =>
const data: JSONResultArray = result.data;

/* Set Digital Specimen Annotation items */
data.data.forEach((dataRow) => {
data.data.forEach(dataRow => {
digitalSpecimenAnnotations.push(dataRow.attributes as Annotation);
});
} catch (error: any) {
Expand Down
45 changes: 45 additions & 0 deletions src/api/digitalSpecimen/GetDigitalSpecimenMAS.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/* Import Dependencies */
import axios from 'axios';

/* Import Types */
import { MachineAnnotationService } from 'app/types/MachineAnnotationService';
import { JSONResultArray, Dict } from 'app/Types';

/* Import Exceptions */
import { NotFoundException } from 'app/Exceptions';


/**
* Function for fetching a digital specimen's potential MASs to be run
* @param handle The identifier of the digital specimen
* @returns List of
*/
const GetDigitalSpecimenMASs = async ({ handle }: { handle: string }) => {
const digitalSpecimenMASs: MachineAnnotationService[] = [];

if (handle) {
const endPoint: string = `/digital-specimen/${handle}/mas`;

try {
const result = await axios({
method: 'get',
url: endPoint,
responseType: 'json'
});

/* Get result data from JSON */
const data: JSONResultArray = result.data;

/* Set MASs */
data.data.forEach((dataRow: Dict) => {
digitalSpecimenMASs.push(dataRow.attributes);
});
} catch (error: any) {
throw (NotFoundException('Digital Specimen MASs', error.request.responseURL));
};
};

return digitalSpecimenMASs;
};

export default GetDigitalSpecimenMASs;
57 changes: 57 additions & 0 deletions src/api/digitalSpecimen/GetDigitalSpecimenMASJobRecords.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/* Import Dependencies */
import axios from 'axios';

/* Import Types */
import { JSONResultArray, Dict } from 'app/Types';

/* Import Exceptions */
import { NotFoundException } from 'app/Exceptions';


/**
* Function for fetching a digital specimen's machine annotation service (MAS) job records
* @param handle The identifier of the digital specimen
* @param version The version of the digital specimen
* @returns Object of Digital Specimen
*/
const GetDigitalSpecimenMASJobRecords = async ({ handle, pageSize, pageNumber, state }: { handle: string, pageSize?: number, pageNumber: number, state?: string }) => {
let returnData: {
MASJobRecords: Dict[],
links?: Dict
} = {
MASJobRecords: []
};

if (handle) {
const endPoint: string = `/digital-specimen/${handle.replace(import.meta.env.VITE_DOI_URL, '')}/mjr`;

try {
const result = await axios({
method: 'get',
url: endPoint,
params: {
pageSize: pageSize,
pageNumber: pageNumber ?? 1,
...(state && { state })
},
responseType: 'json'
});

/* Get result data from JSON */
const data: JSONResultArray = result.data;

data.data.forEach(dataFragment => {
returnData.MASJobRecords.push(dataFragment.attributes);
});

/* Set return data */
returnData.links = data.links;
} catch (error: any) {
console.error(NotFoundException('Digital Specimen MAS Job Records', error.request.responseURL));
};
};

return returnData;
};

export default GetDigitalSpecimenMASJobRecords;
66 changes: 66 additions & 0 deletions src/api/digitalSpecimen/ScheduleDigitalSpecimenMAS.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/* Import Dependencies */
import axios from 'axios';
import KeycloakService from 'app/Keycloak';

/* Import Exceptions */
import { PostException } from 'app/Exceptions';

/* Import Types */
import { JSONResultArray, Dict } from 'app/Types';


/**
* Function for posting a scheduling job for scheduling MASs for the digital specimen
* @param digitalSpecimenId The identifier of the digital specimen to schedule MASs for
* @param masList A list of MASs to be scheduled
* @returns Array of scheduled MASs
*/
const ScheduleDigitalSpecimenMAS = async ({ digitalSpecimenId, masList }: { digitalSpecimenId: string, masList: { masId: string }[] }) => {
let digitalSpecimenMAS: Dict = {};

if (digitalSpecimenId) {
const token = KeycloakService.GetToken();

const masRecord: {
data: {
type: 'MasRequest',
attributes: {
mass: {
masId: string
}[]
}
}
} = {
data: {
type: 'MasRequest',
attributes: {
mass: masList
}
}
};

try {
const result = await axios({
method: 'post',
url: `digital-specimen/${digitalSpecimenId.replace(import.meta.env.VITE_DOI_URL, '')}/mas`,
responseType: 'json',
data: masRecord,
headers: {
'Content-type': 'application/json',
'Authorization': `Bearer ${token}`
}
});

/* Set Specimen MAS */
const data: JSONResultArray = result.data;

digitalSpecimenMAS = data.data[0].attributes;
} catch (error: any) {
throw PostException('Machine Annotation Services', error.request.responseURL);
};
};

return digitalSpecimenMAS;
};

export default ScheduleDigitalSpecimenMAS;
5 changes: 4 additions & 1 deletion src/app/GenerateTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ const typesDict = {
digitalSpecimen: 'https://schemas.dissco.tech/schemas/fdo-type/digital-specimen/0.3.0/digital-specimen.json',
annotationTarget: 'https://schemas.dissco.tech/schemas/fdo-type/annotation/0.3.0/annotation-target.json',
annotationBody: 'https://schemas.dissco.tech/schemas/fdo-type/annotation/0.3.0/annotation-body.json',
annotation: 'https://schemas.dissco.tech/schemas/fdo-type/annotation/0.3.0/annotation.json'
annotation: 'https://schemas.dissco.tech/schemas/fdo-type/annotation/0.3.0/annotation.json',
environmentalVariable: 'https://schemas.dissco.tech/schemas/fdo-type/shared-model/0.3.0/environmental-variable.json',
secretVariable: 'https://schemas.dissco.tech/schemas/fdo-type/shared-model/0.3.0/secret-variable.json',
machineAnnotationService: 'https://schemas.dissco.tech/schemas/fdo-type/machine-annotation-service/0.3.0/machine-annotation-service.json'
};

/**
Expand Down
97 changes: 51 additions & 46 deletions src/app/Hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,8 +242,8 @@ const useNotification = () => {
* Paginator Hook for handling pagination with fetch requests and page numbers
* @returns Instance of hook
*/
const usePagination = ({ pageSize, resultKey, allowSearchParams = false, Method, Handler }:
{ pageSize: number, resultKey?: string, allowSearchParams?: boolean, Method: Function, Handler?: Function }
const usePagination = ({ pageSize, resultKey, params, allowSearchParams = false, triggers, Method, Handler }:
{ pageSize: number, resultKey?: string, params?: Dict, allowSearchParams?: boolean, triggers?: any[], Method: Function, Handler?: Function }
) => {
/* Hooks */
const [searchParams] = useSearchParams();
Expand Down Expand Up @@ -313,59 +313,63 @@ const usePagination = ({ pageSize, resultKey, allowSearchParams = false, Method,
setPageNumber(lastPage);
};

/* UseEffect to watch the page number, if changed, trigger the given method */
useEffect(() => {
if (pageNumber) {
/* Set Loading to true */
setLoading(true);
const Fetch = () => {
/* Set Loading to true */
setLoading(true);

/* Fetch data */
(async () => {
try {
const result = await Method({ pageNumber: pageNumber, pageSize, ...(allowSearchParams && { searchFilters: searchFilters.GetSearchFilters() }) });
/* Fetch data */
(async () => {
try {
const result = await Method({ ...params, pageNumber: pageNumber, pageSize, ...(allowSearchParams && { searchFilters: searchFilters.GetSearchFilters() }) });

/* Set return data */
const records = resultKey ? result[resultKey] : result[Object.keys(result)[0]];
/* Set return data */
const records = resultKey ? result[resultKey] : result[Object.keys(result)[0]];

setReturnData({
records,
links: result?.links,
metadata: result?.metadata
});
setReturnData({
records,
links: result?.links,
metadata: result?.metadata
});

/* Calculate last page*/
if (!isEmpty(result.metadata)) {
let lastPage = result.metadata.totalRecords && Math.ceil(result.metadata.totalRecords / 25);
/* Calculate last page*/
if (!isEmpty(result.metadata)) {
let lastPage = result.metadata.totalRecords && Math.ceil(result.metadata.totalRecords / 25);

/* If last page is greater than 400, set to 400 to prevent indexing errors */
if (lastPage > 399) {
lastPage = 399;
};

setLastPage(lastPage);
/* If last page is greater than 400, set to 400 to prevent indexing errors */
if (lastPage > 399) {
lastPage = 399;
};

/* Return records to handler */
Handler?.(records);
} catch (error) {
/* Set return data */
setReturnData({
records: [],
links: {},
metadata: {
totalRecords: 0
}
});

console.error(error);
} finally {
setLoading(false);
setLastPage(lastPage);
};
})();

/* Return records to handler */
Handler?.(records);
} catch (error) {
/* Set return data */
setReturnData({
records: [],
links: {},
metadata: {
totalRecords: 0
}
});

console.error(error);
} finally {
setLoading(false);
};
})();
};

/* UseEffect to watch the page number, if changed, trigger the given method */
useEffect(() => {
if (pageNumber) {
Fetch();
} else {
setPageNumber(1);
};
}, [pageNumber]);
}, [pageNumber, ...(triggers ?? [])]);

/* UseEffect to watch the search parameters if allowed, if so and on change, reset the page number to 1 */
useEffect(() => {
Expand All @@ -382,9 +386,10 @@ const usePagination = ({ pageSize, resultKey, allowSearchParams = false, Method,
currentPage: pageNumber,
lastPage,
loading,
Refresh: Fetch,
GoToPage,
...(('next' in returnData.links && pageNumber !== 399) && { Next }),
...('prev' in returnData.links && { Previous }),
...((returnData.links && 'next' in returnData.links && pageNumber !== 399) && { Next }),
...(returnData.links && 'prev' in returnData.links && { Previous }),
...((lastPage !== pageNumber && lastPage <= 399) && { Last })
};
};
Expand Down
24 changes: 21 additions & 3 deletions src/app/Types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type DataFragment = {
digitalMediaObject: DigitalMediaType,
annotations: Annotation[]
}[],
annotations?: Annotation[]
annotations?: Annotation[],
[property: string]: any
}
}
Expand All @@ -38,7 +38,7 @@ export type JSONResult = {
};

export type JSONResultArray = {
data: DataFragment[],
data: DataFragment[] | Dict[],
links: {
self: string,
first?: string,
Expand Down Expand Up @@ -86,7 +86,10 @@ export type PaginationObject = {
Next?: Function,
Previous?: Function,
Last?: Function
}
};

/* Super class (annotation target) */
export type SuperClass = DigitalSpecimen | DigitalMedia | Dict;

/* Super class (annotation target) */
export type SuperClass = DigitalSpecimen | DigitalMedia | Dict;
Expand Down Expand Up @@ -133,6 +136,21 @@ export type AnnotationTemplate = {
}
};

/* Machine Job Record */
export type MASJobRecord = {
annotations: Dict,
batchingRequested: boolean,
jobHandle: string,
masId: string,
orcid: string,
state: 'SCHEDULED' | 'RUNNING' | 'FAILED' | 'COMPLETED',
targetId: string,
targetType: string,
timeCompleted: string,
timeStarted: string,
timeToLive: number
};

/* Parent class */
export type ParentClass = {
jsonPath: string,
Expand Down
Loading

0 comments on commit 2e5459c

Please sign in to comment.