Skip to content

Commit

Permalink
Add concrete transformations per hour metrics chart.
Browse files Browse the repository at this point in the history
  • Loading branch information
RezzedUp committed Dec 15, 2022
1 parent de754c8 commit 42ed1c4
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import community.leaf.eventful.bukkit.ListenerOrder;
import community.leaf.eventful.bukkit.annotations.CancelledEvents;
import community.leaf.eventful.bukkit.annotations.EventListener;
import community.leaf.survival.concretemixer.metrics.TransformationsPerHour;
import community.leaf.tasks.TaskContext;
import org.bukkit.Material;
import org.bukkit.block.Block;
Expand All @@ -38,10 +39,12 @@ public class CauldronPowderDropListener implements Listener
private final Map<UUID, TaskContext<BukkitTask>> transformationTasksByItemUuid = new HashMap<>();

private final ConcreteMixerPlugin plugin;
private final TransformationsPerHour counter;

public CauldronPowderDropListener(ConcreteMixerPlugin plugin)
public CauldronPowderDropListener(ConcreteMixerPlugin plugin, TransformationsPerHour counter)
{
this.plugin = plugin;
this.counter = counter;
}

@EventListener
Expand Down Expand Up @@ -187,6 +190,7 @@ class IterationCounter
item.setPickupDelay(10);

plugin.effects().concreteTransform(cauldron);
counter.transformed(stack.getAmount());

if (!lowerWaterLevel) { return; }
if (!(cauldron.getBlockData() instanceof Levelled levelled)) { return; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
import com.github.zafarkhaja.semver.Version;
import community.leaf.eventful.bukkit.BukkitEventSource;
import community.leaf.survival.concretemixer.hooks.HookHandler;
import community.leaf.survival.concretemixer.metrics.TransformationsPerHour;
import community.leaf.tasks.bukkit.BukkitTaskSource;
import org.bstats.bukkit.Metrics;
import org.bstats.charts.SingleLineChart;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.java.JavaPlugin;
import pl.tlinkowski.annotation.basic.NullOr;
Expand All @@ -36,12 +38,18 @@ public void onEnable()
this.effects = new EffectHandler(config);
this.hooks = new HookHandler(this);
this.permissions = new PermissionHandler(this);

events().register(new CauldronPowderDropListener(this));

TransformationsPerHour counter = new TransformationsPerHour(config);
events().register(new CauldronPowderDropListener(this, counter));

if (config.getOrDefault(Config.METRICS))
{
new Metrics(this, 15590);
Metrics metrics = new Metrics(this, 15590);

metrics.addCustomChart(new SingleLineChart(
"transformations-per-hour",
counter::totalTransformationsInTheLastHour
));
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright © 2022, RezzedUp and Contributors <https://github.com/LeafCommunity/ConcreteMixer>
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
package community.leaf.survival.concretemixer.metrics;

import community.leaf.survival.concretemixer.Config;

import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;

public class TransformationsPerHour
{
private final Map<Instant, Integer> countersByMinute = new LinkedHashMap<>();

private final Config config;

public TransformationsPerHour(Config config)
{
this.config = config;
}

public void transformed(int totalPowderToConcrete)
{
if (totalPowderToConcrete < 0) { throw new IllegalArgumentException(); }
if (!config.getOrDefault(Config.METRICS)) { return; }

Instant minute = Instant.now().truncatedTo(ChronoUnit.MINUTES);
int counter = countersByMinute.computeIfAbsent(minute, k -> 0);
countersByMinute.put(minute, counter + totalPowderToConcrete);
}

public int totalTransformationsInTheLastHour()
{
int total = 0;
Instant now = Instant.now();
Iterator<Map.Entry<Instant, Integer>> it = countersByMinute.entrySet().iterator();

while (it.hasNext())
{
Map.Entry<Instant, Integer> entry = it.next();

if (ChronoUnit.MINUTES.between(entry.getKey(), now) >= 60) { it.remove(); }
else { total += entry.getValue(); }
}

return total;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
* Copyright © 2022, RezzedUp and Contributors <https://github.com/LeafCommunity/ConcreteMixer>
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
@NonNullPackage
package community.leaf.survival.concretemixer.metrics;

import pl.tlinkowski.annotation.basic.NonNullPackage;

0 comments on commit 42ed1c4

Please sign in to comment.