package no.panopticon.api.internal;

import no.panopticon.storage.RunningUnit;
import no.panopticon.storage.StatusSnapshot;

import java.util.List;
import java.util.stream.Collectors;

import static java.util.Comparator.comparing;

public class UnitDetails {
    public final String environment;
    public final String system;
    public final String component;
    public final String server;
    public final List<Measurement> measurements;

    public UnitDetails(String environment, String system, String component, String server, List<Measurement> measurements) {
        this.environment = environment;
        this.system = system;
        this.component = component;
        this.server = server;
        this.measurements = measurements;
    }

    public static UnitDetails fromStoredStatus(RunningUnit unit, StatusSnapshot statusSnapshot) {
        return new UnitDetails(
                unit.getEnvironment(),
                unit.getSystem(),
                unit.getComponent(),
                unit.getServer(),
                statusSnapshot.getMeasurements().stream()
                        .map(s -> new Measurement(s.getKey(), s.getStatus(), s.getDisplayValue(), s.getNumericValue()))
                        .sorted(comparing(m -> m.key))
                        .collect(Collectors.toList())
        );
    }

    public static class Measurement {
        public final String key;
        public final String status;
        public final String displayValue;
        public final long numericValue;

        public Measurement(String key, String status, String displayValue, long numericValue) {
            this.key = key;
            this.status = status;
            this.displayValue = displayValue;
            this.numericValue = numericValue;
        }
    }
}

@@ -135,20 +135,26 @@
 va.util.
-List
+Comparator
 ;%0Aimport
@@ -168,35 +168,21 @@
 til.
-stream.Collectors;%0A
+List;
 %0Aimport 
 stat
@@ -177,23 +177,16 @@
 %0Aimport 
-static 
 java.uti
@@ -191,28 +191,25 @@
 til.
-Comparator.comparing
+stream.Collectors
 ;%0A%0Ap
@@ -1218,95 +1218,470 @@
 ted(
-comparing(m -%3E m.key))%0A                        .collect(Collectors.toList())%0A        );
+Comparator.%3CMeasurement, Integer%3Ecomparing(m -%3E toSortValue(m.status)).thenComparing(m -%3E m.key))%0A                        .collect(Collectors.toList())%0A        );%0A    %7D%0A%0A    private static Integer toSortValue(String status) %7B%0A        switch (status) %7B%0A            case %22ERROR%22:%0A                return 1;%0A            case %22WARN%22:%0A                return 2;%0A            case %22INFO%22:%0A                return 3;%0A            default:%0A                return 4;%0A        %7D
 %0A   

Add sorting to backend