Skip to content

Commit c23e1b8

Browse files
author
Vladimir Kotal
committed
add API to get last index time
fixes #3423
1 parent 5d26fc6 commit c23e1b8

File tree

3 files changed

+61
-0
lines changed

3 files changed

+61
-0
lines changed

apiary.apib

+11
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,17 @@ Honors the Accept header. The text type works for plain text files only.
181181

182182
+ Response 204
183183

184+
## Get last index time [/system/indextime]
185+
186+
The time is in the ISO 8601 format in UTC time zone.
187+
188+
### Retrieve last index time [GET]
189+
190+
+ Response 200 (application/json)
191+
+ Body
192+
193+
"2021-02-15T16:39:16.409+00:00"
194+
184195
## Index searchers refresh [/system/refresh]
185196

186197
### refreshes index searchers for specified project [PUT]

opengrok-web/src/main/java/org/opengrok/web/api/v1/controller/SystemController.java

+22
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@
2222
*/
2323
package org.opengrok.web.api.v1.controller;
2424

25+
import com.fasterxml.jackson.core.JsonProcessingException;
26+
import com.fasterxml.jackson.databind.ObjectMapper;
27+
import com.fasterxml.jackson.databind.SerializationFeature;
28+
import com.fasterxml.jackson.databind.util.StdDateFormat;
2529
import org.opengrok.indexer.configuration.RuntimeEnvironment;
2630
import org.opengrok.indexer.web.EftarFile;
2731
import org.opengrok.indexer.logger.LoggerFactory;
@@ -31,12 +35,17 @@
3135
import javax.inject.Inject;
3236
import javax.validation.Valid;
3337
import javax.ws.rs.Consumes;
38+
import javax.ws.rs.GET;
3439
import javax.ws.rs.POST;
3540
import javax.ws.rs.PUT;
3641
import javax.ws.rs.Path;
42+
import javax.ws.rs.Produces;
3743
import javax.ws.rs.core.MediaType;
44+
import java.io.File;
3845
import java.io.IOException;
46+
import java.nio.file.Paths;
3947
import java.util.Collections;
48+
import java.util.Date;
4049
import java.util.Set;
4150
import java.util.concurrent.CompletableFuture;
4251
import java.util.logging.Level;
@@ -74,4 +83,17 @@ public void loadPathDescriptions(@Valid final PathDescription[] descriptions) th
7483
ef.create(Set.of(descriptions), env.getDtagsEftarPath().toString());
7584
LOGGER.log(Level.INFO, "reloaded path descriptions with {0} entries", descriptions.length);
7685
}
86+
87+
@GET
88+
@Path("/indextime")
89+
@Produces(MediaType.APPLICATION_JSON)
90+
public String getIndexTime() throws JsonProcessingException {
91+
File indexTimeFile = Paths.get(env.getDataRootFile().toString(), "timestamp").toFile();
92+
Date date = new Date(indexTimeFile.lastModified());
93+
ObjectMapper mapper = new ObjectMapper();
94+
// StdDateFormat is ISO8601 since jackson 2.9
95+
mapper.setDateFormat(new StdDateFormat().withColonInTimeZone(true));
96+
String result = mapper.writeValueAsString(date);
97+
return result;
98+
}
7799
}

opengrok-web/src/test/java/org/opengrok/web/api/v1/controller/SystemControllerTest.java

+28
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,15 @@
4040
import java.nio.file.Files;
4141
import java.nio.file.Path;
4242
import java.nio.file.Paths;
43+
import java.text.ParseException;
44+
import java.text.SimpleDateFormat;
45+
import java.util.Calendar;
46+
import java.util.Date;
4347

48+
import static org.hamcrest.Matchers.containsString;
4449
import static org.junit.Assert.assertEquals;
4550
import static org.junit.Assert.assertNotEquals;
51+
import static org.junit.Assert.assertThat;
4652
import static org.junit.Assert.assertTrue;
4753

4854
public class SystemControllerTest extends OGKJerseyTest {
@@ -130,4 +136,26 @@ public void testDtagsEftarReload() throws IOException {
130136
// Cleanup
131137
IOUtils.removeRecursive(dataRoot);
132138
}
139+
140+
@Test
141+
public void testIndexTime() throws IOException, ParseException {
142+
Path dataRoot = Files.createTempDirectory("indexTimetest");
143+
env.setDataRoot(dataRoot.toString());
144+
File indexTimeFile = Paths.get(dataRoot.toString(), "timestamp").toFile();
145+
indexTimeFile.createNewFile();
146+
assertTrue(indexTimeFile.exists());
147+
SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd_hh:mm:ss+ZZ");
148+
Date date = f.parse("2021-02-16_11:18:01+UTC");
149+
indexTimeFile.setLastModified(date.getTime());
150+
151+
Response r = target("system")
152+
.path("indextime")
153+
.request().get();
154+
String result = r.readEntity(String.class);
155+
156+
assertThat(result, containsString("2021-02-16T11:18:01.000+00:00"));
157+
158+
// Cleanup
159+
IOUtils.removeRecursive(dataRoot);
160+
}
133161
}

0 commit comments

Comments
 (0)