diff --git a/k6/load-test.js b/k6/load-test.js index cd47be0..e4c103e 100644 --- a/k6/load-test.js +++ b/k6/load-test.js @@ -27,7 +27,7 @@ export const options = { export function load_test() { const seriesURL = new URL(`${__ENV.API_BASE_URL}/series`); - const offset = Math.floor(Math.random() * 2183) + const offset = Math.floor(Math.random() * 20) seriesURL.searchParams.append(`limit`, `20`); seriesURL.searchParams.append(`offset`, `${offset}`); @@ -63,6 +63,7 @@ export function load_test() { url.searchParams.append(`limit`, `20`) url.searchParams.append(`offset`, `0`) url.searchParams.append(`seasonId`, `${season.id}`) + url.searchParams.append(`seriesId`, `${season.seriesId}`) episodeRequests.push(['GET', url.toString()]) }) } diff --git a/pkg/repository/database/season.go b/pkg/repository/database/season.go index 7a38cb6..7a53b96 100644 --- a/pkg/repository/database/season.go +++ b/pkg/repository/database/season.go @@ -3,7 +3,6 @@ package database import ( "context" "database/sql" - "errors" "fmt" "strings" @@ -11,6 +10,7 @@ import ( "github.com/CyberAgentHack/server-performance-tuning-2023/pkg/errcode" "github.com/CyberAgentHack/server-performance-tuning-2023/pkg/repository" "github.com/aws/aws-xray-sdk-go/xray" + "go.uber.org/multierr" ) type Season struct { @@ -123,5 +123,53 @@ func (e *Season) BatchGet(ctx context.Context, ids []string) (entity.Seasons, er ctx, seg := xray.BeginSubsegment(ctx, "database.Season#BatchGet") defer seg.Close(nil) - return nil, errcode.New(errors.New("not implemtented yet")) + if len(ids) == 0 { + return nil, nil + } + + newIDs := make([]string, 0, len(ids)) + for _, id := range ids { + if id != "" { + newIDs = append(newIDs, id) + } + } + + fields := []string{ + "seasonID", + "seriesID", + "displayName", + "imageURL", + "displayOrder", + } + + query := fmt.Sprintf( + "SELECT %s FROM seasons WHERE seasonID IN(?%s)", + strings.Join(fields, ","), + strings.Repeat(",?", len(newIDs)-1), + ) + rows, err := e.db.QueryContext(ctx, query, newIDs) + if err != nil { + return nil, errcode.New(err) + } + + var seasons entity.Seasons + var multiErr error + for rows.Next() { + var s entity.Season + if err = rows.Scan(&s); err != nil { + multiErr = multierr.Append(multiErr, err) + continue + } + seasons = append(seasons, &s) + } + + if cerr := rows.Close(); cerr != nil { + return nil, errcode.New(cerr) + } + + if multiErr != nil { + return nil, errcode.New(multiErr) + } + + return seasons, nil } diff --git a/pkg/repository/database/series.go b/pkg/repository/database/series.go index c259ad4..5c9d458 100644 --- a/pkg/repository/database/series.go +++ b/pkg/repository/database/series.go @@ -3,7 +3,6 @@ package database import ( "context" "database/sql" - "errors" "fmt" "strings" @@ -11,6 +10,7 @@ import ( "github.com/CyberAgentHack/server-performance-tuning-2023/pkg/errcode" "github.com/CyberAgentHack/server-performance-tuning-2023/pkg/repository" "github.com/aws/aws-xray-sdk-go/xray" + "go.uber.org/multierr" ) type Series struct { @@ -102,5 +102,44 @@ func (e *Series) BatchGet(ctx context.Context, ids []string) (entity.SeriesMulti ctx, seg := xray.BeginSubsegment(ctx, "database.Series#BatchGet") defer seg.Close(nil) - return nil, errcode.New(errors.New("not implemtented yet")) + if len(ids) == 0 { + return nil, nil + } + + newIDs := make([]string, 0, len(ids)) + for _, id := range ids { + if id != "" { + newIDs = append(newIDs, id) + } + } + + fields := []string{"id", "displayName", "description", "imageUrl", "genreId"} + query := fmt.Sprintf( + "SELECT %s FROM series WHERE seriesID IN(?%s)", + strings.Join(fields, ", "), + strings.Repeat(",?", len(newIDs)-1), + ) + + rows, err := e.db.QueryContext(ctx, query, newIDs) + + var seriesMulti entity.SeriesMulti + var multiErr error + for rows.Next() { + var series entity.Series + if err := rows.Scan(&series); err != nil { + multiErr = multierr.Append(multiErr, err) + continue + } + seriesMulti = append(seriesMulti, &series) + } + + if err := rows.Close(); err != nil { + return nil, errcode.New(err) + } + + if multiErr != nil { + return nil, errcode.New(multiErr) + } + + return seriesMulti, errcode.New(err) }