From f979832286bfe45aae877eb78b69128fe066b95b Mon Sep 17 00:00:00 2001
From: aboeckle <alexander.boeckle@frentix.com>
Date: Wed, 15 Jan 2020 08:42:25 +0100
Subject: [PATCH] OO-4463 Rest endpoint for sum of revisions size

---
 .../restapi/system/MonitoringWebService.java  |  7 ++
 .../restapi/system/VFSStatsWebService.java    | 68 +++++++++++++++++++
 .../olat/restapi/system/vo/VFSStatsVO.java    | 61 +++++++++++++++++
 .../java/org/olat/restapi/SystemTest.java     | 16 ++++-
 4 files changed, 151 insertions(+), 1 deletion(-)
 create mode 100644 src/main/java/org/olat/restapi/system/VFSStatsWebService.java
 create mode 100644 src/main/java/org/olat/restapi/system/vo/VFSStatsVO.java

diff --git a/src/main/java/org/olat/restapi/system/MonitoringWebService.java b/src/main/java/org/olat/restapi/system/MonitoringWebService.java
index 94deb688ca6..a2e68786554 100644
--- a/src/main/java/org/olat/restapi/system/MonitoringWebService.java
+++ b/src/main/java/org/olat/restapi/system/MonitoringWebService.java
@@ -51,6 +51,7 @@ public class MonitoringWebService {
 	private static final MemoryWebService memoryWebService = new MemoryWebService();
 	private static final ThreadsWebService threadsWebService = new ThreadsWebService();
 	private static final OpenOLATStatisticsWebService ooStatsWebService = new OpenOLATStatisticsWebService();
+	private static final VFSStatsWebService vfsStatsWebService = new VFSStatsWebService();
 	
 	public MonitoringWebService() {
 		//make Spring happy
@@ -86,6 +87,12 @@ public class MonitoringWebService {
 		return threadsWebService;
 	}
 	
+	@Path("revisionsSize")
+	public VFSStatsWebService getRevisionsSizeWS() {
+		return vfsStatsWebService;
+	}
+	
+	
 	
 	/**
 	 * Return the configuration of the monitoring, which probes are available,
diff --git a/src/main/java/org/olat/restapi/system/VFSStatsWebService.java b/src/main/java/org/olat/restapi/system/VFSStatsWebService.java
new file mode 100644
index 00000000000..02721e96162
--- /dev/null
+++ b/src/main/java/org/olat/restapi/system/VFSStatsWebService.java
@@ -0,0 +1,68 @@
+/**
+ * <a href="http://www.openolat.org">
+ * OpenOLAT - Online Learning and Training</a><br>
+ * <p>
+ * Licensed under the Apache License, Version 2.0 (the "License"); <br>
+ * you may not use this file except in compliance with the License.<br>
+ * You may obtain a copy of the License at the
+ * <a href="http://www.apache.org/licenses/LICENSE-2.0">Apache homepage</a>
+ * <p>
+ * Unless required by applicable law or agreed to in writing,<br>
+ * software distributed under the License is distributed on an "AS IS" BASIS, <br>
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. <br>
+ * See the License for the specific language governing permissions and <br>
+ * limitations under the License.
+ * <p>
+ * Initial code contributed and copyrighted by<br>
+ * frentix GmbH, http://www.frentix.com
+ * <p>
+ */
+package org.olat.restapi.system;
+
+import java.util.List;
+
+import javax.ws.rs.GET;
+import javax.ws.rs.Produces;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.Response;
+
+import org.olat.core.CoreSpringFactory;
+import org.olat.core.commons.persistence.DB;
+import org.olat.restapi.system.vo.VFSStatsVO;
+import org.springframework.beans.factory.annotation.Autowired;
+
+/**
+ * 
+ * <h3>Description:</h3>
+ * 
+ * Initial Date:  21 juin 2010 <br>
+ * @author srosse, stephane.rosse@frentix.com, www.frentix.com
+ */
+public class VFSStatsWebService {
+	
+	@Autowired
+	private DB dbInstance;
+	
+	public VFSStatsWebService() {
+		CoreSpringFactory.autowireObject(this);
+	}
+	
+	@GET
+	@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
+	public Response getRevisionSizeXML() {
+		StringBuilder sb = new StringBuilder(256);
+		sb.append("select SUM(size) from vfsrevision");
+		
+		List<Long> revisionsSize = dbInstance.getCurrentEntityManager()
+				.createQuery(sb.toString(), Long.class)
+				.getResultList();
+		
+		Long size = revisionsSize == null || revisionsSize.isEmpty() ? Long.valueOf(0) : revisionsSize.get(0);		
+		
+		dbInstance.commitAndCloseSession();
+		
+		VFSStatsVO vo = new VFSStatsVO(size);
+		
+		return Response.ok(vo).build();
+	}
+}
\ No newline at end of file
diff --git a/src/main/java/org/olat/restapi/system/vo/VFSStatsVO.java b/src/main/java/org/olat/restapi/system/vo/VFSStatsVO.java
new file mode 100644
index 00000000000..fd8f7dae934
--- /dev/null
+++ b/src/main/java/org/olat/restapi/system/vo/VFSStatsVO.java
@@ -0,0 +1,61 @@
+/**
+ * <a href="http://www.openolat.org">
+ * OpenOLAT - Online Learning and Training</a><br>
+ * <p>
+ * Licensed under the Apache License, Version 2.0 (the "License"); <br>
+ * you may not use this file except in compliance with the License.<br>
+ * You may obtain a copy of the License at the
+ * <a href="http://www.apache.org/licenses/LICENSE-2.0">Apache homepage</a>
+ * <p>
+ * Unless required by applicable law or agreed to in writing,<br>
+ * software distributed under the License is distributed on an "AS IS" BASIS, <br>
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. <br>
+ * See the License for the specific language governing permissions and <br>
+ * limitations under the License.
+ * <p>
+ * Initial code contributed and copyrighted by<br>
+ * frentix GmbH, http://www.frentix.com
+ * <p>
+ */
+package org.olat.restapi.system.vo;
+
+import javax.xml.bind.annotation.XmlAccessType;
+import javax.xml.bind.annotation.XmlAccessorType;
+import javax.xml.bind.annotation.XmlAttribute;
+import javax.xml.bind.annotation.XmlRootElement;
+
+
+/**
+ * 
+ * <h3>Description:</h3>
+ * 
+ * 
+ * Initial Date:  21 juin 2010 <br>
+ * @author srosse, stephane.rosse@frentix.com, www.frentix.com
+ */
+@XmlAccessorType(XmlAccessType.FIELD)
+@XmlRootElement(name = "vfsStatsVO")
+public class VFSStatsVO {
+	
+	@XmlAttribute(name="revisionsSize", required=true)
+	private long revisionsSize;
+	
+	public VFSStatsVO() {
+		//make JAXB happy
+	}
+	
+	public VFSStatsVO(long revisionsSize) {
+		this.revisionsSize = revisionsSize;
+	}
+
+	public long getRevisionsSize() {
+		return revisionsSize;
+	}
+
+	@Override
+	public String toString() {
+		StringBuilder sb = new StringBuilder();
+		sb.append("revisionsSize=").append(getRevisionsSize());
+		return sb.toString();
+	}
+}
diff --git a/src/test/java/org/olat/restapi/SystemTest.java b/src/test/java/org/olat/restapi/SystemTest.java
index 60007f5f849..7bb55b463cc 100644
--- a/src/test/java/org/olat/restapi/SystemTest.java
+++ b/src/test/java/org/olat/restapi/SystemTest.java
@@ -36,17 +36,18 @@ import org.olat.restapi.system.vo.MemoryStatisticsVO;
 import org.olat.restapi.system.vo.MemoryVO;
 import org.olat.restapi.system.vo.MonitoringInfosVO;
 import org.olat.restapi.system.vo.OpenOLATStatisticsVO;
-import org.olat.restapi.system.vo.StatusVO;
 import org.olat.restapi.system.vo.ReleaseInfosVO;
 import org.olat.restapi.system.vo.RepositoryStatisticsVO;
 import org.olat.restapi.system.vo.RuntimeStatisticsVO;
 import org.olat.restapi.system.vo.SessionsVO;
+import org.olat.restapi.system.vo.StatusVO;
 import org.olat.restapi.system.vo.TasksVO;
 import org.olat.restapi.system.vo.ThreadStatisticsVO;
 import org.olat.restapi.system.vo.ThreadVO;
 import org.olat.restapi.system.vo.ThreadVOes;
 import org.olat.restapi.system.vo.ThreadsVO;
 import org.olat.restapi.system.vo.UserStatisticsVO;
+import org.olat.restapi.system.vo.VFSStatsVO;
 import org.olat.test.OlatRestTestCase;
 
 /**
@@ -427,4 +428,17 @@ public class SystemTest extends OlatRestTestCase {
 
 		conn.shutdown();	
 	}
+	
+	@Test
+	public void testVFSStats() throws IOException, URISyntaxException {
+		RestConnection conn = new RestConnection();
+		assertTrue(conn.login("administrator", "openolat"));
+		
+		URI systemUri = conn.getContextURI().path("system").path("monitoring").path("revisionsSize").build();
+		VFSStatsVO revisionsInfos = conn.get(systemUri, VFSStatsVO.class);
+		assertNotNull(revisionsInfos);
+		assertNotNull(revisionsInfos.getRevisionsSize());
+		
+		conn.shutdown();
+	}
 }
-- 
GitLab