Skip to content

Commit

Permalink
added get ordered nuggets endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
cophilot committed Feb 8, 2024
1 parent 00b6172 commit 2570fbd
Show file tree
Hide file tree
Showing 10 changed files with 222 additions and 54 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
"prepare": "husky install",
"generate": "python scripts/generate.py",
"g": "python scripts/generate.py",
"todo": "python scripts/todo.py",
"comment:check": "python scripts/check_comment.py",
"backend:check": "python scripts/check_backend.py",
"backend:create": "npm run backend:create:tables",
Expand Down
17 changes: 7 additions & 10 deletions src/components/DocbaseViewer/DocbaseViewer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,15 @@ function DocbaseViewer({ docBase, onClose }: Props) {
onClose();
}}
></AttributeAdder>
{/* <ul className="ver">
{docBase.attributes.map((attribute, index) => {
return (
<li key={index}>
<b>{attribute}</b>
</li>
);
})}
</ul> */}
<h2>Documents</h2>
{docBase.docs.map((doc, index) => {
return <NuggetDocumentViewer key={index} doc={doc} />;
return (
<NuggetDocumentViewer
key={index}
doc={doc}
docBase={docBase}
/>
);
})}
<button className="btn mb ml mt" onClick={onClose}>
Close
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ function InteractiveDocBaseViewer({ docBase }: Props) {
key={index}
doc={doc}
interactive={true}
docBase={docBase}
/>
);
})}
Expand Down
4 changes: 2 additions & 2 deletions src/components/NuggetText/NuggetText.scss
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
}

.highlighted-unselected {
background-color: rgba(255, 255, 0, 0.4);
background-color: rgba(255, 255, 0, 0.2);
color: black;
border-radius: 5px;
}
Expand All @@ -14,7 +14,7 @@
overflow-x: scroll;
}
.nugget {
background-color: rgba(255, 255, 0, 0.4);
background-color: rgba(255, 255, 0, 0.2);
color: black;
border-radius: 5px;
padding: 5px;
Expand Down
11 changes: 7 additions & 4 deletions src/components/NuggetText/NuggetText.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import NuggetDocument from '../../types/NuggetDocument';
import './NuggetText.scss';
import Nugget from '../../types/Nugget';
import Icon from '../Icon/Icon';
import Logger from '../../utils/Logger';

interface Props {
doc: NuggetDocument;
Expand Down Expand Up @@ -67,27 +68,29 @@ function NuggetText({ doc, interactive = false }: Props) {
color: 'black',
fontSize: '1.2rem',
marginRight: '5px',
paddingRight: '10px',
}}
cls="bi bi-hand-thumbs-up icon ml"
onClicked={() => {
console.log('Confirm Nugget');
Logger.log('Confirm Nugget');
alert('Not implemented');
}}
>
Confirm Nugget
</Icon>
<Icon
{/* <Icon
style={{
color: 'black',
fontSize: '1.2rem',
paddingRight: '10px',
}}
cls="bi bi-hand-thumbs-down icon"
onClicked={() => {
console.log('Decline Nugget');
Logger.log('Decline Nugget');
}}
>
Decline Nugget
</Icon>
</Icon> */}
</>
)}
</span>
Expand Down
27 changes: 26 additions & 1 deletion src/components/NuggetViewer/NuggetDocumentViewer.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import NuggetDocument from '../../types/NuggetDocument';
import Icon from '../Icon/Icon';
import NuggetText from '../NuggetText/NuggetText';
import './NuggetDocumentViewer.scss';
import { useGetOrderedNuggets } from '../../providers/DocBaseTaskProvider';
import DocBase from '../../types/DocBase';

