diff --git a/src/main/java/org/olat/course/nodes/cl/ui/CheckboxAssessmentDataModelSorter.java b/src/main/java/org/olat/course/nodes/cl/ui/CheckboxAssessmentDataModelSorter.java index 657ca3acb19b966b7994211ad5aaa9956056ed70..c4764e12b2007ea1adfaf6ef28e8eaa8032a02bb 100644 --- a/src/main/java/org/olat/course/nodes/cl/ui/CheckboxAssessmentDataModelSorter.java +++ b/src/main/java/org/olat/course/nodes/cl/ui/CheckboxAssessmentDataModelSorter.java @@ -1,3 +1,22 @@ +/** + * <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.course.nodes.cl.ui; import java.util.Collections; diff --git a/src/main/java/org/olat/repository/RepositoryManager.java b/src/main/java/org/olat/repository/RepositoryManager.java index 64f3da28c030de369f28f411be0ab46aac9af30f..b5224b9141a3e2272e9bb7c852d6ae23a3d16787 100644 --- a/src/main/java/org/olat/repository/RepositoryManager.java +++ b/src/main/java/org/olat/repository/RepositoryManager.java @@ -855,6 +855,20 @@ public class RepositoryManager { dbquery.setCacheable(true); return ((Long)dbquery.list().get(0)).intValue(); } + + public long countPublished(String restrictedType) { + StringBuilder query = new StringBuilder(400); + query.append("select count(*) from org.olat.repository.RepositoryEntry v") + .append(" inner join v.olatResource res") + .append(" where res.resName=:restrictedType ") + .append(" and ((v.access=").append(RepositoryEntry.ACC_OWNERS).append(" and v.membersOnly=true) or v.access>=").append(RepositoryEntry.ACC_USERS).append(")"); + + List<Number> count = dbInstance.getCurrentEntityManager() + .createQuery(query.toString(), Number.class) + .setParameter("restrictedType", restrictedType) + .getResultList(); + return count == null || count.isEmpty() || count.get(0) == null ? null : count.get(0).longValue(); + } /** * Query by type, limit by ownership or role accessability. diff --git a/src/main/java/org/olat/restapi/system/MonitoringModule.java b/src/main/java/org/olat/restapi/system/MonitoringModule.java index 88d5799df853877c22c91b732f7d4599ed202203..7729c843a31c76eb968df1a6720b601d9345d7a2 100644 --- a/src/main/java/org/olat/restapi/system/MonitoringModule.java +++ b/src/main/java/org/olat/restapi/system/MonitoringModule.java @@ -19,8 +19,12 @@ */ package org.olat.restapi.system; +import java.io.File; + import org.olat.core.configuration.AbstractSpringModule; import org.olat.core.configuration.ConfigOnOff; +import org.olat.core.logging.OLog; +import org.olat.core.logging.Tracing; import org.olat.core.util.StringHelper; import org.olat.core.util.coordinate.CoordinatorManager; import org.springframework.beans.factory.annotation.Autowired; @@ -33,6 +37,8 @@ import org.springframework.stereotype.Service; */ @Service("monitoringModule") public class MonitoringModule extends AbstractSpringModule implements ConfigOnOff { + + private static final OLog log = Tracing.createLoggerFor(MonitoringModule.class); private static final String ENABLED = "monitoring.enabled"; private static final String MONITORED_PROBES = "probes"; @@ -48,6 +54,8 @@ public class MonitoringModule extends AbstractSpringModule implements ConfigOnOf private String server; @Value("${monitoring.instance.description}") private String description; + @Value("${monitoring.proc.file:}") + private String procFile; @Autowired public MonitoringModule(CoordinatorManager coordinatorManager) { @@ -73,6 +81,16 @@ public class MonitoringModule extends AbstractSpringModule implements ConfigOnOf if(StringHelper.containsNonWhitespace(descriptionObj)) { description = descriptionObj; } + + if(StringHelper.containsNonWhitespace(procFile)) { + File xmlFile = new File(procFile); + if(!xmlFile.exists()) { + File parent = xmlFile.getParentFile(); + if(!parent.exists() || !parent.canWrite()) { + log.warn("Cannot write proc file: " + xmlFile); + } + } + } } @@ -122,4 +140,7 @@ public class MonitoringModule extends AbstractSpringModule implements ConfigOnOf setStringProperty(DESCRIPTION, description, true); } + public String getProcFile() { + return procFile; + } } diff --git a/src/main/java/org/olat/restapi/system/MonitoringService.java b/src/main/java/org/olat/restapi/system/MonitoringService.java new file mode 100644 index 0000000000000000000000000000000000000000..82c7cd92119012d36d7463edc1afa0da50460d03 --- /dev/null +++ b/src/main/java/org/olat/restapi/system/MonitoringService.java @@ -0,0 +1,163 @@ +/** + * <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 org.olat.admin.sysinfo.manager.DatabaseStatsManager; +import org.olat.admin.sysinfo.model.DatabaseConnectionVO; +import org.olat.basesecurity.BaseSecurity; +import org.olat.basesecurity.Constants; +import org.olat.course.CourseModule; +import org.olat.group.BusinessGroupService; +import org.olat.repository.RepositoryManager; +import org.olat.restapi.system.vo.SessionsVO; +import org.olat.search.SearchServiceStatus; +import org.olat.search.service.SearchServiceFactory; +import org.olat.search.service.SearchServiceStatusImpl; +import org.olat.search.service.indexer.FullIndexerStatus; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +/** + * Small stateful service to cache some data. + * + * Initial date: 15 févr. 2017<br> + * @author srosse, stephane.rosse@frentix.com, http://www.frentix.com + * + */ +@Service +public class MonitoringService { + + private static final int RENEW_RATE = 60 * 60 * 1000;// once an hour + + private long start; + private long activeUserCount; + private long totalGroupCount; + private long publishedCourses; + + @Autowired + private BaseSecurity securityManager; + @Autowired + private RepositoryManager repositoryManager; + @Autowired + private BusinessGroupService businessGroupService; + @Autowired + private DatabaseStatsManager databaseStatsManager; + + public Statistics getStatistics() { + Statistics statistics = new Statistics(); + + SessionsVO sessionsVo = new OpenOLATStatisticsWebService().getSessionsVO(); + statistics.setSessionsVo(sessionsVo); + + SearchServiceStatus status = SearchServiceFactory.getService().getStatus(); + if(status instanceof SearchServiceStatusImpl) { + SearchServiceStatusImpl statusImpl = (SearchServiceStatusImpl)status; + FullIndexerStatus fStatus = statusImpl.getFullIndexerStatus(); + statistics.setLastFullIndexTime(fStatus.getLastFullIndexDateString()); + } + + // activeUserCount="88" // registered and activated identities, same as in GUI + if(start < 1 || (System.currentTimeMillis() - start) > RENEW_RATE) { + start = System.currentTimeMillis(); + activeUserCount = securityManager.countIdentitiesByPowerSearch(null, null, false, null, null, null, null, null, null, null, Constants.USERSTATUS_ACTIVE); + totalGroupCount = businessGroupService.countBusinessGroups(null, null); + publishedCourses = repositoryManager.countPublished(CourseModule.ORES_TYPE_COURSE); + } + statistics.setActiveUserCount(activeUserCount); + statistics.setTotalGroupCount(totalGroupCount); + statistics.setPublishedCourses(publishedCourses); + + + DatabaseConnectionVO connections = databaseStatsManager.getConnectionInfos(); + if(connections != null) { + statistics.setActiveConnectionCount(connections.getActiveConnectionCount()); + statistics.setCurrentConnectionCount(connections.getCurrentConnectionCount()); + } + return statistics; + } + + public class Statistics { + + private long activeUserCount = -1; + private long totalGroupCount = -1; + private long publishedCourses = -1; + private SessionsVO sessionsVo; + private String lastFullIndexTime; + + private long activeConnectionCount; + private long currentConnectionCount; + + public long getActiveUserCount() { + return activeUserCount; + } + + public void setActiveUserCount(long activeUserCount) { + this.activeUserCount = activeUserCount; + } + + public long getTotalGroupCount() { + return totalGroupCount; + } + + public void setTotalGroupCount(long totalGroupCount) { + this.totalGroupCount = totalGroupCount; + } + + public long getPublishedCourses() { + return publishedCourses; + } + + public void setPublishedCourses(long publishedCourses) { + this.publishedCourses = publishedCourses; + } + + public SessionsVO getSessionsVo() { + return sessionsVo; + } + + public void setSessionsVo(SessionsVO sessionsVo) { + this.sessionsVo = sessionsVo; + } + + public String getLastFullIndexTime() { + return lastFullIndexTime; + } + + public void setLastFullIndexTime(String lastFullIndexTime) { + this.lastFullIndexTime = lastFullIndexTime; + } + + public long getActiveConnectionCount() { + return activeConnectionCount; + } + + public void setActiveConnectionCount(long activeConnectionCount) { + this.activeConnectionCount = activeConnectionCount; + } + + public long getCurrentConnectionCount() { + return currentConnectionCount; + } + + public void setCurrentConnectionCount(long currentConnectionCount) { + this.currentConnectionCount = currentConnectionCount; + } + } +} diff --git a/src/main/java/org/olat/restapi/system/OpenOLATStatisticsWebService.java b/src/main/java/org/olat/restapi/system/OpenOLATStatisticsWebService.java index 0f24a425001c8747def534751dcb6a749a78fa3f..cc20f8c70b9b15716e7ca734350215f31a51ef2b 100644 --- a/src/main/java/org/olat/restapi/system/OpenOLATStatisticsWebService.java +++ b/src/main/java/org/olat/restapi/system/OpenOLATStatisticsWebService.java @@ -237,7 +237,7 @@ public class OpenOLATStatisticsWebService implements Sampler { return tasks; } - private SessionsVO getSessionsVO() { + protected SessionsVO getSessionsVO() { SessionsVO vo = new SessionsVO(); SessionStatsManager sessionStatsManager = CoreSpringFactory.getImpl(SessionStatsManager.class); diff --git a/src/main/java/org/olat/restapi/system/SamplerJob.java b/src/main/java/org/olat/restapi/system/SamplerJob.java index 7657ec18675be2d8d02bd4d88cced92be4f06eef..fd4d722a34d75d1e68fe03c9cf81a628a8ee116a 100644 --- a/src/main/java/org/olat/restapi/system/SamplerJob.java +++ b/src/main/java/org/olat/restapi/system/SamplerJob.java @@ -19,8 +19,29 @@ */ package org.olat.restapi.system; +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStream; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.transform.Transformer; +import javax.xml.transform.TransformerFactory; +import javax.xml.transform.dom.DOMSource; +import javax.xml.transform.stream.StreamResult; + +import org.olat.core.CoreSpringFactory; +import org.olat.core.logging.OLog; +import org.olat.core.logging.Tracing; +import org.olat.core.util.StringHelper; +import org.olat.restapi.system.MonitoringService.Statistics; +import org.olat.restapi.system.vo.SessionsVO; import org.quartz.JobExecutionContext; import org.springframework.scheduling.quartz.QuartzJobBean; +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.NodeList; /** * @@ -35,8 +56,82 @@ import org.springframework.scheduling.quartz.QuartzJobBean; */ public class SamplerJob extends QuartzJobBean { + private static final OLog log = Tracing.createLoggerFor(SamplerJob.class); + @Override protected void executeInternal(JobExecutionContext context) { MonitoringWebService.takeSample(); + MonitoringModule monitoringModule = CoreSpringFactory.getImpl(MonitoringModule.class); + if(StringHelper.containsNonWhitespace(monitoringModule.getProcFile())) { + writeProcFile(monitoringModule.getProcFile()); + } + } + + public void writeProcFile(String procFile) { + try { + File xmlFile = new File(procFile); + if(!xmlFile.exists()) { + File parent = xmlFile.getParentFile(); + if(!parent.exists() || !parent.canWrite()) { + return; + } + } + + Statistics statistics = CoreSpringFactory.getImpl(MonitoringService.class).getStatistics(); + DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); + DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); + Document doc; + if(xmlFile.exists()) { + doc = dBuilder.parse(xmlFile); + } else { + doc = dBuilder.newDocument(); + doc.appendChild(doc.createElement("root")); + } + + Element rootEl = doc.getDocumentElement(); + //sessions + SessionsVO sessionsVo = statistics.getSessionsVo(); + addValue("secureAuthenticatedCount", sessionsVo.getSecureAuthenticatedCount(), rootEl, doc); + addValue("secureRestCount", sessionsVo.getSecureRestCount(), rootEl, doc); + addValue("secureWebdavCount", sessionsVo.getSecureWebdavCount(), rootEl, doc); + //clicks + addValue("authenticatedClickCountLastFiveMinutes", sessionsVo.getAuthenticatedClickCountLastFiveMinutes(), rootEl, doc); + addValue("concurrentDispatchThreads", sessionsVo.getConcurrentDispatchThreads(), rootEl, doc); + addValue("requestLastFiveMinutes", sessionsVo.getRequestLastFiveMinutes(), rootEl, doc); + addValue("requestLastMinute", sessionsVo.getRequestLastMinute(), rootEl, doc); + //openolat + addValue("activeUserCount", statistics.getActiveUserCount(), rootEl, doc); + addValue("totalGroupCount", statistics.getTotalGroupCount(), rootEl, doc); + addValue("publishedCourses", statistics.getPublishedCourses(), rootEl, doc); + //indexer + addValue("lastFullIndexTime", statistics.getLastFullIndexTime(), rootEl, doc); + + // Use a Transformer for output + try(OutputStream out = new FileOutputStream(xmlFile)) { + TransformerFactory tFactory = TransformerFactory.newInstance(); + Transformer transformer = tFactory.newTransformer(); + transformer.transform(new DOMSource(doc), new StreamResult(out)); + } catch(IOException e) { + log.error("", e); + } + } catch (Exception e) { + log.error("", e); + } + } + + private void addValue(String name, long value, Element rootEl, Document doc) { + addValue(name, Long.toString(value), rootEl, doc); + } + + private void addValue(String name, String value, Element rootEl, Document doc) { + NodeList currentEls = rootEl.getElementsByTagName(name); + if(currentEls.getLength() == 0) { + Element element = doc.createElement(name); + element.setAttribute("value", value); + rootEl.appendChild(element); + } else { + Element element = (Element)currentEls.item(0); + element.setAttribute("value", value); + } } } \ No newline at end of file