]> git.proxmox.com Git - ceph.git/blame - ceph/src/pybind/mgr/dashboard/frontend/src/app/shared/services/formatter.service.ts
update ceph source to reef 18.2.1
[ceph.git] / ceph / src / pybind / mgr / dashboard / frontend / src / app / shared / services / formatter.service.ts
CommitLineData
11fdf7f2
TL
1import { Injectable } from '@angular/core';
2
f67539c2 3import _ from 'lodash';
11fdf7f2 4
11fdf7f2 5@Injectable({
81eedcae 6 providedIn: 'root'
11fdf7f2
TL
7})
8export class FormatterService {
11fdf7f2
TL
9 format_number(n: any, divisor: number, units: string[], decimals: number = 1): string {
10 if (_.isString(n)) {
11 n = Number(n);
12 }
13 if (!_.isNumber(n)) {
14 return '-';
15 }
1e59de90
TL
16 if (_.isNaN(n)) {
17 return 'N/A';
18 }
11fdf7f2
TL
19 let unit = n < 1 ? 0 : Math.floor(Math.log(n) / Math.log(divisor));
20 unit = unit >= units.length ? units.length - 1 : unit;
21 let result = _.round(n / Math.pow(divisor, unit), decimals).toString();
22 if (result === '') {
23 return '-';
24 }
25 if (units[unit] !== '') {
26 result = `${result} ${units[unit]}`;
27 }
28 return result;
29 }
30
05a536ef
TL
31 /**
32 * Converts a value from one set of units to another using a conversion factor
33 * @param n The value to be converted
34 * @param units The data units of the value
35 * @param targetedUnits The wanted data units to convert to
36 * @param conversionFactor The factor of convesion
37 * @param unitsArray An ordered array containing the data units
38 * @param decimals The number of decimals on the returned value
39 * @returns Returns a string of the given value formated to the targeted data units.
40 */
41 formatNumberFromTo(
42 n: any,
43 units: any,
aee94f69 44 targetedUnits: string = '',
05a536ef
TL
45 conversionFactor: number,
46 unitsArray: string[],
47 decimals: number = 1
48 ): string {
49 if (_.isString(n)) {
50 n = Number(n);
51 }
52 if (!_.isNumber(n)) {
53 return '-';
54 }
55 const unitsArrayLowerCase = unitsArray.map((str) => str.toLowerCase());
56 if (
57 !unitsArrayLowerCase.includes(units.toLowerCase()) ||
58 !unitsArrayLowerCase.includes(targetedUnits.toLowerCase())
59 ) {
60 return `${n} ${units}`;
61 }
62 const index =
63 unitsArrayLowerCase.indexOf(units.toLowerCase()) -
64 unitsArrayLowerCase.indexOf(targetedUnits.toLocaleLowerCase());
65 const convertedN =
66 index > 0
67 ? n * Math.pow(conversionFactor, index)
68 : n / Math.pow(conversionFactor, Math.abs(index));
69 let result = _.round(convertedN, decimals).toString();
70 result = `${result} ${targetedUnits}`;
71 return result;
72 }
73
11fdf7f2
TL
74 /**
75 * Convert the given value into bytes.
76 * @param {string} value The value to be converted, e.g. 1024B, 10M, 300KiB or 1ZB.
77 * @param error_value The value returned in case the regular expression did not match. Defaults to
78 * null.
79 * @returns Returns the given value in bytes without any unit appended or the defined error value
80 * in case xof an error.
81 */
9f95a23c 82 toBytes(value: string, error_value: number = null): number | null {
11fdf7f2
TL
83 const base = 1024;
84 const units = ['b', 'k', 'm', 'g', 't', 'p', 'e', 'z', 'y'];
85 const m = RegExp('^(\\d+(.\\d+)?) ?([' + units.join('') + ']?(b|ib|B/s)?)?$', 'i').exec(value);
86 if (m === null) {
87 return error_value;
88 }
89 let bytes = parseFloat(m[1]);
90 if (_.isString(m[3])) {
91 bytes = bytes * Math.pow(base, units.indexOf(m[3].toLowerCase()[0]));
92 }
93 return Math.round(bytes);
94 }
95
96 /**
97 * Converts `x ms` to `x` (currently) or `0` if the conversion fails
98 */
99 toMilliseconds(value: string): number {
100 const pattern = /^\s*(\d+)\s*(ms)?\s*$/i;
101 const testResult = pattern.exec(value);
102
103 if (testResult !== null) {
104 return +testResult[1];
105 }
106
107 return 0;
108 }
109
110 /**
111 * Converts `x IOPS` to `x` (currently) or `0` if the conversion fails
112 */
113 toIops(value: string): number {
114 const pattern = /^\s*(\d+)\s*(IOPS)?\s*$/i;
115 const testResult = pattern.exec(value);
116
117 if (testResult !== null) {
118 return +testResult[1];
119 }
120
121 return 0;
122 }
aee94f69
TL
123
124 toOctalPermission(modes: any) {
125 const scopes = ['owner', 'group', 'others'];
126 let octalMode = '';
127 for (const scope of scopes) {
128 let scopeValue = 0;
129 const mode = modes[scope];
130
131 if (mode) {
132 if (mode.includes('read')) scopeValue += 4;
133 if (mode.includes('write')) scopeValue += 2;
134 if (mode.includes('execute')) scopeValue += 1;
135 }
136
137 octalMode += scopeValue.toString();
138 }
139 return octalMode;
140 }
11fdf7f2 141}