package com.hubspot.singularity.resources;

import java.lang.management.ManagementFactory;
import java.lang.management.RuntimeMXBean;

import javax.ws.rs.GET;
import javax.ws.rs.Path;

import org.apache.mesos.Protos;

import com.google.inject.Inject;
import com.hubspot.singularity.SingularityManaged;
import com.hubspot.singularity.SingularityState;
import com.hubspot.singularity.data.RequestManager;
import com.hubspot.singularity.data.TaskManager;

@Path("/state")
public class StateResource {

  private final RequestManager requestManager;
  private final TaskManager taskManager;
  private final SingularityManaged managed;
  
  @Inject
  public StateResource(RequestManager requestManager, TaskManager taskManager, SingularityManaged managed) {
    this.requestManager = requestManager;
    this.taskManager = taskManager;
    this.managed = managed;
  }

  @GET
  public SingularityState getState() {
    final boolean isMaster = managed.isMaster();
    final Protos.Status driverStatus = managed.getCurrentStatus();
    
    final RuntimeMXBean mxBean = ManagementFactory.getRuntimeMXBean();
    final long uptime = mxBean.getUptime();
    final long startTime = System.currentTimeMillis() - uptime;
    
    final int activeTasks = taskManager.getNumActiveTasks();
    final int scheduledTasks = taskManager.getNumScheduledTasks();
    
    final int requests = requestManager.getNumRequests();
    final int pendingRequests = requestManager.getSizeOfPendingQueue();
    final int cleaningRequests = requestManager.getSizeOfCleanupQueue();
    
    return new SingularityState(isMaster, startTime, activeTasks, requests, scheduledTasks, pendingRequests, cleaningRequests, driverStatus != null ? driverStatus.name() : "-");
  }
  
}

@@ -176,16 +176,80 @@
 rs.Path;
+%0Aimport javax.ws.rs.Produces;%0Aimport javax.ws.rs.core.MediaType;
 %0A%0Aimport
@@ -526,16 +526,58 @@
 state%22)%0A
+@Produces(%7B MediaType.APPLICATION_JSON %7D)%0A
 public c

add json