package yanagishima.servlet;

import io.prometheus.client.Counter;
import io.prometheus.client.Summary;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import yanagishima.config.YanagishimaConfig;
import yanagishima.service.PrestoService;
import yanagishima.util.JsonUtil;

import javax.inject.Inject;
import javax.inject.Singleton;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Optional;

@Singleton
public class PrestoAsyncServlet extends HttpServlet {

	private static Logger LOGGER = LoggerFactory.getLogger(PrestoAsyncServlet.class);

	private static final long serialVersionUID = 1L;

	private final PrestoService prestoService;

	private final YanagishimaConfig yanagishimaConfig;

	static final Summary requestLatency = Summary.build()
			.name("yanagishima_requests_latency_seconds")
			.help("Request latency in seconds.").register();
	static final Counter requestSuccesses = Counter.build()
			.name("yanagishima_requests_successes_total")
			.help("Request successes.").register();
	static final Counter requestFailures = Counter.build()
			.name("yanagishima_requests_failures_total")
			.help("Request failures.").register();

	@Inject
	public PrestoAsyncServlet(PrestoService prestoService, YanagishimaConfig yanagishimaConfig) {
		this.prestoService = prestoService;
		this.yanagishimaConfig = yanagishimaConfig;
	}

	@Override
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		Summary.Timer requestTimer = requestLatency.startTimer();
		
		HashMap<String, Object> retVal = new HashMap<String, Object>();
		
		try {
			Optional<String> queryOptional = Optional.ofNullable(request.getParameter("query"));
			queryOptional.ifPresent(query -> {
				String userName = request.getHeader(yanagishimaConfig.getAuditHttpHeaderName());
				String datasource = Optional.ofNullable(request.getParameter("datasource")).get();
				if(userName != null) {
					LOGGER.info(String.format("%s executed %s in %s", userName, query, datasource));
				}
				String queryid = prestoService.doQueryAsync(datasource, query, userName);
				retVal.put("queryid", queryid);
				requestSuccesses.inc();
			});
		} catch (Throwable e) {
			LOGGER.error(e.getMessage(), e);
			retVal.put("error", e.getMessage());
			requestFailures.inc();
		} finally {
			requestTimer.observeDuration();
		}

		JsonUtil.writeJSON(response, retVal);

	}

}

@@ -27,82 +27,8 @@
 t;%0A%0A
-import io.prometheus.client.Counter;%0Aimport io.prometheus.client.Summary;%0A
 impo
@@ -48,16 +48,16 @@
 Logger;%0A
+
 import o
@@ -817,460 +817,8 @@
 g;%0A%0A
-%09static final Summary requestLatency = Summary.build()%0A%09%09%09.name(%22yanagishima_requests_latency_seconds%22)%0A%09%09%09.help(%22Request latency in seconds.%22).register();%0A%09static final Counter requestSuccesses = Counter.build()%0A%09%09%09.name(%22yanagishima_requests_successes_total%22)%0A%09%09%09.help(%22Request successes.%22).register();%0A%09static final Counter requestFailures = Counter.build()%0A%09%09%09.name(%22yanagishima_requests_failures_total%22)%0A%09%09%09.help(%22Request failures.%22).register();%0A%0A
 %09@In
@@ -1144,71 +1144,8 @@
  %7B%0A%0A
-%09%09Summary.Timer requestTimer = requestLatency.startTimer();%0A%09%09%0A
 %09%09Ha
@@ -1752,36 +1752,8 @@
 d);%0A
-%09%09%09%09requestSuccesses.inc();%0A
 %09%09%09%7D
@@ -1817,16 +1817,16 @@
 (), e);%0A
+
 %09%09%09retVa
@@ -1861,83 +1861,8 @@
 ));%0A
-%09%09%09requestFailures.inc();%0A%09%09%7D finally %7B%0A%09%09%09requestTimer.observeDuration();%0A
 %09%09%7D%0A

remove prometheus