Skip to content

Commit

Permalink
added ConfigResetStat command in node.
Browse files Browse the repository at this point in the history
  • Loading branch information
Adan Wattad authored Aug 30, 2023
1 parent 63c70bb commit 72bbd02
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 4 deletions.
2 changes: 1 addition & 1 deletion node/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export { InfoOptions, SetOptions } from "./src/Commands";
export { InfoOptions, SetOptions, parseInfoResponse } from "./src/Commands";
export { setLoggerConfig } from "./src/Logger";
export { ConnectionOptions, RedisClient, ReturnType } from "./src/RedisClient";
export { RedisClusterClient } from "./src/RedisClusterClient";
Expand Down
18 changes: 18 additions & 0 deletions node/src/Commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,19 @@ function isLargeCommand(args: string[]) {
return false;
}

export function parseInfoResponse(response : string): Record<string, string>{
const lines = response.split('\n');
const parsedResponse : Record<string, string> = {};
for (const line of lines) {
/// Ignore lines that start with '#'
if (!line.startsWith('#')) {
const [key, value] = line.trim().split(':');
parsedResponse[key] = value;
}
}
return parsedResponse;
}

function createCommand(
requestType: redis_request.RequestType,
args: string[]
Expand Down Expand Up @@ -163,6 +176,11 @@ export function createSelect(
return createCommand(RequestType.Select, [index.toString()]);
}

export function createConfigResetStat(
): redis_request.Command {
return createCommand(RequestType.ConfigResetStat, []);
}

export function createCustomCommand(commandName: string, args: string[]) {
return createCommand(RequestType.CustomCommand, [commandName, ...args]);
}
10 changes: 10 additions & 0 deletions node/src/RedisClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { Buffer, BufferWriter, Reader, Writer } from "protobufjs";
import {
InfoOptions,
SetOptions,
createConfigResetStat,
createCustomCommand,
createDel,
createGet,
Expand Down Expand Up @@ -263,6 +264,15 @@ export class RedisClient {
return this.createWritePromise(createSelect(index));
}

/** Resets the statistics reported by Redis using the INFO and LATENCY HISTOGRAM commands.
* See https://redis.io/commands/config-resetstat/ for details.
*
* @returns always "OK"
*/
public ConfigResetStat(): Promise<"OK"> {
return this.createWritePromise(createConfigResetStat());
}

/** Executes a single command, without checking inputs. Every part of the command, including subcommands,
* should be added as a separate value in args.
*
Expand Down
14 changes: 13 additions & 1 deletion node/src/RedisClusterClient.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as net from "net";
import { InfoOptions, createCustomCommand, createInfo } from "./Commands";
import { InfoOptions, createConfigResetStat, createCustomCommand, createInfo } from "./Commands";
import { connection_request, redis_request } from "./ProtobufMessage";
import { ConnectionOptions, RedisClient, ReturnType } from "./RedisClient";

Expand Down Expand Up @@ -129,4 +129,16 @@ export class RedisClusterClient extends RedisClient {
public info(options?: InfoOptions[], route?: Routes): Promise<string> {
return this.createWritePromise(createInfo(options), toProtobufRoute(route));
}

/** Resets the statistics reported by Redis using the INFO and LATENCY HISTOGRAM commands.
* See https://redis.io/commands/config-resetstat/ for details.
*
* @param route The command will be routed automatically, unless `route` is provided, in which
* case the client will initially try to route the command to the nodes defined by `route`.
*
* @returns always "OK"
*/
public ConfigResetStat(route?: Routes): Promise<"OK"> {
return this.createWritePromise(createConfigResetStat(), toProtobufRoute(route));
}
}
10 changes: 10 additions & 0 deletions node/src/Transaction.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
InfoOptions,
SetOptions,
createConfigResetStat,
createCustomCommand,
createDel,
createGet,
Expand Down Expand Up @@ -39,6 +40,15 @@ export class BaseTransaction {
this.commands.push(createDel(keys));
}

/** Resets the statistics reported by Redis using the INFO and LATENCY HISTOGRAM commands.
* See https://redis.io/commands/config-resetstat/ for details.
*
* Returns always "OK"
*/
public ConfigResetStat() {
this.commands.push(createConfigResetStat());
}

/** Executes a single command, without checking inputs. Every part of the command, including subcommands,
* should be added as a separate value in args.
*
Expand Down
23 changes: 21 additions & 2 deletions node/tests/SharedTests.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { expect, it } from "@jest/globals";
import { v4 as uuidv4 } from "uuid";
import { ReturnType, SetOptions } from "../";
import { Client, GetAndSetRandomValue } from "./TestUtilities";
import { InfoOptions, ReturnType, SetOptions, parseInfoResponse } from "../";
import { Client, GetAndSetRandomValue, getFirstResult } from "./TestUtilities";

type BaseClient = {
set: (
Expand All @@ -11,6 +11,8 @@ type BaseClient = {
) => Promise<string | "OK" | null>;
get: (key: string) => Promise<string | null>;
del: (keys: string[]) => Promise<number>;
info(options?: InfoOptions[]): Promise<string | string[][]>;
ConfigResetStat: () => Promise<"OK">;
customCommand: (commandName: string, args: string[]) => Promise<ReturnType>;
};

Expand Down Expand Up @@ -161,6 +163,23 @@ export function runBaseTests<Context>(config: {
},
config.timeout
);

it(
"info stats before and after Config ResetStat is different",
async () => {
await runTest(async (client: BaseClient) => {
/// we execute set and info so the total_commands_processed will be greater than 1
/// after the configResetStat call we initiate an info command and the the total_commands_processed will be 1.
await client.set("foo", "bar");
const OldResult = await client.info([InfoOptions.Stats]);
expect(Number(parseInfoResponse(getFirstResult(OldResult))["total_commands_processed"])).toBeGreaterThan(1);
expect(await client.ConfigResetStat()).toEqual("OK");
const result = await client.info([InfoOptions.Stats]);
expect(parseInfoResponse(getFirstResult(result))["total_commands_processed"]).toEqual("1");
});
},
config.timeout
);
}

export function runCommonTests<Context>(config: {
Expand Down
8 changes: 8 additions & 0 deletions node/tests/TestUtilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,11 @@ export function flushallOnPort(port: number): Promise<void> {
})
);
}

/// This function takes the first result of the response if it got more than one response (like CME responses).
export function getFirstResult(res: string | string[][]): string {
if(typeof res == "string"){
return res;
}
return res[0][1];
}

0 comments on commit 72bbd02

Please sign in to comment.