diff --git a/dashboard/src/app/dashboard.component.html b/dashboard/src/app/dashboard.component.html index 4dded5e..9fda40a 100644 --- a/dashboard/src/app/dashboard.component.html +++ b/dashboard/src/app/dashboard.component.html @@ -11,7 +11,5 @@
-
-

{{e.Name}}

-
+

{{e['Play Count']}}: {{e.Name}} - {{e.Artist}}

\ No newline at end of file diff --git a/dashboard/src/app/dashboard.component.ts b/dashboard/src/app/dashboard.component.ts index fa2b737..45e4c19 100644 --- a/dashboard/src/app/dashboard.component.ts +++ b/dashboard/src/app/dashboard.component.ts @@ -36,15 +36,8 @@ export class DashboardComponent implements OnInit { this.elsService.getTrackCount("album") .then(result => this.trackCountAlbum = result); - this.elsService.getMostPlayedTrackO().subscribe( - data => { - data.forEach(element => { - this.mostPlayedSongs.push(element._source); - }); - this.mostPlayedSongs.forEach(element => { - console.log(element.Name) - }); - } + this.elsService.getMostPlayedTrack().subscribe( + data => this.mostPlayedSongs = data ); } diff --git a/dashboard/src/app/els.service.ts b/dashboard/src/app/els.service.ts index 54ef5c2..fe75908 100644 --- a/dashboard/src/app/els.service.ts +++ b/dashboard/src/app/els.service.ts @@ -30,27 +30,32 @@ export class ElsService { .catch(this.handleError); } - getTrackCountO(type: string): Observable { - return this.http.get(this.elsUrl + type + "/_count") - .map(res => res.json().count as number); - } + getMostPlayedTrack(): Observable { + // Thank to http://chariotsolutions.com/blog/post/angular2-observables-http-separating-services-components/ - getMostPlayedTrack(): Promise { - return this.http.post(this.elsUrl + "song/_search", - JSON.stringify({"sort":[{"Play Count":{"order":"desc"}}],"size": 5}), - {headers: this.headers}) - .toPromise() - .then(res => res.json().hits.hits) - .catch(this.handleError); - } - - getMostPlayedTrackO(): Observable { + // Could be shorter but I think it's more readable like this. return this.http - .post(this.elsUrl + "song/_search", - JSON.stringify({"sort":[{"Play Count":{"order":"desc"}}],"size": 5}), - {headers: this.headers}) - .map(response => response.json().hits.hits as Object[]); - // TODO Treat data here to return song instead of in subscribe + .post(this.elsUrl + "song/_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 = []; + hits.forEach((hit) => { + result.push(hit._source); + }); + return result; + }); + // Shorter way: + // .map(res => { + // let result: Array = []; + // res.json().hits.hits.forEach(element => { + // result.push(element._source); + // }); + // return result; + // }); } private handleError(error: any): Promise { diff --git a/dashboard/src/app/els/Agg.ts b/dashboard/src/app/els/Agg.ts deleted file mode 100644 index 7554f23..0000000 --- a/dashboard/src/app/els/Agg.ts +++ /dev/null @@ -1,5 +0,0 @@ -export class Agg { - aggs : { - "intraday_return" : { "sum" : { "field" : "change" } } - } -} \ No newline at end of file diff --git a/dashboard/src/app/object/hits.ts b/dashboard/src/app/object/hits.ts deleted file mode 100644 index 97b55ed..0000000 --- a/dashboard/src/app/object/hits.ts +++ /dev/null @@ -1,6 +0,0 @@ -import { Song } from './song'; - -export class Hits { - _id: string; - _source: Song; -} \ No newline at end of file diff --git a/dashboard/src/app/object/song.ts b/dashboard/src/app/object/song.ts index 182a42a..a3c6ac1 100644 --- a/dashboard/src/app/object/song.ts +++ b/dashboard/src/app/object/song.ts @@ -1,3 +1,5 @@ export class Song { Name: string; + Artist: string; + "Play Count": number; } \ No newline at end of file