Skip to content

Commit

Permalink
v5.0.2
Browse files Browse the repository at this point in the history
  • Loading branch information
novykh committed May 15, 2024
1 parent 20f7867 commit 96a121a
Show file tree
Hide file tree
Showing 10 changed files with 164 additions and 502 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@netdata/charts",
"version": "5.0.1",
"version": "5.0.2",
"description": "Netdata frontend SDK and chart utilities",
"main": "dist/index.js",
"module": "dist/es6/index.js",
Expand Down
70 changes: 55 additions & 15 deletions src/helpers/units/all.js
Original file line number Diff line number Diff line change
Expand Up @@ -350,25 +350,27 @@ export default {
},
Kibit: {
symbol: "Kibit",
name: "kibibit",
print_symbol: "Kibit",
name: "kilobit",
print_symbol: "kb",
is_metric: true,
is_special: false,
is_additive: true,
is_binary: true,
is_binary: false,
is_bit: true,
base_unit: "bit",
prefix_symbol: "Ki",
prefix_symbol: "k",
},
"Kibit/s": {
symbol: "Kibit/s",
name: "kibibit per second",
print_symbol: "Kibit/s",
name: "kilobit per second",
print_symbol: "kb/s",
is_metric: true,
is_special: false,
is_additive: true,
is_binary: true,
is_binary: false,
is_bit: true,
base_unit: "bit/s",
prefix_symbol: "Ki",
prefix_symbol: "k",
},
MHz: {
symbol: "MHz",
Expand Down Expand Up @@ -405,14 +407,15 @@ export default {
},
"Mibit/s": {
symbol: "Mibit/s",
name: "mebibit per second",
print_symbol: "Mibit/s",
name: "megabit per second",
print_symbol: "mb/s",
is_metric: true,
is_special: false,
is_additive: true,
is_binary: true,
is_binary: false,
is_bit: true,
base_unit: "bit/s",
prefix_symbol: "Mi",
prefix_symbol: "M",
},
V: {
symbol: "V",
Expand Down Expand Up @@ -475,7 +478,8 @@ export default {
is_metric: true,
is_special: false,
is_additive: true,
is_binary: true,
is_binary: false,
is_bit: true,
},
"bit/s": {
symbol: "bit/s",
Expand All @@ -484,7 +488,8 @@ export default {
is_metric: true,
is_special: false,
is_additive: true,
is_binary: true,
is_binary: false,
is_bit: true,
},
"c[CPU]": {
symbol: "c[CPU]",
Expand All @@ -508,6 +513,33 @@ export default {
base_unit: "B[mW]",
prefix_symbol: "d",
},
d: {
symbol: "d",
name: "day",
print_symbol: "d",
is_metric: false,
is_special: false,
is_additive: true,
is_binary: false,
},
mo: {
symbol: "mo",
name: "month",
print_symbol: "Mo",
is_metric: false,
is_special: false,
is_additive: true,
is_binary: false,
},
a: {
symbol: "a",
name: "year",
print_symbol: "yr",
is_metric: false,
is_special: false,
is_additive: true,
is_binary: false,
},
h: {
symbol: "h",
name: "hour",
Expand All @@ -517,6 +549,15 @@ export default {
is_additive: true,
is_binary: false,
},
wk: {
symbol: "wk",
name: "week",
print_symbol: "wk",
is_metric: false,
is_special: false,
is_additive: true,
is_binary: false,
},
"m[CPU]": {
symbol: "m[CPU]",
name: "milliCPU",
Expand Down Expand Up @@ -3668,7 +3709,6 @@ export default {
"keys/s": "{key}/s",
"KiB/operation": "KiBy/{operation}",
"KiB/s": "KiBy/s",
"Kibit/s": "Kibit/s",
"kills/s": "{kill}/s",
"kilobits/s": "Kibit/s",
"listeners/s": "{listener}/s",
Expand Down
110 changes: 98 additions & 12 deletions src/helpers/units/conversableUnits.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,40 +45,126 @@ export default {
convert: value => (value * 9) / 5 + 32,
},
},
s: {
nanoseconds: {
check: (chart, max) => chart.getAttribute("secondsAsTime") && max < 0.000001,
convert: twoFixed(1000_000_000),
ns: {
ns: {
check: (chart, max) => chart.getAttribute("secondsAsTime") && max < 1_000,
convert: function (nanoseconds) {
let tms = Math.round(nanoseconds * 10)
nanoseconds = Math.floor(tms / 10)

tms -= nanoseconds * 10

return `${nanoseconds}.${zeropad(tms)}`
},
},
us: {
check: (chart, max) =>
chart.getAttribute("secondsAsTime") && max >= 1_000 && max < 1_000 * 1_000,
convert: function (nanoseconds) {
nanoseconds = Math.round(nanoseconds)

let microseconds = Math.floor(nanoseconds / 1_000)
nanoseconds -= microseconds * 1_000

nanoseconds = Math.round(nanoseconds / 10)

return `${microseconds}.${zeropad(nanoseconds)}`
},
},
ms: {
check: (chart, max) =>
chart.getAttribute("secondsAsTime") && max >= 1_000 * 1_000 && max < 1_000 * 1_000 * 1_000,
convert: function (nanoseconds) {
nanoseconds = Math.round(nanoseconds)

let milliseconds = Math.floor(nanoseconds / 1_000 / 1_000)
nanoseconds -= milliseconds * 1_000 * 1_000

nanoseconds = Math.round(nanoseconds / 1_000 / 10)

return `${milliseconds}.${zeropad(nanoseconds)}`
},
},
s: {
check: (chart, max) => chart.getAttribute("secondsAsTime") && max >= 1_000 * 1_000 * 1_000,
convert: nanoseconds => {
nanoseconds = Math.round(nanoseconds)

let seconds = Math.floor(nanoseconds / 1_000 / 1_000 / 1_000)
nanoseconds -= seconds * 1_000 * 1_000 * 1_000

nanoseconds = Math.round(nanoseconds / 1_000 / 1_000 / 10)

return `${seconds}.${zeropad(nanoseconds)}`
},
},
},
ms: {
us: {
check: (chart, max) => chart.getAttribute("secondsAsTime") && max < 1,
convert: twoFixed(1_000),
},
ms: {
check: (chart, max) => chart.getAttribute("secondsAsTime") && max >= 1 && max < 1_000,
convert: twoFixed(),
},
s: {
check: (chart, max) => chart.getAttribute("secondsAsTime") && max >= 1_000 && max < 60_000,
convert: twoFixed(0.001),
},
min: {
check: (chart, max) =>
chart.getAttribute("secondsAsTime") && max >= 60_000 && max < 3_600_000,
convert: value => seconds2time(value / 1_000, "MINUTES"),
},
h: {
check: (chart, max) =>
chart.getAttribute("secondsAsTime") && max >= 3_600_000 && max < 86_400_000,
convert: value => seconds2time(value / 1_000, "HOURS"),
},
microseconds: {
d: {
check: (chart, max) => chart.getAttribute("secondsAsTime") && max >= 86_400_000,
convert: value => seconds2time(value / 1_000, "DAYS"),
},
mo: {
check: (chart, max) => chart.getAttribute("secondsAsTime") && max >= 86_400_000 * 30,
convert: value => seconds2time(value, "DAYS"),
},
a: {
check: (chart, max) => chart.getAttribute("secondsAsTime") && max >= 86_400_000 * 30 * 12,
convert: value => seconds2time(value, "DAYS"),
},
},
s: {
us: {
check: (chart, max) => chart.getAttribute("secondsAsTime") && max < 0.001,
convert: twoFixed(1000_000),
},
milliseconds: {
ms: {
check: (chart, max) => chart.getAttribute("secondsAsTime") && max >= 0.001 && max < 1,
convert: twoFixed(1000),
},
seconds: {
s: {
check: (chart, max) => chart.getAttribute("secondsAsTime") && max >= 1 && max < 60,
convert: twoFixed(1),
},
"duration (minutes, seconds)": {
min: {
check: (chart, max) => chart.getAttribute("secondsAsTime") && max >= 60 && max < 3_600,
convert: value => seconds2time(value, "MINUTES"),
},
"duration (hours, minutes)": {
h: {
check: (chart, max) => chart.getAttribute("secondsAsTime") && max >= 3_600 && max < 86_400,
convert: value => seconds2time(value, "HOURS"),
},
"duration (days, hours)": {
d: {
check: (chart, max) => chart.getAttribute("secondsAsTime") && max >= 86_400,
convert: value => seconds2time(value, "DAYS"),
},
"duration (months, days)": {
mo: {
check: (chart, max) => chart.getAttribute("secondsAsTime") && max >= 86_400 * 30,
convert: value => seconds2time(value, "DAYS"),
},
"duration (years, months)": {
a: {
check: (chart, max) => chart.getAttribute("secondsAsTime") && max >= 86_400 * 30 * 12,
convert: value => seconds2time(value, "DAYS"),
},
Expand Down
11 changes: 7 additions & 4 deletions src/helpers/units/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export const getUnitConfig = u =>
is_additive: true,
is_metric: true,
is_binary: false,
is_bit: false,
}

const findCurly = u => {
Expand All @@ -29,26 +30,28 @@ export const isAdditive = u =>
typeof u === "string" ? getUnitConfig(u).is_additive : u.is_additive
export const isMetric = u => (typeof u === "string" ? getUnitConfig(u).is_metric : u.is_metric)
export const isBinary = u => (typeof u === "string" ? getUnitConfig(u).is_binary : u.is_binary)
export const isBit = u => (typeof u === "string" ? getUnitConfig(u).is_bit : u.is_bit)

export const getScales = u => {
if (!isAdditive(u)) return [[], {}]
if (isBinary(u)) return [[...keys.binary], scalableUnits.binary]
if (!isMetric(u)) return [[...keys.decimal], scalableUnits.decimal]
if (isBit(u)) return [[...keys.bit], scalableUnits.num]

return [[...keys.num], scalableUnits.num]
}

const labelify = (base, config, long) => {
if (!config || !base) return base
if (long) return config.name || base
return config.print_symbol || base
if (long) return typeof config.name === "undefined" ? base : config.name
return typeof config.print_symbol === "undefined" ? base : config.print_symbol
}

export const getUnitsString = (u, prefix = "", base = "", long) => {
if (!isAdditive(u)) return base

if (isMetric(u) || isBinary(u))
return `${labelify(prefix, allUnits.prefixes[prefix], long)}${isBinary(u) ? "" : " "}${labelify(base, u, long)}`.trim()
if (isMetric(u) || isBinary(u) || isBit(u))
return `${labelify(prefix, allUnits.prefixes[prefix], long)}${isBinary(u) || isBit(u) ? "" : " "}${labelify(base, u, long)}`.trim()

return `${labelify(prefix, allUnits.decimal_prefixes[prefix], long)} ${labelify(base, u, long)}`.trim()
}
Expand Down
1 change: 1 addition & 0 deletions src/helpers/units/scalableUnits.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export const keys = {
binary: ["Ki", "Mi", "Gi", "Ti"],
decimal: ["K", "M", "B", "T"],
bit: ["k", "M", "G", "T", "P", "E", "Z", "Y"],
num: [
"y",
"z",
Expand Down
4 changes: 2 additions & 2 deletions src/sdk/plugins/unitConversion/getConversionUnits.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const scalable = (units, delta, desiredUnits) => {
if (desiredScales) {
return [
"adjust",
value => (value / (scaleByKey[prefix] || 1)) * (scaleByKey[desiredPrefix] || 1),
value => (value * (scaleByKey[prefix] || 1)) / (scaleByKey[desiredPrefix] || 1),
desiredPrefix,
desiredBase,
]
Expand All @@ -27,7 +27,7 @@ const scalable = (units, delta, desiredUnits) => {
return scale
? [
"adjust",
value => (value / (scaleByKey[prefix] || 1)) * (scaleByKey[scale] || 1),
value => (value * (scaleByKey[prefix] || 1)) / (scaleByKey[scale] || 1),
scale,
base,
]
Expand Down
2 changes: 0 additions & 2 deletions src/sdk/plugins/unitConversion/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ const baseConvert = (chart, unitsKey = "units", min, max) => {

const unitsStsByContext = chart.getAttribute(`${unitsKey}StsByContext`)

console.log(unitsStsByContext)

chart.updateAttribute(
`${unitsKey}ByContext`,
Object.keys(unitsStsByContext).reduce((h, ctx) => {
Expand Down
Loading

0 comments on commit 96a121a

Please sign in to comment.