Skip to content

Commit

Permalink
[enhance] Make uptime ajax enabled to update every minute
Browse files Browse the repository at this point in the history
  • Loading branch information
hazendaz committed Jul 16, 2023
1 parent dff2cfb commit 3a70f50
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
*/
package psiprobe.controllers;

import java.lang.management.ManagementFactory;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.List;
Expand Down Expand Up @@ -77,19 +76,6 @@ protected ModelAndView handleRequestInternal(HttpServletRequest request,
Properties version = (Properties) getApplicationContext().getBean("version");
request.setAttribute("version", version.getProperty("probe.version"));

long uptimeStartValue = ManagementFactory.getRuntimeMXBean().getStartTime();
long uptime = System.currentTimeMillis() - uptimeStartValue;
long uptimeDays = uptime / (1000 * 60 * 60 * 24);

uptime = uptime % (1000 * 60 * 60 * 24);
long uptimeHours = uptime / (1000 * 60 * 60);

uptime = uptime % (1000 * 60 * 60);
long uptimeMins = uptime / (1000 * 60);

request.setAttribute("uptime_days", uptimeDays);
request.setAttribute("uptime_hours", uptimeHours);
request.setAttribute("uptime_mins", uptimeMins);
//
// Work out the language of the interface by matching resource files that we have
// to the request locale.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Licensed under the GPL License. You may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
*
* THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
* WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
* PURPOSE.
*/
package psiprobe.controllers.apps;

import java.lang.management.ManagementFactory;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.ParameterizableViewController;

/**
* The Class AjaxUptimeController.
*/
@Controller
public class AjaxUptimeController extends ParameterizableViewController {

@GetMapping(path = "/uptime.ajax")
@Override
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
throws Exception {
return super.handleRequest(request, response);
}

@Override
protected ModelAndView handleRequestInternal(HttpServletRequest request,
HttpServletResponse response) throws Exception {
long uptimeStartValue = ManagementFactory.getRuntimeMXBean().getStartTime();
long uptime = System.currentTimeMillis() - uptimeStartValue;
long uptimeDays = uptime / (1000 * 60 * 60 * 24);

uptime = uptime % (1000 * 60 * 60 * 24);
long uptimeHours = uptime / (1000 * 60 * 60);

uptime = uptime % (1000 * 60 * 60);
long uptimeMins = uptime / (1000 * 60);

request.setAttribute("uptime_days", uptimeDays);
request.setAttribute("uptime_hours", uptimeHours);
request.setAttribute("uptime_mins", uptimeMins);

return new ModelAndView(getViewName());
}

@Value("ajax/uptime")
@Override
public void setViewName(String viewName) {
super.setViewName(viewName);
}

}
16 changes: 16 additions & 0 deletions psi-probe-web/src/main/webapp/WEB-INF/jsp/ajax/uptime.jsp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<%--
Licensed under the GPL License. You may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
PURPOSE.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" session="false" %>
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring" %>

<span class="uptime"><spring:message code="probe.jsp.uptime" arguments="${uptime_days},${uptime_hours},${uptime_mins}"/></span>
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@
alt="PSI Probe Logo"/></a></li>
<li id="runtime">
<spring:message code="probe.jsp.version" arguments="${version},<b>${hostname}</b>"/>,
<span class="uptime"><spring:message code="probe.jsp.uptime"
arguments="${uptime_days},${uptime_hours},${uptime_mins}"/></span></li>
<span id="uptime"></span>
</li>
<li id="title"><decorator:title default="Probe"/></li>
</ul>
</div>
Expand Down Expand Up @@ -198,5 +198,8 @@
</p>
</div>

<script>
new Ajax.PeriodicalUpdater('uptime', '<c:url value="/uptime.ajax"/>', {method:'get',frequency: 60});
</script>
</body>
</html>

0 comments on commit 3a70f50

Please sign in to comment.