diff --git a/dashboard/src/app/album.component.ts b/dashboard/src/app/album.component.ts index e3d54b1..e058502 100644 --- a/dashboard/src/app/album.component.ts +++ b/dashboard/src/app/album.component.ts @@ -1,8 +1,8 @@ import { Component, OnInit } from '@angular/core'; -import { ActivatedRoute, Params } from '@angular/router' -import { Location } from '@angular/common' +import { ActivatedRoute, Params } from '@angular/router'; +import { Location } from '@angular/common'; -import { ElsService } from './els.service' +import { ElsService } from './els.service'; import { Song } from './object/song'; import { Album } from './object/album'; @@ -13,23 +13,23 @@ import { Album } from './object/album'; }) export class AlbumComponent implements OnInit { + albumName = ''; + songs: Array = []; + album: Album = new Album(); // If album not found, will be replaced by 'undefined' + moreDataAvailable: boolean = false; + atBottom: boolean = false; + constructor( private elsService: ElsService, private route: ActivatedRoute, private location: Location ) { } - albumName = ""; - songs: Array = []; - album: Album = new Album(); // If album not found, will be replaced by 'undefined' - moreDataAvailable: boolean = false; - atBottom: boolean = false; - ngOnInit(): void { this.route.params .subscribe((params: Params) => this.albumName = params['name']); - this.loadSongs() + this.loadSongs(); this.elsService.getAlbum(this.albumName).subscribe(data => this.album = data); } @@ -37,11 +37,11 @@ export class AlbumComponent implements OnInit { loadSongs(): void { this.elsService.getAlbumSongs(this.albumName, this.songs.length).subscribe( data => { - this.moreDataAvailable = data.length == ElsService.DEFAULT_SIZE + this.moreDataAvailable = data.length === ElsService.DEFAULT_SIZE; // Erase song array with result for first load, then add elements one by one // instead use concat => concat will sort table at each load, very consuming! and not user friendly - if (this.songs.length == 0) { + if (this.songs.length === 0) { this.songs = data; } else { data.forEach(song => { @@ -53,7 +53,7 @@ export class AlbumComponent implements OnInit { } scrollTop(): void { - window.scrollTo(0,0); + window.scrollTo(0, 0); } /** @@ -73,8 +73,8 @@ export class AlbumComponent implements OnInit { this.atBottom = true; } - if (window.scrollY == 0) { + if (window.scrollY === 0) { this.atBottom = false; } } -} \ No newline at end of file +} diff --git a/dashboard/src/app/app.component.ts b/dashboard/src/app/app.component.ts index 3c79b95..5410cc8 100644 --- a/dashboard/src/app/app.component.ts +++ b/dashboard/src/app/app.component.ts @@ -14,4 +14,4 @@ import { Component } from '@angular/core'; export class AppComponent { title = 'Tour of Heroes'; -} \ No newline at end of file +} diff --git a/dashboard/src/app/app.module.ts b/dashboard/src/app/app.module.ts index 33e77cb..fb87a3a 100644 --- a/dashboard/src/app/app.module.ts +++ b/dashboard/src/app/app.module.ts @@ -1,7 +1,7 @@ import { NgModule } from '@angular/core'; import { BrowserModule } from '@angular/platform-browser'; -import { FormsModule } from '@angular/forms' -import { HttpModule } from '@angular/http' +import { FormsModule } from '@angular/forms'; +import { HttpModule } from '@angular/http'; // Imports for loading & configuring the in-memory web api // import { InMemoryWebApiModule } from 'angular-in-memory-web-api'; @@ -10,17 +10,17 @@ import { HttpModule } from '@angular/http' import { AppComponent } from './app.component'; import { HeroDetailComponent } from './hero-detail.component'; import { HeroesComponent } from './heroes.component'; -import { DashboardComponent } from './dashboard.component' -import { AlbumComponent } from './album.component' -import { ArtistComponent } from './artist.component' +import { DashboardComponent } from './dashboard.component'; +import { AlbumComponent } from './album.component'; +import { ArtistComponent } from './artist.component'; import { HeroService } from './hero.service'; import { ElsService } from './els.service'; -import { AppRoutingModule } from './app-routing.module' +import { AppRoutingModule } from './app-routing.module'; -import { ConvertMsPipe } from './convertms.pipe' -import { SortPipe } from './sortby.pipe' +import { ConvertMsPipe } from './convertms.pipe'; +import { SortPipe } from './sortby.pipe'; @NgModule({ imports: [ diff --git a/dashboard/src/app/artist.component.ts b/dashboard/src/app/artist.component.ts index 8db8628..72d2822 100644 --- a/dashboard/src/app/artist.component.ts +++ b/dashboard/src/app/artist.component.ts @@ -1,12 +1,12 @@ import { Component, OnInit } from '@angular/core'; -import { ActivatedRoute, Params } from '@angular/router' -import { Location } from '@angular/common' +import { ActivatedRoute, Params } from '@angular/router'; +import { Location } from '@angular/common'; -import { ElsService } from './els.service' +import { ElsService } from './els.service'; import { Song } from './object/song'; import { Artist } from './object/artist'; -import { SortPipe } from './sortby.pipe' +import { SortPipe } from './sortby.pipe'; @Component({ selector: 'artist-component', @@ -15,13 +15,7 @@ import { SortPipe } from './sortby.pipe' }) export class ArtistComponent implements OnInit { - constructor( - private elsService: ElsService, - private route: ActivatedRoute, - private location: Location - ) { } - - artistName = ""; + artistName = ''; songs: Array = []; artist: Artist = new Artist(); // To activate button in interface var @@ -31,6 +25,12 @@ export class ArtistComponent implements OnInit { lockLoadData: boolean = false; + constructor( + private elsService: ElsService, + private route: ActivatedRoute, + private location: Location + ) { } + ngOnInit(): void { this.route.params.subscribe((params: Params) => this.artistName = params['name']); @@ -41,18 +41,18 @@ export class ArtistComponent implements OnInit { // TODO Duplicate code! loadSongs(): void { if (this.lockLoadData) { - console.debug("Loading data locked"); + console.debug('Loading data locked'); return; } this.lockLoadData = true; this.elsService.getArtistSongs(this.artistName, this.songs.length).subscribe( data => { - this.moreDataAvailable = data.length == ElsService.DEFAULT_SIZE; + this.moreDataAvailable = data.length === ElsService.DEFAULT_SIZE; // Erase song array with result for first load, then add elements one by one // instead use concat => concat will sort table at each load, very consuming! and not user friendly - if (this.songs.length == 0) { + if (this.songs.length === 0) { this.songs = data; } else { this.sortable = true; @@ -60,14 +60,14 @@ export class ArtistComponent implements OnInit { this.songs.push(song); }); } - console.debug("Unlock load data"); + console.debug('Unlock load data'); this.lockLoadData = false; } ); } scrollTop(): void { - window.scrollTo(0,0); + window.scrollTo(0, 0); } /** @@ -87,7 +87,7 @@ export class ArtistComponent implements OnInit { this.atBottom = true; } - if (window.scrollY == 0) { + if (window.scrollY === 0) { this.atBottom = false; } } diff --git a/dashboard/src/app/convertms.pipe.ts b/dashboard/src/app/convertms.pipe.ts index 8cc0769..57c0d6a 100644 --- a/dashboard/src/app/convertms.pipe.ts +++ b/dashboard/src/app/convertms.pipe.ts @@ -1,31 +1,31 @@ import { Pipe, PipeTransform } from '@angular/core'; -@Pipe({name: "convertMs"}) +@Pipe({name: 'convertMs'}) export class ConvertMsPipe implements PipeTransform { transform(timeMs: number): string { - let x = timeMs / 1000 + let x = timeMs / 1000; - let seconds = Math.round(x % 60) - x /= 60 + let seconds = Math.round(x % 60); + x /= 60; - let minutes = 0 - if (x > 1) minutes = Math.round(x % 60) - x /= 60 + let minutes = 0; + if (x > 1) { minutes = Math.round(x % 60); } + x /= 60; - let hours = 0 - if (x > 1) hours = Math.round(x % 24) + let hours = 0; + if (x > 1) { hours = Math.round(x % 24); } // TODO Enable/disable day - x /= 24 + x /= 24; let days = Math.round(x); // Final string - let ret = ""; - if (days > 0) ret += ('0' + days).slice(-2) + ":"; - if (hours > 0) ret += ('0' + hours).slice(-2) + ":"; - if (minutes > 0) ret += ('0' + minutes).slice(-2) + ":"; - if (seconds > 0) ret += ('0' + seconds).slice(-2); + let ret = ''; + if (days > 0) { ret += ('0' + days).slice(-2) + ':'; } + if (hours > 0) { ret += ('0' + hours).slice(-2) + ':'; } + if (minutes > 0) { ret += ('0' + minutes).slice(-2) + ':'; } + if (seconds > 0) { ret += ('0' + seconds).slice(-2); } return ret; - // return ('0' + days).slice(-2) + ":" + ('0' + hours).slice(-2) + ":" + ('0' + minutes).slice(-2) + ":" + ('0' + seconds).slice(-2); + // return ('0' + days).slice(-2) + ':' + ('0' + hours).slice(-2) + ':' + ('0' + minutes).slice(-2) + ':' + ('0' + seconds).slice(-2); } -} \ No newline at end of file +} diff --git a/dashboard/src/app/dashboard.component.ts b/dashboard/src/app/dashboard.component.ts index 9b368b7..be3cb44 100644 --- a/dashboard/src/app/dashboard.component.ts +++ b/dashboard/src/app/dashboard.component.ts @@ -1,8 +1,8 @@ import { Component, OnInit } from '@angular/core'; -import { Hero } from './hero' -import { HeroService } from './hero.service' -import { ElsService } from './els.service' +import { Hero } from './hero'; +import { HeroService } from './hero.service'; +import { ElsService } from './els.service'; import { Song } from './object/song'; import { Bucket } from './object/bucket'; @@ -15,7 +15,7 @@ import { Bucket } from './object/bucket'; export class DashboardComponent implements OnInit { totalTime: number = 0; totalSize: number = 0; - totalSizeSt = ""; + totalSizeSt = ''; trackCountSong: number = 0; trackCountArtist: number = 0; trackCountAlbum: number = 0; @@ -38,11 +38,11 @@ export class DashboardComponent implements OnInit { this.totalSizeSt = this.convertSizeToString(result); }); - this.elsService.getCountSong("song") + this.elsService.getCountSong('song') .then(result => this.trackCountSong = result); - this.elsService.getCountSong("artist") + this.elsService.getCountSong('artist') .then(result => this.trackCountArtist = result); - this.elsService.getCountSong("album") + this.elsService.getCountSong('album') .then(result => this.trackCountAlbum = result); this.elsService.getCountNeverListenSong() @@ -53,7 +53,7 @@ export class DashboardComponent implements OnInit { ); this.elsService.getGenres().subscribe(data => this.topGenres = data); - this.elsService.getGenres("asc").subscribe(data => this.bottomGenres = data); + this.elsService.getGenres('asc').subscribe(data => this.bottomGenres = data); this.elsService.getGenreCount().subscribe(data => console.log(data)); } @@ -65,13 +65,14 @@ export class DashboardComponent implements OnInit { convertSizeToString(size: number) { let units = ['Bytes', 'KiB', 'MiB', 'GiB', 'TiB']; - if (size == 0) + if (size === 0) { return '0 Byte'; + } let i = Math.floor(Math.log(size) / Math.log(1024)); - let calcSize = size / Math.pow(1024, i) + let calcSize = size / Math.pow(1024, i); calcSize = Math.round(calcSize * 100) / 100; return calcSize + ' ' + units[i]; } -} \ No newline at end of file +} diff --git a/dashboard/src/app/els.service.ts b/dashboard/src/app/els.service.ts index 8db6400..035b244 100644 --- a/dashboard/src/app/els.service.ts +++ b/dashboard/src/app/els.service.ts @@ -12,22 +12,31 @@ import { Bucket } from './object/bucket'; @Injectable() export class ElsService { - public static readonly DEFAULT_SIZE:number = 50; - private static readonly INDEX_NAME = "itunessongs"; + public static readonly DEFAULT_SIZE: number = 50; + private static readonly INDEX_NAME = 'itunessongs'; - private static readonly ACTION_SEARCH = "/_search"; - private static readonly ACTION_COUNT = "/_count"; + private static readonly ACTION_SEARCH = '/_search'; + private static readonly ACTION_COUNT = '/_count'; - private elsUrl = 'http://localhost:9200/' + ElsService.INDEX_NAME + "/"; + private elsUrl = 'http://localhost:9200/' + ElsService.INDEX_NAME + '/'; private headers = new Headers({'Content-Type': 'application/json'}); constructor(private http: Http) { } getTime(): Promise { - return this.http.post(this.elsUrl + 'song/' + ElsService.ACTION_SEARCH, JSON.stringify({aggs:{sum_time:{sum:{field:"Total Time"}}},"size":0}), {headers: this.headers}) - .toPromise() - .then(res => res.json().aggregations.sum_time.value as number) - .catch(this.handleError); + return this.http + .post(this.elsUrl + 'song' + ElsService.ACTION_SEARCH, + JSON.stringify({ + aggs: { + sum_time: { + sum: { field: 'Total Time'} + } + }, + 'size': 0 + }), {headers: this.headers}) + .toPromise() + .then(res => res.json().aggregations.sum_time.value as number) + .catch(this.handleError); } getTimeSlowly(): Promise { @@ -37,10 +46,19 @@ export class ElsService { } getSize(): Promise { - return this.http.post(this.elsUrl + ElsService.ACTION_SEARCH, JSON.stringify({aggs:{sum_time:{sum:{field:"Size"}}},"size":0}), {headers: this.headers}) - .toPromise() - .then(res => res.json().aggregations.sum_time.value as number) - .catch(this.handleError); + return this.http + .post(this.elsUrl + ElsService.ACTION_SEARCH, + JSON.stringify({ + aggs: { + sum_time: { + sum: { field: 'Size' } + } + }, + 'size': 0 + }), {headers: this.headers}) + .toPromise() + .then(res => res.json().aggregations.sum_time.value as number) + .catch(this.handleError); } getSizeSlowly(): Promise { @@ -50,17 +68,25 @@ export class ElsService { } getCountSong(type: string): Promise { - return this.http.get(this.elsUrl + type + ElsService.ACTION_COUNT) - .toPromise() - .then(res => res.json().count as number) - .catch(this.handleError); + return this.http + .get(this.elsUrl + type + ElsService.ACTION_COUNT) + .toPromise() + .then(res => res.json().count as number) + .catch(this.handleError); } getCountNeverListenSong(): Promise { return this.http - .post(this.elsUrl + "song" + ElsService.ACTION_COUNT, - JSON.stringify({"query":{"bool":{"must_not": {"exists": {"field": "Play Count"}}}}}), - {headers: this.headers}) + .post(this.elsUrl + 'song' + ElsService.ACTION_COUNT, + JSON.stringify({ + 'query': { + 'bool': { + 'must_not': { + 'exists': { 'field': 'Play Count'} + } + } + } + }), {headers: this.headers}) .toPromise() .then(res => res.json().count as number) .catch(this.handleError); @@ -78,21 +104,20 @@ export class ElsService { // Could be shorter but I think it's more readable like this. return this.http - .post(this.elsUrl + "song" + ElsService.ACTION_SEARCH, - JSON.stringify( - { - "sort": [ { - "Play Count": { - "order": "desc" - } } ], - "size": 5 - }), - {headers: this.headers}) + .post(this.elsUrl + 'song' + ElsService.ACTION_SEARCH, + JSON.stringify({ + 'sort': [ { + 'Play Count': { + 'order': 'desc' + } + } ], + 'size': 5 + }), {headers: this.headers}) .map(res => { return res.json().hits.hits; }) .map((hits: Array) => { - let result:Array = []; + let result: Array = []; hits.forEach((hit) => { result.push(hit._source); }); @@ -109,16 +134,21 @@ export class ElsService { } getAlbumSongs(albumName: string, from: number = 0): Observable { - console.debug("getAlbumSongs- Album name: " + albumName + " - from: " + from); + console.debug('getAlbumSongs- Album name: ' + albumName + ' - from: ' + from); return this.http - .post(this.elsUrl + "song" + ElsService.ACTION_SEARCH, - JSON.stringify({"query":{"match_phrase":{"Album":albumName}},"size": ElsService.DEFAULT_SIZE, "from": from}), - {headers: this.headers}) + .post(this.elsUrl + 'song' + ElsService.ACTION_SEARCH, + JSON.stringify({ + 'query': { + 'match_phrase': { 'Album': albumName } + }, + 'size': ElsService.DEFAULT_SIZE, + 'from': from + }), {headers: this.headers}) .map(res => { return res.json().hits.hits; }) .map((hits: Array) => { - let result:Array = []; + let result: Array = []; hits.forEach((hit) => { result.push(hit._source); }); @@ -127,28 +157,26 @@ export class ElsService { } getArtistSongs(artistName: string, from: number = 0): Observable { - console.debug("getArtistSongs- Artist name: " + artistName + " - from: " + from); + console.debug('getArtistSongs- Artist name: ' + artistName + ' - from: ' + from); return this.http - .post(this.elsUrl + "song" + ElsService.ACTION_SEARCH, - JSON.stringify( - { - "query": { - "bool": { - "should": [ - {"match_phrase" : { "Album Artist" : artistName }}, - {"match_phrase" : { "Artist" : artistName }} - ] - } - }, - "size": ElsService.DEFAULT_SIZE, - "from": from - }), - {headers: this.headers}) + .post(this.elsUrl + 'song' + ElsService.ACTION_SEARCH, + JSON.stringify({ + 'query': { + 'bool': { + 'should': [ + {'match_phrase' : { 'Album Artist' : artistName }}, + {'match_phrase' : { 'Artist' : artistName }} + ] + } + }, + 'size': ElsService.DEFAULT_SIZE, + 'from': from + }), {headers: this.headers}) .map(res => { return res.json().hits.hits; }) .map((hits: Array) => { - let result:Array = []; + let result: Array = []; hits.forEach((hit) => { result.push(hit._source); }); @@ -158,9 +186,13 @@ export class ElsService { getAlbum(albumName: string): Observable { return this.http - .post(this.elsUrl + "album" + ElsService.ACTION_SEARCH, - JSON.stringify({"query":{"match_phrase":{"Album":albumName}},"size": ElsService.DEFAULT_SIZE}), - {headers: this.headers}) + .post(this.elsUrl + 'album' + ElsService.ACTION_SEARCH, + JSON.stringify({ + 'query': { + 'match_phrase': { 'Album': albumName } + }, + 'size': ElsService.DEFAULT_SIZE + }), {headers: this.headers}) .map(res => { return res.json().hits.hits; }) @@ -178,9 +210,13 @@ export class ElsService { getArtist(artistName: string): Observable { return this.http - .post(this.elsUrl + "artist" + ElsService.ACTION_SEARCH, - JSON.stringify({"query":{"match_phrase":{"Artist":artistName}},"size": ElsService.DEFAULT_SIZE}), - {headers: this.headers}) + .post(this.elsUrl + 'artist' + ElsService.ACTION_SEARCH, + JSON.stringify({ + 'query': { + 'match_phrase': { 'Artist': artistName } + }, + 'size': ElsService.DEFAULT_SIZE + }), {headers: this.headers}) .map(res => res.json().hits.hits) .map((hits: Array) => { // Theorically, my script prevent to found two documents with this query. @@ -191,7 +227,7 @@ export class ElsService { } if (hits.length > 1) { console.error('More than one artist "' + artistName + '" found (' + hits.length + '), return the first.'); - console.error('This is not normal!') + console.error('This is not normal!'); } return hits[0]._source; }); @@ -199,48 +235,44 @@ export class ElsService { getGenres(ordering: string = 'desc'): Observable { return this.http - .post(this.elsUrl + "song" + ElsService.ACTION_SEARCH, - JSON.stringify( - { - "aggs" : { - "genres" : { - "terms" : { - "field" : "Genre.original", - "size" : 10, - "missing": "N/A", - "order": { "_count" : ordering } - } + .post(this.elsUrl + 'song' + ElsService.ACTION_SEARCH, + JSON.stringify({ + 'aggs' : { + 'genres' : { + 'terms' : { + 'field' : 'Genre.original', + 'size' : 10, + 'missing': 'N/A', + 'order': { '_count' : ordering } } - }, - "size": 0 - }), - {headers: this.headers}) + } + }, + 'size': 0 + }), {headers: this.headers}) .map(res => res.json().aggregations.genres.buckets) .map((hits: Array) => { - let result:Array = []; + let result: Array = []; hits.forEach((bucket) => { result.push(bucket); }); return result; - });; + }); } getGenreCount(): Observable { return this.http - .post(this.elsUrl + "song" + ElsService.ACTION_SEARCH, - JSON.stringify( - { - "aggs" : { - "genres" : { - "cardinality" : { - "field" : "Genre.original", - "missing": "N/A", - } + .post(this.elsUrl + 'song' + ElsService.ACTION_SEARCH, + JSON.stringify({ + 'aggs' : { + 'genres' : { + 'cardinality' : { + 'field' : 'Genre.original', + 'missing': 'N/A', } - }, - "size": 0 - }), - {headers: this.headers}) + } + }, + 'size': 0 + }), {headers: this.headers}) .map(res => res.json().aggregations.genres.value); } diff --git a/dashboard/src/app/hero-detail.component.ts b/dashboard/src/app/hero-detail.component.ts index f5a9a28..bb79cc0 100644 --- a/dashboard/src/app/hero-detail.component.ts +++ b/dashboard/src/app/hero-detail.component.ts @@ -1,9 +1,9 @@ import 'rxjs/add/operator/switchMap'; import { Component, OnInit } from '@angular/core'; -import { ActivatedRoute, Params } from '@angular/router' -import { Location } from '@angular/common' +import { ActivatedRoute, Params } from '@angular/router'; +import { Location } from '@angular/common'; -import { HeroService } from './hero.service' +import { HeroService } from './hero.service'; import { Hero } from './hero'; @Component({ @@ -34,4 +34,4 @@ export class HeroDetailComponent implements OnInit { goBack(): void { this.location.back(); } -} \ No newline at end of file +} diff --git a/dashboard/src/app/hero.service.ts b/dashboard/src/app/hero.service.ts index 4f7c0d9..4c521e8 100644 --- a/dashboard/src/app/hero.service.ts +++ b/dashboard/src/app/hero.service.ts @@ -54,4 +54,4 @@ export class HeroService { console.error('An error occurred', error); // for demo purposes only return Promise.reject(error.message || error); } -} \ No newline at end of file +} diff --git a/dashboard/src/app/hero.ts b/dashboard/src/app/hero.ts index de9c91e..e3eac51 100644 --- a/dashboard/src/app/hero.ts +++ b/dashboard/src/app/hero.ts @@ -1,4 +1,4 @@ export class Hero { id: number; name: string; -} \ No newline at end of file +} diff --git a/dashboard/src/app/heroes.component.ts b/dashboard/src/app/heroes.component.ts index 367a844..94cdfa3 100644 --- a/dashboard/src/app/heroes.component.ts +++ b/dashboard/src/app/heroes.component.ts @@ -37,7 +37,7 @@ export class HeroesComponent implements OnInit { add(name: string): void { name = name.trim(); if (!name) { - return + return; } this.heroService.create(name) diff --git a/dashboard/src/app/sortby.pipe.ts b/dashboard/src/app/sortby.pipe.ts index b483a9e..e8af7d1 100644 --- a/dashboard/src/app/sortby.pipe.ts +++ b/dashboard/src/app/sortby.pipe.ts @@ -3,21 +3,21 @@ import { Pipe, PipeTransform } from '@angular/core'; -@Pipe({name: "sortBy"}) +@Pipe({name: 'sortBy'}) export class SortPipe implements PipeTransform { transform(array: Array, ...args: any[]): Array { - array.sort((a: any, b: any) => { - for (let i = 0; i < array.length; i++) { - let arg = args[i]; + array.sort((a: any, b: any) => { + for (let i = 0; i < array.length; i++) { + let arg = args[i]; - if (a[arg] == undefined && b[arg] != undefined) return -1; - if (a[arg] != undefined && b[arg] == undefined) return 1; + if (a[arg] === undefined && b[arg] !== undefined) { return -1; } + if (a[arg] !== undefined && b[arg] === undefined) { return 1; } - if (a[arg] < b[arg]) return -1; - if (a[arg] > b[arg]) return 1; - } - return 0; - }); + if (a[arg] < b[arg]) { return -1; } + if (a[arg] > b[arg]) { return 1; } + } + return 0; + }); return array; } -} \ No newline at end of file +}