package com.hpcloud.mon.persister.repository;

import com.codahale.metrics.Timer;
import com.hpcloud.mon.common.event.AlarmStateTransitionedEvent;
import com.hpcloud.mon.persister.configuration.MonPersisterConfiguration;
import io.dropwizard.setup.Environment;
import org.skife.jdbi.v2.DBI;
import org.skife.jdbi.v2.PreparedBatch;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.inject.Inject;
import java.security.NoSuchAlgorithmException;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

public class VerticaAlarmStateHistoryRepository extends VerticaRepository {

    private static final Logger logger = LoggerFactory.getLogger(VerticaAlarmStateHistoryRepository.class);
    private final MonPersisterConfiguration configuration;
    private final Environment environment;

    private static final String SQL_INSERT_INTO_ALARM_HISTORY =
            "insert into MonAlarms.StateHistory (tenant_id, alarm_id, alarm_name, alarm_description, old_state, new_state, reason, time_stamp) values (:tenant_id, :alarm_id, :alarm_name, :alarm_description, :old_state, :new_state, :reason, :time_stamp)";
    private PreparedBatch batch;
    private final Timer commitTimer;
    private final SimpleDateFormat simpleDateFormat;

    @Inject
    public VerticaAlarmStateHistoryRepository(DBI dbi, MonPersisterConfiguration configuration,
                                              Environment environment) throws NoSuchAlgorithmException, SQLException {
        super(dbi);
        logger.debug("Instantiating: " + this);

        this.configuration = configuration;
        this.environment = environment;
        this.commitTimer = this.environment.metrics().timer(this.getClass().getName() + "." + "commits-timer");

        handle.getConnection().setAutoCommit(false);
        batch = handle.prepareBatch(SQL_INSERT_INTO_ALARM_HISTORY);
        handle.begin();

        simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        simpleDateFormat.setTimeZone(TimeZone.getTimeZone("GMT-0"));
    }

    public void addToBatch(AlarmStateTransitionedEvent message) {
        String timeStamp = simpleDateFormat.format(new Date(message.timestamp * 1000));
        batch.add()
                .bind(0, message.tenantId)
                .bind(1, message.alarmId)
                .bind(2, message.alarmName)
                .bind(3, message.alarmDescription)
                .bind(4, message.oldState.name())
                .bind(5, message.newState.name())
                .bind(6, message.stateChangeReason)
                .bind(7, timeStamp);
    }

    public void flush() {
        commitBatch();
    }

    private void commitBatch() {
        long startTime = System.currentTimeMillis();
        Timer.Context context = commitTimer.time();
        batch.execute();
        handle.commit();
        handle.begin();
        context.stop();
        long endTime = System.currentTimeMillis();
        logger.debug("Commiting batch took " + (endTime - startTime) / 1000 + " seconds");
    }
}
@@ -998,39 +998,8 @@
 _id,
- alarm_name, alarm_description,
  old
@@ -1024,16 +1024,29 @@
  reason,
+ reason_data,
  time_st
@@ -1084,41 +1084,8 @@
 _id,
- :alarm_name, :alarm_description,
  :ol
@@ -1113,16 +1113,30 @@
 :reason,
+ :reason_data,
  :time_s
@@ -2350,68 +2350,23 @@
 age.
-alarmName)%0A                .bind(3, message.alarmDescription
+oldState.name()
 )%0A  
@@ -2385,17 +2385,17 @@
   .bind(
-4
+3
 , messag
@@ -2388,35 +2388,35 @@
 bind(3, message.
-old
+new
 State.name())%0A  
@@ -2439,9 +2439,9 @@
 ind(
-5
+4
 , me
@@ -2442,39 +2442,41 @@
 (4, message.
-newState.name()
+stateChangeReason
 )%0A          
@@ -2491,36 +2491,15 @@
 ind(
-6, message.stateChangeReason
+5, %22%7B%7D%22
 )%0A  
@@ -2522,9 +2522,9 @@
 ind(
-7
+6
 , ti

Remove alarm_name and alarm_description. Added reason_data, which is hard-coded to "{}" for now.