Most played song - good way to retrieve data
This commit is contained in:
@@ -11,7 +11,5 @@
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div *ngFor="let e of mostPlayedSongs">
|
<div *ngFor="let e of mostPlayedSongs">
|
||||||
<div class="module hero">
|
<h4>{{e['Play Count']}}: {{e.Name}} - {{e.Artist}}</h4>
|
||||||
<h4>{{e.Name}}</h4>
|
|
||||||
</div>
|
|
||||||
</div>
|
</div>
|
||||||
@@ -36,15 +36,8 @@ export class DashboardComponent implements OnInit {
|
|||||||
this.elsService.getTrackCount("album")
|
this.elsService.getTrackCount("album")
|
||||||
.then(result => this.trackCountAlbum = result);
|
.then(result => this.trackCountAlbum = result);
|
||||||
|
|
||||||
this.elsService.getMostPlayedTrackO().subscribe(
|
this.elsService.getMostPlayedTrack().subscribe(
|
||||||
data => {
|
data => this.mostPlayedSongs = data
|
||||||
data.forEach(element => {
|
|
||||||
this.mostPlayedSongs.push(element._source);
|
|
||||||
});
|
|
||||||
this.mostPlayedSongs.forEach(element => {
|
|
||||||
console.log(element.Name)
|
|
||||||
});
|
|
||||||
}
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -30,27 +30,32 @@ export class ElsService {
|
|||||||
.catch(this.handleError);
|
.catch(this.handleError);
|
||||||
}
|
}
|
||||||
|
|
||||||
getTrackCountO(type: string): Observable<number> {
|
getMostPlayedTrack(): Observable<Song[]> {
|
||||||
return this.http.get(this.elsUrl + type + "/_count")
|
// Thank to http://chariotsolutions.com/blog/post/angular2-observables-http-separating-services-components/
|
||||||
.map(res => res.json().count as number);
|
|
||||||
}
|
|
||||||
|
|
||||||
getMostPlayedTrack(): Promise<Hits[]> {
|
// 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})
|
|
||||||
.toPromise()
|
|
||||||
.then(res => res.json().hits.hits)
|
|
||||||
.catch(this.handleError);
|
|
||||||
}
|
|
||||||
|
|
||||||
getMostPlayedTrackO(): Observable<Object[]> {
|
|
||||||
return this.http
|
return this.http
|
||||||
.post(this.elsUrl + "song/_search",
|
.post(this.elsUrl + "song/_search",
|
||||||
JSON.stringify({"sort":[{"Play Count":{"order":"desc"}}],"size": 5}),
|
JSON.stringify({"sort":[{"Play Count":{"order":"desc"}}],"size": 5}),
|
||||||
{headers: this.headers})
|
{headers: this.headers})
|
||||||
.map(response => response.json().hits.hits as Object[]);
|
.map(res => {
|
||||||
// TODO Treat data here to return song instead of in subscribe
|
return res.json().hits.hits;
|
||||||
|
})
|
||||||
|
.map((hits: Array<any>) => {
|
||||||
|
let result:Array<Song> = [];
|
||||||
|
hits.forEach((hit) => {
|
||||||
|
result.push(hit._source);
|
||||||
|
});
|
||||||
|
return result;
|
||||||
|
});
|
||||||
|
// Shorter way:
|
||||||
|
// .map(res => {
|
||||||
|
// let result: Array<Song> = [];
|
||||||
|
// res.json().hits.hits.forEach(element => {
|
||||||
|
// result.push(element._source);
|
||||||
|
// });
|
||||||
|
// return result;
|
||||||
|
// });
|
||||||
}
|
}
|
||||||
|
|
||||||
private handleError(error: any): Promise<any> {
|
private handleError(error: any): Promise<any> {
|
||||||
|
|||||||
@@ -1,5 +0,0 @@
|
|||||||
export class Agg {
|
|
||||||
aggs : {
|
|
||||||
"intraday_return" : { "sum" : { "field" : "change" } }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,6 +0,0 @@
|
|||||||
import { Song } from './song';
|
|
||||||
|
|
||||||
export class Hits {
|
|
||||||
_id: string;
|
|
||||||
_source: Song;
|
|
||||||
}
|
|
||||||
@@ -1,3 +1,5 @@
|
|||||||
export class Song {
|
export class Song {
|
||||||
Name: string;
|
Name: string;
|
||||||
|
Artist: string;
|
||||||
|
"Play Count": number;
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user