Skip to content

Commit

Permalink
Add library statistics
Browse files Browse the repository at this point in the history
For each folder, display the number of artists, alnums, tracks, the
total duration and size.

Fixes DocMarty84/koozic#6
  • Loading branch information
DocMarty84 committed Apr 24, 2018
1 parent ca7282d commit cfc333d
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 7 deletions.
39 changes: 39 additions & 0 deletions models/oomusic_folder.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ class MusicFolder(models.Model):
[('0', '0'), ('1', '1'), ('2', '2'), ('3', '3'), ('4', '4'), ('5', '5')],
'Rating', default='0',
)
root_total_artists = fields.Integer('Total Artists', compute='_compute_root_total')
root_total_albums = fields.Integer('Total Albums', compute='_compute_root_total')
root_total_tracks = fields.Integer('Total Tracks', compute='_compute_root_total')
root_total_duration = fields.Integer('Total Duration', compute='_compute_root_total')
root_total_size = fields.Integer('Total Size', compute='_compute_root_total')

image_folder = fields.Binary(
'Folder Image', compute='_compute_image_folder',
Expand Down Expand Up @@ -93,6 +98,40 @@ def _compute_in_playlist(self):
if folder.track_ids <= track_ids_in_playlist:
folder.in_playlist = True

def _compute_root_total(self):
for folder in self.filtered('root'):
self.env.cr.execute('''
SELECT COUNT(*) OVER()
FROM oomusic_artist AS a
JOIN oomusic_track AS t ON a.id = t.album_id
WHERE a.user_id = %s
AND t.root_folder_id = %s
GROUP BY a.id
''', (self.env.user.id, folder.id))
res_artists = self.env.cr.fetchall()
self.env.cr.execute('''
SELECT COUNT(*) OVER()
FROM oomusic_album AS a
JOIN oomusic_track AS t ON a.id = t.album_id
WHERE a.user_id = %s
AND t.root_folder_id = %s
GROUP BY a.id
''', (self.env.user.id, folder.id))
res_albums = self.env.cr.fetchall()
self.env.cr.execute('''
SELECT COUNT(t.id), SUM(duration_min)/60.0, SUM(size)
FROM oomusic_track AS t
WHERE t.user_id = %s
AND t.root_folder_id = %s
''', (self.env.user.id, folder.id))
res_tracks = self.env.cr.fetchall()

folder.root_total_artists = res_artists[0][0] if res_artists else 0
folder.root_total_albums = res_albums[0][0] if res_albums else 0
folder.root_total_tracks = res_tracks[0][0] if res_tracks else 0
folder.root_total_duration = res_tracks[0][1] if res_tracks else 0
folder.root_total_size = res_tracks[0][2] if res_tracks else 0

def _compute_image_folder(self):
accepted_names = ['folder', 'cover', 'front']
for folder in self:
Expand Down
1 change: 1 addition & 0 deletions models/oomusic_folder_scan.py
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,7 @@ def _scan_folder(self, folder_id):
else:
vals['bitrate'] = 0
vals['duration_min'] = float(vals['duration']) / 60
vals['size'] = (os.path.getsize(fn_path) or 0.0) / (1024.0*1024.0)
vals['path'] = fn_path
vals['last_modification'] = mtime
vals['root_folder_id'] = folder_id
Expand Down
1 change: 1 addition & 0 deletions models/oomusic_track.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class MusicTrack(models.Model):
duration_min = fields.Float('Duration', readonly=True)
bitrate = fields.Integer('Bitrate', readonly=True)
path = fields.Char('Path', required=True, index=True, readonly=True)
size = fields.Float('File Size (MiB)', readonly=True)
play_count = fields.Integer('Play Count', default=0, readonly=True)
last_play = fields.Datetime('Last Played', index=True, readonly=True)
last_modification = fields.Integer('Last Modification', readonly=True)
Expand Down
36 changes: 29 additions & 7 deletions views/oomusic_folder_views.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,35 @@
</h1>
</div>
<group>
<field name="path"/>
<field name="exclude_autoscan"/>
</group>
<group>
<field name="last_scan" readonly="1"/>
<field name="last_scan_duration" readonly="1" groups="base.group_no_one"/>
<field name="locked" groups="base.group_no_one"/>
<group>
<field name="path"/>
<field name="exclude_autoscan"/>
<field name="last_scan" readonly="1"/>
<field name="last_scan_duration" readonly="1" groups="base.group_no_one"/>
<field name="locked" groups="base.group_no_one"/>
</group>
<group>
<label for="root_total_artists" string="Library"/>
<div>
<field name="root_total_artists"/> artists
</div>
<label for="root_total_albums" string=""/>
<div>
<field name="root_total_albums"/> albums
</div>
<label for="root_total_tracks" string=""/>
<div>
<field name="root_total_tracks"/> tracks
</div>
<label for="root_total_duration" string=""/>
<div>
<field name="root_total_duration"/> hours
</div>
<label for="root_total_size" string=""/>
<div>
<field name="root_total_size"/> MiB
</div>
</group>
</group>
</sheet>
</form>
Expand Down
1 change: 1 addition & 0 deletions views/oomusic_track_views.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
</group>
<group>
<field name="path"/>
<field name="size"/>
</group>
<notebook>
<page string="Download Links">
Expand Down

0 comments on commit cfc333d

Please sign in to comment.