Start dashboard on ELS data

This commit is contained in:
2017-05-04 02:51:31 +02:00
parent 726cf70a84
commit 49aed4575b
7 changed files with 145 additions and 15 deletions

View File

@@ -4,14 +4,16 @@ 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';
import { InMemoryDataService } from './in-memory-data.service';
// import { InMemoryWebApiModule } from 'angular-in-memory-web-api';
// import { InMemoryDataService } from './in-memory-data.service';
import { AppComponent } from './app.component';
import { HeroDetailComponent } from './hero-detail.component';
import { HeroesComponent } from './heroes.component';
import { HeroService } from './hero.service';
import { DashboardComponent } from './dashboard.component'
import { HeroService } from './hero.service';
import { ElsService } from './els.service';
import { AppRoutingModule } from './app-routing.module'
@@ -20,7 +22,7 @@ import { AppRoutingModule } from './app-routing.module'
BrowserModule,
FormsModule,
HttpModule,
InMemoryWebApiModule.forRoot(InMemoryDataService),
// InMemoryWebApiModule.forRoot(InMemoryDataService),
AppRoutingModule
],
declarations: [
@@ -30,7 +32,8 @@ import { AppRoutingModule } from './app-routing.module'
DashboardComponent
],
providers: [
HeroService
HeroService,
ElsService
],
bootstrap: [ AppComponent ]
})

View File

@@ -1,8 +1,17 @@
<h3>Top Heroes</h3>
<h3>Important information</h3>
<div class="grid grid-pad">
<a *ngFor="let hero of heroes" [routerLink]="['/detail', hero.id]" class="col-1-4">
<div class="module hero">
<h4>{{hero.name}}</h4>
</div>
</a>
<div class="module hero">
<h4>{{totalTimeSt}}</h4>
</div>
</div>
<div class="grid grid-pad">
<div class="module hero">
<h4>{{trackCountSong}} chansons, {{trackCountArtist}} artistes, {{trackCountAlbum}} albums</h4>
</div>
</div>
<div *ngFor="let e of mostPlayedSongs">
<div class="module hero">
<h4>{{e.Name}}</h4>
</div>
</div>

View File

@@ -2,6 +2,8 @@ import { Component, OnInit } from '@angular/core';
import { Hero } from './hero'
import { HeroService } from './hero.service'
import { ElsService } from './els.service'
import { Song } from './object/song';
@Component({
selector: 'my-dashboard',
@@ -10,12 +12,54 @@ import { HeroService } from './hero.service'
})
export class DashboardComponent implements OnInit {
heroes: Hero[] = [];
totalTimeSt = "";
totalTime: number = 0;
trackCountSong: number = 0;
trackCountArtist: number = 0;
trackCountAlbum: number = 0;
constructor(private heroService: HeroService) { }
mostPlayedSongs: Song[] = [];
constructor(private elsService: ElsService) { }
ngOnInit(): void {
this.heroService.getHeroes()
.then(heroes => this.heroes = heroes.slice(1,5));
this.elsService.getTime()
.then(result => {
this.totalTime = result;
this.totalTimeSt = this.convertMsToTime(this.totalTime);
});
this.elsService.getTrackCount("song")
.then(result => this.trackCountSong = result);
this.elsService.getTrackCount("artist")
.then(result => this.trackCountArtist = result);
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)
});
}
);
}
convertMsToTime(ms: number): string {
let x = ms / 1000
let seconds = Math.round(x % 60)
x /= 60
let minutes = Math.round(x % 60)
x /= 60
let hours = Math.round(x % 24)
// TODO Enable/disable day
x /= 24
let days = Math.round(x);
// return days + "d" + hours + "h" + minutes + ":" + seconds;
return days + " days, " + hours + " hours, " + minutes + " minutes and " + seconds + " seconds";
}
}

View File

@@ -0,0 +1,60 @@
import { Injectable } from '@angular/core';
import { Headers, Http } from '@angular/http';
import { Hits } from './object/hits';
import { Song } from './object/song';
import 'rxjs/add/operator/toPromise';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/map';
@Injectable()
export class ElsService {
private elsUrl = 'http://localhost:9200/itunessongs/';
private headers = new Headers({'Content-Type': 'application/json'});
constructor(private http: Http) { }
getTime(): Promise<number> {
return this.http.post(this.elsUrl + "_search", JSON.stringify({aggs:{sum_time:{sum:{field:"Total Time"}}}}), {headers: this.headers})
.toPromise()
.then(res => res.json().aggregations.sum_time.value as number)
.catch(this.handleError);
}
getTrackCount(type: string): Promise<number> {
return this.http.get(this.elsUrl + type + "/_count")
.toPromise()
.then(res => res.json().count as number)
.catch(this.handleError);
}
getTrackCountO(type: string): Observable<number> {
return this.http.get(this.elsUrl + type + "/_count")
.map(res => res.json().count as number);
}
getMostPlayedTrack(): Promise<Hits[]> {
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
.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
}
private handleError(error: any): Promise<any> {
console.error('An error occurred', error); // for demo purposes only
return Promise.reject(error.message || error);
}
}

View File

@@ -0,0 +1,5 @@
export class Agg {
aggs : {
"intraday_return" : { "sum" : { "field" : "change" } }
}
}

View File

@@ -0,0 +1,6 @@
import { Song } from './song';
export class Hits {
_id: string;
_source: Song;
}

View File

@@ -0,0 +1,3 @@
export class Song {
Name: string;
}