/*
 * Druid - a distributed column store.
 * Copyright (C) 2012  Metamarkets Group Inc.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

package com.metamx.druid.http;

import com.metamx.druid.master.DruidMaster;
import com.metamx.druid.master.LoadPeonCallback;

import javax.inject.Inject;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.core.Response;
import java.util.List;

/**
 */
@Path("/master")
public class MasterResource
{
  private final DruidMaster master;

  @Inject
  public MasterResource(
      DruidMaster master
  )
  {
    this.master = master;
  }

  @POST
  @Path("/move")
  @Consumes("application/json")
  public Response moveSegment(List<SegmentToMove> segmentsToMove)
  {
    Response resp = Response.status(Response.Status.OK).build();
    for (SegmentToMove segmentToMove : segmentsToMove) {
      try {
        master.moveSegment(
            segmentToMove.getFromServer(),
            segmentToMove.getToServer(),
            segmentToMove.getSegmentName(),
            new LoadPeonCallback()
            {
              @Override
              protected void execute()
              {
                return;
              }
            }
        );
      }
      catch (Exception e) {
        resp = Response
            .status(Response.Status.BAD_REQUEST)
            .entity(e.getMessage())
            .build();
        break;
      }
    }
    return resp;
  }

  @POST
  @Path("/drop")
  @Consumes("application/json")
  public Response dropSegment(List<SegmentToDrop> segmentsToDrop)
  {
    Response resp = Response.status(Response.Status.OK).build();
    for (SegmentToDrop segmentToDrop : segmentsToDrop) {
      try {
        master.dropSegment(
            segmentToDrop.getFromServer(), segmentToDrop.getSegmentName(), new LoadPeonCallback()
        {
          @Override
          protected void execute()
          {
            return;
          }
        }
        );
      }
      catch (Exception e) {
        resp = Response
            .status(Response.Status.BAD_REQUEST)
            .entity(e.getMessage())
            .build();
        break;
      }
    }
    return resp;
  }


}
@@ -991,16 +991,40 @@
 nsumes;%0A
+import javax.ws.rs.GET;%0A
 import j
@@ -1065,16 +1065,45 @@
 s.Path;%0A
+import javax.ws.rs.Produces;%0A
 import j
@@ -1150,16 +1150,38 @@
 il.List;
+%0Aimport java.util.Map;
 %0A%0A/**%0A *
@@ -2928,10 +2928,159 @@
 p;%0A  %7D%0A%0A
+  @GET%0A  @Path(%22/loadstatus%22)%0A  @Produces(%22application/json%22)%0A  public Map%3CString, Double%3E getLoadStatus()%0A  %7B%0A    return master.getLoadStatus();%0A  %7D
 %0A%7D

revert removal of master loadstatus