Skip to content

Commit f79e9de

Browse files
author
Siwei Zhang
committed
Fix parsing of sampling to match tsp specification
The server was modified to comply to tsp specification for sampling, update accordingly here. Signed-off-by: Siwei Zhang <[email protected]>
1 parent 565e2d9 commit f79e9de

File tree

3 files changed

+128
-99
lines changed

3 files changed

+128
-99
lines changed

tsp-typescript-client/fixtures/tsp-client/fetch-generic-xy-0.json

Lines changed: 80 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,26 @@
66
"seriesId": 3,
77
"seriesName": "15652",
88
"xValues": [
9-
[
10-
0,
11-
604229397
12-
],
13-
[
14-
604229398,
15-
1208458795
16-
],
17-
[
18-
1208458796,
19-
1812688193
20-
],
21-
[
22-
1812688194,
23-
2416917590
24-
],
25-
[
26-
2416917591,
27-
3021146988
28-
]
9+
{
10+
"start": 0,
11+
"end": 604229397
12+
},
13+
{
14+
"start": 604229398,
15+
"end": 1208458795
16+
},
17+
{
18+
"start": 1208458796,
19+
"end": 1812688193
20+
},
21+
{
22+
"start": 1812688194,
23+
"end": 2416917590
24+
},
25+
{
26+
"start": 2416917591,
27+
"end": 3021146988
28+
}
2929
],
3030
"yValues": [
3131
3.0,
@@ -61,26 +61,26 @@
6161
"seriesId": 4,
6262
"seriesName": "15653",
6363
"xValues": [
64-
[
65-
0,
66-
604229397
67-
],
68-
[
69-
604229398,
70-
1208458795
71-
],
72-
[
73-
1208458796,
74-
1812688193
75-
],
76-
[
77-
1812688194,
78-
2416917590
79-
],
80-
[
81-
2416917591,
82-
3021146988
83-
]
64+
{
65+
"start": 0,
66+
"end": 604229397
67+
},
68+
{
69+
"start": 604229398,
70+
"end": 1208458795
71+
},
72+
{
73+
"start": 1208458796,
74+
"end": 1812688193
75+
},
76+
{
77+
"start": 1812688194,
78+
"end": 2416917590
79+
},
80+
{
81+
"start": 2416917591,
82+
"end": 3021146988
83+
}
8484
],
8585
"yValues": [
8686
3.0,
@@ -116,26 +116,26 @@
116116
"seriesId": 2,
117117
"seriesName": "15646",
118118
"xValues": [
119-
[
120-
0,
121-
604229397
122-
],
123-
[
124-
604229398,
125-
1208458795
126-
],
127-
[
128-
1208458796,
129-
1812688193
130-
],
131-
[
132-
1812688194,
133-
2416917590
134-
],
135-
[
136-
2416917591,
137-
3021146988
138-
]
119+
{
120+
"start": 0,
121+
"end": 604229397
122+
},
123+
{
124+
"start": 604229398,
125+
"end": 1208458795
126+
},
127+
{
128+
"start": 1208458796,
129+
"end": 1812688193
130+
},
131+
{
132+
"start": 1812688194,
133+
"end": 2416917590
134+
},
135+
{
136+
"start": 2416917591,
137+
"end": 3021146988
138+
}
139139
],
140140
"yValues": [
141141
3.0,
@@ -171,26 +171,26 @@
171171
"seriesId": 1,
172172
"seriesName": "15647",
173173
"xValues": [
174-
[
175-
0,
176-
604229397
177-
],
178-
[
179-
604229398,
180-
1208458795
181-
],
182-
[
183-
1208458796,
184-
1812688193
185-
],
186-
[
187-
1812688194,
188-
2416917590
189-
],
190-
[
191-
2416917591,
192-
3021146988
193-
]
174+
{
175+
"start": 0,
176+
"end": 604229397
177+
},
178+
{
179+
"start": 604229398,
180+
"end": 1208458795
181+
},
182+
{
183+
"start": 1208458796,
184+
"end": 1812688193
185+
},
186+
{
187+
"start": 1812688194,
188+
"end": 2416917590
189+
},
190+
{
191+
"start": 2416917591,
192+
"end": 3021146988
193+
}
194194
],
195195
"yValues": [
196196
3.0,

tsp-typescript-client/src/models/sampling.ts

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,25 @@
1-
import { toBigInt } from '../protocol/serialization';
1+
import { createNormalizer, toBigInt } from '../protocol/serialization';
22

3-
export type StartEndRange = [bigint, bigint];
3+
export const StartEndRange = createNormalizer<StartEndRange>({
4+
start: BigInt,
5+
end: BigInt,
6+
});
7+
8+
/**
9+
* Represents a closed interval {start, end} as an object.
10+
*/
11+
export interface StartEndRange {
12+
/**
13+
* Start time of the range
14+
*/
15+
start: bigint;
16+
17+
/**
18+
* End time of the range
19+
*/
20+
end: bigint;
21+
}
422

5-
export const StartEndRange = (v: unknown): StartEndRange => {
6-
if (!Array.isArray(v) || v.length !== 2) {
7-
throw new Error('StartEndRange: expected [start,end]');
8-
}
9-
const [s, e] = v;
10-
return [toBigInt(s as any), toBigInt(e as any)];
11-
};
1223

1324
/**
1425
* Represent sampling on a list of timestamps.
@@ -30,8 +41,21 @@ export type RangeSampling = StartEndRange[];
3041
*/
3142
export type Sampling = TimestampSampling | CategorySampling | RangeSampling;
3243

33-
export const isRangeSampling = (s: Sampling): s is [bigint, bigint][] =>
34-
Array.isArray(s) && Array.isArray(s[0]);
44+
export const isRangeSampling = (s: Sampling): s is StartEndRange[] => {
45+
if (!Array.isArray(s)) {
46+
return false;
47+
}
48+
if (s.length === 0) {
49+
return true;
50+
}
51+
const firstElement = s[0];
52+
return (
53+
typeof firstElement === 'object' &&
54+
firstElement !== null &&
55+
'start' in firstElement &&
56+
'end' in firstElement
57+
);
58+
};
3559

3660
export const isTimestampSampling = (s: Sampling): s is bigint[] =>
3761
Array.isArray(s) && typeof s[0] === 'bigint';
@@ -57,9 +81,9 @@ export const Sampling = (v: unknown): Sampling => {
5781
return arr.map(toBigInt) as bigint[];
5882
}
5983

60-
// ranges → array of [bigint, bigint]
61-
if (Array.isArray(first) && first.length === 2) {
62-
return (v as unknown[]).map(StartEndRange);
84+
// Range sampling (array of {start, end} objects)
85+
if (typeof first === 'object' && first !== null && 'start' in first && 'end' in first) {
86+
return v.map(StartEndRange);
6387
}
6488

6589
// Category sampling → strings; leave as-is

tsp-typescript-client/src/protocol/tsp-client.test.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { DataType } from '../models/data-type';
77
import { ConfigurationParameterDescriptor } from '../models/configuration-source';
88
import { QueryHelper } from '../models/query/query-helper';
99
import { isAxisDomainCategorical, isAxisDomainRange } from '../models/axis-domain';
10+
import { StartEndRange } from '../models/sampling';
1011

1112
describe('HttpTspClient Deserialization', () => {
1213

@@ -372,12 +373,16 @@ describe('HttpTspClient Deserialization', () => {
372373
expect(serie.xValues).toHaveLength(5);
373374
expect(serie.yValues).toHaveLength(5);
374375
for (const xValue of serie.xValues) {
375-
if (Array.isArray(xValue) && xValue.length === 2) {
376-
const [start, end] = xValue;
377-
expect(typeof start).toBe('bigint');
378-
expect(typeof end).toBe('bigint');
376+
if (
377+
typeof xValue === 'object' &&
378+
xValue !== null &&
379+
!Array.isArray(xValue)
380+
) {
381+
const rangeObject = xValue as StartEndRange;
382+
expect(typeof rangeObject.start).toBe('bigint');
383+
expect(typeof rangeObject.end).toBe('bigint');
379384
} else {
380-
fail('xValues is not RangeSampling ([start,end] tuple)');
385+
throw new Error('xValue is not a valid StartEndRange object ({start, end})');
381386
}
382387
}
383388

0 commit comments

Comments
 (0)