/*
 * Druid - a distributed column store.
 * Copyright (C) 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 com.metamx.druid.http;

import com.google.common.collect.ImmutableMap;
import com.metamx.emitter.core.Emitter;
import com.metamx.emitter.core.Event;
import org.joda.time.DateTime;

import java.util.Map;

public class EmittingRequestLogger implements RequestLogger
{

  final String service;
  final String host;
  final Emitter emitter;
  final String feed;

  public EmittingRequestLogger(String service, String host, Emitter emitter, String feed)
  {
    this.emitter = emitter;
    this.host = host;
    this.service = service;
    this.feed = feed;
  }

  @Override
  public void log(final RequestLogLine requestLogLine) throws Exception
  {
    emitter.emit(new RequestLogEvent(service, host, feed, requestLogLine));
  }

  public static class RequestLogEvent implements Event
  {
    final String service;
    final String host;
    final String feed;
    final RequestLogLine request;

    public RequestLogEvent(String service, String host, String feed, RequestLogLine request)
    {
      this.service = service;
      this.host = host;
      this.request = request;
      this.feed = feed;
    }

    @Override
    public Map<String, Object> toMap()
    {
      return ImmutableMap.<String, Object>builder()
              .put("feed", getFeed())
              .put("timestamp", request.getTimestamp())
              .put("service", service)
              .put("host", host)
              .put("query", request.getQuery())
              .put("remoteAddr", request.getRemoteAddr())
              .build();
    }

    @Override
    public String getFeed()
    {
      return feed;
    }

    @Override
    public DateTime getCreatedTime()
    {
      return request.getTimestamp();
    }

    @Override
    public boolean isSafeToBuffer()
    {
      return true;
    }
  }
}

@@ -840,16 +840,121 @@
 .http;%0A%0A
+import com.fasterxml.jackson.annotation.JsonProperty;%0Aimport com.fasterxml.jackson.annotation.JsonValue;%0A
 import c
@@ -992,16 +992,47 @@
 bleMap;%0A
+import com.metamx.druid.Query;%0A
 import c
@@ -2081,417 +2081,285 @@
     
-public Map%3CString, Object%3E toMap()%0A    %7B%0A      return ImmutableMap.%3CString, Object%3Ebuilder()%0A              .put(%22feed%22, getFeed())%0A              .put(%22timestamp%22, request.getTimestamp())%0A              .put(%22service%22, service)%0A              .put(%22host%22, host)%0A              .put(%22query%22, request.getQuery())%0A              .put(%22remoteAddr%22, request.getRemoteAddr())%0A              .build();%0A    %7D%0A%0A    @Override
+// override JsonValue serialization, instead use annotations%0A    // to include type information for polymorphic Query objects%0A    @JsonValue(value=false)%0A    public Map%3CString, Object%3E toMap()%0A    %7B%0A      return ImmutableMap.of();%0A    %7D%0A%0A    @Override%0A    @JsonProperty(%22feed%22)
 %0A   
@@ -2421,32 +2421,63 @@
 %0A%0A    @Override%0A
+    @JsonProperty(%22timestamp%22)%0A
     public DateT
@@ -2539,32 +2539,431 @@
 stamp();%0A    %7D%0A%0A
+    @JsonProperty(%22service%22)%0A    public String getService()%0A    %7B%0A      return service;%0A    %7D%0A%0A    @JsonProperty(%22host%22)%0A    public String getHost()%0A    %7B%0A      return host;%0A    %7D%0A%0A    @JsonProperty(%22query%22)%0A    public Query getQuery()%0A    %7B%0A      return request.getQuery();%0A    %7D%0A%0A    @JsonProperty(%22remoteAddr%22)%0A    public String getRemoteAddr()%0A    %7B%0A      return request.getRemoteAddr();%0A    %7D%0A%0A
     @Override%0A  

fix serialization of request logs