Skip to content

Commit

Permalink
Merge pull request #1008 from bcc-code/feat/add-scores-to-shorts-algo
Browse files Browse the repository at this point in the history
Add Scores to shorts alogrithm
  • Loading branch information
KillerX authored Sep 24, 2024
2 parents b988d65 + 5a7c6a3 commit 6ee03da
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 4 deletions.
12 changes: 12 additions & 0 deletions backend/cmd/api/loaders.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,18 @@ func getLoadersForRoles(db *sql.DB, queries *sqlc.Queries, collectionLoader *loa
}), nil
}, cache.WithExpiration(time.Minute*5))
},

ShortWithScoresLoader: func(ctx context.Context) ([]uuid.UUID, error) {
return memorycache.GetOrSet(ctx, fmt.Sprintf("shortIDs:roles:%s", key), func(ctx context.Context) ([]uuid.UUID, error) {
rows, err := queries.ListSegmentedShortIDsForRolesWithScores(ctx, roles)
if err != nil {
return nil, err
}
return lo.Map(rows, func(i sqlc.ListSegmentedShortIDsForRolesWithScoresRow, _ int) uuid.UUID {
return i.ID
}), nil
}, cache.WithExpiration(time.Minute*5))
},
}

// Canceling the context on delete stops janitors nested inside the loaders as well.
Expand Down
7 changes: 4 additions & 3 deletions backend/common/loaders.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,10 @@ type FilteredLoaders struct {
LinkStudyLessonsLoader *loaders.Loader[int, []*uuid.UUID]

// Lists
PromptIDsLoader func(ctx context.Context) ([]uuid.UUID, error)
FAQCategoryIDsLoader func(ctx context.Context) ([]uuid.UUID, error)
ShortIDsLoader func(ctx context.Context) ([][]uuid.UUID, error)
PromptIDsLoader func(ctx context.Context) ([]uuid.UUID, error)
FAQCategoryIDsLoader func(ctx context.Context) ([]uuid.UUID, error)
ShortIDsLoader func(ctx context.Context) ([][]uuid.UUID, error)
ShortWithScoresLoader func(ctx context.Context) ([]uuid.UUID, error)
}

// ProfileLoaders contains loaders per profile
Expand Down
16 changes: 15 additions & 1 deletion backend/graph/api/shorts.utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package graph
import (
"context"
"errors"
"github.com/bcc-code/bcc-media-platform/backend/unleash"
"time"

"github.com/99designs/gqlgen/graphql"
Expand Down Expand Up @@ -60,7 +61,20 @@ func (r *Resolver) getShuffledShortIDsWithCursor(ctx context.Context, p *common.
cursor.Seed = &seed
}

shortIDSegments, err := r.GetFilteredLoaders(ctx).ShortIDsLoader(ctx)
ginCtx, _ := utils.GinCtx(ctx)
featureFlags := utils.GetFeatureFlags(ginCtx)

var shortIDSegments [][]uuid.UUID
var err error

if featureFlags.Has(unleash.ShortsWithScores) {
shortIDs, iErr := r.GetFilteredLoaders(ctx).ShortWithScoresLoader(ctx)
err = iErr
shortIDSegments = [][]uuid.UUID{shortIDs}
} else {
shortIDSegments, err = r.GetFilteredLoaders(ctx).ShortIDsLoader(ctx)
}

if err != nil {
return nil, err
}
Expand Down
49 changes: 49 additions & 0 deletions backend/sqlc/shorts.sql.go

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

7 changes: 7 additions & 0 deletions backend/unleash/flags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package unleash

// ShortsWithScores enables shorts sorting by scores
//
// Started: 20.9.2024
// To be Removed Latest: 20.12.2024
const ShortsWithScores = "shorts-with-scores"
19 changes: 19 additions & 0 deletions queries/shorts.sql
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,25 @@ WHERE s.status = 'published'
GROUP BY week
ORDER BY week DESC;

-- name: ListSegmentedShortIDsForRolesWithScores :many
SELECT s.id,
-- We need a date and if we do not have a published_at date, we need to assume that the created date is when it was published
EXTRACT(DAY FROM current_date - COALESCE(mi.published_at, mi.date_created)) age,

-- For 10 days the shorts is boosted. It starts with a 5 points boost, and the boost "degrades" by 0.5
-- points per day, reaching 0 boost on day 10. It stops there
(((10 - LEAST(10, EXTRACT(DAY FROM current_date - COALESCE(mi.published_at, mi.date_created)))) * 0.5) + score)::float8 as final_score

FROM shorts s
JOIN mediaitems mi ON s.mediaitem_id = mi.id
JOIN (SELECT r.shorts_id, array_agg(r.usergroups_code) as roles
FROM shorts_usergroups r
GROUP BY r.shorts_id) r
ON s.id = r.shorts_id
WHERE s.status = 'published'
AND r.roles && @roles::varchar[]
ORDER BY final_score DESC;

-- name: getShorts :many
SELECT s.id,
s.status,
Expand Down

0 comments on commit 6ee03da

Please sign in to comment.