Added Uptime

This commit is contained in:
Erik C. Thauvin 2023-03-17 21:16:48 -07:00
parent 9e0cd0d9ac
commit 26e03588ea
10 changed files with 229 additions and 6 deletions

View file

@ -27,6 +27,8 @@ import java.nio.charset.StandardCharsets;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.temporal.ChronoField;
import java.util.Properties;
import java.util.concurrent.TimeUnit;
/**
* Collection of utility-type methods commonly used by the renderers.
@ -118,6 +120,22 @@ public final class RenderUtils {
return "";
}
/**
* Returns the plural form of a word, if count > 1.
*
* @param count the count.
* @param word the singular word.
* @param plural the plural word.
* @return the singular or plural string.
*/
public static String plural(final long count, final String word, final String plural) {
if (count > 1) {
return plural;
} else {
return word;
}
}
/**
* Translates a String to/from ROT13.
*
@ -285,4 +303,57 @@ public final class RenderUtils {
return buff.toString();
}
/**
* Returns the formatted server uptime.
*
* @param uptime the uptime in milliseconds.
* @param properties the format properties.
* @return The formatted uptime.
*/
public static String uptime(long uptime, Properties properties) {
var sb = new StringBuilder();
var days = TimeUnit.MILLISECONDS.toDays(uptime);
var years = days / 365;
days %= 365;
var months = days / 30;
days %= 30;
var weeks = days / 7;
days %= 7;
var hours = TimeUnit.MILLISECONDS.toHours(uptime) - TimeUnit.DAYS.toHours(TimeUnit.MILLISECONDS.toDays(uptime));
var minutes = TimeUnit.MILLISECONDS.toMinutes(uptime) - TimeUnit.HOURS.toMinutes(
TimeUnit.MILLISECONDS.toHours(uptime));
if (years > 0) {
sb.append(years).append(plural(years, properties.getProperty("year", " year "),
properties.getProperty("years", " years ")));
}
if (months > 0) {
sb.append(months).append(plural(months, properties.getProperty("month", " month "),
properties.getProperty("months", " months ")));
}
if (weeks > 0) {
sb.append(weeks).append(plural(weeks, properties.getProperty("week", " week "),
properties.getProperty("weeks", " weeks ")));
}
if (days > 0) {
sb.append(days).append(plural(days, properties.getProperty("day", " day "),
properties.getProperty("days", " days ")));
}
if (hours > 0) {
sb.append(hours).append(plural(hours, properties.getProperty("hour", " hour "),
properties.getProperty("hours", " hours ")));
}
sb.append(minutes).append(plural(minutes, properties.getProperty("minute", " minute"),
properties.getProperty("minutes", " minutes")));
return sb.toString();
}
}

View file

@ -0,0 +1,94 @@
/*
* Copyright 2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package rife.render;
import rife.template.Template;
import rife.template.ValueRenderer;
import java.io.IOException;
import java.io.StringReader;
import java.lang.management.ManagementFactory;
import java.util.Properties;
/**
* Renders the server uptime.
*
* <p>Usage:</p>
*
* <pre>
* &lt;!--v render:rife.render.Uptime/--&gt;
* {{v render:rife.render.Uptime:valueId/}}
* </pre>
*
* <p>You can also specify custom formatting using the following properties:</p>
*
* <pre>
* {{v: render:rife.renader.Uptime}}
* year=Y-
* years=Y-
* month=M-
* months=M-
* week=W-
* weeks=W-
* day=D-
* days=D-
* hour=H-
* hours=H-
* minute=M-
* minutes=M-
* {{/v}}
* </pre>
*
* <p>which would render something like</p>
*
* <pre>
* 17Y-2M-2W-1D-9H-33M
* </pre>
*
* <p>You can also specify the uptime via a template attribute</p>
*
* <pre>
* template.setAttribute(Uptime.class.getName(), 120000L);
* </pre>
*
* @author <a href="https://erik.thauvin.net/">Erik C. Thauvin</a>
* @since 1.0
*/
public class Uptime implements ValueRenderer {
/**
* {@inheritDoc}
*/
@Override
public String render(Template template, String valueId, String differentiator) {
var properties = new Properties();
if (template.hasDefaultValue(valueId)) {
try {
properties.load(new StringReader(template.getDefaultValue(valueId)));
} catch (IOException ignore) {
// ignore
}
}
if (template.hasAttribute(Uptime.class.getName())) {
return RenderUtils.uptime((long) template.getAttribute(Uptime.class.getName()), properties);
} else {
return RenderUtils.uptime(ManagementFactory.getRuntimeMXBean().getUptime(), properties);
}
}
}