From 96534c44b7f9cbd60aad84771d9d2002192372dd Mon Sep 17 00:00:00 2001 From: Ricardo Gonzalez Date: Sun, 11 Oct 2020 17:45:10 +0100 Subject: [PATCH] Improve humanizeHours --- webapp/frontend/package-lock.json | 10 ++++++++ webapp/frontend/package.json | 2 ++ .../modules/dashboard/dashboard.component.ts | 22 +++++++----------- .../app/modules/detail/detail.component.html | 2 +- .../app/modules/detail/detail.component.ts | 23 ++++++++----------- 5 files changed, 30 insertions(+), 29 deletions(-) diff --git a/webapp/frontend/package-lock.json b/webapp/frontend/package-lock.json index 03eb54d..f2ac183 100644 --- a/webapp/frontend/package-lock.json +++ b/webapp/frontend/package-lock.json @@ -1922,6 +1922,11 @@ "integrity": "sha512-pGF/zvYOACZ/gLGWdQH8zSwteQS1epp68yRcVLJMgUck/MjEn/FBYmPub9pXT8C1e4a8YZfHo1CKyV8q1vKUnQ==", "dev": true }, + "@types/humanize-duration": { + "version": "3.18.1", + "resolved": "https://registry.npmjs.org/@types/humanize-duration/-/humanize-duration-3.18.1.tgz", + "integrity": "sha512-MUgbY3CF7hg/a/jogixmAufLjJBQT7WEf8Q+kYJkOc47ytngg1IuZobCngdTjAgY83JWEogippge5O5fplaQlw==" + }, "@types/jasmine": { "version": "3.5.10", "resolved": "https://registry.npmjs.org/@types/jasmine/-/jasmine-3.5.10.tgz", @@ -6064,6 +6069,11 @@ } } }, + "humanize-duration": { + "version": "3.24.0", + "resolved": "https://registry.npmjs.org/humanize-duration/-/humanize-duration-3.24.0.tgz", + "integrity": "sha512-B3udnqisaDeRsvUSb+5n2hjxhABI9jotB+i1IEhgHhguTeM5LxIUKoVIu7UpeyaPOygr/Fnv7UhOi45kYYG+tg==" + }, "humanize-ms": { "version": "1.2.1", "resolved": "https://registry.npmjs.org/humanize-ms/-/humanize-ms-1.2.1.tgz", diff --git a/webapp/frontend/package.json b/webapp/frontend/package.json index fb8c724..748b322 100644 --- a/webapp/frontend/package.json +++ b/webapp/frontend/package.json @@ -39,9 +39,11 @@ "@fullcalendar/moment": "4.4.0", "@fullcalendar/rrule": "4.4.0", "@fullcalendar/timegrid": "4.4.0", + "@types/humanize-duration": "^3.18.1", "apexcharts": "3.19.0", "crypto-js": "3.3.0", "highlight.js": "10.0.1", + "humanize-duration": "^3.24.0", "lodash": "4.17.15", "moment": "2.24.0", "ng-apexcharts": "1.2.3", diff --git a/webapp/frontend/src/app/modules/dashboard/dashboard.component.ts b/webapp/frontend/src/app/modules/dashboard/dashboard.component.ts index b244655..0d44110 100644 --- a/webapp/frontend/src/app/modules/dashboard/dashboard.component.ts +++ b/webapp/frontend/src/app/modules/dashboard/dashboard.component.ts @@ -8,6 +8,7 @@ import { DashboardService } from 'app/modules/dashboard/dashboard.service'; import * as moment from "moment"; import {MatDialog} from '@angular/material/dialog'; import { DashboardSettingsComponent } from 'app/layout/common/dashboard-settings/dashboard-settings.component'; +import humanizeDuration from 'humanize-duration' @Component({ selector : 'example', @@ -184,23 +185,16 @@ export class DashboardComponent implements OnInit, AfterViewInit, OnDestroy return item.id || index; } - humanizeHours(hours: number): string { + humanizeHours(hours: number, full: boolean = false): string { + let duration: string = '' if(!hours){ return '--' } - - var value: number - let unit = "" - if(hours > (24*365)){ //more than a year - value = Math.round((hours/(24*365)) * 10)/10 - unit = "years" - } else if (hours > 24){ - value = Math.round((hours/24) *10 )/10 - unit = "days" - } else{ - value = hours - unit = "hours" + if(!full){ + duration = humanizeDuration(hours * 60 * 60 * 1000, { round: true, largest: 1, units: ['y', 'd', 'h', 'm', 's'] }); + } else { + duration = humanizeDuration(hours * 60 * 60 * 1000, { conjunction: " and ", serialComma: false }); } - return `${value} ${unit}` + return duration } } diff --git a/webapp/frontend/src/app/modules/detail/detail.component.html b/webapp/frontend/src/app/modules/detail/detail.component.html index 7706f90..3b3e322 100644 --- a/webapp/frontend/src/app/modules/detail/detail.component.html +++ b/webapp/frontend/src/app/modules/detail/detail.component.html @@ -97,7 +97,7 @@
Power Cycle Count
-
{{humanizeHours(data.data.smart_results[0]?.power_on_hours)}}
+
{{humanizeHours(data.data.smart_results[0]?.power_on_hours)}}
Powered On
diff --git a/webapp/frontend/src/app/modules/detail/detail.component.ts b/webapp/frontend/src/app/modules/detail/detail.component.ts index 43a3684..cd84d38 100644 --- a/webapp/frontend/src/app/modules/detail/detail.component.ts +++ b/webapp/frontend/src/app/modules/detail/detail.component.ts @@ -8,6 +8,7 @@ import {takeUntil} from "rxjs/operators"; import {fadeOut} from "../../../@treo/animations/fade"; import {DetailSettingsComponent} from "app/layout/common/detail-settings/detail-settings.component"; import {MatDialog} from "@angular/material/dialog"; +import humanizeDuration from 'humanize-duration'; @Component({ selector: 'detail', @@ -320,23 +321,17 @@ export class DetailComponent implements OnInit, AfterViewInit, OnDestroy { // return item.id || index; } - humanizeHours(hours: number): string { + + humanizeHours(hours: number, full: boolean = false): string { + let duration: string = '' if(!hours){ return '--' } - - var value: number - let unit = "" - if(hours > (24*365)){ //more than a year - value = Math.round((hours/(24*365)) * 10)/10 - unit = "years" - } else if (hours > 24){ - value = Math.round((hours/24) *10 )/10 - unit = "days" - } else{ - value = hours - unit = "hours" + if(!full){ + duration = humanizeDuration(hours * 60 * 60 * 1000, { round: true, largest: 1, units: ['y', 'd', 'h', 'm', 's'] }); + } else { + duration = humanizeDuration(hours * 60 * 60 * 1000, { conjunction: " and ", serialComma: false }); } - return `${value} ${unit}` + return duration } }