Skip to content

Commit

Permalink
📝 feat: optimize event preview query (#1022)
Browse files Browse the repository at this point in the history
  • Loading branch information
garrettladley authored Jun 13, 2024
1 parent 2dd0789 commit 57d115a
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 30 deletions.
2 changes: 1 addition & 1 deletion backend/entities/events/previews/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func NewService(serviceParams types.ServiceParams) Service {

func (h *Handler) GetEventPreviews(pageInfo *fiberpaginate.PageInfo, start string, end string) ([]EventPreview, error) {
if start == "" || end == "" {
return GetEventPreivews(h.DB, pageInfo)
return GetEventPreviews(h.DB, pageInfo)
}

startTime, err := utilities.ParseTime(start, utilities.YYYY_dash_MM_dash_DD)
Expand Down
66 changes: 46 additions & 20 deletions backend/entities/events/previews/transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ import (
"time"

"github.com/garrettladley/fiberpaginate"
"github.com/google/uuid"

"github.com/GenerateNU/sac/backend/database/cache"
"github.com/GenerateNU/sac/backend/entities/clubs"
"github.com/GenerateNU/sac/backend/entities/events/tags"
"github.com/GenerateNU/sac/backend/entities/models"
"github.com/GenerateNU/sac/backend/utilities"

"gorm.io/gorm"
)

func GetEventPreivews(db *gorm.DB, pageInfo *fiberpaginate.PageInfo) ([]EventPreview, error) {
func GetEventPreviews(db *gorm.DB, pageInfo *fiberpaginate.PageInfo) ([]EventPreview, error) {
db = cache.SetUseCache(db, true)

var events []EventStub
Expand All @@ -24,19 +24,16 @@ func GetEventPreivews(db *gorm.DB, pageInfo *fiberpaginate.PageInfo) ([]EventPre
return processEventStubs(db, events)
}

func GetEventPreviewsByTime(db *gorm.DB, pageInfo *fiberpaginate.PageInfo, startTime *time.Time, endTime *time.Time) ([]EventPreview, error) {
var events []EventStub
func GetEventPreviewsByTime(db *gorm.DB, pageInfo *fiberpaginate.PageInfo, startTime, endTime *time.Time) ([]EventPreview, error) {
db = cache.SetUseCache(db, true)

query := cache.SetUseCache(db, true).Model(&events)
var events []EventStub
query := db.Model(&events).Scopes(utilities.IntoScope(*pageInfo, db))

if startTime != nil && endTime != nil {
query = query.Where("start_time >= ? AND end_time <= ?", *startTime, *endTime)
}

if pageInfo != nil {
query = query.Scopes(utilities.IntoScope(*pageInfo, db))
}

if err := query.Find(&events).Error; err != nil {
return nil, err
}
Expand All @@ -45,19 +42,48 @@ func GetEventPreviewsByTime(db *gorm.DB, pageInfo *fiberpaginate.PageInfo, start
}

func processEventStubs(db *gorm.DB, events []EventStub) ([]EventPreview, error) {
eventPreviews := make([]EventPreview, len(events))
eventIDs := make([]uuid.UUID, len(events))
for index, event := range events {
eventTags, err := tags.GetEventTags(db, event.ID)
if err != nil {
return nil, err
}
eventIDs[index] = event.ID
}

var eventTags []struct {
EventID uuid.UUID `gorm:"column:event_id"`
TagID uuid.UUID `gorm:"column:tag_id"`
Name string `gorm:"column:name"`
}
if err := db.Table("tags").Select("tags.id as tag_id, tags.name, event_tags.event_id").
Joins("JOIN event_tags ON event_tags.tag_id = tags.id").
Where("event_tags.event_id IN ?", eventIDs).
Scan(&eventTags).Error; err != nil {
return nil, err
}

eventTagsMap := make(map[uuid.UUID][]models.Tag)
for _, et := range eventTags {
eventTagsMap[et.EventID] = append(eventTagsMap[et.EventID],
models.Tag{
Model: models.Model{
ID: et.TagID,
},
Name: et.Name,
})
}

var hosts []models.Club
if err := db.Where("id IN ?", eventIDs).Find(&hosts).Error; err != nil {
return nil, err
}

host, err := clubs.GetClub(db, event.Host)
if err != nil {
return nil, err
}
hostsMap := make(map[uuid.UUID]models.Club)
for _, host := range hosts {
hostsMap[host.ID] = host
}

eventPreviews[index] = *event.Into(eventTags, host.Name, host.Logo)
eventPreviews := make([]EventPreview, len(events))
for index, event := range events {
eventPreviews[index] = *event.Into(eventTagsMap[event.ID], hostsMap[event.Host].Name, hostsMap[event.Host].Logo)
}

return eventPreviews, nil
}
12 changes: 3 additions & 9 deletions backend/entities/events/tags/transactions.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package tags

import (
"github.com/GenerateNU/sac/backend/database/cache"
"github.com/GenerateNU/sac/backend/entities/events"

"github.com/GenerateNU/sac/backend/entities/models"
Expand All @@ -26,15 +25,10 @@ func CreateEventTags(db *gorm.DB, id uuid.UUID, tags []models.Tag) ([]models.Tag
}

func GetEventTags(db *gorm.DB, id uuid.UUID) ([]models.Tag, error) {
db = cache.SetUseCache(db, true)

var tags []models.Tag
event, err := events.GetEvent(db, id, transactions.PreloadTag())
if err != nil {
return nil, err
}

if err := db.Model(&event).Association("Tag").Find(&tags); err != nil {
if err := db.Joins("JOIN event_tags ON event_tags.tag_id = tags.id").
Where("event_tags.event_id = ?", id).
Find(&tags).Error; err != nil {
return nil, err
}
return tags, nil
Expand Down

0 comments on commit 57d115a

Please sign in to comment.