Skip to content

Commit c74cd9d

Browse files
fix: add protobuf time formatters
1 parent e325d98 commit c74cd9d

File tree

7 files changed

+135
-15
lines changed

7 files changed

+135
-15
lines changed

src/types/api/common.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export interface IProtobufTimeObject {
2+
/** int64 */
3+
seconds?: string;
4+
nanos?: number;
5+
}

src/types/api/consumer.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
/* eslint-disable camelcase */
22

3+
import {IProtobufTimeObject} from './common';
4+
35
/**
46
* endpoint: /json/describe_consumer
57
*
@@ -65,27 +67,27 @@ interface PartitionConsumerStats {
6567
*
6668
* Timestamp of providing this partition to this session by server.
6769
*/
68-
partition_read_session_create_time?: string;
70+
partition_read_session_create_time?: string | IProtobufTimeObject;
6971

7072
/**
7173
* google.protobuf.Timestamp
7274
*
7375
* Timestamp of last read from this partition. */
74-
last_read_time?: string;
76+
last_read_time?: string | IProtobufTimeObject;
7577

7678
/**
7779
* google.protobuf.Duration
7880
*
7981
* Maximum of differences between timestamp of read and write timestamp for all messages, read during last minute.
8082
*/
81-
max_read_time_lag?: string;
83+
max_read_time_lag?: string | IProtobufTimeObject;
8284

8385
/**
8486
* google.protobuf.Duration
8587
*
8688
* Maximum of differences between write timestamp and create timestamp for all messages, read during last minute.
8789
*/
88-
max_write_time_lag?: string;
90+
max_write_time_lag?: string | IProtobufTimeObject;
8991

