Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
168 changes: 84 additions & 84 deletions tsp-typescript-client/fixtures/tsp-client/fetch-generic-xy-0.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,27 @@
{
"seriesId": 3,
"seriesName": "15652",
"xValues": [
[
0,
604229397
],
[
604229398,
1208458795
],
[
1208458796,
1812688193
],
[
1812688194,
2416917590
],
[
2416917591,
3021146988
]
"xRanges": [
{
"start": 0,
"end": 604229397
},
{
"start": 604229398,
"end": 1208458795
},
{
"start": 1208458796,
"end": 1812688193
},
{
"start": 1812688194,
"end": 2416917590
},
{
"start": 2416917591,
"end": 3021146988
}
],
"yValues": [
3.0,
Expand Down Expand Up @@ -60,27 +60,27 @@
{
"seriesId": 4,
"seriesName": "15653",
"xValues": [
[
0,
604229397
],
[
604229398,
1208458795
],
[
1208458796,
1812688193
],
[
1812688194,
2416917590
],
[
2416917591,
3021146988
]
"xRanges": [
{
"start": 0,
"end": 604229397
},
{
"start": 604229398,
"end": 1208458795
},
{
"start": 1208458796,
"end": 1812688193
},
{
"start": 1812688194,
"end": 2416917590
},
{
"start": 2416917591,
"end": 3021146988
}
],
"yValues": [
3.0,
Expand Down Expand Up @@ -115,27 +115,27 @@
{
"seriesId": 2,
"seriesName": "15646",
"xValues": [
[
0,
604229397
],
[
604229398,
1208458795
],
[
1208458796,
1812688193
],
[
1812688194,
2416917590
],
[
2416917591,
3021146988
]
"xRanges": [
{
"start": 0,
"end": 604229397
},
{
"start": 604229398,
"end": 1208458795
},
{
"start": 1208458796,
"end": 1812688193
},
{
"start": 1812688194,
"end": 2416917590
},
{
"start": 2416917591,
"end": 3021146988
}
],
"yValues": [
3.0,
Expand Down Expand Up @@ -170,27 +170,27 @@
{
"seriesId": 1,
"seriesName": "15647",
"xValues": [
[
0,
604229397
],
[
604229398,
1208458795
],
[
1208458796,
1812688193
],
[
1812688194,
2416917590
],
[
2416917591,
3021146988
]
"xRanges": [
{
"start": 0,
"end": 604229397
},
{
"start": 604229398,
"end": 1208458795
},
{
"start": 1208458796,
"end": 1812688193
},
{
"start": 1812688194,
"end": 2416917590
},
{
"start": 2416917591,
"end": 3021146988
}
],
"yValues": [
3.0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
{
"seriesId": 3,
"seriesName": "15652",
"xValues": [
"xCategories": [
"red",
"blue",
"green",
Expand Down
52 changes: 38 additions & 14 deletions tsp-typescript-client/src/models/sampling.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
import { toBigInt } from '../protocol/serialization';
import { createNormalizer, toBigInt } from '../protocol/serialization';

export type StartEndRange = [bigint, bigint];
export const StartEndRange = createNormalizer<StartEndRange>({
start: BigInt,
end: BigInt,
});

/**
* Represents a closed interval {start, end} as an object.
*/
export interface StartEndRange {
/**
* Start time of the range
*/
start: bigint;

/**
* End time of the range
*/
end: bigint;
}

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

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

export const isRangeSampling = (s: Sampling): s is [bigint, bigint][] =>
Array.isArray(s) && Array.isArray(s[0]);
export const isRangeSampling = (s: Sampling): s is StartEndRange[] => {
if (!Array.isArray(s)) {
return false;
}
if (s.length === 0) {
return true;
}
const firstElement = s[0];
return (
typeof firstElement === 'object' &&
firstElement !== null &&
'start' in firstElement &&
'end' in firstElement
);
};

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

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

// Category sampling → strings; leave as-is
Expand Down
12 changes: 8 additions & 4 deletions tsp-typescript-client/src/models/xy.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { array, assertNumber, createNormalizer } from '../protocol/serialization';
import { array, assertNumber, createNormalizer, toBigInt } from '../protocol/serialization';
import { AxisDomain } from './axis-domain';
import { DataType } from './data-type';
import { Entry } from './entry';
import { Sampling } from './sampling';
import { StartEndRange } from './sampling';
import { OutputElementStyle } from "./styles";

export const XYAxisDescription = createNormalizer<XYAxisDescription>({
Expand All @@ -11,7 +11,8 @@ export const XYAxisDescription = createNormalizer<XYAxisDescription>({

export const XYSeries = createNormalizer<XYSeries>({
seriesId: assertNumber,
xValues: Sampling,
xValues: array(toBigInt),
xRanges: array(StartEndRange),
yValues: array(assertNumber),
tags: array(assertNumber),
style: OutputElementStyle,
Expand Down Expand Up @@ -53,8 +54,11 @@ export interface XYSeries {
/**
* Series' X values
*/
xValues: Sampling;
xValues?: bigint[];

xRanges?: StartEndRange[];

xCategories?: string[];
/**
* Series' Y values
*/
Expand Down
43 changes: 28 additions & 15 deletions tsp-typescript-client/src/protocol/tsp-client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -348,11 +348,17 @@ describe('HttpTspClient Deserialization', () => {
expect(xy.series).toHaveLength(1);
for (const serie of xy.series) {
expect(typeof serie.seriesId).toEqual('number');
expect(serie.xValues).toBeDefined();
expect(serie.xRanges).toBeUndefined();
expect(serie.xCategories).toBeUndefined();
expect(serie.xValues).toHaveLength(3);
expect(serie.yValues).toHaveLength(3);
for (const xValue of serie.xValues) {
expect(typeof xValue).toEqual('bigint');
}
if (serie.xValues) {
for (const xValue of serie.xValues) {
expect(typeof xValue).toEqual('bigint');
}
}

for (const yValue of serie.yValues) {
expect(typeof yValue).toEqual('number');
}
Expand All @@ -369,17 +375,18 @@ describe('HttpTspClient Deserialization', () => {
expect(xy.series).toHaveLength(4);
for (const serie of xy.series) {
expect(typeof serie.seriesId).toEqual('number');
expect(serie.xValues).toHaveLength(5);
expect(serie.xValues).toBeUndefined();
expect(serie.xRanges).toBeDefined();
expect(serie.xCategories).toBeUndefined();
expect(serie.xRanges).toHaveLength(5);
expect(serie.yValues).toHaveLength(5);
for (const xValue of serie.xValues) {
if (Array.isArray(xValue) && xValue.length === 2) {
const [start, end] = xValue;
expect(typeof start).toBe('bigint');
expect(typeof end).toBe('bigint');
} else {
fail('xValues is not RangeSampling ([start,end] tuple)');

if (serie.xRanges) {
for (const xValue of serie.xRanges) {
expect(typeof xValue.start).toBe('bigint');
expect(typeof xValue.end).toBe('bigint');
}
}
}

for (const yValue of serie.yValues) {
expect(typeof yValue).toEqual('number');
Expand Down Expand Up @@ -414,10 +421,16 @@ describe('HttpTspClient Deserialization', () => {
expect(xy_categorized.series).toHaveLength(1);
for (const serie of xy_categorized.series) {
expect(typeof serie.seriesId).toEqual('number');
expect(serie.xValues).toHaveLength(5);
expect(serie.xValues).toBeUndefined();
expect(serie.xRanges).toBeUndefined();
expect(serie.xCategories).toBeDefined();

expect(serie.xCategories).toHaveLength(5);
expect(serie.yValues).toHaveLength(5);
for (const xValue of serie.xValues) {
expect(typeof xValue).toEqual('string');
if (serie.xCategories) {
for (const xValue of serie.xCategories) {
expect(typeof xValue).toEqual('string');
}
}

for (const yValue of serie.yValues) {
Expand Down