From 87df9af88af7ad296e593c7aac58d3e1f28d2d24 Mon Sep 17 00:00:00 2001 From: "Maxence G. de Montauzan" Date: Thu, 4 May 2017 03:44:54 +0200 Subject: [PATCH] Calc size --- dashboard/src/app/dashboard.component.html | 5 +++ dashboard/src/app/dashboard.component.ts | 47 +++++++++++++++------- dashboard/src/app/els.service.ts | 10 ++++- 3 files changed, 46 insertions(+), 16 deletions(-) diff --git a/dashboard/src/app/dashboard.component.html b/dashboard/src/app/dashboard.component.html index 9fda40a..98e1ab1 100644 --- a/dashboard/src/app/dashboard.component.html +++ b/dashboard/src/app/dashboard.component.html @@ -4,6 +4,11 @@

{{totalTimeSt}}

+
+
+

{{totalSizeSt}}

+
+

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

diff --git a/dashboard/src/app/dashboard.component.ts b/dashboard/src/app/dashboard.component.ts index 45e4c19..584f800 100644 --- a/dashboard/src/app/dashboard.component.ts +++ b/dashboard/src/app/dashboard.component.ts @@ -14,6 +14,8 @@ import { Song } from './object/song'; export class DashboardComponent implements OnInit { totalTimeSt = ""; totalTime: number = 0; + totalSize: number = 0; + totalSizeSt = ""; trackCountSong: number = 0; trackCountArtist: number = 0; trackCountAlbum: number = 0; @@ -23,22 +25,26 @@ export class DashboardComponent implements OnInit { constructor(private elsService: ElsService) { } ngOnInit(): void { - this.elsService.getTime() - .then(result => { - this.totalTime = result; - this.totalTimeSt = this.convertMsToTime(this.totalTime); - }); + 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.getSize().then(result => { + this.totalSize = result; + this.totalSizeSt = this.convertSizeToString(result); + }); - this.elsService.getMostPlayedTrack().subscribe( - data => this.mostPlayedSongs = data - ); + 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.getMostPlayedTrack().subscribe( + data => this.mostPlayedSongs = data + ); } convertMsToTime(ms: number): string { @@ -55,4 +61,17 @@ export class DashboardComponent implements OnInit { // return days + "d" + hours + "h" + minutes + ":" + seconds; return days + " days, " + hours + " hours, " + minutes + " minutes and " + seconds + " seconds"; } + + convertSizeToString(size: number) { + let units = ['Bytes', 'KiB', 'MiB', 'GiB', 'TiB']; + + if (size == 0) + return '0 Byte'; + + let i = Math.floor(Math.log(size) / Math.log(1024)); + 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 fe75908..85d6a2d 100644 --- a/dashboard/src/app/els.service.ts +++ b/dashboard/src/app/els.service.ts @@ -1,7 +1,6 @@ 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'; @@ -17,7 +16,14 @@ export class ElsService { 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}) + return this.http.post(this.elsUrl + "_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); + } + + getSize(): Promise { + return this.http.post(this.elsUrl + "_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);