/**
 * Copyright (C) FuseSource, Inc.
 * http://fusesource.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.fusesource.insight.log.elasticsearch;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.osgi.service.event.Event;
import org.osgi.service.event.EventConstants;
import org.osgi.service.event.EventHandler;

import static org.fusesource.insight.log.elasticsearch.ElasticSender.quote;

public class InsightEventHandler implements EventHandler {

    private String name;
    private String index;
    private String type;
    private ElasticSender sender;

    private SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getIndex() {
        return index;
    }

    public void setIndex(String index) {
        this.index = index;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public ElasticSender getSender() {
        return sender;
    }

    public void setSender(ElasticSender sender) {
        this.sender = sender;
    }

    public void init() {
        CreateIndexRequest request = new CreateIndexRequest(index);
        sender.createIndexIfNeeded(request);
    }

    public void handleEvent(final Event event) {
        try {
            StringBuilder writer = new StringBuilder();
            writer.append("{ \"host\": ");
            quote(name, writer);
            writer.append(", \"topic\": ");
            quote(event.getTopic(), writer);
            writer.append(", \"properties\": { ");
            boolean first = true;
            for (String name : event.getPropertyNames()) {
                if (first) {
                    first = false;
                } else {
                    writer.append(", ");
                }
                quote(name, writer);
                writer.append(": ");
                if (EventConstants.TIMESTAMP.equals(name)) {
                    quote(formatDate((Long) event.getProperty(name)), writer);
                } else {
                    quote(event.getProperty(name).toString(), writer);
                }
            }
            writer.append(" } }");

            IndexRequest request = new IndexRequest()
                    .index(index)
                    .type(type)
                    .source(writer.toString())
                    .create(true);
            sender.put(request);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private String formatDate(long timestamp) {
        return simpleDateFormat.format(new Date(timestamp));
    }

}

@@ -726,16 +726,89 @@
 il.Date;
+%0Aimport java.util.HashMap;%0Aimport java.util.concurrent.atomic.AtomicLong;
 %0A%0Aimport
@@ -1191,16 +1191,315 @@
 dler %7B%0A%0A
+    // Making an assumption here that we will log less than 1,000,000 events/sec,  next this JVM%0A    // restarts, the next sequence number should be %3C any previously generated sequence numbers.%0A    static final private AtomicLong SEQUENCE_COUNTER = new AtomicLong(System.currentTimeMillis()*1000);%0A%0A
     priv
@@ -1613,16 +1613,22 @@
 private 
+final 
 SimpleDa
@@ -2349,16 +2349,474 @@
 index);%0A
+%0A        HashMap%3CString, Object%3E not_analyzed = new HashMap%3CString, Object%3E();%0A        not_analyzed.put(%22type%22, %22string%22);%0A        not_analyzed.put(%22index%22, %22not_analyzed%22);%0A%0A        HashMap%3CString, Object%3E properties = new HashMap%3CString, Object%3E();%0A        properties.put(%22seq%22, not_analyzed);%0A%0A        HashMap%3CString, Object%3E options = new HashMap%3CString, Object%3E();%0A        options.put(%22properties%22, properties);%0A        request.mapping(type, options);%0A%0A
         
@@ -3046,32 +3046,112 @@
 (name, writer);%0A
+            writer.append(%22, %5C%22seq%5C%22 : %22 + SEQUENCE_COUNTER.incrementAndGet());%0A
             writ
@@ -3601,24 +3601,167 @@
             
+Object value = event.getProperty(name);%0A                if (value == null) %7B%0A                    writer.append(%22null%22);%0A                %7D else 
 if (EventCon
@@ -3789,16 +3789,41 @@
 ls(name)
+ && value instanceof Long
 ) %7B%0A    
@@ -3866,117 +3866,589 @@
 ng) 
-event.getProperty(name)), writer);%0A                %7D else %7B%0A                    quote(event.getProperty(name)
+value), writer);%0A                %7D else if (value.getClass().isArray()) %7B%0A                    writer.append(%22 %5B %22);%0A                    boolean vfirst = true;%0A                    for (Object v : ((Object%5B%5D) value)) %7B%0A                        if (!vfirst) %7B%0A                            writer.append(%22, %22);%0A                        %7D else %7B%0A                            vfirst = false;%0A                        %7D%0A                        quote(v.toString(), writer);%0A                    %7D%0A                    writer.append(%22 %5D %22);%0A                %7D else %7B%0A                    quote(value
 .toS
@@ -4528,25 +4528,24 @@
 nd(%22 %7D %7D%22);%0A
-%0A
             

Add a sequence to event admin stored in ES, improve json reprensentation