storage: change GetReadTime() function to use entries_feed_id_hash_key index

This commit is contained in:
Frédéric Guillot 2024-04-04 21:19:02 -07:00
parent 35edd8ea92
commit 38b80d96ea
2 changed files with 16 additions and 15 deletions

View file

@ -251,6 +251,11 @@ func getUrlFromEntry(feed *model.Feed, entry *model.Entry) string {
} }
func updateEntryReadingTime(store *storage.Storage, feed *model.Feed, entry *model.Entry, entryIsNew bool, user *model.User) { func updateEntryReadingTime(store *storage.Storage, feed *model.Feed, entry *model.Entry, entryIsNew bool, user *model.User) {
if !user.ShowReadingTime {
slog.Debug("Skip reading time estimation for this user", slog.Int64("user_id", user.ID))
return
}
if shouldFetchYouTubeWatchTime(entry) { if shouldFetchYouTubeWatchTime(entry) {
if entryIsNew { if entryIsNew {
watchTime, err := fetchYouTubeWatchTime(entry.URL) watchTime, err := fetchYouTubeWatchTime(entry.URL)
@ -266,7 +271,7 @@ func updateEntryReadingTime(store *storage.Storage, feed *model.Feed, entry *mod
} }
entry.ReadingTime = watchTime entry.ReadingTime = watchTime
} else { } else {
entry.ReadingTime = store.GetReadTime(entry, feed) entry.ReadingTime = store.GetReadTime(feed.ID, entry.Hash)
} }
} }
@ -285,16 +290,15 @@ func updateEntryReadingTime(store *storage.Storage, feed *model.Feed, entry *mod
} }
entry.ReadingTime = watchTime entry.ReadingTime = watchTime
} else { } else {
entry.ReadingTime = store.GetReadTime(entry, feed) entry.ReadingTime = store.GetReadTime(feed.ID, entry.Hash)
} }
} }
// Handle YT error case and non-YT entries. // Handle YT error case and non-YT entries.
if entry.ReadingTime == 0 { if entry.ReadingTime == 0 {
if user.ShowReadingTime {
entry.ReadingTime = readingtime.EstimateReadingTime(entry.Content, user.DefaultReadingSpeed, user.CJKReadingSpeed) entry.ReadingTime = readingtime.EstimateReadingTime(entry.Content, user.DefaultReadingSpeed, user.CJKReadingSpeed)
} }
} }
}
func shouldFetchYouTubeWatchTime(entry *model.Entry) bool { func shouldFetchYouTubeWatchTime(entry *model.Entry) bool {
if !config.Opts.FetchYouTubeWatchTime() { if !config.Opts.FetchYouTubeWatchTime() {

View file

@ -231,24 +231,21 @@ func (s *Storage) IsNewEntry(feedID int64, entryHash string) bool {
return !result return !result
} }
// GetReadTime fetches the read time of an entry based on its hash, and the feed id and user id from the feed. func (s *Storage) GetReadTime(feedID int64, entryHash string) int {
// It's intended to be used on entries objects created by parsing a feed as they don't contain much information.
// The feed param helps to scope the search to a specific user and feed in order to avoid hash clashes.
func (s *Storage) GetReadTime(entry *model.Entry, feed *model.Feed) int {
var result int var result int
// Note: This query uses entries_feed_id_hash_key index
s.db.QueryRow( s.db.QueryRow(
`SELECT `SELECT
reading_time reading_time
FROM FROM
entries entries
WHERE WHERE
user_id=$1 AND feed_id=$1 AND
feed_id=$2 AND hash=$2
hash=$3
`, `,
feed.UserID, feedID,
feed.ID, entryHash,
entry.Hash,
).Scan(&result) ).Scan(&result)
return result return result
} }