(front) Improve ToSort using album index

This commit is contained in:
2023-01-29 20:07:25 +01:00
parent 7e0a791fbe
commit 2ddafaff1b
3 changed files with 19 additions and 78 deletions

View File

@@ -115,7 +115,7 @@ export class ElsSortService extends ElsService {
getNbAlbums(): Promise<number> { getNbAlbums(): Promise<number> {
return this.http return this.http
.post<any>(this.elsUrl + ElsService.SONG_INDEX_NAME + ElsService.ACTION_SEARCH, .post<any>(this.elsUrl + ElsService.ALBUM_INDEX_NAME + ElsService.ACTION_SEARCH,
JSON.stringify({ JSON.stringify({
'query': { 'query': {
'bool': { 'bool': {
@@ -142,9 +142,9 @@ export class ElsSortService extends ElsService {
.catch(error => this.handleError(error, 'getNbAlbums()')); .catch(error => this.handleError(error, 'getNbAlbums()'));
} }
getAlbums(): Observable<Bucket[]> { getAlbums(): Observable<Album[]> {
return this.http return this.http
.post(this.elsUrl + ElsService.SONG_INDEX_NAME + ElsService.ACTION_SEARCH, .post(this.elsUrl + ElsService.ALBUM_INDEX_NAME + ElsService.ACTION_SEARCH,
JSON.stringify({ JSON.stringify({
query: { query: {
bool: { bool: {
@@ -157,19 +157,13 @@ export class ElsSortService extends ElsService {
] ]
} }
}, },
'size': 0, 'size': 550,
'aggs': { "sort": [
'albums' : { { "Play Count": "desc"}
'terms': { ]
'field' : 'Album.raw',
'order': { '_key': 'asc' },
'size': 50
}
}
}
}), {headers: this.headers}) }), {headers: this.headers})
.pipe( .pipe(
map(res => this.responseAggregationToBucket(res, "albums")), map(res => this.responseToAlbums(res)),
catchError(error => this.handleError(error, 'getAlbums')) catchError(error => this.handleError(error, 'getAlbums'))
); );
} }

View File

@@ -91,22 +91,22 @@
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
<tr *ngFor="let album of toSortAlbum"> <tr *ngFor="let album of albums">
<td> <td>
<a [routerLink]="['/album', album.key]">{{album.key}}</a>&nbsp; <a [routerLink]="['/album', album.Name]">{{album.Name}}</a>&nbsp;
</td> </td>
<td>{{albums[album.key]['Track Count']}}</td> <td>{{album['Track Count']}}</td>
<ng-template [ngIf]="albums[album.key]['Album Artist']" [ngIfElse]="artistSection"> <ng-template [ngIf]="album['Album Artist']" [ngIfElse]="artistSection">
<td> <td>
<a [routerLink]="['/artist', albums[album.key]['Album Artist']]">{{albums[album.key]['Album Artist']}}</a>&nbsp; <a [routerLink]="['/artist', album['Album Artist']]">{{album['Album Artist']}}</a>&nbsp;
</td> </td>
</ng-template> </ng-template>
<ng-template #artistSection> <ng-template #artistSection>
<td> <td>
<a [routerLink]="['/artist', albums[album.key].Artist[0]]">{{albums[album.key].Artist}}</a>&nbsp; <a [routerLink]="['/artist', album.Artist[0]]">{{album.Artist}}</a>&nbsp;
</td> </td>
</ng-template> </ng-template>
@@ -117,12 +117,12 @@
</ng-template> </ng-template>
<td> <td>
{{albums[album.key]['Play Count']}} ({{albums[album.key]['Play Count']/albums[album.key]['Track Count'] | number:'1.0-0'}}/songs) {{album['Play Count']}} ({{album['Play Count']/album['Track Count'] | number:'1.0-0'}}/songs)
</td> </td>
<td class="star" [title]="(albums[album.key]['Album Rating Computed']?'Computed Rating: ':'Rating: ') + albums[album.key]['Album Rating']"> <td class="star" [title]="(album['Album Rating Computed']?'Computed Rating: ':'Rating: ') + album['Album Rating']">
<span *ngFor="let item of numberToArray(albums[album.key]['Album Rating'], 20)"> <span *ngFor="let item of numberToArray(album['Album Rating'], 20)">
<span class="glyphicon" [ngClass]="albums[album.key]['Album Rating Computed']?'glyphicon-star-empty':'glyphicon-star'"></span> <span class="glyphicon" [ngClass]="album['Album Rating Computed']?'glyphicon-star-empty':'glyphicon-star'"></span>
</span> </span>
</td> </td>
</tr> </tr>

View File

@@ -1,8 +1,6 @@
import { Component, OnInit } from '@angular/core'; import { Component, OnInit } from '@angular/core';
import { ElsSortService } from '../els-sort.service'; import { ElsSortService } from '../els-sort.service';
import { Album } from '../model/album';
import { Bucket } from '../model/bucket'; import { Bucket } from '../model/bucket';
import { Song } from '../model/song';
import { Utils } from '../utils'; import { Utils } from '../utils';
@@ -34,58 +32,7 @@ export class ToSortComponent implements OnInit {
this.elsService.getCountNeverListenSong().then(result => this.neverListenSong = result); this.elsService.getCountNeverListenSong().then(result => this.neverListenSong = result);
this.elsService.getNbAlbums().then(result => this.nbAlbums = result); this.elsService.getNbAlbums().then(result => this.nbAlbums = result);
// **** GET ALBUMS ****// this.elsService.getAlbums().subscribe(data => this.albums = data)
const tmpToSortAlbums: Bucket[] = [];
this.elsService.getAlbums().subscribe(buckets => {
buckets.forEach(bucket => {
if (tmpToSortAlbums.length === 0) {
tmpToSortAlbums.push(bucket);
} else {
let found = false;
tmpToSortAlbums.forEach(element => {
if (element.key === bucket.key) {
element.doc_count += bucket.doc_count;
found = true;
}
});
if (!found) {
tmpToSortAlbums.push(bucket);
}
}
});
this.toSortAlbum = tmpToSortAlbums;
this.toSortAlbum.forEach(bucket => this.getAlbum(bucket));
// console.log(this.toSortAlbum)
// console.log(this.albums)
});
}
private getAlbum(albumBucket: Bucket) {
// For each bucket.key (album name), get Album document
// Use track count to compare
this.elsService.getArtistFromAlbumName(albumBucket.key).subscribe(albums => {
// Identification of the good album
let goodAlbum;
if (albums.length > 1) {
// More than one result for an album name: search good by track count
albums.forEach(album => {
if (album['Track Count'] === albumBucket.doc_count) {
goodAlbum = album;
}
});
} else {
// Just one result for album name
goodAlbum = albums[0];
}
// TODO Crap security if no good album found
if (goodAlbum == undefined) {
goodAlbum = albums[0]
}
this.albums[albumBucket.key] = goodAlbum;
});
} }
} }