From 49aed4575bb25ac2497ebbcc7578d4ea89d99f4b Mon Sep 17 00:00:00 2001 From: "Maxence G. de Montauzan" Date: Thu, 4 May 2017 02:51:31 +0200 Subject: [PATCH] Start dashboard on ELS data --- dashboard/src/app/app.module.ts | 13 +++-- dashboard/src/app/dashboard.component.html | 21 +++++--- dashboard/src/app/dashboard.component.ts | 52 +++++++++++++++++-- dashboard/src/app/els.service.ts | 60 ++++++++++++++++++++++ dashboard/src/app/els/Agg.ts | 5 ++ dashboard/src/app/object/hits.ts | 6 +++ dashboard/src/app/object/song.ts | 3 ++ 7 files changed, 145 insertions(+), 15 deletions(-) create mode 100644 dashboard/src/app/els.service.ts create mode 100644 dashboard/src/app/els/Agg.ts create mode 100644 dashboard/src/app/object/hits.ts create mode 100644 dashboard/src/app/object/song.ts diff --git a/dashboard/src/app/app.module.ts b/dashboard/src/app/app.module.ts index 68e67ec..0065399 100644 --- a/dashboard/src/app/app.module.ts +++ b/dashboard/src/app/app.module.ts @@ -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 ] }) diff --git a/dashboard/src/app/dashboard.component.html b/dashboard/src/app/dashboard.component.html index 8c916a4..4dded5e 100644 --- a/dashboard/src/app/dashboard.component.html +++ b/dashboard/src/app/dashboard.component.html @@ -1,8 +1,17 @@ -

Top Heroes

+

Important information

- -
-

{{hero.name}}

-
-
+
+

{{totalTimeSt}}

+
+
+
+
+

{{trackCountSong}} chansons, {{trackCountArtist}} artistes, {{trackCountAlbum}} albums

+
+
+ +
+
+

{{e.Name}}

+
\ No newline at end of file diff --git a/dashboard/src/app/dashboard.component.ts b/dashboard/src/app/dashboard.component.ts index 2af59e3..fa2b737 100644 --- a/dashboard/src/app/dashboard.component.ts +++ b/dashboard/src/app/dashboard.component.ts @@ -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"; } } \ No newline at end of file diff --git a/dashboard/src/app/els.service.ts b/dashboard/src/app/els.service.ts new file mode 100644 index 0000000..54ef5c2 --- /dev/null +++ b/dashboard/src/app/els.service.ts @@ -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 { + 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 { + return this.http.get(this.elsUrl + type + "/_count") + .toPromise() + .then(res => res.json().count as number) + .catch(this.handleError); + } + + getTrackCountO(type: string): Observable { + return this.http.get(this.elsUrl + type + "/_count") + .map(res => res.json().count as number); + } + + 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 { + 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 { + 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/els/Agg.ts b/dashboard/src/app/els/Agg.ts new file mode 100644 index 0000000..7554f23 --- /dev/null +++ b/dashboard/src/app/els/Agg.ts @@ -0,0 +1,5 @@ +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 new file mode 100644 index 0000000..97b55ed --- /dev/null +++ b/dashboard/src/app/object/hits.ts @@ -0,0 +1,6 @@ +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 new file mode 100644 index 0000000..182a42a --- /dev/null +++ b/dashboard/src/app/object/song.ts @@ -0,0 +1,3 @@ +export class Song { + Name: string; +} \ No newline at end of file