// 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();

            if (count > 0) {
                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 {

    }
}

@@ -940,42 +940,8 @@
 ();%0A
-%0A            if (count %3E 0) %7B%0A    
     
@@ -1041,36 +1041,32 @@
 ));%0A            
-    
 builder.append(k
@@ -1090,36 +1090,32 @@
 ));%0A            
-  
-  
 builder.append(%22
@@ -1117,28 +1117,24 @@
 end(%22 : %22);%0A
-    
             
@@ -1162,28 +1162,24 @@
 ughput: %22);%0A
-    
             
@@ -1285,36 +1285,32 @@
 %22);%0A            
-    
 builder.append(%22
@@ -1350,17 +1350,16 @@
 ,10.4f%22,
- 
 k.mean(T
@@ -1401,36 +1401,32 @@
 %22);%0A            
-    
 builder.append(%22
@@ -1465,17 +1465,16 @@
 ,10.4f%22,
- 
 k.max(Ti
@@ -1503,36 +1503,33 @@
 .append(%22 ms%22);%0A
-    
+%0A
             buil
@@ -1550,22 +1550,8 @@
 %22);%0A
-            %7D%0A
     

Revert "chore: remove empty snapshots"