interface Props {
docBase: DocBase;
doc: NuggetDocument;
interactive?: boolean;
}
Expand All @@ -11,13 +15,34 @@ interface Props {
* This displays a NuggetDocument
* @param doc The NuggetDocument to display
*/
function NuggetDocumentViewer({ doc, interactive = false }: Props) {
function NuggetDocumentViewer({ docBase, doc, interactive = false }: Props) {
const getOrderedNuggets = useGetOrderedNuggets();

return (
<div className="ver">
<p>
<b>
Document: <i>{doc.name}</i>
</b>
{!interactive && (
<Icon
cls="bi bi-info-circle ml"
style={{
//TODO
display: 'none',
}}
onClicked={() => {
getOrderedNuggets(
docBase.organizationId,
docBase.name,
doc.name,
doc.content
);
}}
>
Get more information about this document
</Icon>
)}
</p>
<NuggetText doc={doc} interactive={interactive} />
</div>
Expand Down
147 changes: 140 additions & 7 deletions src/providers/DocBaseTaskProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ const DocBaseTaskContext = React.createContext({
_organizationId: number,
_baseName: string
) => {},
getOrderedNuggets: (
_organizationId: number,
_baseName: string,
_documentName: string,
_documentContent: string
) => {},
});

// eslint-disable-next-line react-refresh/only-export-components
Expand Down Expand Up @@ -77,6 +83,7 @@ export function useLoadDocbaseTask() {
}
return context.loadDocbaseTask;
}

// eslint-disable-next-line react-refresh/only-export-components
export function useStartInteractiveTablePopulation() {
const context = React.useContext(DocBaseTaskContext);
Expand All @@ -88,6 +95,17 @@ export function useStartInteractiveTablePopulation() {
return context.startInteractiveTablePopulation;
}

// eslint-disable-next-line react-refresh/only-export-components
export function useGetOrderedNuggets() {
const context = React.useContext(DocBaseTaskContext);
if (!context) {
throw new Error(
'useGetOrderedNuggets must be used within a DocBaseTaskProvider'
);
}
return context.getOrderedNuggets;
}

interface Props {
children: ReactNode;
}
Expand All @@ -111,7 +129,7 @@ export function DocBaseTaskProvider({ children }: Props) {
);

useEffect(() => {
console.log('DocBaseTaskProvider mounted');
Logger.log('DocBaseTaskProvider mounted');
const type = sessionStorage.getItem('docbasetask-type');
const taskId = sessionStorage.getItem('docbaseId');
if (
Expand Down Expand Up @@ -436,15 +454,15 @@ export function DocBaseTaskProvider({ children }: Props) {
if (res.state === 'SUCCESS') {
setLoadingScreenLock(false);
setLoadingScreen(false);
console.log(res);
Logger.log(res);

const docBase = new DocBase(
baseName,
organizationId,
res.meta.document_base_to_ui.msg.attributes ?? []
);
try {
console.log(res.meta.document_base_to_ui.msg.nuggets);
Logger.log(res.meta.document_base_to_ui.msg.nuggets);
for (const nugget of res.meta.document_base_to_ui.msg
.nuggets) {
docBase.addNugget(
Expand Down Expand Up @@ -520,7 +538,7 @@ export function DocBaseTaskProvider({ children }: Props) {
return;
}

Logger.log('Task: Load Docbase ' + baseName);
Logger.log('Task: Start interactive table population ' + baseName);
Logger.log('Task ID: ' + taskId);

sessionStorage.setItem(
Expand Down Expand Up @@ -572,7 +590,7 @@ export function DocBaseTaskProvider({ children }: Props) {
) {
setLoadingScreenLock(false);
setLoadingScreen(false);
console.log(res);
Logger.log(res);

const att = res.meta.feedback_request_to_ui.attribute.name;
let attList: string[] = [];
Expand All @@ -586,7 +604,7 @@ export function DocBaseTaskProvider({ children }: Props) {
attList
);
try {
console.log(res.meta.document_base_to_ui.msg.nuggets);
Logger.log(res.meta.document_base_to_ui.msg.nuggets);
for (const nugget of res.meta.document_base_to_ui.msg
.nuggets) {
docBase.addNugget(
Expand Down Expand Up @@ -624,7 +642,7 @@ export function DocBaseTaskProvider({ children }: Props) {
// update loading screen
setLoadingScreen(
true,
'Loading Docbase ' + baseName + '...',
'Start Interactive Table Population...',
info,
taskId,
true
Expand All @@ -636,6 +654,120 @@ export function DocBaseTaskProvider({ children }: Props) {
const updateInterval = setInterval(updateBody, intervalTime);
};

const getOrderedNuggets = async (
organizationId: number,
baseName: string,
documentName: string,
documentContent: string
) => {
if (isRunning) {
Logger.warn(
'Docbase task is already running, cannot start another'
);
return;
}

const taskId = await APIService.getOrderedNuggets(
organizationId,
baseName,
documentName,
documentContent
);

if (taskId == undefined) {
showNotification(
'Error',
'Could not get ordered nuggets for ' + documentName
);
return;
}

Logger.log('Task: Get ordered nuggets ' + baseName);
Logger.log('Task ID: ' + taskId);

sessionStorage.setItem('docbaseId', taskId);
/* setLoadingScreen(
true,
'Loading Docbase ' + baseName + '...',
'Please wait...',
taskId
); */

//setLoadingScreenLock(true);
setIsRunning(true);

const updateBody = () => {
if (taskId == undefined) {
return;
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
APIService.getTaskStatus(taskId).then((res): any => {
Logger.log(res);

if (
res == undefined ||
res.state.toUpperCase().trim() === 'FAILURE'
) {
setLoadingScreenLock(false);
setLoadingScreen(false);
playAudio(MyAudio.ERROR);

showNotification(
'Error',
'Something went wrong getting ordered nuggets.'
);
sessionStorage.removeItem('docbaseId');

setIsRunning(false);
clearInterval(updateInterval);
return;
} else if (res.state.toUpperCase().trim() === 'SUCCESS') {
setLoadingScreenLock(false);
setLoadingScreen(false);
Logger.log(res);
Logger.log(JSON.stringify(res));

/* const att = res.meta.feedback_request_to_ui.attribute.name;
let attList: string[] = [];
if (att) {
attList = [att];
}
const docBase = new DocBase(
baseName,
organizationId,
attList
);
try {
Logger.log(res.meta.document_base_to_ui.msg.nuggets);
for (const nugget of res.meta.document_base_to_ui.msg
.nuggets) {
docBase.addNugget(
nugget.document.name,
nugget.document.text,
nugget.start_char,
nugget.end_char
);
}
} catch (error) {
Logger.error(error);
showNotification(
'Error',
'Something went wrong translating the nuggets.'
);
} */
sessionStorage.removeItem('docbaseId');
//setDocBase(docBase);
setIsRunning(false);
clearInterval(updateInterval);
return;
}
});
};
updateBody();
const updateInterval = setInterval(updateBody, intervalTime);
};

const isDocbaseTaskRunning = () => {
return isRunning;
};
Expand All @@ -653,6 +785,7 @@ export function DocBaseTaskProvider({ children }: Props) {
updateDocbaseAttributesTask: updateDocbaseAttributesTask,
startInteractiveTablePopulation:
startInteractiveTablePopulation,
getOrderedNuggets: getOrderedNuggets,
}}
>
{docBase && !useInteractiveViewer && (
Expand Down
Loading

0 comments on commit 2570fbd

Please sign in to comment.