Skip to content

Commit fe5dfe9

Browse files
committed
Add the ability to load the various languages through a user interface
1 parent 121fdfe commit fe5dfe9

File tree

5 files changed

+196
-31
lines changed

5 files changed

+196
-31
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
<groupId>org.exist-db</groupId>
1616
<artifactId>stanford-nlp</artifactId>
17-
<version>0.8.0</version>
17+
<version>0.9.0</version>
1818

1919
<name>Stanford Natural Language Processing</name>
2020
<description>Integrates the Stanford CoreNLP annotation pipeline library into eXist-db.</description>

src/main/js/frontend/package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/main/js/frontend/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "stanford-npl",
3-
"version": "0.8.0",
3+
"version": "0.9.0",
44
"private": true,
55
"homepage": ".",
66
"proxy": "http://localhost:8080/",

src/main/js/frontend/src/LoadingContent.tsx

Lines changed: 149 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,159 @@
11
import 'bootstrap/dist/css/bootstrap.min.css';
2-
import React from "react";
2+
import React, { useState, useEffect } from "react";
33
import './App.css';
4-
import {Button} from "react-bootstrap";
4+
import {Button, Spinner} from "react-bootstrap";
5+
import {Check } from 'react-bootstrap-icons';
56

67
function LoadingContent() {
8+
const [logs, setLogs] = useState([{
9+
timestamp: "",
10+
language: "",
11+
message: ""
12+
}]);
13+
const [running, setRunning] = useState({
14+
"arabic": { "start": null, "end": null, "isRunning": false, isLoaded: false },
15+
"english-kbp": { "start": null, "end": null, "isRunning": false, isLoaded: false },
16+
"english": { "start": null, "end": null, "isRunning": false, isLoaded: false },
17+
"chinese": { "start": null, "end": null, "isRunning": false, isLoaded: false },
18+
"french": { "start": null, "end": null, "isRunning": false, isLoaded: false },
19+
"german": { "start": null, "end": null, "isRunning": false, isLoaded: false },
20+
"spanish": { "start": null, "end": null, "isRunning": false, isLoaded: false }
21+
})
22+
const [last, setLast] = useState(null);
23+
const [counter, setCounter] = useState(0);
24+
25+
useEffect(() => {
26+
const intervalVar = setInterval(fetchLogs, 10000);
27+
28+
return () => clearInterval(intervalVar);
29+
}, [])
30+
31+
function fetchLogs() {
32+
let uri = '/exist/restxq/stanford/nlp/logs';
33+
34+
if (last) {
35+
uri += "?timestamp=" + last;
36+
}
37+
38+
fetch(uri)
39+
.then((response) => response.json())
40+
.then(
41+
(result) => {
42+
if (last) {
43+
setLogs([result.logs, ...logs]);
44+
} else {
45+
setLogs(result.logs);
46+
}
47+
setRunning(result.running);
48+
setLast(result.timestamp);
49+
},
50+
(error) => {
51+
52+
}
53+
)
54+
}
55+
56+
function loadLanguage(theLanguage: string) {
57+
let aRunning = running;
58+
// @ts-ignore
59+
aRunning[theLanguage].isRunning = true;
60+
// @ts-ignore
61+
aRunning[theLanguage].isLoaded = false;
62+
setRunning(aRunning);
63+
setCounter(counter + 1)
64+
65+
fetch("/exist/restxq/stanford/nlp/load/" + theLanguage)
66+
.then((response) => response.json())
67+
.then(
68+
(result) => {
69+
},
70+
(error) => {
71+
72+
}
73+
)
74+
}
75+
776
return (
877
<div className={'LoadingContent'}>
978
<h1>Load</h1>
10-
<Button>Arabic</Button>
11-
<Button>Chinese</Button>
12-
<Button>English</Button>
13-
<Button>English KBP</Button>
14-
<Button>French</Button>
15-
<Button>German</Button>
16-
<Button>Spanish</Button>
79+
<Button onClick={() => loadLanguage('arabic')} disabled={running.arabic.isRunning}>
80+
{
81+
running.arabic.isRunning ?
82+
<Spinner as="span" animation="grow" size="sm" role="status" aria-hidden="true"/>
83+
: running.arabic.isLoaded ?
84+
<Check/>
85+
:null
86+
}
87+
Arabic
88+
</Button>
89+
<Button onClick={() => loadLanguage('chinese')} disabled={running.chinese.isRunning}>{
90+
running.chinese.isRunning ?
91+
<Spinner as="span" animation="grow" size="sm" role="status" aria-hidden="true"/>
92+
: running.chinese.isLoaded ?
93+
<Check/>
94+
:null
95+
}
96+
Chinese</Button>
97+
<Button onClick={() => loadLanguage('english')} disabled={running.english.isRunning}>{
98+
running.english.isRunning ?
99+
<Spinner as="span" animation="grow" size="sm" role="status" aria-hidden="true"/>
100+
: running.english.isLoaded ?
101+
<Check/>
102+
:null
103+
}
104+
English</Button>
105+
<Button onClick={() => loadLanguage('english-kbp')} disabled={running['english-kbp'].isRunning}>{
106+
running['english-kbp'].isRunning ?
107+
<Spinner as="span" animation="grow" size="sm" role="status" aria-hidden="true"/>
108+
: running['english-kbp'].isLoaded ?
109+
<Check/>
110+
:null
111+
}
112+
English KBP</Button>
113+
<Button onClick={() => loadLanguage('french')} disabled={running.french.isRunning}>{
114+
running.french.isRunning ?
115+
<Spinner as="span" animation="grow" size="sm" role="status" aria-hidden="true"/>
116+
: running.french.isLoaded ?
117+
<Check/>
118+
:null
119+
}
120+
French</Button>
121+
<Button onClick={() => loadLanguage('german')} disabled={running.german.isRunning}>{
122+
running.german.isRunning ?
123+
<Spinner as="span" animation="grow" size="sm" role="status" aria-hidden="true"/>
124+
: running.german.isLoaded ?
125+
<Check/>
126+
:null
127+
}
128+
German</Button>
129+
<Button onClick={() => loadLanguage('spanish')} disabled={running.spanish.isRunning}>{
130+
running.spanish.isRunning ?
131+
<Spinner as="span" animation="grow" size="sm" role="status" aria-hidden="true"/>
132+
: running.spanish.isLoaded ?
133+
<Check/>
134+
:null
135+
}
136+
Spanish</Button>
137+
<table>
138+
<thead>
139+
<tr>
140+
<th>Timestamp</th>
141+
<th>Language</th>
142+
<th>Log</th>
143+
</tr>
144+
</thead>
145+
<tbody>{
146+
logs.map((log) => {
147+
return (
148+
<tr>
149+
<td>{log.timestamp}</td>
150+
<td>{log.language}</td>
151+
<td>{log.message}</td>
152+
</tr>
153+
)
154+
})
155+
}</tbody>
156+
</table>
17157
</div>
18158
)
19159
}

