Skip to content

Commit 1e343b9

Browse files
Cyrille Bourgoisjleveugle
authored andcommitted
feat(cloud-univer-components): add services and filter
* add `cucBytes` filter * add `CucPriceHelper` service * add `CucFlavorService` service
1 parent 97d2cb8 commit 1e343b9

File tree

9 files changed

+465
-0
lines changed

9 files changed

+465
-0
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import map from 'lodash/map';
2+
import indexOf from 'lodash/indexOf';
3+
4+
export default /* @ngInject */ ($translate) => {
5+
// TODO: Add this filter in UX components
6+
const units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
7+
const unitsKibi = ['B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB'];
8+
9+
function translateUnit(unit) {
10+
const key = `unit_size_${unit}`;
11+
const translatedUnit = $translate.instant(key);
12+
// if translation is not found use the default unit
13+
return key === translatedUnit ? unit : translatedUnit;
14+
}
15+
16+
const translatedUnits = map(units, translateUnit);
17+
const translatedUnitsKibi = map(unitsKibi, translateUnit);
18+
19+
return function bytesFilter(bytesParams, precisionParam, toKibi, fromUnit, toRawBytes) {
20+
let bytes = bytesParams;
21+
let precision = precisionParam;
22+
23+
if (fromUnit) {
24+
const fromKibiUnitIndex = indexOf(unitsKibi, fromUnit);
25+
const fromUnitIndex = indexOf(units, fromUnit);
26+
27+
if (fromKibiUnitIndex !== -1) {
28+
if (fromKibiUnitIndex > 0) {
29+
bytes *= Math.pow(1024, fromKibiUnitIndex); // eslint-disable-line
30+
}
31+
} else if (fromUnitIndex !== -1) {
32+
if (fromUnitIndex > 0) {
33+
bytes *= Math.pow(1000, fromUnitIndex); // eslint-disable-line
34+
}
35+
} else {
36+
return '?';
37+
}
38+
}
39+
40+
if (toRawBytes === true) {
41+
return bytes;
42+
}
43+
44+
if (bytes === 0) {
45+
return '0';
46+
}
47+
if (Number.isNaN(parseFloat(bytes)) || !Number.isFinite(bytes)) {
48+
return '?';
49+
}
50+
if (typeof precision === 'undefined') {
51+
precision = 0;
52+
}
53+
54+
const divider = toKibi ? 1024 : 1000;
55+
const number = Math.floor(Math.log(bytes) / Math.log(divider));
56+
57+
let value = (bytes / Math.pow(divider, Math.floor(number))).toFixed(precision); // eslint-disable-line
58+
59+
if (/\.0+$/.test(value)) {
60+
value = value.replace(/\.0+$/, '');
61+
}
62+
63+
return `${value} ${toKibi ? translatedUnitsKibi[number] : translatedUnits[number]}`;
64+
};
65+
};
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import angular from 'angular';
2+
import '@ovh-ux/ng-translate-async-loader';
3+
import 'angular-translate';
4+
5+
import filter from './filter';
6+
7+
const moduleName = 'cucBytes';
8+
9+
angular
10+
.module(moduleName, [])
11+
.filter('cucBytes', filter)
12+
.run(/* @ngTranslationsInject:json ./translations */);
13+
14+
export default moduleName;
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"unit_size_B": "o",
3+
"unit_size_KB": "ko",
4+
"unit_size_MB": "Mo",
5+
"unit_size_GB": "Go",
6+
"unit_size_TB": "To",
7+
"unit_size_PB": "Po",
8+
"unit_size_EB": "Eo",
9+
"unit_size_ZB": "Zo",
10+
"unit_size_YB": "Yo",
11+
"unit_size_KiB": "Kio",
12+
"unit_size_MiB": "Mio",
13+
"unit_size_GiB": "Gio",
14+
"unit_size_TiB": "Tio",
15+
"unit_size_PiB": "Pio",
16+
"unit_size_EiB": "Eio",
17+
"unit_size_ZiB": "Zio",
18+
"unit_size_YiB": "Yio"
19+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
export const FLAVORTYPE_CATEGORY = [
2+
{
3+
id: 'balanced',
4+
types: [
5+
'ovh.ceph.eg',
6+
'ovh.ssd.eg',
7+
],
8+
migrationNotAllowed: [
9+
'vps',
10+
],
11+
order: 1,
12+
},
13+
{
14+
id: 'cpu',
15+
types: [
16+
'ovh.cpu',
17+
'ovh.ssd.cpu',
18+
'ovh.ceph.hg',
19+
],
20+
migrationNotAllowed: [
21+
'vps',
22+
],
23+
order: 2,
24+
},
25+
{
26+
id: 'ram',
27+
types: [
28+
'ovh.ram',
29+
'ovh.ssd.ram',
30+
],
31+
migrationNotAllowed: [
32+
'vps',
33+
],
34+
order: 3,
35+
},
36+
{
37+
id: 'accelerated',
38+
types: [
39+
'ovh.ssd.gpu',
40+
'ovh.ssd.gpu2',
41+
'ovh.ssd.gpu3',
42+
'ovh.ssd.fpga2',
43+
'ovh.raid-nvme.t1',
44+
],
45+
migrationNotAllowed: [
46+
'vps',
47+
],
48+
order: 4,
49+
},
50+
{
51+
id: 'vps',
52+
types: [
53+
'ovh.vps-ssd',
54+
],
55+
migrationNotAllowed: [],
56+
order: 5,
57+
},
58+
];
59+
60+
export const INSTANCE_CPU_FREQUENCY = {
61+
'ovh.vps-ssd': 2.4,
62+
'ovh.cpu': 3.1,
63+
'ovh.ram': 2.4,
64+
'ovh.ceph.eg': 2.3,
65+
'ovh.ssd.ram': 2.4,
66+
'ovh.ssd.cpu': 3.1,
67+
'ovh.ssd.eg': 2.3,
68+
'ovh.ssd.gpu': 3.1,
69+
'ovh.ssd.gpu2': 3.1,
70+
'ovh.ssd.gpu3': 3.1,
71+
'ovh.raid-nvme.t1': 2.1,
72+
};
73+
74+
export const INSTANCE_NUMBER_OF_GPUS = {
75+
default: 1,
76+
120: 3,
77+
45: 1,
78+
90: 2,
79+
};
80+
81+
export default {
82+
FLAVORTYPE_CATEGORY,
83+
INSTANCE_CPU_FREQUENCY,
84+
INSTANCE_NUMBER_OF_GPUS,
85+
};
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import angular from 'angular';
2+
3+
import cucBytes from '../bytes';
4+
5+
import {
6+
FLAVORTYPE_CATEGORY,
7+
INSTANCE_CPU_FREQUENCY,
8+
INSTANCE_NUMBER_OF_GPUS,
9+
} from './constants';
10+
11+
import service from './service';
12+
13+
const moduleName = 'cucFlavor';
14+
15+
angular
16+
.module(moduleName, [
17+
cucBytes,
18+
])
19+
.constant('CUC_FLAVOR_FLAVORTYPE_CATEGORY', FLAVORTYPE_CATEGORY)
20+
.constant('CUC_FLAVOR_INSTANCE_CPU_FREQUENCY', INSTANCE_CPU_FREQUENCY)
21+
.constant('CUC_FLAVOR_INSTANCE_NUMBER_OF_GPUS', INSTANCE_NUMBER_OF_GPUS)
22+
.service('CucFlavorService', service);
23+
24+
export default moduleName;

0 commit comments

Comments
 (0)