package org.orienteer.metrics;

import java.util.HashMap;
import java.util.Map;

import org.apache.wicket.ISessionListener;
import org.apache.wicket.Session;
import org.apache.wicket.request.http.WebRequest;
import org.orienteer.core.OrienteerWebApplication;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.prometheus.client.CollectorRegistry;
import io.prometheus.client.Counter;
import io.prometheus.client.Gauge;
import io.prometheus.client.Histogram;

/**
 * {@link ISessionListener} to monitor creation and unbounding of sessions
 */
public class OMetricSessionListener implements ISessionListener {
	
	private static final Logger LOG = LoggerFactory.getLogger(OMetricSessionListener.class);
	
	private static OMetricSessionListener listener;
	
	
	private static final Counter GAUGE_CREATED_SESSIONS = Counter.build()
														.namespace("wicket")
														.subsystem("session")
														.name("created")
														.help("Sessions created")
														.create();
	
	private static final Counter GAUGE_UNBOUND_SESSIONS = Counter.build()
															.namespace("wicket")
															.subsystem("session")
															.name("unbound")
															.help("Sessions unbound")
															.create();
	
	
	private OMetricSessionListener() {
		CollectorRegistry.defaultRegistry.register(GAUGE_CREATED_SESSIONS);
		CollectorRegistry.defaultRegistry.register(GAUGE_UNBOUND_SESSIONS);
	}

	@Override
	public void onCreated(Session session) {
		GAUGE_CREATED_SESSIONS.inc();
	}

	@Override
	public void onUnbound(String sessionId) {
		GAUGE_UNBOUND_SESSIONS.inc();
	}
	
	protected void onDestroy() {
		CollectorRegistry.defaultRegistry.unregister(GAUGE_CREATED_SESSIONS);
		CollectorRegistry.defaultRegistry.unregister(GAUGE_UNBOUND_SESSIONS);
	}
	
	public static synchronized void install(OrienteerWebApplication app) {
		if(listener!=null) deinstall(app);
		listener = new OMetricSessionListener();
		app.getSessionListeners().add(listener);
	}

	public static synchronized void deinstall(OrienteerWebApplication app) {
		if(listener!=null) {
			listener.onDestroy();
			app.getSessionListeners().remove(listener);
			listener = null;
		}
	}

}

@@ -188,23 +188,113 @@
 est.
-http.WebRequest
+Request;%0Aimport org.apache.wicket.request.http.WebRequest;%0Aimport org.apache.wicket.session.ISessionStore
 ;%0Aim
@@ -705,16 +705,44 @@
 Listener
+, ISessionStore.BindListener
  %7B%0A%09%0A%09pr
@@ -913,21 +913,23 @@
 Counter 
-GAUGE
+COUNTER
 _CREATED
@@ -1155,21 +1155,259 @@
 Counter 
-GAUGE
+COUNTER_BOUND_SESSIONS = Counter.build()%0A%09%09%09%09%09%09%09%09%09%09%09%09%09%09.namespace(%22wicket%22)%0A%09%09%09%09%09%09%09%09%09%09%09%09%09%09.subsystem(%22session%22)%0A%09%09%09%09%09%09%09%09%09%09%09%09%09%09.name(%22bound%22)%0A%09%09%09%09%09%09%09%09%09%09%09%09%09%09.help(%22Sessions bound%22)%0A%09%09%09%09%09%09%09%09%09%09%09%09%09%09.create();%0A%09%0A%09private static final Counter COUNTER
 _UNBOUND
@@ -1683,37 +1683,39 @@
 gistry.register(
-GAUGE
+COUNTER
 _CREATED_SESSION
@@ -1755,37 +1755,109 @@
 gistry.register(
-GAUGE
+COUNTER_BOUND_SESSIONS);%0A%09%09CollectorRegistry.defaultRegistry.register(COUNTER
 _UNBOUND_SESSION
@@ -1919,28 +1919,145 @@
 on) %7B%0A%09%09
-GAUGE_CREATE
+COUNTER_CREATED_SESSIONS.inc();%0A%09%7D%0A%09%0A%09@Override%0A%09public void bindingSession(Request request, Session newSession) %7B%0A%09%09COUNTER_BOUN
 D_SESSIO
@@ -2126,21 +2126,23 @@
 Id) %7B%0A%09%09
-GAUGE
+COUNTER
 _UNBOUND
@@ -2240,21 +2240,23 @@
 egister(
-GAUGE
+COUNTER
 _CREATED
@@ -2318,13 +2318,87 @@
 ter(
-GAUGE
+COUNTER_BOUND_SESSIONS);%0A%09%09CollectorRegistry.defaultRegistry.unregister(COUNTER
 _UNB
@@ -2613,16 +2613,72 @@
 tener);%0A
+%09%09app.getSessionStore().registerBindListener(listener);%0A
 %09%7D%0A%0A%09pub
@@ -2795,16 +2795,75 @@
 troy();%0A
+%09%09%09app.getSessionStore().unregisterBindListener(listener);%0A
 %09%09%09app.g
@@ -2929,10 +2929,11 @@
 %09%09%7D%0A%09%7D%0A%0A
+%0A
 %7D%0A

Add counter for session bind for issue #395