Skip to content

Commit

Permalink
adds cpu usage information
Browse files Browse the repository at this point in the history
  • Loading branch information
EquipableMC committed Sep 3, 2024
1 parent 0945caa commit d99b22c
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 0 deletions.
3 changes: 3 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ allprojects {
maven { url 'https://repo.papermc.io/repository/maven-public/' }
maven { url = 'https://s01.oss.sonatype.org/content/repositories/snapshots/' } // needed for paper adventure snapshot
maven { url 'https://ci.emc.gs/nexus/content/groups/aikar/' }
maven { url 'https://oss.sonatype.org/content/repositories/snapshots' }
}
}

Expand All @@ -37,6 +38,8 @@ dependencies {
exclude group: 'org.bstats', module: 'bstats-bukkit'
}

compileOnly 'me.lucko:spark-api:0.1-SNAPSHOT'

implementation fileTree(dir: 'lib', include: '*.jar')

testShadow group: 'junit', name: 'junit', version: '4.13.2'
Expand Down
105 changes: 105 additions & 0 deletions src/main/java/ch/njol/skript/expressions/ExprCpuUsage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package ch.njol.skript.expressions;

import ch.njol.skript.Skript;
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.ExpressionType;
import ch.njol.skript.lang.SkriptParser;
import ch.njol.skript.lang.util.SimpleExpression;
import ch.njol.util.Kleenean;
import ch.njol.util.coll.CollectionUtils;
import me.lucko.spark.api.Spark;
import me.lucko.spark.api.SparkProvider;
import me.lucko.spark.api.statistic.StatisticWindow;
import me.lucko.spark.api.statistic.types.DoubleStatistic;
import org.bukkit.Bukkit;
import org.bukkit.event.Event;
import org.bukkit.plugin.Plugin;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.List;

public class ExprCpuUsage extends SimpleExpression<Number> {

private int index;
private String expr = "cpu";

static {
Skript.registerExpression(ExprCpuUsage.class, Number.class, ExpressionType.SIMPLE,
"cpu usage from [the] last 10[ ]s[seconds]",
"cpu usage from [the] last ([1] minute|1[ ]m[inute])",
"cpu usage from [the] last 15[ ]m[inutes]",
"[the] cpu usage");
}

private static Spark getSparkInstance() {
try {
Spark spark = SparkProvider.get();
if (spark != null) {
return SparkProvider.get();
}
} catch (Exception e) {
return null;
}
return null;
}

@Override
public boolean init(Expression<?>[] expressions, int matchedPattern, Kleenean isDelayed, SkriptParser.ParseResult parseResult) {
Spark spark = getSparkInstance();
if (spark != null) {
expr = parseResult.expr;
index = matchedPattern;
return true;
}
return false;
}

@Override
protected Number @Nullable [] get(Event event) {
Spark spark = SparkProvider.get();
DoubleStatistic<StatisticWindow.CpuUsage> cpuUsage = spark.cpuSystem();
return switch (index) {
case 0 -> {
double usageLast10Seconds = cpuUsage.poll(StatisticWindow.CpuUsage.SECONDS_10);
Number usage10Seconds = (Number) (usageLast10Seconds * 100);
yield new Number[]{usage10Seconds};
}
case 1 -> {
double usageLast1Minute = cpuUsage.poll(StatisticWindow.CpuUsage.MINUTES_1);
Number usage1Minute = (Number) (usageLast1Minute * 100);
yield new Number[]{usage1Minute};
}
case 2 -> {
double usageLast15Minutes = cpuUsage.poll(StatisticWindow.CpuUsage.MINUTES_15);
Number usage15Minutes = (Number) (usageLast15Minutes * 100);
yield new Number[]{usage15Minutes};
}
default -> {
double usageLast10Seconds = cpuUsage.poll(StatisticWindow.CpuUsage.SECONDS_10);
double usageLast1Minute = cpuUsage.poll(StatisticWindow.CpuUsage.MINUTES_1);
double usageLast15Minutes = cpuUsage.poll(StatisticWindow.CpuUsage.MINUTES_15);
Number number1 = (Number) (usageLast10Seconds * 100);
Number number2 = (Number) (usageLast1Minute * 100);
Number number3 = (Number) (usageLast15Minutes * 100);
yield new Number[]{number1, number2, number3};
}
};
}

@Override
public boolean isSingle() {
return index != 3;
}

@Override
public Class<? extends Number> getReturnType() {
return Number.class;
}

@Override
public String toString(@Nullable Event event, boolean debug) {
return expr;
}

}

0 comments on commit d99b22c

Please sign in to comment.