/*
 * Copyright (C) 2015 SoftIndex LLC.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.datakernel.jmx;

import java.util.ArrayList;
import java.util.List;

import static java.util.Arrays.asList;

public final class ExceptionStats implements JmxStats<ExceptionStats> {
	private Throwable throwable;
	private Object causeObject;
	private int count;

	public ExceptionStats() {}

	public void recordException(Throwable throwable, Object causeObject) {
		this.count++;
		this.throwable = throwable;
		this.causeObject = causeObject;
	}

	public void resetStats() {
		this.count = 0;
		this.throwable = null;
		this.causeObject = null;
	}

	@Override
	public void add(ExceptionStats counter) {
		this.count += counter.count;
		if (counter.count > this.count) {
			this.throwable = counter.throwable;
			this.causeObject = counter.causeObject;
		}
	}

	@JmxAttribute
	public int getTotal() {
		return count;
	}

	@JmxAttribute
	public String getLastExceptionType() {
		return throwable != null ? throwable.getClass().getName() : "";
	}

	public Throwable getLastException() {
		return throwable;
	}

	@JmxAttribute
	public String getLastExceptionMessage() {
		return throwable != null ? throwable.getMessage() : "";
	}

	@JmxAttribute
	public Object getLastExceptionCause() {
		return causeObject;
	}

	@JmxAttribute
	public List<String> getLastExceptionStackTrace() {
		if (throwable != null) {
			return asList(MBeanFormat.formatException(throwable));
		} else {
			return new ArrayList<>();
		}
	}
}

@@ -636,22 +636,80 @@
 ava.
-util.ArrayList
+text.SimpleDateFormat;%0Aimport java.util.ArrayList;%0Aimport java.util.Date
 ;%0Aim
@@ -842,16 +842,123 @@
 tats%3E %7B%0A
+%09public static final SimpleDateFormat TIMESTAMP_FORMAT = new SimpleDateFormat(%22yyyy-MM-dd HH:mm:ss.SSS%22);%0A%0A
 %09private
@@ -1027,16 +1027,54 @@
 t count;
+%0A%09private long lastExceptionTimestamp;
 %0A%0A%09publi
@@ -1244,24 +1244,84 @@
 auseObject;%0A
+%09%09this.lastExceptionTimestamp = System.currentTimeMillis();%0A
 %09%7D%0A%0A%09public 
@@ -1457,21 +1457,21 @@
 onStats 
-count
+anoth
 er) %7B%0A%09%09
@@ -1484,21 +1484,21 @@
 ount += 
-count
+anoth
 er.count
@@ -1509,34 +1509,68 @@
 if (
-counter.count %3E this.count
+another.lastExceptionTimestamp %3E this.lastExceptionTimestamp
 ) %7B%0A
@@ -1589,21 +1589,21 @@
 wable = 
-count
+anoth
 er.throw
@@ -1630,21 +1630,21 @@
 bject = 
-count
+anoth
 er.cause
@@ -1651,16 +1651,81 @@
 Object;%0A
+%09%09%09this.lastExceptionTimestamp = another.lastExceptionTimestamp;%0A
 %09%09%7D%0A%09%7D%0A%0A
@@ -1905,24 +1905,156 @@
 ) : %22%22;%0A%09%7D%0A%0A
+%09@JmxAttribute%0A%09public String getLastExceptionTimestamp() %7B%0A%09%09return TIMESTAMP_FORMAT.format(new Date(lastExceptionTimestamp));%0A%09%7D%0A%0A
 %09public Thro

fix bug with ExceptionStats, add timestamp to Exception stats