/*
 * Druid - a distributed column store.
 * Copyright (C) 2012, 2013  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 io.druid.server.metrics;

import com.google.inject.Inject;
import com.metamx.emitter.service.ServiceEmitter;
import com.metamx.emitter.service.ServiceMetricEvent;
import com.metamx.metrics.AbstractMonitor;
import io.druid.client.DruidServerConfig;
import io.druid.server.coordination.ServerManager;

import java.util.Map;

public class ServerMonitor extends AbstractMonitor
{
  private final DruidServerConfig serverConfig;
  private final ServerManager serverManager;

  @Inject
  public ServerMonitor(
      DruidServerConfig serverConfig,
      ServerManager serverManager
  )
  {
    this.serverConfig = serverConfig;
    this.serverManager = serverManager;
  }

  @Override
  public boolean doMonitor(ServiceEmitter emitter)
  {
    emitter.emit(new ServiceMetricEvent.Builder().build("server/segment/max", serverConfig.getMaxSize()));
    for (Map.Entry<String, Long> entry : serverManager.getDataSourceSizes().entrySet()) {
      String dataSource = entry.getKey();
      long used = entry.getValue();
      final ServiceMetricEvent.Builder builder = new ServiceMetricEvent.Builder().setUser1(dataSource)
                                                                                 .setUser2(serverConfig.getTier());

      emitter.emit(builder.build("server/segment/used", used));
      emitter.emit(builder.build("server/segment/usedPercent", used / (double) serverConfig.getMaxSize()));
    }

    for (Map.Entry<String, Long> entry : serverManager.getDataSourceCounts().entrySet()) {
      String dataSource = entry.getKey();
      long count = entry.getValue();
      final ServiceMetricEvent.Builder builder = new ServiceMetricEvent.Builder().setUser1(dataSource)
                                                                                 .setUser2(serverConfig.getTier());

      emitter.emit(builder.build("server/segment/count", count));
    }

    return true;
  }
}

@@ -1659,24 +1659,74 @@
 axSize()));%0A
+    long totalUsed = 0;%0A    long totalCount = 0;%0A%0A
     for (Map
@@ -1795,32 +1795,32 @@
 ).entrySet()) %7B%0A
-
       String dat
@@ -1873,32 +1873,58 @@
 try.getValue();%0A
+      totalUsed += used;%0A%0A
       final Serv
@@ -2472,24 +2472,51 @@
 getValue();%0A
+      totalCount += count;%0A
       final 
@@ -2785,31 +2785,408 @@
 %22, count));%0A
-
     %7D%0A%0A
+    final ServiceMetricEvent.Builder builder = new ServiceMetricEvent.Builder().setUser2(serverConfig.getTier());%0A    emitter.emit(builder.build(%22server/segment/totalUsed%22, totalUsed));%0A    emitter.emit(builder.build(%22server/segment/totalUsedPercent%22, totalUsed / (double) serverConfig.getMaxSize()));%0A    emitter.emit(builder.build(%22server/segment/totalCount%22, totalCount));%0A%0A
     return t

add total segment size/count metric