All files / src/resp/command/server info-command.ts

83.01% Statements 44/53
25% Branches 4/16
75% Functions 6/8
83.01% Lines 44/53

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 1441x 1x       1x 1x   1x 4x   4x   4x   1x   1x   1x   1x   1x   1x   1x   1x   1x   1x   1x   1x   4x     1x 1x 1x       1x 1x 1x 1x                             1x       1x                           1x   1x                                 1x                                       1x 1x 1x 1x 1x       1x 4x   1x     1x   1x 1x      
import * as os from "os";
import { Logger } from "../../../logger";
import { IRequest } from "../../../server/request";
import { IServerContext } from "../../../server/server-context";
import { Database } from "../../data/database";
import { RedisToken } from "../../protocol/redis-token";
import { IRespCommand } from "../resp-command";
 
export class InfoCommand extends IRespCommand {
    public maxParams = 1
 
    public minParams = 0
 
    public name = "info"
 
    private static SHARP = "# ";
 
    private static SEPARATOR = ":";
 
    private static DELIMITER = "\r\n";
 
    private static SECTION_KEYSPACE = "keyspace";
 
    private static SECTION_COMMANDSTATS = "commandstats";
 
    private static SECTION_CPU = "cpu";
 
    private static SECTION_STATS = "stats";
 
    private static SECTION_PERSISTENCE = "persistence";
 
    private static SECTION_MEMORY = "memory";
 
    private static SECTION_CLIENTS = "clients";
 
    private static SECTION_REPLICATION = "replication";
 
    private static SECTION_SERVER = "server";
 
    private logger: Logger = new Logger(module.id);
 
    public execSync(request: IRequest, db: Database): RedisToken {
        this.logger.debug(`executeDBRequest(${request}, ${db})`);
        const sections: any = {};
        this.logger.debug(
            `request is ${request.constructor.name}`,
            `${request}`
        );
        Eif (request.getParams().length > 0) {
            const section = request.getParam(0).toString();
            Eif (this.allSections().indexOf(section.toLowerCase()) > -1) {
                sections[section] = this.section(
                    section,
                    request.getServerContext()
                );
            } else {
                return RedisToken.string(" ");
            }
        } else {
            for (const section of this.allSections()) {
                sections[section] = this.section(
                    section,
                    request.getServerContext()
                );
            }
        }
        return RedisToken.string(this.makeString(sections));
    }
 
    private allSections(): string[] {
        return [
            InfoCommand.SECTION_SERVER,
            InfoCommand.SECTION_REPLICATION,
            InfoCommand.SECTION_CLIENTS,
            InfoCommand.SECTION_MEMORY,
            InfoCommand.SECTION_PERSISTENCE,
            InfoCommand.SECTION_STATS,
            InfoCommand.SECTION_CPU,
            InfoCommand.SECTION_COMMANDSTATS,
            InfoCommand.SECTION_KEYSPACE
        ];
    }
 
    private section(section: string, ctx: IServerContext): any {
        switch (section.toLowerCase()) {
        case InfoCommand.SECTION_SERVER:
            return this.server(ctx);
        case InfoCommand.SECTION_CLIENTS:
            return this.clients(ctx);
        case InfoCommand.SECTION_MEMORY:
            return this.memory(ctx);
        case InfoCommand.SECTION_STATS:
        case InfoCommand.SECTION_CPU:
        case InfoCommand.SECTION_COMMANDSTATS:
        case InfoCommand.SECTION_KEYSPACE:
        case InfoCommand.SECTION_REPLICATION:
        case InfoCommand.SECTION_PERSISTENCE:
        default:
            return { "section": "Not Implemented" };
        }
    }
 
    private server(ctx: IServerContext): any {
        return {
            "server": {
                "redis_version": "1.0.1",
                // Tslint:disable-next-line
                "node_version": process.version,
                "os": `${os.platform()} ${os.release()} ${os.arch()}`,
                "tcp_port": ctx.getPort()
            }
        };
    }
 
    private clients(ctx: IServerContext): any {
        return { "connected_clients": `${ctx.getClients()}` };
    }
 
    private memory(ctx: IServerContext): any {
        return { "memory_use": process.memoryUsage() };
    }
 
    private makeString(sections: any): string {
        let result = "";
        for (const section of Object.keys(sections)) {
            result += `${InfoCommand.SHARP}${section}${InfoCommand.DELIMITER}`;
            for (const subkey of Object.keys(sections[section])) {
                Iif (sections[section][subkey].constructor.name === "String") {
                    result += `${subkey}${InfoCommand.SEPARATOR}${sections[section][subkey]}${InfoCommand.DELIMITER}`;
                } else {
                    //          Result += `${subkey}${InfoCommand.SEPARATOR}`;
                    for (const sskey of Object.keys(sections[section][subkey])) {
                        result += `${sskey}${InfoCommand.SEPARATOR}\t${sections[section][subkey][sskey]}${InfoCommand.DELIMITER}`;
                    }
                    result += `${InfoCommand.DELIMITER}`;
                }
            }
            result += InfoCommand.DELIMITER;
        }
        result += InfoCommand.DELIMITER;
        return result;
    }
}