From 331458edbd02d7b9cc84d9dc07b80d61ac313e5c Mon Sep 17 00:00:00 2001 From: "Maxence G. de Montauzan" Date: Sat, 7 Oct 2017 11:50:57 +0200 Subject: [PATCH] Get last added album improved - Take number of month - Extract method to treat bucket --- dashboard/src/app/dashboard.component.ts | 35 +++++++++++++++--------- dashboard/src/app/els.service.ts | 6 ++-- 2 files changed, 25 insertions(+), 16 deletions(-) diff --git a/dashboard/src/app/dashboard.component.ts b/dashboard/src/app/dashboard.component.ts index c8218ca..29ec069 100644 --- a/dashboard/src/app/dashboard.component.ts +++ b/dashboard/src/app/dashboard.component.ts @@ -61,22 +61,31 @@ export class DashboardComponent implements OnInit { this.elsService.getGenres('asc').subscribe(data => this.bottomGenres = data); // this.elsService.getGenreCount().subscribe(data => console.log(data)); - this.elsService.getLastAddedAlbums().subscribe(buckets => { + this.elsService.getLastAddedAlbums(6).subscribe(buckets => { this.lastAddedAlbums = buckets; - buckets.forEach(bucket => { - this.elsService.getArtistFromAlbumName(bucket.key).subscribe(data => { - if (data.length > 1) { - data.forEach(album => { - if (album['Track Count'] === bucket.doc_count) { - console.log(album.Artist.toString()); - this.albumArtists[album.Name] = album.Artist.toString(); - } - }); - } else { - this.albumArtists[data[0].Name] = data[0].Artist.toString(); + buckets.forEach(bucket => this.getArtistName(bucket)); + }); + } + + private getArtistName(albumBucket: Bucket) { + // For each bucket.key (album name), search artist. + // Use track count to compare + this.elsService.getArtistFromAlbumName(albumBucket.key).subscribe(albums => { + if (albums.length > 1) { + // More than one result for an album name + // Search good artist by track count + albums.forEach(album => { + if (album['Track Count'] === albumBucket.doc_count) { + this.albumArtists[album.Name] = album.Artist.toString(); } }); - }); + } else { + // Just one artistresult for album name + this.albumArtists[albums[0].Name] = albums[0].Artist.toString(); + } + if (this.albumArtists[albums[0].Name].length > 50) { + this.albumArtists[albums[0].Name] = this.albumArtists[albums[0].Name].substring(0, 50) + '...'; + } }); } diff --git a/dashboard/src/app/els.service.ts b/dashboard/src/app/els.service.ts index c652d91..f268c05 100644 --- a/dashboard/src/app/els.service.ts +++ b/dashboard/src/app/els.service.ts @@ -270,14 +270,14 @@ export class ElsService { .map(res => res.json().aggregations.genres.value); } - getLastAddedAlbums(): Observable { + getLastAddedAlbums(month: number): Observable { return this.http .post(this.elsUrl + 'song' + ElsService.ACTION_SEARCH, JSON.stringify({ 'query': { 'range' : { 'Date Added' : { - 'gte' : 'now-6M/d', + 'gte' : 'now-' + month + 'M/d', 'lte' : 'now/d' } } @@ -286,7 +286,7 @@ export class ElsService { 'album' : { 'terms' : { 'field' : 'Album.original', - 'size': 5 + 'size': 10 } } },