-
Notifications
You must be signed in to change notification settings - Fork 258
Expand file tree
/
Copy pathindex.ts
More file actions
418 lines (359 loc) · 12.6 KB
/
Copy pathindex.ts
File metadata and controls
418 lines (359 loc) · 12.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
// Copyright 2022 The Parca Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import colors from 'tailwindcss/colors';
import {Label} from '@parca/client';
import {abs, divide} from './bigint';
import {unitsInTimeMs, unitsInTimeNs} from './time';
export * from './time';
export * from './string';
export * from './binary-search';
export * from './bigint';
export type NavigateFunction = (
path: string,
queryParams: any,
options?: {replace?: boolean}
) => void;
export const SEARCH_STRING_COLOR = '#e39c9c';
export const capitalize = (a: string): string =>
a
.split(' ')
.map(p => p[0].toUpperCase() + p.substring(1).toLocaleLowerCase())
.join(' ');
export const capitalizeOnlyFirstLetter = (a: string): string => {
return a[0].toUpperCase() + a.substring(1).toLocaleLowerCase();
};
interface Unit {
multiplier: number;
symbol: string;
}
const unitsInBytes = {
bytes: {multiplier: 1, symbol: 'Bytes'},
kilobytes: {multiplier: 1e3, symbol: 'kB'},
megabytes: {multiplier: 1e6, symbol: 'MB'},
gigabytes: {multiplier: 1e9, symbol: 'GB'},
terabytes: {multiplier: 1e12, symbol: 'TB'},
petabytes: {multiplier: 1e15, symbol: 'PB'},
exabytes: {multiplier: 1e18, symbol: 'EB'},
};
const unitsInBytesPerSecond = {
bytes_per_second: {multiplier: 1, symbol: 'Bytes/s'},
kilobytes_per_second: {multiplier: 1e3, symbol: 'kB/s'},
megabytes_per_second: {multiplier: 1e6, symbol: 'MB/s'},
gigabytes_per_second: {multiplier: 1e9, symbol: 'GB/s'},
terabytes_per_second: {multiplier: 1e12, symbol: 'TB/s'},
petabytes_per_second: {multiplier: 1e15, symbol: 'PB/s'},
exabytes_per_second: {multiplier: 1e18, symbol: 'EB/s'},
};
const unitsInCount = {
unit: {multiplier: 1, symbol: ''},
kilo: {multiplier: 1e3, symbol: 'k'},
mega: {multiplier: 1e6, symbol: 'M'},
giga: {multiplier: 1e9, symbol: 'G'},
tera: {multiplier: 1e12, symbol: 'T'},
peta: {multiplier: 1e15, symbol: 'P'},
exa: {multiplier: 1e18, symbol: 'E'},
};
const unitsInCores = {
unit: {multiplier: 1, symbol: 'cores'},
kilo: {multiplier: 1e3, symbol: 'k'},
mega: {multiplier: 1e6, symbol: 'M'},
giga: {multiplier: 1e9, symbol: 'G'},
tera: {multiplier: 1e12, symbol: 'T'},
peta: {multiplier: 1e15, symbol: 'P'},
exa: {multiplier: 1e18, symbol: 'E'},
};
const unitsInWatts = {
unit: {multiplier: 1, symbol: 'W'},
kilo: {multiplier: 1e3, symbol: 'kW'},
mega: {multiplier: 1e6, symbol: 'MW'},
giga: {multiplier: 1e9, symbol: 'GW'},
tera: {multiplier: 1e12, symbol: 'TW'},
peta: {multiplier: 1e15, symbol: 'PW'},
exa: {multiplier: 1e18, symbol: 'EW'},
};
const unitsInCelsius = {
unit: {multiplier: 1, symbol: '°C'},
};
const unitsInHertz = {
unit: {multiplier: 1, symbol: 'Hz'},
kilo: {multiplier: 1e3, symbol: 'kHz'},
mega: {multiplier: 1e6, symbol: 'MHz'},
giga: {multiplier: 1e9, symbol: 'GHz'},
tera: {multiplier: 1e12, symbol: 'THz'},
};
const knownValueFormatters = {
bytes: unitsInBytes,
nanoseconds: unitsInTimeNs,
count: unitsInCount,
'CPU Cores': unitsInCores,
milliseconds: unitsInTimeMs,
watts: unitsInWatts,
bytes_per_second: unitsInBytesPerSecond,
celsius: unitsInCelsius,
hertz: unitsInHertz,
};
export const roundToDecimals = (n: number, decimals: number): number => {
const log10 = Math.floor(Math.log10(n));
const div = log10 < 0 ? Math.pow(10, decimals - log10 - 1) : Math.pow(10, decimals);
return Math.round(n * div) / div;
};
export const getPrecision = (value: number): number =>
String(roundToDecimals(value, 2)).replace('.', '').length - value.toFixed().length;
export const valueFormatter = (
num: bigint | number,
unit: string,
digits: number,
renderTight = false
): string => {
const isBigInt = typeof num === 'bigint';
const absoluteNum = isBigInt ? abs(num) : Math.abs(num);
if (unit === 'percent') {
return `${num.toString()}%`;
}
const formatter = knownValueFormatters[unit as keyof typeof knownValueFormatters];
if (formatter == null) {
return num.toString();
}
const format: Unit[] = Object.values(formatter);
const rx = /\.0+$|(\.[0-9]*[1-9])0+$/;
let i: number;
for (i = format.length - 1; i > 0; i--) {
if (absoluteNum >= format[i].multiplier) {
break;
}
}
const multiplier = format[i].multiplier;
return `${(isBigInt ? divide(num, BigInt(multiplier)) : num / multiplier)
.toFixed(digits)
.replace(rx, '$1')}${renderTight ? '' : ' '}${format[i].symbol}`;
};
export const isDevModeOrPreview = (): boolean => {
return isDevMode() || import.meta.env.REACT_APP_VERCEL_ENV === 'preview';
};
export const isDevMode = (): boolean => {
return import.meta.env.DEV;
};
export const getLastItem = (thePath: string | undefined | null): string | undefined => {
if (thePath === undefined || thePath === null || thePath === '') return;
const index = thePath.lastIndexOf('/');
if (index === -1) return thePath;
return thePath.substring(index + 1);
};
const transformToArray = (params: string): string[] => params.split(',');
export const parseParams = (
querystring: string,
encodeValues?: boolean
): Record<string, string | string[] | undefined> => {
const params = new URLSearchParams(querystring);
const obj: Record<string, string | string[]> = {};
for (const key of Array.from(params.keys())) {
let values = params.getAll(key);
if (encodeValues === true && (key === 'expression_a' || key === 'expression_b')) {
values = values.map(value => (isUrlEncoded(value) ? value : encodeURIComponent(value)));
}
if (values.length > 1) {
obj[key] = values;
} else {
if (values[0]?.includes(',')) {
obj[key] = transformToArray(values[0]);
} else {
obj[key] = values[0];
}
}
}
return obj;
};
export const selectQueryParam = (key: string): string | string[] | undefined => {
if (typeof window === 'undefined') {
return;
}
const router = parseParams(window.location.search);
if (key === 'dashboard_items') {
let dashboardItems = router[key] ?? [];
if (typeof dashboardItems === 'string') {
dashboardItems = [dashboardItems];
}
return dashboardItems;
}
if (key === 'compare_a' || key === 'compare_b') {
return router[key] === 'true' ? 'true' : 'false';
}
return router[key];
};
export const convertToQueryParams = (params: {[key: string]: string | string[]}): string =>
Object.keys(params)
.map((key: string) => `${key}=${params[key] as string}`)
.join('&');
export function convertUTCToLocalDate(date: Date): Date {
if (date === null) {
return date;
}
return new Date(
date.getUTCFullYear(),
date.getUTCMonth(),
date.getUTCDate(),
date.getUTCHours(),
date.getUTCMinutes(),
date.getUTCSeconds()
);
}
export function convertLocalToUTCDate(date: Date): Date {
if (date === null) {
return date;
}
return new Date(
Date.UTC(
date.getFullYear(),
date.getMonth(),
date.getDate(),
date.getHours(),
date.getMinutes(),
date.getSeconds()
)
);
}
export type ColorProfileName = 'default' | 'subtle' | 'ocean' | 'warm' | 'rainbow';
export type ColorsDuo = [string, string];
export interface ColorConfig {
colors: ColorsDuo[];
colorForSimilarNodes: string;
}
export const COLOR_PROFILES: {
[key in ColorProfileName]: ColorConfig;
} = {
ocean: {
colors: [
[colors.green['300'], colors.green['300']],
[colors.emerald['300'], colors.emerald['300']],
[colors.teal['300'], colors.teal['300']],
[colors.cyan['300'], colors.cyan['300']],
[colors.sky['300'], colors.sky['300']],
[colors.blue['300'], colors.blue['300']],
[colors.indigo['300'], colors.indigo['300']],
[colors.violet['300'], colors.violet['300']],
[colors.purple['300'], colors.purple['300']],
],
colorForSimilarNodes: 'stroke-indigo-600 dark:stroke-sky-100',
},
warm: {
colors: [
[colors.red['300'], colors.red['300']],
[colors.orange['300'], colors.orange['300']],
[colors.amber['300'], colors.amber['300']],
[colors.yellow['300'], colors.yellow['300']],
[colors.lime['300'], colors.lime['300']],
[colors.green['300'], colors.green['300']],
[colors.emerald['300'], colors.emerald['300']],
],
colorForSimilarNodes: 'stroke-blue-700 dark:stroke-sky-100',
},
subtle: {
colors: [
[colors.slate['200'], colors.slate['200']],
[colors.orange['200'], colors.orange['200']],
[colors.yellow['200'], colors.yellow['200']],
[colors.green['100'], colors.green['100']],
[colors.emerald['200'], colors.emerald['200']],
[colors.indigo['200'], colors.indigo['200']],
[colors.pink['200'], colors.pink['200']],
],
colorForSimilarNodes: 'stroke-blue-700 dark:stroke-indigo-600',
},
rainbow: {
colors: [
[colors.red['300'], colors.red['300']],
[colors.orange['300'], colors.orange['300']],
[colors.amber['300'], colors.amber['300']],
[colors.yellow['300'], colors.yellow['300']],
[colors.lime['300'], colors.lime['300']],
[colors.green['300'], colors.green['300']],
[colors.emerald['300'], colors.emerald['300']],
[colors.teal['300'], colors.teal['300']],
[colors.cyan['300'], colors.cyan['300']],
[colors.sky['300'], colors.sky['300']],
[colors.blue['300'], colors.blue['300']],
[colors.indigo['300'], colors.indigo['300']],
[colors.violet['300'], colors.violet['300']],
[colors.purple['300'], colors.purple['300']],
[colors.fuchsia['300'], colors.fuchsia['300']],
[colors.pink['300'], colors.pink['300']],
[colors.rose['300'], colors.rose['300']],
],
colorForSimilarNodes: 'stroke-blue-700 dark:stroke-sky-50',
},
default: {
colors: [['#929FEB', '#B3BAE1']],
colorForSimilarNodes: 'stroke-sky-400 dark:stroke-sky-50',
},
};
export const getNewSpanColor = (isDarkMode: boolean): string => {
return isDarkMode ? '#B3BAE1' : '#929FEB';
};
export const getIncreasedSpanColor = (transparency: number, isDarkMode: boolean): string => {
return isDarkMode
? `rgba(255, 177, 204, ${transparency})`
: `rgba(254, 153, 187, ${transparency})`;
};
export const getReducedSpanColor = (transparency: number, isDarkMode: boolean): string => {
return isDarkMode
? `rgba(103, 158, 92, ${transparency})`
: `rgba(164, 214, 153, ${transparency})`;
};
const DIFF_RATIO_THRESHOLD = 0.001;
export const diffColor = (diff: bigint, cumulative: bigint, isDarkMode: boolean): string => {
const prevValue = cumulative - diff;
const diffRatio = prevValue > 0 ? (diff !== 0n ? divide(diff, prevValue) : 0) : 1.0;
const hasDiff = Math.abs(diffRatio) > DIFF_RATIO_THRESHOLD;
return diffColorRatio(hasDiff, diffRatio, isDarkMode);
};
const diffColorRatio = (hasDiff: boolean, diffRatio: number, isDarkMode: boolean): string => {
const diffTransparency = hasDiff ? 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: string = !hasDiff
? newSpanColor
: diffRatio > 0
? increasedSpanColor
: reducedSpanColor;
return color;
};
export const isSearchMatch = (currentSearchString: string | undefined, name: string): boolean => {
if (currentSearchString === undefined || currentSearchString === '') return false;
return name.toLowerCase().includes(currentSearchString.toLowerCase());
};
export const saveAsBlob = (blob: Blob, filename: string): void => {
const link = document.createElement('a');
link.href = window.URL.createObjectURL(blob);
link.download = filename;
link.click();
};
export const sanitizeLabelValue = (labels: string[]): string[] => {
return labels.map((value: string) =>
value.includes('\\') ? value.replaceAll('\\', '\\\\') : value
);
};
export const sanitizeHighlightedValues = (labels: Label[]): Label[] =>
labels.map(v => {
return {
...v,
value: v.value.includes('\\') ? v.value.replaceAll('\\', '\\\\') : v.value,
};
});
export const isUrlEncoded = (str: string): boolean => {
try {
return decodeURIComponent(str) !== str;
} catch (e) {
return false; // Invalid encoding
}
};