package com.informatica.surf.sources.kinesis;

import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.AWSCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.kinesis.clientlibrary.lib.worker.KinesisClientLibConfiguration;
import com.amazonaws.services.kinesis.clientlibrary.lib.worker.Worker;
import com.informatica.vds.api.VDSConfiguration;
import com.informatica.vds.api.VDSEventList;
import com.informatica.vds.api.VDSSource;
import com.lmax.disruptor.EventHandler;
import com.lmax.disruptor.RingBuffer;
import com.lmax.disruptor.dsl.Disruptor;

import java.io.FileReader;
import java.io.IOException;
import java.net.InetAddress;
import java.util.*;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

/**
 * Created by jerry on 11/03/14.
 */
public class KinesisSource implements VDSSource {
    private Worker _worker;
    private final ArrayBlockingQueue<KinesisEvent> _eventList = new ArrayBlockingQueue<>(1000);
    private final int MAX_EVENTS = 100; // Maximum number of events to send to Surf at one time
    @Override
    public void open(VDSConfiguration ctx) throws Exception {

        Executor executor = Executors.newCachedThreadPool();
        Disruptor<KinesisEvent> disruptor = new Disruptor<>(KinesisEvent.EVENT_FACTORY, 128, executor);
        disruptor.handleEventsWith(new EventHandler<KinesisEvent>() {
            @Override
            public void onEvent(KinesisEvent kinesisEvent, long l, boolean b) throws Exception {
                _eventList.put(kinesisEvent);
            }
        });
        RingBuffer<KinesisEvent> buffer = disruptor.start();

        // Generate a unique worker ID
        String workerId = InetAddress.getLocalHost().getCanonicalHostName() + ":" + UUID.randomUUID();
        String accessid = ctx.getString("aws-access-key-id");
        String secretkey = ctx.getString("aws-secret-key");
        String streamname = ctx.getString("aws-kinesis-stream-name");
        String appName = ctx.getString("application-name");
        BasicAWSCredentials creds = new BasicAWSCredentials(accessid, secretkey);
        CredProvider credprovider = new CredProvider(creds);
        KinesisClientLibConfiguration config = new KinesisClientLibConfiguration(appName, streamname,  credprovider, workerId);

        _worker = new Worker(new RecordProcessorFactory(buffer), config, new MetricsFactory());
    }

    @Override
    public void read(VDSEventList readEvents) throws Exception {
        int i = 0;
        while(i < MAX_EVENTS && !_eventList.isEmpty()){
            KinesisEvent evt = _eventList.poll();
            if(evt == null){
                break;
            }
            byte buf[] = evt.getData().getBytes();
            readEvents.addEvent(buf, buf.length);
        }
    }

    @Override
    public void close() throws IOException {
        _worker.shutdown();

    }
    static class CredProvider implements AWSCredentialsProvider {
        AWSCredentials _creds;
        public CredProvider(AWSCredentials creds){
            _creds = creds;
        }
        @Override
        public AWSCredentials getCredentials() {
            return _creds;
        }

        @Override
        public void refresh() {
            // NOOP
        }

    }
}

@@ -598,16 +598,73 @@
 sruptor;
+%0Aimport org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;
 %0A%0Aimport
@@ -885,16 +885,54 @@
 ecutors;
+%0Aimport java.util.concurrent.TimeUnit;
 %0A%0A/**%0A *
@@ -1235,16 +1235,153 @@
 one time
+; also max records fetched from Kinesis at a time%0A    private static final Logger _logger = LoggerFactory.getLogger(KinesisSource.class);
 %0A    @Ov
@@ -1449,16 +1449,68 @@
 ption %7B%0A
+        _logger.info(%22Initializing Kinesis Source%22);
 %0A       
@@ -1877,34 +1877,171 @@
    _
-eventList.put(kinesisEvent
+logger.debug(%22Received a Kinesis Event%22);%0A                _eventList.put(kinesisEvent);%0A                _logger.debug(%22eventlist size is now %7B%7D%22, _eventList.size()
 );%0A 
@@ -2790,16 +2790,60 @@
 orkerId)
+%0A                .withMaxRecords(MAX_EVENTS)
 ;%0A%0A     
@@ -2929,24 +2929,61 @@
 Factory());%0A
+        new Thread(_worker).start();%0A
     %7D%0A%0A    @
@@ -3107,33 +3107,8 @@
 ENTS
- && !_eventList.isEmpty()
 )%7B%0A 
@@ -3153,16 +3153,42 @@
 st.poll(
+300, TimeUnit.MILLISECONDS
 );%0A     
@@ -3211,16 +3211,66 @@
  null)%7B%0A
+                _logger.warn(%22Got a null event%22);%0A
         
@@ -3399,16 +3399,33 @@
 ength);%0A
+            i++;%0A
         

Improve logging, and handle floods of records better