Skip to content

Commit 60499fc

Browse files
committed
update time series for resp3 to work consistently
1 parent 77592d2 commit 60499fc

File tree

5 files changed

+158
-140
lines changed

5 files changed

+158
-140
lines changed
Lines changed: 27 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { CommandArguments, Command, BlobStringReply, ArrayReply, UnwrapReply, Resp2Reply, MapReply, TuplesReply } from '@redis/client/dist/lib/RESP/types';
22
import { RedisVariadicArgument, pushVariadicArguments } from '@redis/client/dist/lib/commands/generic-transformers';
3-
import { RawLabels, SampleRawReply, transformSampleReply } from '.';
3+
import { RawLabels2, RawLabels3, resp3MapToValue, SampleRawReply, transformSampleReply } from '.';
44

55
export interface TsMGetOptions {
66
LATEST?: boolean;
@@ -21,22 +21,41 @@ export function pushFilterArgument(args: CommandArguments, filter: RedisVariadic
2121

2222
export type MGetRawReply2 = ArrayReply<[
2323
key: BlobStringReply,
24-
labels: RawLabels,
24+
labels: RawLabels2,
2525
sample: Resp2Reply<SampleRawReply>
2626
]>;
2727

28-
export type MGetRawReply3 = MapReply<BlobStringReply, TuplesReply<[labels: RawLabels, sample: SampleRawReply]>>;
28+
export type MGetRawReplyValue3 = TuplesReply<[
29+
labels: RawLabels3,
30+
sample: SampleRawReply
31+
]>;
32+
33+
export type MGetRawReply3 = MapReply<
34+
BlobStringReply,
35+
MGetRawReplyValue3
36+
>
2937

3038
export interface MGetReply2 {
31-
key: string | BlobStringReply;
39+
key: BlobStringReply;
3240
sample: ReturnType<typeof transformSampleReply[2]>;
3341
}
3442

3543
export interface MGetReply3 {
36-
key: string;
44+
key: BlobStringReply | string
3745
sample: ReturnType<typeof transformSampleReply[3]>;
3846
}
3947

48+
export function parseResp3Mget(
49+
key: BlobStringReply | string,
50+
value: UnwrapReply<MGetRawReplyValue3>
51+
): MGetReply3 {
52+
53+
return {
54+
key: key,
55+
sample: transformSampleReply[3](value[1])
56+
}
57+
}
58+
4059
export default {
4160
FIRST_KEY_INDEX: undefined,
4261
IS_READ_ONLY: true,
@@ -47,37 +66,12 @@ export default {
4766
transformReply: {
4867
2(reply: UnwrapReply<MGetRawReply2>): Array<MGetReply2> {
4968
return reply.map(([key, _, sample]) => ({
50-
key: key.toString(),
69+
key: key,
5170
sample: transformSampleReply[2](sample)
5271
}));
5372
},
54-
3(reply: UnwrapReply<MapReply<any, any>>): Array<MGetReply3> {
55-
const args: Array<MGetReply3> = [];
56-
57-
if (reply instanceof Array) {
58-
for (const [key, value] of reply) {
59-
args.push({
60-
key,
61-
sample: transformSampleReply[3](value[1])
62-
});
63-
}
64-
} else if (reply instanceof Map) {
65-
for (const [key, value] of reply) {
66-
args.push({
67-
key,
68-
sample: transformSampleReply[3](value[1])
69-
});
70-
}
71-
} else {
72-
for (const [key, value] of Object.entries(reply)) {
73-
args.push({
74-
key,
75-
sample: transformSampleReply[3](value[1])
76-
});
77-
}
78-
}
79-
80-
return args;
73+
3(reply: UnwrapReply<MGetRawReply3>): Array<MGetReply3> {
74+
return resp3MapToValue(reply, parseResp3Mget)
8175
}
8276
}
8377
} as const satisfies Command;
Lines changed: 18 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { Command, MapReply, UnwrapReply } from '@redis/client/dist/lib/RESP/types';
1+
import { BlobStringReply, Command, UnwrapReply } from '@redis/client/dist/lib/RESP/types';
22
import { RedisVariadicArgument } from '@redis/client/dist/lib/commands/generic-transformers';
3-
import { TsMGetOptions, pushLatestArgument, pushFilterArgument, MGetReply2, MGetRawReply2, MGetReply3 } from './MGET';
4-
import { Labels, pushWithLabelsArgument, transformLablesReply2, transformLablesReply3, transformSampleReply } from '.';
3+
import { TsMGetOptions, pushLatestArgument, pushFilterArgument, MGetReply2, MGetRawReply2, MGetReply3, MGetRawReplyValue3, MGetRawReply3, parseResp3Mget } from './MGET';
4+
import { Labels, pushWithLabelsArgument, resp3MapToValue, transformLablesReply2, transformLablesReply3, transformSampleReply } from '.';
55

66
export interface TsMGetWithLabelsOptions extends TsMGetOptions {
77
SELECTED_LABELS?: RedisVariadicArgument;
@@ -15,6 +15,16 @@ export interface MGetWithLabelsReply3 extends MGetReply3 {
1515
labels: Labels;
1616
};
1717

18+
function parseResp3MgetWithLabels(
19+
key: BlobStringReply | string,
20+
value: UnwrapReply<MGetRawReplyValue3>
21+
): MGetWithLabelsReply3 {
22+
const ret = parseResp3Mget(key, value) as MGetWithLabelsReply3;
23+
ret.labels = transformLablesReply3(value[0]);
24+
25+
return ret;
26+
}
27+
1828
export default {
1929
FIRST_KEY_INDEX: undefined,
2030
IS_READ_ONLY: true,
@@ -24,43 +34,15 @@ export default {
2434
return pushFilterArgument(args, filter);
2535
},
2636
transformReply: {
27-
2: (reply: UnwrapReply<MGetRawReply2>): Array<MGetWithLabelsReply2> => {
37+
2(reply: UnwrapReply<MGetRawReply2>): Array<MGetWithLabelsReply2> {
2838
return reply.map(([key, labels, sample]) => ({
29-
key,
39+
key: key,
3040
labels: transformLablesReply2(labels),
3141
sample: transformSampleReply[2](sample)
3242
}));
3343
},
34-
3: (reply: UnwrapReply<MapReply<any, any>>): Array<MGetWithLabelsReply3> => {
35-
const args: Array<MGetWithLabelsReply3> = [];
36-
37-
if (reply instanceof Array) {
38-
for (const [key, value] of reply) {
39-
args.push({
40-
key,
41-
labels: transformLablesReply3(value[0]),
42-
sample: transformSampleReply[3](value[1])
43-
});
44-
}
45-
} else if (reply instanceof Map) {
46-
for (const [key, value] of reply) {
47-
args.push({
48-
key,
49-
labels: transformLablesReply3(value[0]),
50-
sample: transformSampleReply[3](value[1])
51-
});
52-
}
53-
} else {
54-
for (const [key, value] of Object.entries(reply)) {
55-
args.push({
56-
key,
57-
labels: transformLablesReply3(value[0]),
58-
sample: transformSampleReply[3](value[1])
59-
});
60-
}
61-
}
62-
63-
return args;
64-
},
44+
3(reply: UnwrapReply<MGetRawReply3>): Array<MGetReply3> {
45+
return resp3MapToValue(reply, parseResp3MgetWithLabels)
46+
}
6547
},
6648
} as const satisfies Command;

packages/time-series/lib/commands/MRANGE.ts

Lines changed: 53 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { RedisArgument, Command, CommandArguments, UnwrapReply, ArrayReply, BlobStringReply, Resp2Reply, MapReply } from '@redis/client/dist/lib/RESP/types';
1+
import { RedisArgument, Command, CommandArguments, UnwrapReply, ArrayReply, BlobStringReply, Resp2Reply, MapReply, TuplesReply } from '@redis/client/dist/lib/RESP/types';
22
import { RedisVariadicArgument } from '@redis/client/dist/lib/commands/generic-transformers';
3-
import { RawLabels, SampleRawReply, Timestamp, transformSampleReply, transformSamplesReply } from '.';
3+
import { RawLabels2, RawLabels3, resp3MapToValue, SampleRawReply, Timestamp, transformSampleReply, transformSamplesReply } from '.';
44
import { TsRangeOptions, pushRangeArguments } from './RANGE';
55
import { pushFilterArgument } from './MGET';
66

@@ -34,6 +34,8 @@ export function pushGroupByArgument(args: CommandArguments, groupBy?: TsMRangeOp
3434
'REDUCE',
3535
groupBy.reducer
3636
);
37+
38+
args.preserve = true;
3739
}
3840

3941
return args;
@@ -60,26 +62,70 @@ export function transformMRangeArguments(
6062

6163
export type MRangeRawReply2 = ArrayReply<[
6264
key: BlobStringReply,
63-
labels: RawLabels,
65+
labels: RawLabels2,
6466
samples: ArrayReply<Resp2Reply<SampleRawReply>>
6567
]>;
6668

69+
70+
export type MrangeRawReplyValue3 = TuplesReply<[
71+
labels: RawLabels3,
72+
// TODO: unsure what tod with this element, not part of resp2 at all
73+
_: MapReply<BlobStringReply, ArrayReply<unknown>>,
74+
samples: ArrayReply<SampleRawReply>
75+
]>;
76+
77+
export type MrangeRawReplyValueGrouped3 = TuplesReply<[
78+
labels: RawLabels3,
79+
reducers: MapReply<BlobStringReply, ArrayReply<unknown>>,
80+
sources: MapReply<BlobStringReply, ArrayReply<unknown>>,
81+
samples: ArrayReply<SampleRawReply>
82+
]>;
83+
84+
export type MRangeRawReply3 = MapReply<
85+
BlobStringReply,
86+
MrangeRawReplyValue3 | MrangeRawReplyValueGrouped3
87+
>;
88+
6789
export interface MRangeReplyItem2 {
6890
key: BlobStringReply;
6991
samples: Array<ReturnType<typeof transformSampleReply[2]>>;
7092
}
7193

7294
export interface MRangeReplyItem3 {
73-
key: BlobStringReply;
95+
key: BlobStringReply | string;
7496
samples: Array<ReturnType<typeof transformSampleReply[3]>>;
7597
}
7698

99+
export function getSamples(
100+
v: UnwrapReply<MrangeRawReplyValue3> | UnwrapReply<MrangeRawReplyValueGrouped3>,
101+
grouped?: boolean
102+
): ArrayReply<SampleRawReply> {
103+
if (grouped) {
104+
const value = v as unknown as UnwrapReply<MrangeRawReplyValueGrouped3>;
105+
return value[3];
106+
} else {
107+
const value = v as unknown as UnwrapReply<MrangeRawReplyValue3>;
108+
return value[2];
109+
}
110+
}
111+
112+
export function parseResp3Mrange(
113+
key: BlobStringReply | string,
114+
value: UnwrapReply<MrangeRawReplyValue3> | UnwrapReply<MrangeRawReplyValueGrouped3>,
115+
grouped?: boolean
116+
): MRangeReplyItem3 {
117+
return {
118+
key,
119+
samples: transformSamplesReply[3](getSamples(value, grouped))
120+
}
121+
}
122+
77123
export default {
78124
FIRST_KEY_INDEX: undefined,
79125
IS_READ_ONLY: true,
80126
transformArguments: transformMRangeArguments.bind(undefined, 'TS.MRANGE'),
81127
transformReply: {
82-
2: (reply: UnwrapReply<MRangeRawReply2>): Array<MRangeReplyItem2> => {
128+
2(reply: UnwrapReply<MRangeRawReply2>): Array<MRangeReplyItem2> {
83129
const args = [];
84130

85131
for (const [key, _, samples] of reply) {
@@ -91,33 +137,8 @@ export default {
91137

92138
return args;
93139
},
94-
3: (reply: UnwrapReply<MapReply<any, any>>): Array<MRangeReplyItem3> => {
95-
const args = [];
96-
97-
if (reply instanceof Array) {
98-
for (const [key, _, samples] of reply) {
99-
args.push({
100-
key,
101-
samples: transformSamplesReply[3](samples)
102-
});
103-
}
104-
} else if (reply instanceof Map) {
105-
for (const [key, value] of reply) {
106-
args.push({
107-
key,
108-
samples: transformSamplesReply[3](value[2])
109-
})
110-
}
111-
} else {
112-
for (const [key, value] of Object.entries(reply)) {
113-
args.push({
114-
key,
115-
samples: transformSamplesReply[3](value[2])
116-
})
117-
}
118-
}
119-
120-
return args;
140+
3(reply: UnwrapReply<MRangeRawReply3>, grouped?: boolean): Array<MRangeReplyItem3> {
141+
return resp3MapToValue(reply, parseResp3Mrange, grouped)
121142
}
122143
},
123144
} as const satisfies Command;
Lines changed: 17 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { RedisArgument, Command, UnwrapReply, MapReply } from '@redis/client/dist/lib/RESP/types';
1+
import { RedisArgument, Command, UnwrapReply, BlobStringReply } from '@redis/client/dist/lib/RESP/types';
22
import { RedisVariadicArgument } from '@redis/client/dist/lib/commands/generic-transformers';
3-
import { MRangeRawReply2, MRangeReplyItem2, MRangeReplyItem3, TsMRangeOptions, pushGroupByArgument } from './MRANGE';
4-
import { Labels, Timestamp, pushWithLabelsArgument, transformLablesReply2, transformLablesReply3, transformSamplesReply } from '.';
3+
import { MRangeRawReply2, MRangeRawReply3, MRangeReplyItem2, MRangeReplyItem3, MrangeRawReplyValue3, MrangeRawReplyValueGrouped3, TsMRangeOptions, parseResp3Mrange, pushGroupByArgument } from './MRANGE';
4+
import { Labels, Timestamp, pushWithLabelsArgument, resp3MapToValue, transformLablesReply2, transformLablesReply3, transformSamplesReply } from '.';
55
import { pushFilterArgument } from './MGET';
66
import { pushRangeArguments } from './RANGE';
77

@@ -30,12 +30,23 @@ export interface MRangeWithLabelsReplyItem3 extends MRangeReplyItem3 {
3030
labels: Labels;
3131
}
3232

33+
function parseResp3MrangeWithLabels(
34+
key: BlobStringReply | string,
35+
value: UnwrapReply<MrangeRawReplyValue3> | UnwrapReply<MrangeRawReplyValueGrouped3>,
36+
grouped?: boolean
37+
): MRangeWithLabelsReplyItem3 {
38+
const ret = parseResp3Mrange(key, value, grouped) as MRangeWithLabelsReplyItem3;
39+
ret.labels = transformLablesReply3(value[0])
40+
41+
return ret;
42+
}
43+
3344
export default {
3445
FIRST_KEY_INDEX: undefined,
3546
IS_READ_ONLY: true,
3647
transformArguments: transformMRangeWithLabelsArguments.bind(undefined, 'TS.MRANGE'),
3748
transformReply: {
38-
2: (reply: UnwrapReply<MRangeRawReply2>): Array<MRangeWithLabelsReplyItem2> => {
49+
2(reply: UnwrapReply<MRangeRawReply2>): Array<MRangeWithLabelsReplyItem2> {
3950
const args = [];
4051

4152
for (const [key, labels, samples] of reply) {
@@ -48,36 +59,8 @@ export default {
4859

4960
return args;
5061
},
51-
3: (reply: UnwrapReply<MapReply<any, any>>): Array<MRangeReplyItem3> => {
52-
const args = [];
53-
54-
if (reply instanceof Array) {
55-
for (const [key, labels, samples] of reply) {
56-
args.push({
57-
key,
58-
labels: transformLablesReply3(labels),
59-
samples: transformSamplesReply[3](samples)
60-
});
61-
}
62-
} else if (reply instanceof Map) {
63-
for (const [key, value] of reply) {
64-
args.push({
65-
key,
66-
labels: transformLablesReply3(value[0]),
67-
samples: transformSamplesReply[3](value[2])
68-
})
69-
}
70-
} else {
71-
for (const [key, value] of Object.entries(reply)) {
72-
args.push({
73-
key,
74-
labels: transformLablesReply3(value[0]),
75-
samples: transformSamplesReply[3](value[2])
76-
})
77-
}
78-
}
79-
80-
return args;
62+
3(reply: UnwrapReply<MRangeRawReply3>, grouped?: boolean): Array<MRangeWithLabelsReplyItem3> {
63+
return resp3MapToValue(reply, parseResp3MrangeWithLabels, grouped);
8164
}
8265
},
8366
} as const satisfies Command;

0 commit comments

Comments
 (0)