src/main/xar-resources/modules/rest-api.xqm

Lines changed: 43 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,13 @@ declare namespace output = "http://www.w3.org/2010/xslt-xquery-serialization";
1010

1111
declare function api:schedule-language($language as xs:string*) as map(*)
1212
{
13+
let $qq := update delete fn:doc("/db/apps/stanford-nlp/data/log.xml")//logs/log[@language = $language]
1314
let $a :=
1415
scheduler:schedule-xquery-periodic-job(
1516
"/db/apps/stanford-nlp/modules/load.xq",
1617
500,
1718
"nlp-load-" || $language || "-" || util:uuid(),
18-
<parameters>
19-
<param name="language" value="{$language}" />
20-
</parameters>,
19+
<parameters><param name="language" value="{$language}" /></parameters>,
2120
1000,
2221
0
2322
)
@@ -86,23 +85,49 @@ $language as xs:string*
8685
declare
8786
%rest:GET
8887
%rest:path("/stanford/nlp/logs")
88+
%rest:query-param("timestamp", "{$timestamp}")
8989
%rest:produces("application/json")
9090
%output:media-type("application/json")
9191
%output:method("json")
92-
function api:logs(
93-
) as array(*)
92+
function api:logs($timestamp as xs:string*) as map(*)
9493
{
95-
array {
96-
for $log in fn:doc("/db/apps/stanford-nlp/data/log.xml")//logs/log
97-
let $timestamp := xs:string($log/@timestamp)
98-
let $language := xs:string($log/@language)
99-
let $text := $log/text()
100-
order by $timestamp ascending
101-
return
102-
map {
103-
'timestamp': $timestamp,
104-
'language': $language,
105-
'message': $text
106-
}
107-
}
94+
let $allLogs := fn:doc("/db/apps/stanford-nlp/data/log.xml")//logs/log
95+
let $logs := if (fn:exists($timestamp))
96+
then $allLogs[@timestamp ge $timestamp]
97+
else $allLogs
98+
let $languages := fn:sort(fn:distinct-values($allLogs/@language/string()))
99+
return
100+
map {
101+
"timestamp": fn:current-dateTime(),
102+
"running": map:merge(
103+
for $language in ('arabic', 'chinese', 'english', 'english-kbp', 'french', 'german', 'spanish')
104+
let $running := $allLogs[@language = $language]
105+
let $start := fn:max($running[. = "start"]/@timestamp/string())
106+
let $end := fn:max($running[. = "end"]/@timestamp/string())
107+
let $isRunning := ((fn:exists($start) and fn:not($end)) or ($end le $start))
108+
return
109+
map {
110+
$language: map {
111+
"start": $start,
112+
"end": if ($isRunning) then () else $end,
113+
"isRunning": $isRunning,
114+
"isLoaded": fn:exists($end)
115+
}
116+
}
117+
),
118+
"logs":
119+
array {
120+
for $log in $logs
121+
let $timestamp := xs:string($log/@timestamp)
122+
let $language := xs:string($log/@language)
123+
let $text := $log/text()
124+
order by $timestamp descending
125+
return
126+
map {
127+
'timestamp': $timestamp,
128+
'language': $language,
129+
'message': $text
130+
}
131+
}
132+
}
108133
};

0 commit comments

Comments
 (0)