Skip to content

Commit

Permalink
Add time series display to overview.
Browse files Browse the repository at this point in the history
  • Loading branch information
mnlipp committed Oct 27, 2023
1 parent e0c3237 commit e14cc20
Show file tree
Hide file tree
Showing 11 changed files with 564 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
import java.math.BigInteger;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.function.Supplier;

Expand Down Expand Up @@ -209,6 +211,21 @@ public Optional<Boolean> getAsBoolean(Object... selectors) {
.map(JsonPrimitive::getAsBoolean);
}

/**
* Returns the elements of the selected {@link JsonArray} as list.
*
* @param <T> the generic type
* @param cls the cls
* @param selectors the selectors
* @return the list
*/
@SuppressWarnings("unchecked")
public <T extends JsonElement> List<T> getAsListOf(Class<T> cls,
Object... selectors) {
return get(JsonArray.class, selectors).map(a -> (List<T>) a.asList())
.orElse(Collections.emptyList());
}

/**
* Sets the selected value. This pointer must point to a
* {@link JsonObject} or {@link JsonArray}. The selector must
Expand Down
5 changes: 4 additions & 1 deletion org.jdrupes.vmoperator.vmconlet/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ dependencies {

implementation 'org.jgrapes:org.jgrapes.webconsole.base:[1.2.0,2)'
implementation 'org.jgrapes:org.jgrapes.webconsole.provider.vue:[1,2)'
implementation 'org.jgrapes:org.jgrapes.webconsole.provider.jgwcvuecomponents:[1,2)'
implementation 'org.jgrapes:org.jgrapes.webconsole.provider.jgwcvuecomponents:[1.2,2)'
implementation 'org.jgrapes:org.jgrapes.webconsole.provider.chartjs:[1.2,2)'

}

apply plugin: 'com.github.node-gradle.node'
Expand All @@ -22,6 +24,7 @@ task extractDependencies(type: Copy) {
|| it.name.contains('org.jgrapes.webconsole.base')
}
.collect{ zipTree (it) }
exclude '*.class'
into 'build/unpacked'
duplicatesStrategy 'include'
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
<div class="jdrupes-vmoperator-vmconlet jdrupes-vmoperator-vmconlet-preview"
data-conlet-grid-rows="5"
data-jgwc-on-load="orgJDrupesVmOperatorVmConlet.initPreview"
data-jgwc-on-unload="JGConsole.jgwc.unmountVueApps">
data-jgwc-on-unload="JGConsole.jgwc.unmountVueApps">
<table>
<tbody>
<tr>
<td>{{ localize("Total VMs") }}:</td>
<td>{{ data.totalVms }}</td>
</tr>
<tr>
<td>{{ localize("Running VMs") }}:</td>
<td>{{ data.runningVms }}</td>
<td>{{ localize("VMsSummary") }}:</td>
<td>{{ vmSummary.runningVms }} / {{ vmSummary.totalVms }}</td>
</tr>
<tr>
<td>{{ localize("Used CPUs") }}:</td>
<td>{{ data.usedCpus }}</td>
<td>{{ vmSummary.usedCpus }}</td>
</tr>
<tr>
<td>{{ localize("Used RAM") }}:</td>
<td>{{ formatMemory(data.usedRam) }}</td>
<td>{{ formatMemory(BigInt(vmSummary.usedRam)) }}</td>
</tr>
</tbody>
</table>
<div class="vmsChart-wrapper">
<canvas class="vmsChart"></canvas>
</div>

</div>
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
conletName = VM Viewer

VMsSummary = VMs (running/total)

currentCpus = Current CPUs
currentRam = Current RAM
maximumCpus = Maximum CPUs
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
conletName = VM Anzeige

Running\ VMs = Gestartete VMs
Total\ VMs = Gesamtzahl VMs
VMsSummary = VMs (gestartet/gesamt)

Used\ CPUs = Verwendete CPUs
Used\ RAM = Verwendetes RAM

Expand Down
3 changes: 2 additions & 1 deletion org.jdrupes.vmoperator.vmconlet/rollup.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ let pathsMap = {
"jgconsole": "../../console-base-resource/jgconsole.js",
"jgwc": "../../page-resource/jgwc-vue-components/jgwc-components.js",
"l10nBundles": "./" + baseName + "-l10nBundles.ftl.js",
"vue": "../../page-resource/vue/vue.esm-browser.js"
"vue": "../../page-resource/vue/vue.esm-browser.js",
"chartjs": "../../page-resource/chart.js/auto.js"
}

export default {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
/*
* VM-Operator
* Copyright (C) 2023 Michael N. Lipp
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package org.jdrupes.vmoperator.vmconlet;

import java.time.Duration;
import java.time.Instant;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

/**
* The Class TimeSeries.
*/
@SuppressWarnings("PMD.DataflowAnomalyAnalysis")
public class TimeSeries {

private final List<Entry> data = new LinkedList<>();
private final Duration period;

/**
* Instantiates a new time series.
*
* @param series the number of series
*/
public TimeSeries(Duration period) {
this.period = period;
}

/**
* Adds data to the series.
*
* @param time the time
* @param numbers the numbers
* @return the time series
*/
@SuppressWarnings("PMD.AvoidLiteralsInIfCondition")
public TimeSeries add(Instant time, Number... numbers) {
var newEntry = new Entry(time, numbers);
boolean adjust = false;
if (data.size() >= 2) {
var lastEntry = data.get(data.size() - 1);
var lastButOneEntry = data.get(data.size() - 2);
adjust = lastEntry.valuesEqual(lastButOneEntry)
&& lastEntry.valuesEqual(newEntry);
}
if (adjust) {
data.get(data.size() - 1).adjustTime(time);
} else {
data.add(new Entry(time, numbers));
}

// Purge
Instant limit = time.minus(period);
while (data.size() > 2
&& data.get(0).getTime().isBefore(limit)
&& data.get(1).getTime().isBefore(limit)) {
data.remove(0);
}
return this;
}

/**
* Returns the entries.
*
* @return the list
*/
public List<Entry> entries() {
return data;
}

/**
* The Class Entry.
*/
public static class Entry {
private Instant timestamp;
private final Number[] values;

/**
* Instantiates a new entry.
*
* @param time the time
* @param numbers the numbers
*/
@SuppressWarnings("PMD.ArrayIsStoredDirectly")
public Entry(Instant time, Number... numbers) {
timestamp = time;
values = numbers;
}

/**
* Changes the entry's time.
*
* @param time the time
*/
public void adjustTime(Instant time) {
timestamp = time;
}

/**
* Returns the entry's time.
*
* @return the instant
*/
public Instant getTime() {
return timestamp;
}

/**
* Returns the values.
*
* @return the number[]
*/
@SuppressWarnings("PMD.MethodReturnsInternalArray")
public Number[] getValues() {
return values;
}

/**
* Returns `true` if both entries have the same values.
*
* @param other the other
* @return true, if successful
*/
public boolean valuesEqual(Entry other) {
return Arrays.equals(values, other.values);
}
}
}
Loading

0 comments on commit e14cc20

Please sign in to comment.