import { Component, OnInit } from '@angular/core'; import { ElsSortService } from '../els-sort.service'; import { Album } from '../model/album'; import { Bucket } from '../model/bucket'; import { Song } from '../model/song'; import { Utils } from '../utils'; @Component({ selector: 'app-to-sort', templateUrl: './to-sort.component.html', styleUrls: ['./to-sort.component.css'] }) export class ToSortComponent implements OnInit { numberToArray = Utils.numberToArray; // Stats var totalTime = 0; totalSize = 0; trackCountSong = 0; neverListenSong = 0; // Global var toSortAlbum: Bucket[] = []; albums = [] constructor(private elsService: ElsSortService) { } ngOnInit(): void { // **** GET STATS ****// this.elsService.getTime().then(result => this.totalTime = result); this.elsService.getSize().then(result => this.totalSize = result); this.elsService.getCountSong().then(result => this.trackCountSong = result); this.elsService.getCountNeverListenSong().then(result => this.neverListenSong = result); // **** GET ALBUMS ****// 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; }); } }