// Copyright 2021 The Terasology Foundation
// SPDX-License-Identifier: Apache-2.0

package org.terasology.engine.rendering.nui.layers.ingame.metrics;

import io.micrometer.core.instrument.distribution.HistogramSnapshot;
import io.micrometer.core.instrument.search.Search;
import org.terasology.engine.monitoring.DisplayMetricsMonitor;

import java.util.concurrent.TimeUnit;

final class RunningThreadsMode extends MetricsMode implements AutoCloseable {

     RunningThreadsMode() {
        super("\n- Task Metrics -");
    }

    @Override
    public String getMetrics() {

        StringBuilder builder = new StringBuilder();
        builder.append(getName());
        builder.append("\n");

        Search.in(DisplayMetricsMonitor.metricRegistry)
                .tag("monitor","display-metric")
                .timers().forEach(k -> {

            HistogramSnapshot snapshot = k.takeSnapshot();
            long count = snapshot.count();
            double throughput = (count / (double) DisplayMetricsMonitor.captureDuration.toMillis());
            builder.append(k.getId().getName());
            builder.append(" : ");
            builder.append(" throughput: ");
            builder.append(Double.isNaN(throughput) ? "NAN" : String.format("%,10.4f", throughput)).append(" /s");
            builder.append(" mean: ").append(String.format("%,10.4f",k.mean(TimeUnit.MILLISECONDS))).append(" ms");
            builder.append(" max: ").append(String.format("%,10.4f",k.max(TimeUnit.MILLISECONDS))).append(" ms");

            builder.append("\n");
        });
        return builder.toString();
    }

    @Override
    public boolean isAvailable() {
        return true;
    }

    @Override
    public boolean isPerformanceManagerMode() {
        return false;
    }

    @Override
    public void close() throws Exception {

    }
}

@@ -932,24 +932,58 @@
 ot.count();%0A
+%0A            if (count %3E 0) %7B%0A    
             
@@ -1075,32 +1075,36 @@
 ));%0A            
+    
 builder.append(k
@@ -1128,32 +1128,36 @@
 ));%0A            
+  
+  
 builder.append(%22
@@ -1159,24 +1159,28 @@
 end(%22 : %22);%0A
+    
             
@@ -1208,24 +1208,28 @@
 ughput: %22);%0A
+    
             
@@ -1335,32 +1335,36 @@
 %22);%0A            
+    
 builder.append(%22
@@ -1404,16 +1404,17 @@
 ,10.4f%22,
+ 
 k.mean(T
@@ -1456,32 +1456,36 @@
 %22);%0A            
+    
 builder.append(%22
@@ -1524,16 +1524,17 @@
 ,10.4f%22,
+ 
 k.max(Ti
@@ -1567,25 +1567,28 @@
 end(%22 ms%22);%0A
-%0A
+    
             
@@ -1605,24 +1605,38 @@
 pend(%22%5Cn%22);%0A
+            %7D%0A
         %7D);%0A

remove empty snapshots