package org.atlasapi.system;

import com.datastax.driver.core.Session;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;
import java.util.Map.Entry;

import static com.google.common.base.Preconditions.checkNotNull;

@Controller
public class HealthController {

    private final Gson gson;
    private final Session cassandra;

    public HealthController(Session cassandra) {
        this.cassandra = cassandra;
        this.gson = new GsonBuilder().create();
    }

    @RequestMapping("/system/threads")
    public void showThreads(HttpServletResponse response) throws IOException {
        response.setContentType("text/html");
        response.setStatus(200);
        ServletOutputStream out = response.getOutputStream();
        out.print("<html><body>");
        final Map<Thread, StackTraceElement[]> traces = Thread.getAllStackTraces();
        for (Entry<Thread, StackTraceElement[]> entry : traces.entrySet()) {
            out.print(String.format("<font color='blue'>Stack trace for thread '%s'</font>:<br/>", entry.getKey().getName()));
            for (StackTraceElement e : entry.getValue()) {
                out.print(e.toString() + "<br/>");
            }
            out.print("<hr/>");
        }
        out.print("</body></html>");
        out.close();
    }

    @RequestMapping("/system/cassandra")
    public void showCassandraMetrics(HttpServletResponse response) throws IOException {
        gson.toJson(cassandra.getCluster().getMetrics().getRegistry(), response.getWriter());
        response.setStatus(200);
        response.flushBuffer();
    }
}
@@ -125,24 +125,60 @@
 sonBuilder;%0A
+import com.google.gson.JsonElement;%0A
 import org.s
@@ -1714,16 +1714,35 @@
 %0A       
+ JsonElement json =
  gson.to
@@ -1745,16 +1745,20 @@
 n.toJson
+Tree
 (cassand
@@ -1803,29 +1803,75 @@
 ry()
-, response.getWriter(
+.getMetrics());%0A        response.getWriter().write(gson.toJson(json
 ));%0A

Correct serialisation of metrics registry