@parca/utilities
Advanced tools
Comparing version 0.0.7 to 0.0.8
@@ -6,2 +6,6 @@ # Change Log | ||
## 0.0.8 (2023-06-15) | ||
**Note:** Version bump only for package @parca/utilities | ||
## 0.0.7 (2023-05-03) | ||
@@ -8,0 +12,0 @@ |
@@ -17,8 +17,8 @@ // Copyright 2022 The Parca Authors | ||
} | ||
var startIndex = 0; | ||
var endIndex = sortedArray.length - 1; | ||
let startIndex = 0; | ||
let endIndex = sortedArray.length - 1; | ||
while (startIndex <= endIndex) { | ||
if (endIndex - startIndex === 1) { | ||
var distanceToStart = seekElement - sortedArray[startIndex]; | ||
var distanceToEnd = sortedArray[endIndex] - seekElement; | ||
const distanceToStart = seekElement - sortedArray[startIndex]; | ||
const distanceToEnd = sortedArray[endIndex] - seekElement; | ||
if (distanceToStart < distanceToEnd) { | ||
@@ -31,4 +31,4 @@ return startIndex; | ||
} | ||
var mid = startIndex + Math.floor((endIndex - startIndex) / 2); | ||
var guess = sortedArray[mid]; | ||
const mid = startIndex + Math.floor((endIndex - startIndex) / 2); | ||
const guess = sortedArray[mid]; | ||
if (guess > seekElement) { | ||
@@ -35,0 +35,0 @@ endIndex = mid; |
@@ -5,2 +5,3 @@ import { Label } from '@parca/client'; | ||
export * from './binary-search'; | ||
export * from './bigint'; | ||
export type NavigateFunction = (path: string, queryParams: any, options?: { | ||
@@ -11,3 +12,3 @@ replace?: boolean; | ||
export declare const capitalize: (a: string) => string; | ||
export declare const valueFormatter: (num: number, unit: string, digits: number) => string; | ||
export declare const valueFormatter: (num: bigint | number, unit: string, digits: number) => string; | ||
export declare const isDevMode: () => boolean; | ||
@@ -32,3 +33,3 @@ export declare const getLastItem: (thePath: string | undefined) => string | undefined; | ||
export declare const getReducedSpanColor: (transparency: number, isDarkMode: boolean) => string; | ||
export declare const diffColor: (diff: number, cumulative: number, isDarkMode: boolean) => string; | ||
export declare const diffColor: (diff: bigint, cumulative: bigint, isDarkMode: boolean) => string; | ||
export declare const isSearchMatch: (currentSearchString: string | undefined, name: string) => boolean; | ||
@@ -35,0 +36,0 @@ export declare const saveAsBlob: (blob: Blob, filename: string) => void; |
@@ -13,14 +13,4 @@ // Copyright 2022 The Parca Authors | ||
// limitations under the License. | ||
var __assign = (this && this.__assign) || function () { | ||
__assign = Object.assign || function(t) { | ||
for (var s, i = 1, n = arguments.length; i < n; i++) { | ||
s = arguments[i]; | ||
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) | ||
t[p] = s[p]; | ||
} | ||
return t; | ||
}; | ||
return __assign.apply(this, arguments); | ||
}; | ||
import colors from 'tailwindcss/colors'; | ||
import { abs, divide } from './bigint'; | ||
import { unitsInTime } from './time'; | ||
@@ -30,10 +20,9 @@ export * from './time'; | ||
export * from './binary-search'; | ||
export var SEARCH_STRING_COLOR = '#e39c9c'; | ||
export var capitalize = function (a) { | ||
return a | ||
.split(' ') | ||
.map(function (p) { return p[0].toUpperCase() + p.substring(1).toLocaleLowerCase(); }) | ||
.join(' '); | ||
}; | ||
var unitsInBytes = { | ||
export * from './bigint'; | ||
export const SEARCH_STRING_COLOR = '#e39c9c'; | ||
export const capitalize = (a) => a | ||
.split(' ') | ||
.map(p => p[0].toUpperCase() + p.substring(1).toLocaleLowerCase()) | ||
.join(' '); | ||
const unitsInBytes = { | ||
bytes: { multiplier: 1, symbol: 'Bytes' }, | ||
@@ -47,3 +36,3 @@ kilobytes: { multiplier: 1e3, symbol: 'kB' }, | ||
}; | ||
var unitsInCount = { | ||
const unitsInCount = { | ||
unit: { multiplier: 1, symbol: '' }, | ||
@@ -57,3 +46,3 @@ kilo: { multiplier: 1e3, symbol: 'k' }, | ||
}; | ||
var knownValueFormatters = { | ||
const knownValueFormatters = { | ||
bytes: unitsInBytes, | ||
@@ -63,16 +52,11 @@ nanoseconds: unitsInTime, | ||
}; | ||
export var valueFormatter = function (num, unit, digits) { | ||
// TODO: remove this after the columnstore backend is the main storage | ||
// backend. This is a temporary fix while the columnstore backend does not | ||
// return the correct unit. | ||
if (unit === undefined || unit === '') { | ||
return num.toFixed(digits); | ||
} | ||
var absoluteNum = Math.abs(num); | ||
var format = Object.values(knownValueFormatters[unit]); | ||
export const valueFormatter = (num, unit, digits) => { | ||
const isBigInt = typeof num === 'bigint'; | ||
const absoluteNum = isBigInt ? abs(num) : Math.abs(num); | ||
const format = Object.values(knownValueFormatters[unit]); | ||
if (format === undefined || format === null) { | ||
return num.toString(); | ||
} | ||
var rx = /\.0+$|(\.[0-9]*[1-9])0+$/; | ||
var i; | ||
const rx = /\.0+$|(\.[0-9]*[1-9])0+$/; | ||
let i; | ||
for (i = format.length - 1; i > 0; i--) { | ||
@@ -83,11 +67,14 @@ if (absoluteNum >= format[i].multiplier) { | ||
} | ||
return "".concat((num / format[i].multiplier).toFixed(digits).replace(rx, '$1')).concat(format[i].symbol); | ||
const multiplier = format[i].multiplier; | ||
return `${(isBigInt ? divide(num, BigInt(multiplier)) : num / multiplier) | ||
.toFixed(digits) | ||
.replace(rx, '$1')}${format[i].symbol}`; | ||
}; | ||
export var isDevMode = function () { | ||
export const isDevMode = () => { | ||
return process.env.NODE_ENV === 'development'; | ||
}; | ||
export var getLastItem = function (thePath) { | ||
export const getLastItem = (thePath) => { | ||
if (thePath === undefined || thePath === '') | ||
return; | ||
var index = thePath.lastIndexOf('/'); | ||
const index = thePath.lastIndexOf('/'); | ||
if (index === -1) | ||
@@ -97,10 +84,8 @@ return thePath; | ||
}; | ||
var transformToArray = function (params) { return params.split(','); }; | ||
export var parseParams = function (querystring) { | ||
var _a; | ||
var params = new URLSearchParams(querystring); | ||
var obj = {}; | ||
for (var _i = 0, _b = Array.from(params.keys()); _i < _b.length; _i++) { | ||
var key = _b[_i]; | ||
var values = params.getAll(key); | ||
const transformToArray = (params) => params.split(','); | ||
export const parseParams = (querystring) => { | ||
const params = new URLSearchParams(querystring); | ||
const obj = {}; | ||
for (const key of Array.from(params.keys())) { | ||
const values = params.getAll(key); | ||
if (values.length > 1) { | ||
@@ -110,3 +95,3 @@ obj[key] = values; | ||
else { | ||
if ((_a = values[0]) === null || _a === void 0 ? void 0 : _a.includes(',')) { | ||
if (values[0]?.includes(',')) { | ||
obj[key] = transformToArray(values[0]); | ||
@@ -121,12 +106,11 @@ } | ||
}; | ||
export var selectQueryParam = function (key) { | ||
var _a; | ||
export const selectQueryParam = (key) => { | ||
if (typeof window === 'undefined') { | ||
return; | ||
} | ||
var router = parseParams(window.location.search); | ||
const router = parseParams(window.location.search); | ||
if (key === 'dashboard_items') { | ||
var dashboardItems = router[key]; | ||
let dashboardItems = router[key]; | ||
if (typeof dashboardItems === 'string') { | ||
dashboardItems = (_a = [dashboardItems]) !== null && _a !== void 0 ? _a : []; | ||
dashboardItems = [dashboardItems] ?? []; | ||
} | ||
@@ -140,7 +124,5 @@ return dashboardItems; | ||
}; | ||
export var convertToQueryParams = function (params) { | ||
return Object.keys(params) | ||
.map(function (key) { return "".concat(key, "=").concat(params[key]); }) | ||
.join('&'); | ||
}; | ||
export const convertToQueryParams = (params) => Object.keys(params) | ||
.map((key) => `${key}=${params[key]}`) | ||
.join('&'); | ||
export function convertUTCToLocalDate(date) { | ||
@@ -158,3 +140,3 @@ if (date === null) { | ||
} | ||
export var COLOR_PROFILES = { | ||
export const COLOR_PROFILES = { | ||
default: { colors: [['#929FEB', '#B3BAE1']] }, | ||
@@ -218,26 +200,26 @@ subtle: { | ||
}; | ||
export var getNewSpanColor = function (isDarkMode) { | ||
export const getNewSpanColor = (isDarkMode) => { | ||
return isDarkMode ? '#B3BAE1' : '#929FEB'; | ||
}; | ||
export var getIncreasedSpanColor = function (transparency, isDarkMode) { | ||
export const getIncreasedSpanColor = (transparency, isDarkMode) => { | ||
return isDarkMode | ||
? "rgba(255, 177, 204, ".concat(transparency, ")") | ||
: "rgba(254, 153, 187, ".concat(transparency, ")"); | ||
? `rgba(255, 177, 204, ${transparency})` | ||
: `rgba(254, 153, 187, ${transparency})`; | ||
}; | ||
export var getReducedSpanColor = function (transparency, isDarkMode) { | ||
export const getReducedSpanColor = (transparency, isDarkMode) => { | ||
return isDarkMode | ||
? "rgba(103, 158, 92, ".concat(transparency, ")") | ||
: "rgba(164, 214, 153, ".concat(transparency, ")"); | ||
? `rgba(103, 158, 92, ${transparency})` | ||
: `rgba(164, 214, 153, ${transparency})`; | ||
}; | ||
export var diffColor = function (diff, cumulative, isDarkMode) { | ||
var prevValue = cumulative - diff; | ||
var diffRatio = prevValue > 0 ? (Math.abs(diff) > 0 ? diff / prevValue : 0) : 1.0; | ||
var diffTransparency = Math.abs(diff) > 0 ? Math.min((Math.abs(diffRatio) / 2 + 0.5) * 0.8, 0.8) : 0; | ||
var newSpanColor = getNewSpanColor(isDarkMode); | ||
var increasedSpanColor = getIncreasedSpanColor(diffTransparency, isDarkMode); | ||
var reducedSpanColor = getReducedSpanColor(diffTransparency, isDarkMode); | ||
var color = diff === 0 ? newSpanColor : diff > 0 ? increasedSpanColor : reducedSpanColor; | ||
export const diffColor = (diff, cumulative, isDarkMode) => { | ||
const prevValue = cumulative - diff; | ||
const diffRatio = prevValue > 0 ? (diff !== 0n ? divide(diff, prevValue) : 0) : 1.0; | ||
const diffTransparency = diff !== 0n ? Math.min((Math.abs(diffRatio) / 2 + 0.5) * 0.8, 0.8) : 0; | ||
const newSpanColor = getNewSpanColor(isDarkMode); | ||
const increasedSpanColor = getIncreasedSpanColor(diffTransparency, isDarkMode); | ||
const reducedSpanColor = getReducedSpanColor(diffTransparency, isDarkMode); | ||
const color = diff === 0n ? newSpanColor : diff > 0n ? increasedSpanColor : reducedSpanColor; | ||
return color; | ||
}; | ||
export var isSearchMatch = function (currentSearchString, name) { | ||
export const isSearchMatch = (currentSearchString, name) => { | ||
if (currentSearchString === undefined || currentSearchString === '') | ||
@@ -247,4 +229,4 @@ return false; | ||
}; | ||
export var saveAsBlob = function (blob, filename) { | ||
var link = document.createElement('a'); | ||
export const saveAsBlob = (blob, filename) => { | ||
const link = document.createElement('a'); | ||
link.href = window.URL.createObjectURL(blob); | ||
@@ -254,11 +236,10 @@ link.download = filename; | ||
}; | ||
export var sanitizeLabelValue = function (labels) { | ||
return labels.map(function (value) { | ||
return value.includes('\\') ? value.replaceAll('\\', '\\\\') : value; | ||
}); | ||
export const sanitizeLabelValue = (labels) => { | ||
return labels.map((value) => value.includes('\\') ? value.replaceAll('\\', '\\\\') : value); | ||
}; | ||
export var sanitizeHighlightedValues = function (labels) { | ||
return labels.map(function (v) { | ||
return __assign(__assign({}, v), { value: v.value.includes('\\') ? v.value.replaceAll('\\', '\\\\') : v.value }); | ||
}); | ||
}; | ||
export const sanitizeHighlightedValues = (labels) => labels.map(v => { | ||
return { | ||
...v, | ||
value: v.value.includes('\\') ? v.value.replaceAll('\\', '\\\\') : v.value, | ||
}; | ||
}); |
@@ -14,23 +14,23 @@ // Copyright 2022 The Parca Authors | ||
import { TimeUnits, capitalize, convertTime, formatDate, formatDuration, valueFormatter, } from './index'; | ||
describe('capitalize', function () { | ||
it('capitalizes each word in a string', function () { | ||
describe('capitalize', () => { | ||
it('capitalizes each word in a string', () => { | ||
expect(capitalize('john doe')).toBe('John Doe'); | ||
}); | ||
}); | ||
describe('valueFormatter', function () { | ||
it('formats the value passed in count', function () { | ||
expect(valueFormatter(987654321, 'count', 1)).toBe('987.7M'); | ||
describe('valueFormatter', () => { | ||
it('formats the value passed in count', () => { | ||
expect(valueFormatter(987654321n, 'count', 1)).toBe('987.7M'); | ||
}); | ||
it('formats the value passed in time', function () { | ||
expect(valueFormatter(9654321, 'nanoseconds', 1)).toBe('9.7ms'); | ||
it('formats the value passed in time', () => { | ||
expect(valueFormatter(9654321n, 'nanoseconds', 1)).toBe('9.7ms'); | ||
}); | ||
it('formats the value passed in bytes', function () { | ||
expect(valueFormatter(9654321, 'bytes', 1)).toBe('9.7MB'); | ||
it('formats the value passed in bytes', () => { | ||
expect(valueFormatter(9654321n, 'bytes', 1)).toBe('9.7MB'); | ||
}); | ||
it('formats a negative value passed correctly', function () { | ||
expect(valueFormatter(-987654321, 'bytes', 1)).toBe('-987.7MB'); | ||
it('formats a negative value passed correctly', () => { | ||
expect(valueFormatter(-987654321n, 'bytes', 1)).toBe('-987.7MB'); | ||
}); | ||
}); | ||
describe('convertTime', function () { | ||
it('converts larger units to smaller units', function () { | ||
describe('convertTime', () => { | ||
it('converts larger units to smaller units', () => { | ||
expect(convertTime(9, TimeUnits.Seconds, TimeUnits.Nanos)).toBe(9 * 1e9); | ||
@@ -41,3 +41,3 @@ expect(convertTime(9, TimeUnits.Micros, TimeUnits.Nanos)).toBe(9 * 1e3); | ||
}); | ||
it('converts smaller units to larger units', function () { | ||
it('converts smaller units to larger units', () => { | ||
expect(convertTime(9000000000, TimeUnits.Nanos, TimeUnits.Seconds)).toBe(9); | ||
@@ -48,30 +48,30 @@ expect(convertTime(9000000000, TimeUnits.Nanos, TimeUnits.Milliseconds)).toBe(9000); | ||
}); | ||
describe('formatDuration', function () { | ||
it('formats a duration over one second correctly', function () { | ||
describe('formatDuration', () => { | ||
it('formats a duration over one second correctly', () => { | ||
expect(formatDuration({ nanos: 9654321000 })).toBe('9s'); | ||
}); | ||
it('formats a relative duration over one second correctly', function () { | ||
var seconds = 1644414491; | ||
var nanos = 630134000; | ||
var currentNanos = 1644415325511000000; | ||
var durationInNanos = seconds * 1e9 + nanos; | ||
expect(formatDuration({ seconds: seconds, nanos: nanos }, currentNanos)).toBe(formatDuration({ nanos: currentNanos - durationInNanos })); | ||
expect(formatDuration({ seconds: seconds, nanos: nanos }, currentNanos)).toBe('13m 53s'); | ||
it('formats a relative duration over one second correctly', () => { | ||
const seconds = 1644414491; | ||
const nanos = 630134000; | ||
const currentNanos = 1644415325511000000; | ||
const durationInNanos = seconds * 1e9 + nanos; | ||
expect(formatDuration({ seconds, nanos }, currentNanos)).toBe(formatDuration({ nanos: currentNanos - durationInNanos })); | ||
expect(formatDuration({ seconds, nanos }, currentNanos)).toBe('13m 53s'); | ||
}); | ||
it('formats a duration below one second correctly', function () { | ||
it('formats a duration below one second correctly', () => { | ||
expect(formatDuration({ nanos: 9654321 })).toBe('9ms'); | ||
}); | ||
it('formats a relative duration below one second correctly', function () { | ||
var seconds = 1644415325; | ||
var nanos = 511000000; | ||
var currentNanos = 1644415325530134000; | ||
var durationInNanos = seconds * 1e9 + nanos; | ||
expect(formatDuration({ seconds: seconds, nanos: nanos }, currentNanos)).toBe(formatDuration({ nanos: currentNanos - durationInNanos })); | ||
expect(formatDuration({ seconds: seconds, nanos: nanos }, currentNanos)).toBe('19ms'); | ||
it('formats a relative duration below one second correctly', () => { | ||
const seconds = 1644415325; | ||
const nanos = 511000000; | ||
const currentNanos = 1644415325530134000; | ||
const durationInNanos = seconds * 1e9 + nanos; | ||
expect(formatDuration({ seconds, nanos }, currentNanos)).toBe(formatDuration({ nanos: currentNanos - durationInNanos })); | ||
expect(formatDuration({ seconds, nanos }, currentNanos)).toBe('19ms'); | ||
}); | ||
}); | ||
describe('formatDate', function () { | ||
it('formats date to given time format', function () { | ||
describe('formatDate', () => { | ||
it('formats date to given time format', () => { | ||
expect(formatDate(new Date('2019-01-01T00:00:00Z'), "'Date:' dd/MM/yyyy 'Time:' hh:mm:s'")).toBe('Date: 01/01/2019 Time: 12:00:0'); | ||
}); | ||
}); |
@@ -13,10 +13,8 @@ // Copyright 2022 The Parca Authors | ||
// limitations under the License. | ||
export var startsWith = function (str, prefix) { | ||
return str.lastIndexOf(prefix, 0) === 0; | ||
}; | ||
export var cutToMaxStringLength = function (input, len) { | ||
export const startsWith = (str, prefix) => str.lastIndexOf(prefix, 0) === 0; | ||
export const cutToMaxStringLength = (input, len) => { | ||
if (input.length <= len) { | ||
return input; | ||
} | ||
return "".concat(input.substring(0, len), "..."); | ||
return `${input.substring(0, len)}...`; | ||
}; |
@@ -13,15 +13,5 @@ // Copyright 2022 The Parca Authors | ||
// limitations under the License. | ||
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) { | ||
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) { | ||
if (ar || !(i in from)) { | ||
if (!ar) ar = Array.prototype.slice.call(from, 0, i); | ||
ar[i] = from[i]; | ||
} | ||
} | ||
return to.concat(ar || Array.prototype.slice.call(from)); | ||
}; | ||
var _a; | ||
import format from 'date-fns/format'; | ||
import intervalToDuration from 'date-fns/intervalToDuration'; | ||
export var TimeUnits = { | ||
export const TimeUnits = { | ||
Nanos: 'nanos', | ||
@@ -35,14 +25,14 @@ Micros: 'micros', | ||
}; | ||
export var unitsInTime = (_a = {}, | ||
_a[TimeUnits.Nanos] = { multiplier: 1, symbol: 'ns' }, | ||
_a[TimeUnits.Micros] = { multiplier: 1e3, symbol: 'µs' }, | ||
_a[TimeUnits.Milliseconds] = { multiplier: 1e6, symbol: 'ms' }, | ||
_a[TimeUnits.Seconds] = { multiplier: 1e9, symbol: 's' }, | ||
_a[TimeUnits.Minutes] = { multiplier: 6 * 1e10, symbol: 'm' }, | ||
_a[TimeUnits.Hours] = { multiplier: 60 * 60 * 1e9, symbol: 'h' }, | ||
_a[TimeUnits.Days] = { multiplier: 60 * 60 * 24 * 1e9, symbol: 'd' }, | ||
_a); | ||
export var convertTime = function (value, from, to) { | ||
var startUnit = unitsInTime[from]; | ||
var endUnit = unitsInTime[to]; | ||
export const unitsInTime = { | ||
[TimeUnits.Nanos]: { multiplier: 1, symbol: 'ns' }, | ||
[TimeUnits.Micros]: { multiplier: 1e3, symbol: 'µs' }, | ||
[TimeUnits.Milliseconds]: { multiplier: 1e6, symbol: 'ms' }, | ||
[TimeUnits.Seconds]: { multiplier: 1e9, symbol: 's' }, | ||
[TimeUnits.Minutes]: { multiplier: 6 * 1e10, symbol: 'm' }, | ||
[TimeUnits.Hours]: { multiplier: 60 * 60 * 1e9, symbol: 'h' }, | ||
[TimeUnits.Days]: { multiplier: 60 * 60 * 24 * 1e9, symbol: 'd' }, | ||
}; | ||
export const convertTime = (value, from, to) => { | ||
const startUnit = unitsInTime[from]; | ||
const endUnit = unitsInTime[to]; | ||
if (startUnit === undefined || endUnit === undefined) { | ||
@@ -54,11 +44,11 @@ console.error('invalid start or end unit provided'); | ||
}; | ||
export var formatDuration = function (timeObject, to) { | ||
var values = []; | ||
var unitsLargeToSmall = Object.values(TimeUnits).reverse(); | ||
var nanos = Object.keys(timeObject) | ||
.map(function (unit) { | ||
var time = timeObject[unit]; | ||
export const formatDuration = (timeObject, to) => { | ||
let values = []; | ||
const unitsLargeToSmall = Object.values(TimeUnits).reverse(); | ||
let nanos = Object.keys(timeObject) | ||
.map(unit => { | ||
const time = timeObject[unit]; | ||
return time !== undefined ? convertTime(time, unit, TimeUnits.Nanos) : 0; | ||
}) | ||
.reduce(function (prev, curr) { return prev + curr; }, 0); | ||
.reduce((prev, curr) => prev + curr, 0); | ||
if (to !== undefined) { | ||
@@ -69,4 +59,4 @@ nanos = to - nanos; | ||
if (Math.floor(nanos / unitsInTime[TimeUnits.Seconds].multiplier) > 0) { | ||
for (var i = 0; i < unitsLargeToSmall.length; i++) { | ||
var multiplier = unitsInTime[unitsLargeToSmall[i]].multiplier; | ||
for (let i = 0; i < unitsLargeToSmall.length; i++) { | ||
const multiplier = unitsInTime[unitsLargeToSmall[i]].multiplier; | ||
if (nanos > multiplier) { | ||
@@ -77,4 +67,4 @@ if (unitsLargeToSmall[i] === TimeUnits.Milliseconds) { | ||
else { | ||
var amount = Math.floor(nanos / multiplier); | ||
values = __spreadArray(__spreadArray([], values, true), ["".concat(amount).concat(unitsInTime[unitsLargeToSmall[i]].symbol)], false); | ||
const amount = Math.floor(nanos / multiplier); | ||
values = [...values, `${amount}${unitsInTime[unitsLargeToSmall[i]].symbol}`]; | ||
nanos -= amount * multiplier; | ||
@@ -86,5 +76,5 @@ } | ||
else { | ||
var milliseconds = Math.floor(nanos / unitsInTime[TimeUnits.Milliseconds].multiplier); | ||
const milliseconds = Math.floor(nanos / unitsInTime[TimeUnits.Milliseconds].multiplier); | ||
if (milliseconds > 0) { | ||
values = ["".concat(milliseconds).concat(unitsInTime[TimeUnits.Milliseconds].symbol)]; | ||
values = [`${milliseconds}${unitsInTime[TimeUnits.Milliseconds].symbol}`]; | ||
} | ||
@@ -97,11 +87,11 @@ else { | ||
}; | ||
export var formatDate = function (date, timeFormat) { | ||
export const formatDate = (date, timeFormat) => { | ||
if (typeof date === 'number') { | ||
date = new Date(date); | ||
} | ||
var ISOString = date.toISOString().slice(0, -1); | ||
const ISOString = date.toISOString().slice(0, -1); | ||
return format(new Date(ISOString), timeFormat); | ||
}; | ||
export var formatForTimespan = function (from, to) { | ||
var duration = intervalToDuration({ start: from, end: to }); | ||
export const formatForTimespan = (from, to) => { | ||
const duration = intervalToDuration({ start: from, end: to }); | ||
if (duration <= { minutes: 61 }) { | ||
@@ -118,11 +108,11 @@ return 'H:mm'; | ||
}; | ||
export var getStepDuration = function (start, end, stepCount) { | ||
if (stepCount === void 0) { stepCount = 1000; } | ||
var durationSeconds = (end - start) / stepCount / 1000; | ||
var whole = Math.floor(durationSeconds); | ||
var decimal = durationSeconds - whole; | ||
return { seconds: whole.toString(), nanos: Math.floor(decimal * 1e9) }; | ||
export const getStepDuration = (start, end, stepCount = 1000) => { | ||
const durationSeconds = (end - start) / stepCount / 1000; | ||
const whole = Math.floor(durationSeconds); | ||
const decimal = durationSeconds - whole; | ||
return { seconds: BigInt(whole), nanos: Math.floor(decimal * 1e9) }; | ||
}; | ||
export var getStepDurationInMilliseconds = function (stepDuration) { | ||
return parseInt(stepDuration.seconds, 10) * 1000 + stepDuration.nanos / 1e6; | ||
export const getStepDurationInMilliseconds = (stepDuration) => { | ||
// Converts both seconds and nanoseconds to milliseconds and adds them together | ||
return Number(stepDuration.seconds) * 1000 + stepDuration.nanos / 1e6; | ||
}; |
{ | ||
"name": "@parca/utilities", | ||
"version": "0.0.7", | ||
"version": "0.0.8", | ||
"description": "A set of reusable functions for Parca", | ||
"main": "dist/index.js", | ||
"scripts": { | ||
"test": "jest --coverage --config ../../../jest.config.js ./src/*", | ||
"test": "jest --coverage ./src/*", | ||
"prepublish": "yarn build", | ||
@@ -13,3 +13,3 @@ "build": "tsc", | ||
"dependencies": { | ||
"@parca/client": "^0.16.73", | ||
"@parca/client": "^0.16.74", | ||
"@rehooks/local-storage": "^2.4.4", | ||
@@ -25,3 +25,3 @@ "tailwindcss": "3.2.4" | ||
}, | ||
"gitHead": "fc53e202779c1b0d5c46cdfd6bafa7d0396956a4" | ||
"gitHead": "e1dc36ad29a6efe06f45460bb721325df9416a6b" | ||
} |
@@ -31,15 +31,15 @@ // Copyright 2022 The Parca Authors | ||
it('formats the value passed in count', () => { | ||
expect(valueFormatter(987654321, 'count', 1)).toBe('987.7M'); | ||
expect(valueFormatter(987654321n, 'count', 1)).toBe('987.7M'); | ||
}); | ||
it('formats the value passed in time', () => { | ||
expect(valueFormatter(9654321, 'nanoseconds', 1)).toBe('9.7ms'); | ||
expect(valueFormatter(9654321n, 'nanoseconds', 1)).toBe('9.7ms'); | ||
}); | ||
it('formats the value passed in bytes', () => { | ||
expect(valueFormatter(9654321, 'bytes', 1)).toBe('9.7MB'); | ||
expect(valueFormatter(9654321n, 'bytes', 1)).toBe('9.7MB'); | ||
}); | ||
it('formats a negative value passed correctly', () => { | ||
expect(valueFormatter(-987654321, 'bytes', 1)).toBe('-987.7MB'); | ||
expect(valueFormatter(-987654321n, 'bytes', 1)).toBe('-987.7MB'); | ||
}); | ||
@@ -46,0 +46,0 @@ }); |
@@ -18,2 +18,3 @@ // Copyright 2022 The Parca Authors | ||
import {abs, divide} from './bigint'; | ||
import {unitsInTime} from './time'; | ||
@@ -24,2 +25,3 @@ | ||
export * from './binary-search'; | ||
export * from './bigint'; | ||
@@ -71,11 +73,5 @@ export type NavigateFunction = ( | ||
export const valueFormatter = (num: number, unit: string, digits: number): string => { | ||
// TODO: remove this after the columnstore backend is the main storage | ||
// backend. This is a temporary fix while the columnstore backend does not | ||
// return the correct unit. | ||
if (unit === undefined || unit === '') { | ||
return num.toFixed(digits); | ||
} | ||
const absoluteNum = Math.abs(num); | ||
export const valueFormatter = (num: bigint | number, unit: string, digits: number): string => { | ||
const isBigInt = typeof num === 'bigint'; | ||
const absoluteNum = isBigInt ? abs(num) : Math.abs(num); | ||
const format: Unit[] = Object.values( | ||
@@ -96,3 +92,7 @@ knownValueFormatters[unit as keyof typeof knownValueFormatters] | ||
} | ||
return `${(num / format[i].multiplier).toFixed(digits).replace(rx, '$1')}${format[i].symbol}`; | ||
const multiplier = format[i].multiplier; | ||
return `${(isBigInt ? divide(num, BigInt(multiplier)) : num / multiplier) | ||
.toFixed(digits) | ||
.replace(rx, '$1')}${format[i].symbol}`; | ||
}; | ||
@@ -270,8 +270,7 @@ | ||
export const diffColor = (diff: number, cumulative: number, isDarkMode: boolean): string => { | ||
export const diffColor = (diff: bigint, cumulative: bigint, isDarkMode: boolean): string => { | ||
const prevValue = cumulative - diff; | ||
const diffRatio = prevValue > 0 ? (Math.abs(diff) > 0 ? diff / prevValue : 0) : 1.0; | ||
const diffRatio = prevValue > 0 ? (diff !== 0n ? divide(diff, prevValue) : 0) : 1.0; | ||
const diffTransparency = | ||
Math.abs(diff) > 0 ? Math.min((Math.abs(diffRatio) / 2 + 0.5) * 0.8, 0.8) : 0; | ||
const diffTransparency = diff !== 0n ? Math.min((Math.abs(diffRatio) / 2 + 0.5) * 0.8, 0.8) : 0; | ||
@@ -283,3 +282,3 @@ const newSpanColor = getNewSpanColor(isDarkMode); | ||
const color: string = | ||
diff === 0 ? newSpanColor : diff > 0 ? increasedSpanColor : reducedSpanColor; | ||
diff === 0n ? newSpanColor : diff > 0n ? increasedSpanColor : reducedSpanColor; | ||
@@ -286,0 +285,0 @@ return color; |
@@ -134,7 +134,8 @@ // Copyright 2022 The Parca Authors | ||
return {seconds: whole.toString(), nanos: Math.floor(decimal * 1e9)}; | ||
return {seconds: BigInt(whole), nanos: Math.floor(decimal * 1e9)}; | ||
}; | ||
export const getStepDurationInMilliseconds = (stepDuration: Duration): number => { | ||
return parseInt(stepDuration.seconds, 10) * 1000 + stepDuration.nanos / 1e6; | ||
// Converts both seconds and nanoseconds to milliseconds and adds them together | ||
return Number(stepDuration.seconds) * 1000 + stepDuration.nanos / 1e6; | ||
}; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
64015
27
1322
Updated@parca/client@^0.16.74