Skip to content

Commit

Permalink
Merge pull request #1032 from bcc-code/feat/optimize-query
Browse files Browse the repository at this point in the history
Optimize view query
  • Loading branch information
KillerX authored Oct 14, 2024
2 parents bed2c4e + d06c520 commit 2cd35cd
Show file tree
Hide file tree
Showing 18 changed files with 489 additions and 59 deletions.
1 change: 1 addition & 0 deletions .idea/sqldialects.xml

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

7 changes: 7 additions & 0 deletions backend/cmd/api/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"crypto/x509"
"encoding/pem"
"github.com/bcc-code/bcc-media-platform/backend/email"
"github.com/bcc-code/bcc-media-platform/backend/log"
"os"
"strings"

Expand All @@ -16,6 +17,7 @@ import (
"github.com/bcc-code/bcc-media-platform/backend/search"

"github.com/bcc-code/bcc-media-platform/backend/auth0"
"github.com/joho/godotenv"
"github.com/samber/lo"
)

Expand Down Expand Up @@ -155,6 +157,11 @@ func (a awsConfig) GetTempStorageBucket() string {
}

func getEnvConfig() envConfig {
err := godotenv.Load("backend/cmd/api/.env")
if err == nil {
log.L.Warn().Msg("Loaded .env file")
}

aud := lo.Map(strings.Split(os.Getenv("AUTH0_AUDIENCES"), ","),
func(s string, _ int) string {
return strings.TrimSpace(s)
Expand Down
3 changes: 3 additions & 0 deletions backend/cmd/api/loaders.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,9 @@ func initBatchLoaders(queries *sqlc.Queries, membersClient *members.Client) *com
TimedMetadataLoader: loaders.New(ctx, queries.GetTimedMetadata, loaders.WithName("timedmetadata-loader"), loaders.WithKeyFunc(func(i common.TimedMetadata) uuid.UUID {
return i.ID
})),
ChaptersLoader: loaders.NewListLoader(ctx, queries.GetChaptersForEpisodeID, func(i common.TimedMetadata) int {
return int(i.ParentEpisodeID.Int64)
}),
PhraseLoader: loaders.New(ctx, queries.GetPhrases, loaders.WithName("phrases-loader"), loaders.WithKeyFunc(func(i common.Phrase) string {
return i.Key
})),
Expand Down
28 changes: 14 additions & 14 deletions backend/common/items.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,8 @@ type Episode struct {
Assets LocaleMap[int] `json:"assets"`
AssetVersion string `json:"assetVersion"`

Images Images `json:"images"`
TagIDs []int `json:"tagIds"`
TimedMetadataIDs []uuid.UUID `json:"timedMetadataIds"`
Images Images `json:"images"`
TagIDs []int `json:"tagIds"`

PublicTitle null.String `json:"publicTitle"`
Title LocaleString `json:"title"`
Expand Down Expand Up @@ -229,17 +228,18 @@ var (

// TimedMetadata item type
type TimedMetadata struct {
ID uuid.UUID
Type string
Timestamp float64
Duration float64
Title LocaleString `json:"title"`
Description LocaleString `json:"description"`
ContentType ContentType
PersonIDs []uuid.UUID
SongID uuid.NullUUID
MediaItemID uuid.NullUUID
Images Images
ID uuid.UUID
Type string
Timestamp float64
Duration float64
Title LocaleString `json:"title"`
Description LocaleString `json:"description"`
ContentType ContentType
PersonIDs []uuid.UUID
SongID uuid.NullUUID
MediaItemID uuid.NullUUID
Images Images
ParentEpisodeID null.Int
}

// Short item type
Expand Down
2 changes: 1 addition & 1 deletion backend/common/loaders.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package common

import (
"context"

"github.com/bcc-code/bcc-media-platform/backend/loaders"
"github.com/bcc-code/bcc-media-platform/backend/members"
"github.com/google/uuid"
Expand Down Expand Up @@ -94,6 +93,7 @@ type BatchLoaders struct {

MediaItemPrimaryEpisodeIDLoader *loaders.Loader[uuid.UUID, *int]
TimedMetadataLoader *loaders.Loader[uuid.UUID, *TimedMetadata]
ChaptersLoader *loaders.Loader[int, []*TimedMetadata]
PersonLoader *loaders.Loader[uuid.UUID, *Person]
SongLoader *loaders.Loader[uuid.UUID, *Song]
PhraseLoader *loaders.Loader[string, *Phrase]
Expand Down
79 changes: 79 additions & 0 deletions backend/graph/api/chapter.utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package graph
import (
"context"
"fmt"
"strconv"
"strings"

"github.com/bcc-code/bcc-media-platform/backend/common"
Expand All @@ -12,6 +13,84 @@ import (
"github.com/google/uuid"
)

func resolveChapters(ctx context.Context, loaders *common.BatchLoaders, episodeID string) ([]*model.Chapter, error) {
episodeIDInt, err := strconv.Atoi(episodeID)
if err != nil {
return nil, err
}

tms, err := loaders.ChaptersLoader.Get(ctx, episodeIDInt)
if err != nil {
return nil, err
}

chapters := make([]*model.Chapter, 0, len(tms))

for _, tm := range tms {

ginCtx, _ := utils.GinCtx(ctx)
languages := user.GetLanguagesFromCtx(ginCtx)
title := tm.Title.Get(languages)
phrase, _ := loaders.PhraseLoader.Get(ctx, tm.ContentType.Value)
emptyTitle := title == ""
if emptyTitle && phrase != nil {
title = phrase.Value.Get(languages)
}

switch tm.ContentType {
case common.ContentTypeSong, common.ContentTypeSingAlong:
if !tm.SongID.Valid {
break
}
song, _ := loaders.SongLoader.Get(ctx, tm.SongID.UUID)
if song == nil {
break
}
if emptyTitle {
if phrase != nil {
title = fmt.Sprintf("%s - %s", phrase.Value.Get(languages), song.Title.Get(languages))
} else {
title = song.Title.Get(languages)
}
} else {
title = strings.Replace(title, "{{song.title}}", song.Title.Get(languages), -1)
}
case common.ContentTypeSpeech, common.ContentTypeInterview, common.ContentTypeTestimony:
if len(tm.PersonIDs) != 1 {
break
}
personID := tm.PersonIDs[0]
person, _ := loaders.PersonLoader.Get(ctx, personID)
if person == nil {
break
}
if emptyTitle {
if phrase != nil {
title = fmt.Sprintf("%s - %s", phrase.Value.Get(languages), person.Name)
} else {
title = person.Name
}
} else {
title = strings.Replace(title, "{{person.name}}", person.Name, -1)
}
}
chapters = append(chapters, &model.Chapter{
ID: tm.ID.String(),
Title: title,
Description: tm.Description.GetValueOrNil(languages),
Start: int(tm.Timestamp),
Duration: int(tm.Duration),
Image: imageOrFallback(ctx, tm.Images, nil),
ContentType: &model.ContentType{Code: tm.ContentType.Value},
Episode: &model.Episode{
ID: episodeID,
},
})
}

return chapters, nil
}

func resolveChapter(ctx context.Context, loaders *common.BatchLoaders, episodeID string, id uuid.UUID) (*model.Chapter, error) {
tm, err := loaders.TimedMetadataLoader.Get(ctx, id)
if err != nil || tm == nil {
Expand Down
26 changes: 1 addition & 25 deletions backend/graph/api/episode-resolver.utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,29 +322,5 @@ func (r *episodeResolver) getTitleWithCollection(ctx context.Context, episode *m
}

func (r *episodeResolver) getChapters(ctx context.Context, episodeId string) ([]*model.Chapter, error) {
i, err := r.Loaders.EpisodeLoader.Get(ctx, utils.AsInt(episodeId))
if err != nil || !i.AssetID.Valid {
return nil, err
}
metadataItems, err := r.Loaders.TimedMetadataLoader.GetMany(ctx, i.TimedMetadataIDs)
if err != nil {
return nil, err
}
metadataItems = lo.Filter(metadataItems, func(i *common.TimedMetadata, _ int) bool {
return i.Type == "chapter"
})

r.Loaders.PhraseLoader.LoadMany(ctx, lo.Uniq(lo.Map(metadataItems, func(i *common.TimedMetadata, _ int) string {
return i.ContentType.Value
})))

var out []*model.Chapter
for _, tm := range metadataItems {
chapter, err := resolveChapter(ctx, r.Loaders, episodeId, tm.ID)
if err != nil {
return nil, err
}
out = append(out, chapter)
}
return out, nil
return resolveChapters(ctx, r.Loaders, episodeId)
}
2 changes: 0 additions & 2 deletions backend/sqlc/episode-queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ func (q *Queries) mapToEpisodes(episodes []getEpisodesRow) []common.Episode {
TagIDs: lo.Map(e.TagIds, func(id int32, _ int) int {
return int(id)
}),
TimedMetadataIDs: e.TimedmetadataIds,
}
})
}
Expand Down Expand Up @@ -121,7 +120,6 @@ func (q *Queries) mapListToEpisodes(episodes []listEpisodesRow) []common.Episode
TagIDs: lo.Map(e.TagIds, func(id int32, _ int) int {
return int(id)
}),
TimedMetadataIDs: e.TimedmetadataIds,
}
})
}
Expand Down
14 changes: 4 additions & 10 deletions backend/sqlc/episodes.sql.go

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

28 changes: 28 additions & 0 deletions backend/sqlc/models.go

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

36 changes: 35 additions & 1 deletion backend/sqlc/timedmetadata-queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,52 @@ func (q *Queries) GetTimedMetadata(ctx context.Context, ids []uuid.UUID) ([]comm
if contentType == nil {
contentType = &common.ContentTypeSpeech
}

return common.TimedMetadata{
ID: i.ID,
Type: i.Type,
ContentType: *contentType,
PersonIDs: i.PersonIds,
SongID: i.SongID,
Timestamp: float64(i.Seconds),
Duration: float64(i.Duration),
Duration: i.Duration,
Title: title,
Description: description,
MediaItemID: i.MediaitemID,
Images: q.getImages(i.Images),
}
}), nil
}

// GetChaptersForEpisodeID returns chapters for the specified episode id
func (q *Queries) GetChaptersForEpisodeID(ctx context.Context, ids []int) ([]common.TimedMetadata, error) {
idsInt32 := lo.Map(ids, func(i int, _ int) int32 {
return int32(i)
})
rows, err := q.getChaptesFromEpisode(ctx, idsInt32)
if err != nil {
return nil, err
}
return lo.Map(rows, func(i getChaptesFromEpisodeRow, _ int) common.TimedMetadata {
title := toLocaleString(i.Title, i.OriginalTitle.String)
description := toLocaleString(i.Description, i.OriginalDescription.String)
contentType := common.ContentTypes.Parse(i.ContentType.String)
if contentType == nil {
contentType = &common.ContentTypeSpeech
}
return common.TimedMetadata{
ID: i.ID,
Type: i.Type,
ContentType: *contentType,
PersonIDs: i.PersonIds,
SongID: i.SongID,
Timestamp: float64(i.Seconds),
Duration: i.Duration,
Title: title,
Description: description,
MediaItemID: i.MediaitemID,
Images: q.getImages(i.Images),
ParentEpisodeID: i.EpisodeID,
}
}), nil
}
Loading

0 comments on commit 2cd35cd

Please sign in to comment.