Merge pull request #4385 from mempool/mononaut/memory-usage-precision
Round memory usage to 3 significant figures
This commit is contained in:
		
						commit
						61d7fd490a
					
				| @ -237,7 +237,7 @@ | |||||||
|       <div class="card-text" *ngIf="(isLoadingWebSocket$ | async) === false && mempoolInfoData.value; else loadingbig"> |       <div class="card-text" *ngIf="(isLoadingWebSocket$ | async) === false && mempoolInfoData.value; else loadingbig"> | ||||||
|         <div class="progress"> |         <div class="progress"> | ||||||
|           <div class="progress-bar {{ mempoolInfoData.value.mempoolSizeProgress }}" role="progressbar" [ngStyle]="{'width': (mempoolInfoData.value.memPoolInfo.usage / mempoolInfoData.value.memPoolInfo.maxmempool * 100) + '%' }"> </div> |           <div class="progress-bar {{ mempoolInfoData.value.mempoolSizeProgress }}" role="progressbar" [ngStyle]="{'width': (mempoolInfoData.value.memPoolInfo.usage / mempoolInfoData.value.memPoolInfo.maxmempool * 100) + '%' }"> </div> | ||||||
|           <div class="progress-text">‎<span [innerHTML]="mempoolInfoData.value.memPoolInfo.usage | bytes"></span> / <span [innerHTML]="mempoolInfoData.value.memPoolInfo.maxmempool | bytes"></span></div> |           <div class="progress-text">‎<span [innerHTML]="mempoolInfoData.value.memPoolInfo.usage | bytes : 2 : 'B' : null : 3"></span> / <span [innerHTML]="mempoolInfoData.value.memPoolInfo.maxmempool | bytes"></span></div> | ||||||
|         </div> |         </div> | ||||||
|       </div> |       </div> | ||||||
|     </div> |     </div> | ||||||
|  | |||||||
| @ -1,6 +1,6 @@ | |||||||
| /* tslint:disable */ | /* tslint:disable */ | ||||||
| import { Pipe, PipeTransform } from '@angular/core'; | import { Pipe, PipeTransform } from '@angular/core'; | ||||||
| import { isNumberFinite, isPositive, isInteger, toDecimal } from './utils'; | import { isNumberFinite, isPositive, isInteger, toDecimal, toSigFigs } from './utils'; | ||||||
| 
 | 
 | ||||||
| export type ByteUnit = 'B' | 'kB' | 'MB' | 'GB' | 'TB'; | export type ByteUnit = 'B' | 'kB' | 'MB' | 'GB' | 'TB'; | ||||||
| 
 | 
 | ||||||
| @ -17,7 +17,7 @@ export class BytesPipe implements PipeTransform { | |||||||
|         'TB': {max: Number.MAX_SAFE_INTEGER, prev: 'GB'} |         'TB': {max: Number.MAX_SAFE_INTEGER, prev: 'GB'} | ||||||
|     }; |     }; | ||||||
| 
 | 
 | ||||||
|     transform(input: any, decimal: number = 0, from: ByteUnit = 'B', to?: ByteUnit): any { |     transform(input: any, decimal: number = 0, from: ByteUnit = 'B', to?: ByteUnit, sigfigs?: number): any { | ||||||
| 
 | 
 | ||||||
|         if (!(isNumberFinite(input) && |         if (!(isNumberFinite(input) && | ||||||
|                 isNumberFinite(decimal) && |                 isNumberFinite(decimal) && | ||||||
| @ -33,10 +33,14 @@ export class BytesPipe implements PipeTransform { | |||||||
|             unit = BytesPipe.formats[unit].prev!; |             unit = BytesPipe.formats[unit].prev!; | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|  |         let numberFormat = sigfigs == null ? | ||||||
|  |             (number) => toDecimal(number, decimal).toString() : | ||||||
|  |             (number) => toSigFigs(number, sigfigs); | ||||||
|  | 
 | ||||||
|         if (to) { |         if (to) { | ||||||
|             const format = BytesPipe.formats[to]; |             const format = BytesPipe.formats[to]; | ||||||
| 
 | 
 | ||||||
|             const result = toDecimal(BytesPipe.calculateResult(format, bytes), decimal); |             const result = numberFormat(BytesPipe.calculateResult(format, bytes)); | ||||||
| 
 | 
 | ||||||
|             return BytesPipe.formatResult(result, to); |             return BytesPipe.formatResult(result, to); | ||||||
|         } |         } | ||||||
| @ -45,14 +49,14 @@ export class BytesPipe implements PipeTransform { | |||||||
|             const format = BytesPipe.formats[key]; |             const format = BytesPipe.formats[key]; | ||||||
|             if (bytes < format.max) { |             if (bytes < format.max) { | ||||||
| 
 | 
 | ||||||
|                 const result = toDecimal(BytesPipe.calculateResult(format, bytes), decimal); |                 const result = numberFormat(BytesPipe.calculateResult(format, bytes)); | ||||||
| 
 | 
 | ||||||
|                 return BytesPipe.formatResult(result, key); |                 return BytesPipe.formatResult(result, key); | ||||||
|             } |             } | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     static formatResult(result: number, unit: string): string { |     static formatResult(result: string, unit: string): string { | ||||||
|         return `${result} <span class="symbol">${unit}</span>`; |         return `${result} <span class="symbol">${unit}</span>`; | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -54,6 +54,10 @@ export function toDecimal(value: number, decimal: number): number { | |||||||
|   return Math.round(value * Math.pow(10, decimal)) / Math.pow(10, decimal); |   return Math.round(value * Math.pow(10, decimal)) / Math.pow(10, decimal); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | export function toSigFigs(value: number, sigFigs: number): string { | ||||||
|  |   return value >= Math.pow(10, sigFigs - 1) ? Math.round(value).toString() : value.toPrecision(sigFigs); | ||||||
|  | } | ||||||
|  | 
 | ||||||
| export function upperFirst(value: string): string { | export function upperFirst(value: string): string { | ||||||
|   return value.slice(0, 1).toUpperCase() + value.slice(1); |   return value.slice(0, 1).toUpperCase() + value.slice(1); | ||||||
| } | } | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user