/*
 * Copyright (C) 2018 the original author or authors.
 *
 * This file is part of jBB Application Project.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  You may obtain a copy of the License at
 *        http://www.apache.org/licenses/LICENSE-2.0
 */

package org.jbb.lib.logging.health;

import ch.qos.logback.core.status.Status;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import java.time.Instant;
import java.util.List;
import java.util.NavigableMap;
import lombok.experimental.UtilityClass;

@UtilityClass
public class LogbackStateStorage {

    public static final int TAIL_SIZE = 3;

    private static final NavigableMap<Long, Status> STATUSES = Maps
        .synchronizedNavigableMap(Maps.newTreeMap());

    public static void putStatus(Status status) {
        STATUSES.put(Instant.now().toEpochMilli(), status);
        // FIXME clean up old entries
    }

    public static List<Status> getLastStatuses() {
        return Lists.newLinkedList(Iterables.limit(STATUSES.descendingMap().values(), TAIL_SIZE));
    }

}

@@ -316,50 +316,8 @@
 h;%0A%0A
-import ch.qos.logback.core.status.Status;%0A
 impo
@@ -356,16 +356,16 @@
 rables;%0A
+
 import c
@@ -435,16 +435,17 @@
 t.Maps;%0A
+%0A
 import j
@@ -462,16 +462,44 @@
 nstant;%0A
+import java.util.ArrayList;%0A
 import j
@@ -543,16 +543,59 @@
 ableMap;
+%0A%0Aimport ch.qos.logback.core.status.Status;
 %0Aimport 
@@ -721,16 +721,65 @@
 IZE = 3;
+%0A    public static final int STORAGE_LIMIT = 100;
 %0A%0A    pr
@@ -1017,37 +1017,336 @@
     
-// FIXME clean up old entries
+cleanUpOldStatuses();%0A    %7D%0A%0A    private static void cleanUpOldStatuses() %7B%0A        ArrayList%3CLong%3E sortedStatusesTimes = Lists.newArrayList(STATUSES.descendingMap().keySet());%0A        Long limitTime = sortedStatusesTimes.get(Math.min(sortedStatusesTimes.size(), STORAGE_LIMIT) - 1);%0A        STATUSES.headMap(limitTime).clear();
 %0A   

Clean up LogbackStateStorage