-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathanalyzer.js
More file actions
120 lines (101 loc) · 3.48 KB
/
analyzer.js
File metadata and controls
120 lines (101 loc) · 3.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import { getMockResponse, isMockingModeEnabled } from '../../__mocks__/webviewer-redaction-ai.mock.js';
// Send the loaded document text to the server, to be
// analyzed for personal information identification (PII)
const sendTextToServer = async () => {
// *********************************************
// MOCKING MODE: Skip actual server call and
// return mock document id
if (isMockingModeEnabled())
return getMockResponse('documentId');
// *********************************************
try {
const response = await fetch('/api/send-text', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
documentText: loadedDocument.text,
timestamp: new Date().toISOString()
}),
});
if (!response.ok)
throw new Error(`Server error: ${response.status} ${response.statusText}`);
return await response.json();
} catch (error) {
console.error('Error sending document text to server:', error);
throw error;
}
}
// Analyze the loaded document text for personal information identification (PII)
const analyzeDocument = async (documentId) => {
// *********************************************
// MOCKING MODE: Skip actual server call
if (isMockingModeEnabled())
return;
// *********************************************
try {
const response = await fetch('/api/analyze-pii', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
documentId: documentId,
timestamp: new Date().toISOString()
}),
});
if (!response.ok)
throw new Error(`Server error: ${response.status} ${response.statusText}`);
return await response.json();
} catch (error) {
console.error('Error analyzing document for PII:', error);
throw error;
}
}
// Receive analysis result from the server
const getAnalysisResult = async (documentId) => {
// *********************************************
// MOCKING MODE: Return mock analysis result
if (isMockingModeEnabled())
return getMockResponse('analysis');
// *********************************************
try {
const response = await fetch(`/api/get-results/${documentId}`, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
},
});
if (!response.ok)
throw new Error(`Server error: ${response.status} ${response.statusText}`);
return await response.json();
} catch (error) {
console.error('Error getting analysis result:', error);
throw error;
}
}
const analyzeDocumentForPII = async () => {
// Show WebViewer loading spinner
WebViewer.getInstance().UI.openElements('loadingModal');
// *********************************************
// MOCKING MODE: Keep spinner visible for
// 2 seconds
if (isMockingModeEnabled())
await new Promise((resolve) => setTimeout(resolve, 2000));
// *********************************************
try {
// Step 1: Send document text to server
const sendResult = await sendTextToServer();
const documentId = sendResult.documentId;
// Step 2: Analyze document for PII
await analyzeDocument(documentId);
// Step 3: Get analysis result from server
aiAnalysisResult = await getAnalysisResult(documentId);
} catch (error) {
console.error('Failed to analyze document:', error);
}
// Hide WebViewer loading spinner
WebViewer.getInstance().UI.closeElements('loadingModal');
}
export { analyzeDocumentForPII };