Skip to content
Snippets Groups Projects
Commit 2b1e5493 authored by srosse's avatar srosse
Browse files

OO-2545: can write some statistics in an XML file every minute

parent 20c14a6f
No related branches found
No related tags found
No related merge requests found
/**
* <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; package org.olat.course.nodes.cl.ui;
import java.util.Collections; import java.util.Collections;
......
...@@ -855,6 +855,20 @@ public class RepositoryManager { ...@@ -855,6 +855,20 @@ public class RepositoryManager {
dbquery.setCacheable(true); dbquery.setCacheable(true);
return ((Long)dbquery.list().get(0)).intValue(); 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. * Query by type, limit by ownership or role accessability.
......
...@@ -19,8 +19,12 @@ ...@@ -19,8 +19,12 @@
*/ */
package org.olat.restapi.system; package org.olat.restapi.system;
import java.io.File;
import org.olat.core.configuration.AbstractSpringModule; import org.olat.core.configuration.AbstractSpringModule;
import org.olat.core.configuration.ConfigOnOff; 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.StringHelper;
import org.olat.core.util.coordinate.CoordinatorManager; import org.olat.core.util.coordinate.CoordinatorManager;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
...@@ -33,6 +37,8 @@ import org.springframework.stereotype.Service; ...@@ -33,6 +37,8 @@ import org.springframework.stereotype.Service;
*/ */
@Service("monitoringModule") @Service("monitoringModule")
public class MonitoringModule extends AbstractSpringModule implements ConfigOnOff { 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 ENABLED = "monitoring.enabled";
private static final String MONITORED_PROBES = "probes"; private static final String MONITORED_PROBES = "probes";
...@@ -48,6 +54,8 @@ public class MonitoringModule extends AbstractSpringModule implements ConfigOnOf ...@@ -48,6 +54,8 @@ public class MonitoringModule extends AbstractSpringModule implements ConfigOnOf
private String server; private String server;
@Value("${monitoring.instance.description}") @Value("${monitoring.instance.description}")
private String description; private String description;
@Value("${monitoring.proc.file:}")
private String procFile;
@Autowired @Autowired
public MonitoringModule(CoordinatorManager coordinatorManager) { public MonitoringModule(CoordinatorManager coordinatorManager) {
...@@ -73,6 +81,16 @@ public class MonitoringModule extends AbstractSpringModule implements ConfigOnOf ...@@ -73,6 +81,16 @@ public class MonitoringModule extends AbstractSpringModule implements ConfigOnOf
if(StringHelper.containsNonWhitespace(descriptionObj)) { if(StringHelper.containsNonWhitespace(descriptionObj)) {
description = 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 ...@@ -122,4 +140,7 @@ public class MonitoringModule extends AbstractSpringModule implements ConfigOnOf
setStringProperty(DESCRIPTION, description, true); setStringProperty(DESCRIPTION, description, true);
} }
public String getProcFile() {
return procFile;
}
} }
/**
* <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;
}
}
}
...@@ -237,7 +237,7 @@ public class OpenOLATStatisticsWebService implements Sampler { ...@@ -237,7 +237,7 @@ public class OpenOLATStatisticsWebService implements Sampler {
return tasks; return tasks;
} }
private SessionsVO getSessionsVO() { protected SessionsVO getSessionsVO() {
SessionsVO vo = new SessionsVO(); SessionsVO vo = new SessionsVO();
SessionStatsManager sessionStatsManager = CoreSpringFactory.getImpl(SessionStatsManager.class); SessionStatsManager sessionStatsManager = CoreSpringFactory.getImpl(SessionStatsManager.class);
......
...@@ -19,8 +19,29 @@ ...@@ -19,8 +19,29 @@
*/ */
package org.olat.restapi.system; 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.quartz.JobExecutionContext;
import org.springframework.scheduling.quartz.QuartzJobBean; 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; ...@@ -35,8 +56,82 @@ import org.springframework.scheduling.quartz.QuartzJobBean;
*/ */
public class SamplerJob extends QuartzJobBean { public class SamplerJob extends QuartzJobBean {
private static final OLog log = Tracing.createLoggerFor(SamplerJob.class);
@Override @Override
protected void executeInternal(JobExecutionContext context) { protected void executeInternal(JobExecutionContext context) {
MonitoringWebService.takeSample(); 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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment