From 5d4e9b50b9f6164b8580b5e07db91461c564255d Mon Sep 17 00:00:00 2001 From: Vladimir Kotal Date: Tue, 16 Feb 2021 11:31:38 +0100 Subject: [PATCH] add API to get last index time fixes #3423 --- apiary.apib | 11 ++++++++ .../api/v1/controller/SystemController.java | 22 +++++++++++++++ .../v1/controller/SystemControllerTest.java | 28 +++++++++++++++++++ 3 files changed, 61 insertions(+) diff --git a/apiary.apib b/apiary.apib index d298b69b6a9..fb86a2d1591 100644 --- a/apiary.apib +++ b/apiary.apib @@ -181,6 +181,17 @@ Honors the Accept header. The text type works for plain text files only. + Response 204 +## Get last index time [/system/indextime] + +The time is in the ISO 8601 format in UTC time zone. + +### Retrieve last index time [GET] + ++ Response 200 (application/json) + + Body + + "2021-02-15T16:39:16.409+00:00" + ## Index searchers refresh [/system/refresh] ### refreshes index searchers for specified project [PUT] diff --git a/opengrok-web/src/main/java/org/opengrok/web/api/v1/controller/SystemController.java b/opengrok-web/src/main/java/org/opengrok/web/api/v1/controller/SystemController.java index 6994ae4043c..e2113765a47 100644 --- a/opengrok-web/src/main/java/org/opengrok/web/api/v1/controller/SystemController.java +++ b/opengrok-web/src/main/java/org/opengrok/web/api/v1/controller/SystemController.java @@ -22,6 +22,10 @@ */ package org.opengrok.web.api.v1.controller; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.SerializationFeature; +import com.fasterxml.jackson.databind.util.StdDateFormat; import org.opengrok.indexer.configuration.RuntimeEnvironment; import org.opengrok.indexer.web.EftarFile; import org.opengrok.indexer.logger.LoggerFactory; @@ -31,12 +35,17 @@ import javax.inject.Inject; import javax.validation.Valid; import javax.ws.rs.Consumes; +import javax.ws.rs.GET; import javax.ws.rs.POST; import javax.ws.rs.PUT; import javax.ws.rs.Path; +import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; +import java.io.File; import java.io.IOException; +import java.nio.file.Paths; import java.util.Collections; +import java.util.Date; import java.util.Set; import java.util.concurrent.CompletableFuture; import java.util.logging.Level; @@ -74,4 +83,17 @@ public void loadPathDescriptions(@Valid final PathDescription[] descriptions) th ef.create(Set.of(descriptions), env.getDtagsEftarPath().toString()); LOGGER.log(Level.INFO, "reloaded path descriptions with {0} entries", descriptions.length); } + + @GET + @Path("/indextime") + @Produces(MediaType.APPLICATION_JSON) + public String getIndexTime() throws JsonProcessingException { + File indexTimeFile = Paths.get(env.getDataRootFile().toString(), "timestamp").toFile(); + Date date = new Date(indexTimeFile.lastModified()); + ObjectMapper mapper = new ObjectMapper(); + // StdDateFormat is ISO8601 since jackson 2.9 + mapper.setDateFormat(new StdDateFormat().withColonInTimeZone(true)); + String result = mapper.writeValueAsString(date); + return result; + } } diff --git a/opengrok-web/src/test/java/org/opengrok/web/api/v1/controller/SystemControllerTest.java b/opengrok-web/src/test/java/org/opengrok/web/api/v1/controller/SystemControllerTest.java index d4f9b6b2528..1a5ce92491d 100644 --- a/opengrok-web/src/test/java/org/opengrok/web/api/v1/controller/SystemControllerTest.java +++ b/opengrok-web/src/test/java/org/opengrok/web/api/v1/controller/SystemControllerTest.java @@ -40,9 +40,15 @@ import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Calendar; +import java.util.Date; +import static org.hamcrest.Matchers.containsString; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; public class SystemControllerTest extends OGKJerseyTest { @@ -130,4 +136,26 @@ public void testDtagsEftarReload() throws IOException { // Cleanup IOUtils.removeRecursive(dataRoot); } + + @Test + public void testIndexTime() throws IOException, ParseException { + Path dataRoot = Files.createTempDirectory("indexTimetest"); + env.setDataRoot(dataRoot.toString()); + File indexTimeFile = Paths.get(dataRoot.toString(), "timestamp").toFile(); + indexTimeFile.createNewFile(); + assertTrue(indexTimeFile.exists()); + SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd_hh:mm:ss+ZZ"); + Date date = f.parse("2021-02-16_11:18:01+UTC"); + indexTimeFile.setLastModified(date.getTime()); + + Response r = target("system") + .path("indextime") + .request().get(); + String result = r.readEntity(String.class); + + assertThat(result, containsString("2021-02-16T11:18:01.000+00:00")); + + // Cleanup + IOUtils.removeRecursive(dataRoot); + } }