package edu.mit.csail.db.ml.server.storage;

import jooq.sqlite.gen.Tables;
import jooq.sqlite.gen.tables.records.EventRecord;
import modeldb.ResourceNotFoundException;
import org.jooq.Condition;
import org.jooq.DSLContext;
import org.jooq.Record1;

import java.util.Arrays;

import static jooq.sqlite.gen.Tables.EVENT;

public class EventDao {
  public static EventRecord store(int eventId, String eventType, int experimentId, DSLContext ctx) {
    EventRecord ev = ctx.newRecord(EVENT);
    ev.setId(null);
    ev.setExperimentrun(experimentId);
    ev.setEventtype(eventType);
    ev.setEventid(eventId);
    ev.store();
    return ev;
  }

  /**
   * Get the Event ID associated with the specific (e.g. TransformEvent, FitEvent) given event.
   * @param specificEventId - The ID of the specific kind of event.
   * @param eventType - The type of the event. If you provide "fit" as an argument, this will search for both "fit" and
   *                  "pipelineFit".
   */
  public static int getEventId(int specificEventId, String eventType, DSLContext ctx) throws ResourceNotFoundException {
    Condition whereCond = eventType.equals("fit") ?
      Tables.EVENT.EVENTTYPE.in(Arrays.asList("fit", "pipeline fit")) :
      Tables.EVENT.EVENTTYPE.eq(eventType);
    whereCond = whereCond.and(Tables.EVENT.EVENTID.eq(specificEventId));
    Record1<Integer> rec = ctx.select(Tables.EVENT.ID).from(Tables.EVENT).where(whereCond).fetchOne();

    if (rec == null) {
      throw new ResourceNotFoundException(
        String.format("Could not find Event for event type %s and ID %s", eventType, specificEventId)
      );
    }

    return rec.value1();
  }
}

@@ -318,31 +318,954 @@
 T;%0A%0A
-public class EventDao %7B
+/**%0A * Contains logic for storing and reading Events (i.e. entries in the Event table). For most of the events that%0A * ModelDB supports (e.g. CrossValidationEvent, FitEvent), we store entries in the Event table.%0A */%0Apublic class EventDao %7B%0A  /**%0A   * Store an entry in the Event table.%0A   * @param eventId - The ID of the underlying event. For example, if we've created a FitEvent with ID = 1234, then we%0A   *                need to store an entry in the the Event table corresponding to the FitEvent. Therefore, we should%0A   *                pass eventId = 1234 to this function.%0A   * @param eventType - The type of the underlying event. For example, this should be %22fit%22 or %22pipelineFit%22 for%0A   *                  FitEvents, %22transform%22 for TransformEvents, etc.%0A   * @param experimentRunId - The ID of the experiment run that contains the underlying event.%0A   * @param ctx - The database context.%0A   * @return The row of the Event table.%0A   */
 %0A  p
@@ -1340,16 +1340,19 @@
 periment
+Run
 Id, DSLC
@@ -1462,16 +1462,19 @@
 periment
+Run
 Id);%0A   
@@ -1893,16 +1893,163 @@
 neFit%22.%0A
+   * @throws ResourceNotFoundException - Thrown if there's no entry in the Event table that has the given underlying%0A   * event ID and event type.%0A
    */%0A  
@@ -2167,16 +2167,116 @@
 ption %7B%0A
+    // Create the WHERE condition of the SQL statement by checking whether the event type is %22fit%22.%0A
     Cond
@@ -2507,16 +2507,39 @@
 entId));
+%0A%0A    // Run the query.
 %0A    Rec
@@ -2631,24 +2631,70 @@
 etchOne();%0A%0A
+    // Throw an exception if no row is found.%0A
     if (rec 
@@ -2865,16 +2865,87 @@
 %0A    %7D%0A%0A
+    // Return the primary key (i.e. ID) of the row in the Event table.%0A
     retu

Comment EventDao