Skip to content

Commit

Permalink
return summary by status and add required field
Browse files Browse the repository at this point in the history
  • Loading branch information
hvn2k1 committed Jan 23, 2025
1 parent 5fb8870 commit 9af6ab8
Show file tree
Hide file tree
Showing 13 changed files with 1,018 additions and 488 deletions.
20 changes: 20 additions & 0 deletions api-description/web-api.swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5049,6 +5049,18 @@ definitions:
- EXPERIMENT
- OPERATION
default: UNKNOWN
ListExperimentsResponseSummary:
type: object
properties:
totalWaitingCount:
type: string
format: int64
totalRunningCount:
type: string
format: int64
totalStoppedCount:
type: string
format: int64
ListFlagTriggersResponseFlagTriggerWithUrl:
type: object
properties:
Expand Down Expand Up @@ -6763,6 +6775,12 @@ definitions:
type: string
required:
- environmentId
- featureId
- startAt
- stopAt
- goalIds
- name
- baseVariationId
experimentCreateExperimentResponse:
type: object
properties:
Expand Down Expand Up @@ -6952,6 +6970,8 @@ definitions:
totalCount:
type: string
format: int64
summary:
$ref: '#/definitions/ListExperimentsResponseSummary'
experimentListGoalsRequestOrderBy:
type: string
enum:
Expand Down
2 changes: 1 addition & 1 deletion manifests/bucketeer/charts/web/values.yaml

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion pkg/experiment/api/experiment.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ func (s *experimentService) ListExperiments(
return nil, dt.Err()
}
experimentStorage := v2es.NewExperimentStorage(s.mysqlClient)
experiments, nextCursor, totalCount, err := experimentStorage.ListExperiments(
experiments, nextCursor, totalCount, summary, err := experimentStorage.ListExperiments(
ctx,
whereParts,
orders,
Expand Down Expand Up @@ -196,6 +196,7 @@ func (s *experimentService) ListExperiments(
Experiments: experiments,
Cursor: strconv.Itoa(nextCursor),
TotalCount: totalCount,
Summary: summary,
}, nil
}

Expand Down
31 changes: 16 additions & 15 deletions pkg/experiment/storage/v2/experiment.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ var (

//go:embed sql/experiment/select_experiments.sql
selectExperimentsSQL string
//go:embed sql/experiment/count_experiment.sql
countExperimentSQL string
)

type ExperimentStorage interface {
Expand All @@ -44,7 +46,7 @@ type ExperimentStorage interface {
whereParts []mysql.WherePart,
orders []*mysql.Order,
limit, offset int,
) ([]*proto.Experiment, int, int64, error)
) ([]*proto.Experiment, int, int64, *proto.ListExperimentsResponse_Summary, error)
}

type experimentStorage struct {
Expand Down Expand Up @@ -262,14 +264,14 @@ func (s *experimentStorage) ListExperiments(
whereParts []mysql.WherePart,
orders []*mysql.Order,
limit, offset int,
) ([]*proto.Experiment, int, int64, error) {
) ([]*proto.Experiment, int, int64, *proto.ListExperimentsResponse_Summary, error) {
whereSQL, whereArgs := mysql.ConstructWhereSQLString(whereParts)
orderBySQL := mysql.ConstructOrderBySQLString(orders)
limitOffsetSQL := mysql.ConstructLimitOffsetSQLString(limit, offset)
query := fmt.Sprintf(selectExperimentsSQL, whereSQL, orderBySQL, limitOffsetSQL)
rows, err := s.qe.QueryContext(ctx, query, whereArgs...)
if err != nil {
return nil, 0, 0, err
return nil, 0, 0, nil, err
}
defer rows.Close()
experiments := make([]*proto.Experiment, 0, limit)
Expand Down Expand Up @@ -299,27 +301,26 @@ func (s *experimentStorage) ListExperiments(
&mysql.JSONObject{Val: &experiment.Goals},
)
if err != nil {
return nil, 0, 0, err
return nil, 0, 0, nil, err
}
experiment.Status = proto.Experiment_Status(status)
experiments = append(experiments, &experiment)
}
if rows.Err() != nil {
return nil, 0, 0, err
return nil, 0, 0, nil, err
}
nextOffset := offset + len(experiments)
var totalCount int64
countQuery := fmt.Sprintf(`
SELECT
COUNT(1)
FROM
experiment
%s
`, whereSQL,
summary := &proto.ListExperimentsResponse_Summary{}
countQuery := fmt.Sprintf(countExperimentSQL, whereSQL)
err = s.qe.QueryRowContext(ctx, countQuery, whereArgs...).Scan(
&totalCount,
&summary.TotalWaitingCount,
&summary.TotalRunningCount,
&summary.TotalStoppedCount,
)
err = s.qe.QueryRowContext(ctx, countQuery, whereArgs...).Scan(&totalCount)
if err != nil {
return nil, 0, 0, err
return nil, 0, 0, nil, err
}
return experiments, nextOffset, totalCount, nil
return experiments, nextOffset, totalCount, summary, nil
}
2 changes: 1 addition & 1 deletion pkg/experiment/storage/v2/experiment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ func TestListExperiments(t *testing.T) {
if p.setup != nil {
p.setup(storage)
}
experiments, cursor, _, err := storage.ListExperiments(
experiments, cursor, _, _, err := storage.ListExperiments(
context.Background(),
p.whereParts,
p.orders,
Expand Down
7 changes: 4 additions & 3 deletions pkg/experiment/storage/v2/mock/experiment.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions pkg/experiment/storage/v2/sql/experiment/count_experiment.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
SELECT
COUNT(1) AS total,
COALESCE(SUM(IF(status = 0, 1, 0)), 0) AS waiting,
COALESCE(SUM(IF(status = 1, 1, 0)), 0) AS running,
COALESCE(SUM(IF(status = 2 OR status = 3, 1, 0)), 0) AS stopped
FROM
experiment
%s
Binary file modified proto/experiment/proto_descriptor.pb
Binary file not shown.
Loading

0 comments on commit 9af6ab8

Please sign in to comment.