Skip to content

Commit

Permalink
Use experiment names for project params filter (G-Research#1132)
Browse files Browse the repository at this point in the history
* Use experiment names for project params filter

* remove optional experiment IDs from GetMetricKeysAndContextsByExperiments
  • Loading branch information
suprjinx committed Apr 17, 2024
1 parent 273b7ec commit 11b3f9f
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 19 deletions.
8 changes: 2 additions & 6 deletions pkg/api/aim2/dao/repositories/metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type MetricRepositoryProvider interface {
BaseRepositoryProvider
// GetMetricKeysAndContextsByExperiments returns metric keys and contexts by provided experiments.
GetMetricKeysAndContextsByExperiments(
ctx context.Context, namespaceID uint, experiments []int, experimentNames []string,
ctx context.Context, namespaceID uint, experimentNames []string,
) ([]models.LatestMetric, error)
// SearchMetrics returns a sql.Rows cursor for streaming the metrics matching the request.
SearchMetrics(
Expand Down Expand Up @@ -59,7 +59,7 @@ func NewMetricRepository(db *gorm.DB) *MetricRepository {

// GetMetricKeysAndContextsByExperiments returns metric keys and contexts by provided experiments.
func (r MetricRepository) GetMetricKeysAndContextsByExperiments(
ctx context.Context, namespaceID uint, experiments []int, experimentNames []string,
ctx context.Context, namespaceID uint, experimentNames []string,
) ([]models.LatestMetric, error) {
query := r.db.WithContext(ctx).Distinct().Select(
"key", "context_id",
Expand All @@ -75,10 +75,6 @@ func (r MetricRepository) GetMetricKeysAndContextsByExperiments(
).Where(
"runs.lifecycle_stage = ?", models.LifecycleStageActive,
)

if len(experiments) != 0 {
query = query.Where("experiments.experiment_id IN ?", experiments)
}
if len(experimentNames) != 0 {
query = query.Where("experiments.name IN ?", experimentNames)
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/api/aim2/dao/repositories/param.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
// ParamRepositoryProvider provides an interface to work with models.Param entity.
type ParamRepositoryProvider interface {
// GetParamKeysByParameters returns list of param keys by requested parameters.
GetParamKeysByParameters(ctx context.Context, namespaceID uint, experiments []int) ([]string, error)
GetParamKeysByParameters(ctx context.Context, namespaceID uint, experimentNames []string) ([]string, error)
}

// ParamRepository repository to work with models.Param entity.
Expand All @@ -31,7 +31,7 @@ func NewParamRepository(db *gorm.DB) *ParamRepository {

// GetParamKeysByParameters returns list of param keys by requested parameters.
func (r ParamRepository) GetParamKeysByParameters(
ctx context.Context, namespaceID uint, experiments []int,
ctx context.Context, namespaceID uint, experimentNames []string,
) ([]string, error) {
query := r.db.WithContext(ctx).Distinct().Model(
&models.Param{},
Expand All @@ -43,8 +43,8 @@ func (r ParamRepository) GetParamKeysByParameters(
).Where(
"runs.lifecycle_stage = ?", models.LifecycleStageActive,
)
if len(experiments) != 0 {
query = query.Where("experiments.experiment_id IN ?", experiments)
if len(experimentNames) != 0 {
query = query.Where("experiments.name IN ?", experimentNames)
}
var keys []string
if err := query.Pluck("Key", &keys).Error; err != nil {
Expand Down
8 changes: 4 additions & 4 deletions pkg/api/aim2/dao/repositories/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type TagRepositoryProvider interface {
// CreateExperimentTag creates new models.ExperimentTag entity connected to models.Experiment.
CreateExperimentTag(ctx context.Context, experimentTag *models.ExperimentTag) error
// GetTagKeysByParameters returns list of tag keys by requested parameters.
GetTagKeysByParameters(ctx context.Context, namespaceID uint, experiments []int) ([]string, error)
GetTagKeysByParameters(ctx context.Context, namespaceID uint, experimentNames []string) ([]string, error)
}

// TagRepository repository to work with models.Tag entity.
Expand Down Expand Up @@ -57,7 +57,7 @@ func (r TagRepository) GetTagsByNamespace(ctx context.Context, namespaceID uint)

// GetTagKeysByParameters returns list of tag keys by requested parameters.
func (r TagRepository) GetTagKeysByParameters(
ctx context.Context, namespaceID uint, experiments []int,
ctx context.Context, namespaceID uint, experimentNames []string,
) ([]string, error) {
// fetch and process tags.
query := r.db.WithContext(ctx).Model(
Expand All @@ -70,8 +70,8 @@ func (r TagRepository) GetTagKeysByParameters(
).Where(
"runs.lifecycle_stage = ?", models.LifecycleStageActive,
)
if len(experiments) != 0 {
query = query.Where("experiments.experiment_id IN ?", experiments)
if len(experimentNames) != 0 {
query = query.Where("experiments.name IN ?", experimentNames)
}

var keys []string
Expand Down
6 changes: 3 additions & 3 deletions pkg/api/aim2/services/project/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,13 @@ func (s Service) GetProjectParams(

projectParams := models.ProjectParams{}
if !req.ExcludeParams {
paramKeys, err := s.paramRepository.GetParamKeysByParameters(ctx, namespaceID, req.Experiments)
paramKeys, err := s.paramRepository.GetParamKeysByParameters(ctx, namespaceID, req.ExperimentNames)
if err != nil {
return nil, api.NewInternalError("error getting param keys: %s", err)
}
projectParams.ParamKeys = paramKeys

tagKeys, err := s.tagRepository.GetTagKeysByParameters(ctx, namespaceID, req.Experiments)
tagKeys, err := s.tagRepository.GetTagKeysByParameters(ctx, namespaceID, req.ExperimentNames)
if err != nil {
return nil, api.NewInternalError("error getting tag keys: %s", err)
}
Expand All @@ -106,7 +106,7 @@ func (s Service) GetProjectParams(
if slices.Contains(req.Sequences, "metric") {
// fetch metrics only when Experiments or ExperimentNames were provided.
metrics, err := s.metricRepository.GetMetricKeysAndContextsByExperiments(
ctx, namespaceID, req.Experiments, req.ExperimentNames,
ctx, namespaceID, req.ExperimentNames,
)
if err != nil {
return nil, api.NewInternalError("error getting metrics: %s", err)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func (s *GetProjectParamsTestSuite) Test_Ok() {
{
name: "RequestProjectParamsFilteredByExistingExperiment",
request: map[any]any{
"experiments": *s.DefaultExperiment.ID,
"experiment_names": s.DefaultExperiment.Name,
},
response: response.ProjectParamsResponse{
Metric: map[string][]fiber.Map{
Expand All @@ -119,7 +119,7 @@ func (s *GetProjectParamsTestSuite) Test_Ok() {
{
name: "RequestProjectParamsFilteredByNotExistingExperiment",
request: map[any]any{
"experiments": 999,
"experiment_names": "not-existing-experiment",
},
response: response.ProjectParamsResponse{
Metric: map[string][]fiber.Map{},
Expand Down

0 comments on commit 11b3f9f

Please sign in to comment.