9092
/** How much bytes were read during several windows statistics from this partiton. */
9193
bytes_read?: MultipleWindowsStat;
@@ -113,14 +115,14 @@ export interface PartitionStats {
113115
*
114116
* Timestamp of last write.
115117
*/
116-
last_write_time?: string;
118+
last_write_time?: string | IProtobufTimeObject;
117119

118120
/**
119121
* google.protobuf.Duration
120122
*
121123
* Maximum of differences between write timestamp and create timestamp for all messages, written during last minute.
122124
*/
123-
max_write_time_lag?: string;
125+
max_write_time_lag?: string | IProtobufTimeObject;
124126

125127
/** How much bytes were written during several windows in this partition. */
126128
bytes_written?: MultipleWindowsStat;
@@ -148,7 +150,7 @@ export interface Consumer {
148150
*
149151
* All messages with smaller server written_at timestamp will be skipped.
150152
*/
151-
read_from?: string;
153+
read_from?: string | IProtobufTimeObject;
152154

153155
/**
154156
* List of supported codecs by this consumer.
@@ -170,21 +172,21 @@ interface ConsumerStats {
170172
*
171173
* Minimal timestamp of last read from partitions.
172174
*/
173-
min_partitions_last_read_time?: string;
175+
min_partitions_last_read_time?: string | IProtobufTimeObject;
174176

175177
/**
176178
* google.protobuf.Duration
177179
*
178180
* Maximum of differences between timestamp of read and write timestamp for all messages, read during last minute.
179181
*/
180-
max_read_time_lag?: string;
182+
max_read_time_lag?: string | IProtobufTimeObject;
181183

182184
/**
183185
* google.protobuf.Duration
184186
*
185187
* Maximum of differences between write timestamp and create timestamp for all messages, read during last minute.
186188
*/
187-
max_write_time_lag?: string;
189+
max_write_time_lag?: string | IProtobufTimeObject;
188190

189191
/** Bytes read stastics. */
190192
bytes_read?: MultipleWindowsStat;

src/types/api/topic.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/* eslint-disable camelcase */
22

3+
import {IProtobufTimeObject} from './common';
34
import {Consumer, Entry, MultipleWindowsStat, PartitionStats, SupportedCodecs} from './consumer';
45

56
/**
@@ -27,7 +28,7 @@ export interface DescribeTopicResult {
2728
*
2829
* How long data in partition should be stored.
2930
*/
30-
retention_period?: string;
31+
retention_period?: string | IProtobufTimeObject;
3132

3233
/**
3334
* int64
@@ -98,7 +99,7 @@ interface PartitionInfo {
9899
partition_stats?: PartitionStats;
99100
}
100101

101-
interface TopicStats {
102+
export interface TopicStats {
102103
/**
103104
* int64
104105
*
@@ -111,14 +112,14 @@ interface TopicStats {
111112
*
112113
* Minimum of timestamps of last write among all partitions.
113114
*/
114-
min_last_write_time?: string;
115+
min_last_write_time?: string | IProtobufTimeObject;
115116

116117
/**
117118
* google.protobuf.Duration
118119
*
119120
* Maximum of differences between write timestamp and create timestamp for all messages, written during last minute.
120121
*/
121-
max_write_time_lag?: string;
122+
max_write_time_lag?: string | IProtobufTimeObject;
122123

123124
/** How much bytes were written statistics. */
124125
bytes_written?: MultipleWindowsStat;

src/utils/constants.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export const MINUTE_IN_SECONDS = 60;
1818
export const HOUR_IN_SECONDS = 60 * MINUTE_IN_SECONDS;
1919
export const DAY_IN_SECONDS = 24 * HOUR_IN_SECONDS;
2020

21-
export const NANOS_IN_MS = 1000000;
21+
export const MS_IN_NANOSECONDS = 1000000;
2222

2323
export const TABLET_STATES = {
2424
TABLET_VOLATILE_STATE_UNKNOWN: 'unknown',
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import {
2+
parseProtobufDurationToMs,
3+
parseProtobufTimeObjectToMs,
4+
parseProtobufTimestampToMs,
5+
} from '../';
6+
7+
describe('Protobuf time parsers', () => {
8+
// 1 - nanoseconds only
9+
const timeObjectWithNanoseconds = {
10+
nanos: 123000000,
11+
};
12+
const timeWithNanosecondsInMs = 123;
13+
14+
// 2 - seconds only
15+
const timeObjectWithSeconds = {
16+
seconds: '12',
17+
};
18+
const timeWithSecondsInMs = 12000;
19+
const stringDurationSeconds = '12s';
20+
21+
// 3 - days
22+
const timeObjectWithDays = {
23+
seconds: '439234',
24+
nanos: 123000000,
25+
};
26+
const timeWithDaysInMs = 439234123;
27+
28+
// 4 - TimeStamp
29+
const timestamp = '2023-01-16T14:26:34.466Z';
30+
const timestampInMs = 1673879194466;
31+
const timestampObject = {
32+
seconds: '1673879194',
33+
nanos: 466000000,
34+
};
35+
36+
describe('parseProtobufTimeObjectToMs', () => {
37+
it('should work with timestamp object values', () => {
38+
expect(parseProtobufTimeObjectToMs(timeObjectWithDays)).toEqual(timeWithDaysInMs);
39+
});
40+
it('should work with timestamp object without seconds', () => {
41+
expect(parseProtobufTimeObjectToMs(timeObjectWithNanoseconds)).toEqual(
42+
timeWithNanosecondsInMs,
43+
);
44+
});
45+
it('should work with timestamp object without nanos', () => {
46+
expect(parseProtobufTimeObjectToMs(timeObjectWithSeconds)).toEqual(timeWithSecondsInMs);
47+
});
48+
it('should work with empty object values', () => {
49+
expect(parseProtobufTimeObjectToMs({})).toEqual(0);
50+
});
51+
});
52+
describe('parseProtobufTimestampToMs', () => {
53+
it('should work with string date values', () => {
54+
expect(parseProtobufTimestampToMs(timestamp)).toEqual(timestampInMs);
55+
});
56+
it('should work with timestamp object values', () => {
57+
expect(parseProtobufTimestampToMs(timestampObject)).toEqual(timestampInMs);
58+
});
59+
it('should work with empty object values', () => {
60+
expect(parseProtobufTimestampToMs({})).toEqual(0);
61+
});
62+
});
63+
describe('parseProtobufDurationToMs', () => {
64+
it('should work with string values', () => {
65+
expect(parseProtobufDurationToMs(stringDurationSeconds)).toEqual(timeWithSecondsInMs);
66+
});
67+
it('should work with duration object values', () => {
68+
expect(parseProtobufDurationToMs(timeObjectWithDays)).toEqual(timeWithDaysInMs);
69+
});
70+
it('should work with empty object values', () => {
71+
expect(parseProtobufDurationToMs({})).toEqual(0);
72+
});
73+
});
74+
});

src/utils/timeParsers/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * from './formatDuration';
2+
export * from './protobuf';

src/utils/timeParsers/protobuf.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import type {IProtobufTimeObject} from '../../types/api/common';
2+
3+
import {MS_IN_NANOSECONDS} from '../constants';
4+
5+
/**
6+
* Parses Google.protobuf Duration and Timestamp represented by objects
7+
*/
8+
export const parseProtobufTimeObjectToMs = (value: IProtobufTimeObject): number => {
9+
const secondsInMs = value.seconds ? Number(value.seconds) * 1000 : 0;
10+
const nanosecondsInMs = value.nanos ? value.nanos / MS_IN_NANOSECONDS : 0;
11+
return secondsInMs + nanosecondsInMs;
12+
};
13+
14+
/**
15+
* Parses Google.protobuf Timestamp to ms.
16+
* Timestamp could be represented as object or as date string
17+
*/
18+
export const parseProtobufTimestampToMs = (value: string | IProtobufTimeObject) => {
19+
if (typeof value === 'string') {
20+
return Date.parse(value);
21+
} else {
22+
return parseProtobufTimeObjectToMs(value);
23+
}
24+
};
25+
26+
/**
27+
* Parses Google.protobuf Duration to ms.
28+
* Duration could be represented as object or as string with format '12345s'
29+
*/
30+
export const parseProtobufDurationToMs = (value: string | IProtobufTimeObject) => {
31+
if (typeof value === 'string') {
32+
return parseInt(value, 10) * 1000;
33+
} else {
34+
return parseProtobufTimeObjectToMs(value);
35+
}
36+
};

0 commit comments

Comments
 (0)