Add utility functions for converting time units and calculating divisibility factor

This commit is contained in:
Simon Larsen 2024-02-20 19:06:55 +00:00
parent af5a5332e5
commit dd144d00ac
No known key found for this signature in database
GPG Key ID: AB45983AA9C81CDE
4 changed files with 67 additions and 5 deletions

View File

@ -8,6 +8,20 @@ import moment from 'moment-timezone';
export const Moment: typeof moment = moment;
export default class OneUptimeDate {
public getNanoSecondsFromSeconds(seconds: number): number {
return seconds * 1000 * 1000 * 1000;
}
public getMicroSecondsFromSeconds(seconds: number): number {
return seconds * 1000 * 1000;
}
public getMilliSecondsFromSeconds(seconds: number): number {
return seconds * 1000;
}
public static getCurrentDateAsUnixNano(): number {
return this.toUnixNano(this.getCurrentDate());
}

View File

@ -1,4 +1,21 @@
export default class Text {
public static convertBase64ToHex(base64: string): string {
if(!base64) {
return base64;
}
const raw = Buffer.from(base64, 'base64').toString();
let result = '';
for (let i: number = 0; i < raw.length; i++) {
const hex: string = raw.charCodeAt(i).toString(16);
result += hex.length === 2 ? hex : '0' + hex;
}
return result.toUpperCase();
}
public static generateRandomText(length?: number): string {
if (!length) {
length = 10;

View File

@ -28,8 +28,6 @@ if (
const otlpEndpoint: string = process.env['OTEL_EXPORTER_OTLP_ENDPOINT'];
logger.info(otlpEndpoint);
sdk = new opentelemetry.NodeSDK({
traceExporter: new OTLPTraceExporter({
url: otlpEndpoint + '/v1/traces',

View File

@ -161,6 +161,35 @@ const TraceView: FunctionComponent<PageComponentProps> = (
});
}, []);
const getDivisibilityFactor: Function = (totalTimelineTimeInUnixNano: number): {
divisibilityFactor: number;
intervalUnit: string;
} => {
let intervalUnit: string = 'ms';
let divisibilityFactor: number = 1000; // default is in milliseconds
if(totalTimelineTimeInUnixNano < 1000){
intervalUnit = 'ns';
divisibilityFactor = 1; // this is in nanoseconds
} else if(totalTimelineTimeInUnixNano < 1000000){
intervalUnit = 'μs';
divisibilityFactor = 1000; // this is in microseconds
} else if(totalTimelineTimeInUnixNano < 1000000000){
intervalUnit = 'ms';
divisibilityFactor = 1000000; // this is in microseconds
} else if (totalTimelineTimeInUnixNano < 1000000000000){
intervalUnit = 's';
divisibilityFactor = 1000000000; // this is in seconds
}
return {
divisibilityFactor: divisibilityFactor,
intervalUnit: intervalUnit
};
}
React.useEffect(() => {
// convert spans to gantt chart
@ -185,8 +214,12 @@ const TraceView: FunctionComponent<PageComponentProps> = (
const startTimeline: number = 0;
const divisibilityFactor: number = 1000; // 1000 to convert from nanoseconds to ms
const intervalUnit: string = 'ms';
const divisibilityFactorAndIntervalUnit: {
divisibilityFactor: number;
intervalUnit: string;
} = getDivisibilityFactor((timelineEndTimeUnixNano - timelineStartTimeUnixNano));
const divisibilityFactor: number = divisibilityFactorAndIntervalUnit.divisibilityFactor;
const endTimeline: number =
(timelineEndTimeUnixNano - timelineStartTimeUnixNano) /
@ -233,7 +266,7 @@ const TraceView: FunctionComponent<PageComponentProps> = (
start: startTimeline,
end: Math.ceil(endTimeline / interval) * interval,
interval: interval,
intervalUnit: intervalUnit,
intervalUnit: divisibilityFactorAndIntervalUnit.intervalUnit,
},
};