Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions pkg/db/v1beta1/mysql/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ func NewDBInterface(connectTimeout time.Duration) (common.KatibDBInterface, erro
}

func (d *dbConn) RegisterObservationLog(trialName string, observationLog *v1beta1.ObservationLog) error {
if observationLog == nil {
return nil
}
sqlQuery := "INSERT INTO observation_logs (trial_name, time, metric_name, value) VALUES "
values := []interface{}{}

Expand All @@ -81,6 +84,10 @@ func (d *dbConn) RegisterObservationLog(trialName string, observationLog *v1beta
sqlQuery += "(?, ?, ?, ?),"
values = append(values, trialName, sqlTimeStr, mlog.Metric.Name, mlog.Metric.Value)
}
if len(values) == 0 {
// No valid metric logs to insert, skip Prepare/Exec.
return nil
}
sqlQuery = sqlQuery[0 : len(sqlQuery)-1]

// Prepare the statement
Expand Down
37 changes: 37 additions & 0 deletions pkg/db/v1beta1/mysql/mysql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,43 @@ func TestRegisterObservationLog(t *testing.T) {

}

func TestRegisterObservationLogNoValidEntries(t *testing.T) {
obsLog := &api_pb.ObservationLog{
MetricLogs: []*api_pb.MetricLog{
{
TimeStamp: "",
Metric: &api_pb.Metric{
Name: "f1_score",
Value: "88.95",
},
},
},
}

err := dbInterface.RegisterObservationLog("test1_trial1", obsLog)
if err != nil {
t.Errorf("RegisterObservationLog failed: %v", err)
}
}

func TestRegisterObservationLogEmptyMetricLogs(t *testing.T) {
obsLog := &api_pb.ObservationLog{
MetricLogs: []*api_pb.MetricLog{},
}

err := dbInterface.RegisterObservationLog("test1_trial1", obsLog)
if err != nil {
t.Errorf("RegisterObservationLog failed: %v", err)
}
}

func TestRegisterObservationLogNilObservationLog(t *testing.T) {
err := dbInterface.RegisterObservationLog("test1_trial1", nil)
if err != nil {
t.Errorf("RegisterObservationLog failed: %v", err)
}
}

func TestGetObservationLog(t *testing.T) {
mock.ExpectQuery("SELECT").WillReturnRows(
sqlmock.NewRows([]string{"time", "metric_name", "value"}).AddRow(
Expand Down
8 changes: 8 additions & 0 deletions pkg/db/v1beta1/postgres/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ func NewDBInterface(connectTimeout time.Duration) (common.KatibDBInterface, erro
}

func (d *dbConn) RegisterObservationLog(trialName string, observationLog *v1beta1.ObservationLog) error {
if observationLog == nil {
return nil
}
statement := "INSERT INTO observation_logs (trial_name, time, metric_name, value) VALUES "
values := []interface{}{}

Expand All @@ -88,6 +91,11 @@ func (d *dbConn) RegisterObservationLog(trialName string, observationLog *v1beta
index_of_qparam += 4
}

if len(values) == 0 {
// No valid metric logs to insert, skip Prepare/Exec.
return nil
}

statement = statement[:len(statement)-1]

// Prepare the statement
Expand Down
37 changes: 37 additions & 0 deletions pkg/db/v1beta1/postgres/postgres_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,43 @@ func TestRegisterObservationLog(t *testing.T) {

}

func TestRegisterObservationLogNoValidEntries(t *testing.T) {
obsLog := &api_pb.ObservationLog{
MetricLogs: []*api_pb.MetricLog{
{
TimeStamp: "",
Metric: &api_pb.Metric{
Name: "f1_score",
Value: "88.95",
},
},
},
}

err := dbInterface.RegisterObservationLog("test1_trial1", obsLog)
if err != nil {
t.Errorf("RegisterObservationLog failed: %v", err)
}
}

func TestRegisterObservationLogEmptyMetricLogs(t *testing.T) {
obsLog := &api_pb.ObservationLog{
MetricLogs: []*api_pb.MetricLog{},
}

err := dbInterface.RegisterObservationLog("test1_trial1", obsLog)
if err != nil {
t.Errorf("RegisterObservationLog failed: %v", err)
}
}

func TestRegisterObservationLogNilObservationLog(t *testing.T) {
err := dbInterface.RegisterObservationLog("test1_trial1", nil)
if err != nil {
t.Errorf("RegisterObservationLog failed: %v", err)
}
}

func TestGetObservationLog(t *testing.T) {
mock.ExpectQuery("SELECT").WillReturnRows(
sqlmock.NewRows([]string{"time", "metric_name", "value"}).AddRow(
Expand Down