Skip to content

Commit

Permalink
Use SQL RANDOM() in suggestions
Browse files Browse the repository at this point in the history
It speeds up the computation in case of many tracks and albums.
  • Loading branch information
DocMarty84 committed Apr 22, 2019
1 parent fdc2a45 commit d29f312
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
6 changes: 4 additions & 2 deletions models/oomusic_playlist.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,15 +156,17 @@ def _smart_rnd(self):
if current_tracks:
query += "WHERE id NOT IN %s" % (tuple(current_tracks.ids + [0]),)
if folder_sharing == "inactive":
query += "{} user_id = {}".format(
query += "{} user_id = {} ".format(
"WHERE" if not current_tracks else "AND", self.env.uid
)
query += "ORDER BY RANDOM() "
query += "LIMIT {}".format(self.smart_playlist_qty or 1)
self.env.cr.execute(query)
res = self.env.cr.fetchall()
if not res:
return self.env["oomusic.track"]

track_ids = random.sample([r[0] for r in res], min(self.smart_playlist_qty, len(res)))
track_ids = [r[0] for r in res]
return self.env["oomusic.track"].browse(track_ids)

def _smart_played(self):
Expand Down
14 changes: 8 additions & 6 deletions models/oomusic_suggestion.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# -*- coding: utf-8 -*-

import random

from odoo import fields, models, api


Expand Down Expand Up @@ -53,13 +51,15 @@ def _compute_track_random(self):
)
query = "SELECT id FROM oomusic_track "
if folder_sharing == "inactive":
query += "WHERE user_id = {}".format(self.env.uid)
query += "WHERE user_id = {} ".format(self.env.uid)
query += "ORDER BY RANDOM() "
query += "LIMIT 10"
self.env.cr.execute(query)
res = self.env.cr.fetchall()
if not res:
return

self.track_random = random.sample([r[0] for r in res], min(10, len(res)))
self.track_random = [r[0] for r in res]

@api.depends("name_albums")
def _compute_album_recently_added(self):
Expand All @@ -72,10 +72,12 @@ def _compute_album_random(self):
)
query = "SELECT id FROM oomusic_album "
if folder_sharing == "inactive":
query += "WHERE user_id = {}".format(self.env.uid)
query += "WHERE user_id = {} ".format(self.env.uid)
query += "ORDER BY RANDOM() "
query += "LIMIT 15"
self.env.cr.execute(query)
res = self.env.cr.fetchall()
if not res:
return

self.album_random = random.sample([r[0] for r in res], min(15, len(res)))
self.album_random = [r[0] for r in res]

0 comments on commit d29f312

Please sign in to comment.