MXS-2712: Move field definitions out of functions

The fields are now defined separately and no longer use the object keys as
the names of the values. This makes it clearer as to what the field
definition actually is. Following commits will add a description key into
each object that makes it possible to easily build help output.
This commit is contained in:
Markus Mäkelä
2019-10-08 12:50:39 +03:00
parent abe73c7b31
commit 088bae5a47
3 changed files with 554 additions and 159 deletions

View File

@ -112,7 +112,7 @@ module.exports = function() {
row = [] row = []
fields.forEach(function(p) { fields.forEach(function(p) {
var v = _.getPath(i, p[Object.keys(p)[0]], '') var v = _.getPath(i, p.path, '')
if (Array.isArray(v)) { if (Array.isArray(v)) {
v = v.join(', ') v = v.join(', ')
@ -162,7 +162,7 @@ module.exports = function() {
var header = [] var header = []
fields.forEach(function(i) { fields.forEach(function(i) {
header.push(Object.keys(i)) header.push(i.name)
}) })
var table = getTable(header) var table = getTable(header)
@ -188,7 +188,7 @@ module.exports = function() {
var header = [] var header = []
fields.forEach(function(i) { fields.forEach(function(i) {
header.push(Object.keys(i)) header.push(i.name)
}) })
var table = getTable(header) var table = getTable(header)
@ -197,7 +197,7 @@ module.exports = function() {
row = [] row = []
fields.forEach(function(p) { fields.forEach(function(p) {
var v = _.getPath(i, p[Object.keys(p)[0]], '') var v = _.getPath(i, p[p.name], '')
if (Array.isArray(v) && typeof(v[0]) != 'object') { if (Array.isArray(v) && typeof(v[0]) != 'object') {
v = v.join(', ') v = v.join(', ')
@ -229,7 +229,7 @@ module.exports = function() {
separator = '\n' separator = '\n'
var max_field_length = 0 var max_field_length = 0
fields.forEach(function (i) { fields.forEach(function (i) {
var k = Object.keys(i)[0] var k = i.name
if (k.length > max_field_length) { if (k.length > max_field_length) {
max_field_length = k.length max_field_length = k.length
} }
@ -244,8 +244,8 @@ module.exports = function() {
} }
fields.forEach(function(i) { fields.forEach(function(i) {
var k = Object.keys(i)[0] var k = i.name
var path = i[k] var path = i.path
var v = _.getPath(data, path, '') var v = _.getPath(data, path, '')
if (Array.isArray(v) && typeof(v[0]) != 'object') { if (Array.isArray(v) && typeof(v[0]) != 'object') {

View File

@ -13,6 +13,200 @@
require('./common.js')() require('./common.js')()
const list_servers_fields = [
{
name: 'Server',
path: 'id',
},
{
name: 'Address',
path: 'attributes.parameters.address',
},
{
name: 'Port',
path: 'attributes.parameters.port',
},
{
name: 'Connections',
path: 'attributes.statistics.connections',
},
{
name: 'State',
path: 'attributes.state',
},
{
name: 'GTID',
path: 'attributes.gtid_current_pos',
}
]
const list_services_fields = [
{
name: 'Service',
path: 'id',
},
{
name: 'Router',
path: 'attributes.router',
},
{
name: 'Connections',
path: 'attributes.connections',
},
{
name: 'Total Connections',
path: 'attributes.total_connections',
},
{
name: 'Servers',
path: 'relationships.servers.data[].id',
}
]
const list_listeners_fields = [
{
name: 'Name',
path: 'id',
},
{
name: 'Port',
path: 'attributes.parameters.port',
},
{
name: 'Host',
path: 'attributes.parameters.host',
},
{
name: 'State',
path: 'attributes.state',
}
]
const list_monitors_fields = [
{
name: 'Monitor',
path: 'id',
},
{
name: 'State',
path: 'attributes.state',
},
{
name: 'Servers',
path: 'relationships.servers.data[].id',
}
]
const list_sessions_fields = [
{
name: 'Id',
path: 'id',
},
{
name: 'User',
path: 'attributes.user',
},
{
name: 'Host',
path: 'attributes.remote',
},
{
name: 'Connected',
path: 'attributes.connected',
},
{
name: 'Idle',
path: 'attributes.idle',
},
{
name: 'Service',
path: 'relationships.services.data[].id',
}
]
const list_filters_fields = [
{
name: 'Filter',
path: 'id',
},
{
name: 'Service',
path: 'relationships.services.data[].id',
},
{
name: 'Module',
path: 'attributes.module',
}
]
const list_modules_fields = [
{
name: 'Module',
path: 'id',
},
{
name: 'Type',
path: 'attributes.module_type',
},
{
name: 'Version',
path: 'attributes.version',
}
]
const list_threads_fields = [
{
name: 'Id',
path: 'id',
},
{
name: 'Current FDs',
path: 'attributes.stats.current_descriptors',
},
{
name: 'Total FDs',
path: 'attributes.stats.total_descriptors',
},
{
name: 'Load (1s)',
path: 'attributes.stats.load.last_second',
},
{
name: 'Load (1m)',
path: 'attributes.stats.load.last_minute',
},
{
name: 'Load (1h)',
path: 'attributes.stats.load.last_hour',
}
]
const list_users_fields = [
{
name: 'Name',
path: 'id',
},
{
name: 'Type',
path: 'type',
},
{
name: 'Privileges',
path: 'attributes.account',
},
]
const list_commands_fields = [
{
name: 'Module',
path: 'id',
},
{
name: 'Commands',
path: 'attributes.commands[].id',
}
]
exports.command = 'list <command>' exports.command = 'list <command>'
exports.desc = 'List objects' exports.desc = 'List objects'
exports.handler = function() {} exports.handler = function() {}
@ -23,14 +217,6 @@ exports.builder = function(yargs) {
.usage('Usage: list servers') .usage('Usage: list servers')
}, function(argv) { }, function(argv) {
maxctrl(argv, function(host) { maxctrl(argv, function(host) {
fields = [
{'Server': 'id'},
{'Address': 'attributes.parameters.address'},
{'Port': 'attributes.parameters.port'},
{'Connections': 'attributes.statistics.connections'},
{'State': 'attributes.state'},
{'GTID': 'attributes.gtid_current_pos'}
]
// First, get the list of all servers // First, get the list of all servers
return getJson(host, 'servers') return getJson(host, 'servers')
@ -68,8 +254,8 @@ exports.builder = function(yargs) {
} }
}) })
}) })
.then(() => filterResource(res, fields)) .then(() => filterResource(res, list_servers_fields))
.then((res) => rawCollectionAsTable(res, fields)) .then((res) => rawCollectionAsTable(res, list_servers_fields))
}) })
}) })
}) })
@ -78,13 +264,7 @@ exports.builder = function(yargs) {
.usage('Usage: list services') .usage('Usage: list services')
}, function(argv) { }, function(argv) {
maxctrl(argv, function(host) { maxctrl(argv, function(host) {
return getCollection(host, 'services',[ return getCollection(host, 'services', list_services_fields)
{'Service': 'id'},
{'Router': 'attributes.router'},
{'Connections': 'attributes.connections'},
{'Total Connections': 'attributes.total_connections'},
{'Servers': 'relationships.servers.data[].id'}
])
}) })
}) })
.command('listeners <service>', 'List listeners of a service', function(yargs) { .command('listeners <service>', 'List listeners of a service', function(yargs) {
@ -92,12 +272,7 @@ exports.builder = function(yargs) {
.usage('Usage: list listeners <service>') .usage('Usage: list listeners <service>')
}, function(argv) { }, function(argv) {
maxctrl(argv, function(host) { maxctrl(argv, function(host) {
return getSubCollection(host, 'services/' + argv.service, 'attributes.listeners', [ return getSubCollection(host, 'services/' + argv.service, 'attributes.listeners', list_listeners_fields)
{'Name': 'id'},
{'Port': 'attributes.parameters.port'},
{'Host': 'attributes.parameters.host'},
{'State': 'attributes.state'}
])
}) })
}) })
.command('monitors', 'List monitors', function(yargs) { .command('monitors', 'List monitors', function(yargs) {
@ -105,11 +280,7 @@ exports.builder = function(yargs) {
.usage('Usage: list monitors') .usage('Usage: list monitors')
}, function(argv) { }, function(argv) {
maxctrl(argv, function(host) { maxctrl(argv, function(host) {
return getCollection(host, 'monitors', [ return getCollection(host, 'monitors', list_monitors_fields)
{'Monitor': 'id'},
{'State': 'attributes.state'},
{'Servers': 'relationships.servers.data[].id'}
])
}) })
}) })
.command('sessions', 'List sessions', function(yargs) { .command('sessions', 'List sessions', function(yargs) {
@ -117,14 +288,7 @@ exports.builder = function(yargs) {
.usage('Usage: list sessions') .usage('Usage: list sessions')
}, function(argv) { }, function(argv) {
maxctrl(argv, function(host) { maxctrl(argv, function(host) {
return getCollection(host, 'sessions',[ return getCollection(host, 'sessions', list_sessions_fields)
{'Id': 'id'},
{'User': 'attributes.user'},
{'Host': 'attributes.remote'},
{'Connected': 'attributes.connected'},
{'Idle': 'attributes.idle'},
{'Service': 'relationships.services.data[].id'}
])
}) })
}) })
.command('filters', 'List filters', function(yargs) { .command('filters', 'List filters', function(yargs) {
@ -132,11 +296,7 @@ exports.builder = function(yargs) {
.usage('Usage: list filters') .usage('Usage: list filters')
}, function(argv) { }, function(argv) {
maxctrl(argv, function(host) { maxctrl(argv, function(host) {
return getCollection(host, 'filters', [ return getCollection(host, 'filters', list_filters_fields)
{'Filter': 'id'},
{'Service': 'relationships.services.data[].id'},
{'Module': 'attributes.module'}
])
}) })
}) })
.command('modules', 'List loaded modules', function(yargs) { .command('modules', 'List loaded modules', function(yargs) {
@ -144,11 +304,7 @@ exports.builder = function(yargs) {
.usage('Usage: list modules') .usage('Usage: list modules')
}, function(argv) { }, function(argv) {
maxctrl(argv, function(host) { maxctrl(argv, function(host) {
return getCollection(host, 'maxscale/modules',[ return getCollection(host, 'maxscale/modules', list_modules_fields)
{'Module':'id'},
{'Type':'attributes.module_type'},
{'Version': 'attributes.version'}
])
}) })
}) })
.command('threads', 'List threads', function(yargs) { .command('threads', 'List threads', function(yargs) {
@ -156,26 +312,16 @@ exports.builder = function(yargs) {
.usage('Usage: list threads') .usage('Usage: list threads')
}, function(argv) { }, function(argv) {
maxctrl(argv, function(host) { maxctrl(argv, function(host) {
return getCollection(host, 'maxscale/threads', [ return getCollection(host, 'maxscale/threads', list_threads_fields)
{'Id': 'id'},
{'Current FDs': 'attributes.stats.current_descriptors'},
{'Total FDs': 'attributes.stats.total_descriptors'},
{'Load (1s)': 'attributes.stats.load.last_second'},
{'Load (1m)': 'attributes.stats.load.last_minute'},
{'Load (1h)': 'attributes.stats.load.last_hour'}
])
}) })
}) })
.command('users', 'List created users', function(yargs) { .command('users', 'List created users', function(yargs) {
return yargs.epilog('List network the users that can be used to connect to the MaxScale REST API as well as enabled local accounts.') return yargs.epilog('List network the users that can be used to connect to the MaxScale REST API' +
' as well as enabled local accounts.')
.usage('Usage: list users') .usage('Usage: list users')
}, function(argv) { }, function(argv) {
maxctrl(argv, function(host) { maxctrl(argv, function(host) {
return getCollection(host, 'users',[ return getCollection(host, 'users', list_users_fields)
{'Name':'id'},
{'Type':'type'},
{'Privileges':'attributes.account'},
])
}) })
}) })
.command('commands', 'List module commands', function(yargs) { .command('commands', 'List module commands', function(yargs) {
@ -183,10 +329,7 @@ exports.builder = function(yargs) {
.usage('Usage: list commands') .usage('Usage: list commands')
}, function(argv) { }, function(argv) {
maxctrl(argv, function(host) { maxctrl(argv, function(host) {
return getCollection(host, 'maxscale/modules',[ return getCollection(host, 'maxscale/modules', list_commands_fields)
{'Module':'id'},
{'Commands': 'attributes.commands[].id'}
])
}) })
}) })
.usage('Usage: list <command>') .usage('Usage: list <command>')

View File

@ -14,94 +14,360 @@
require('./common.js')() require('./common.js')()
const server_fields = [ const server_fields = [
{'Server': 'id'}, {
{'Address': 'attributes.parameters.address'}, name: 'Server',
{'Port': 'attributes.parameters.port'}, path: 'id',
{'State': 'attributes.state'}, },
{'Last Event': 'attributes.last_event'}, {
{'Triggered At': 'attributes.triggered_at'}, name: 'Address',
{'Services': 'relationships.services.data[].id'}, path: 'attributes.parameters.address',
{'Monitors': 'relationships.monitors.data[].id'}, },
{'Master ID': 'attributes.master_id'}, {
{'Node ID': 'attributes.node_id'}, name: 'Port',
{'Slave Server IDs': 'attributes.slaves'}, path: 'attributes.parameters.port',
{'Statistics': 'attributes.statistics'}, },
{'Parameters': 'attributes.parameters'} {
name: 'State',
path: 'attributes.state',
},
{
name: 'Last Event',
path: 'attributes.last_event',
},
{
name: 'Triggered At',
path: 'attributes.triggered_at',
},
{
name: 'Services',
path: 'relationships.services.data[].id',
},
{
name: 'Monitors',
path: 'relationships.monitors.data[].id',
},
{
name: 'Master ID',
path: 'attributes.master_id',
},
{
name: 'Node ID',
path: 'attributes.node_id',
},
{
name: 'Slave Server IDs',
path: 'attributes.slaves',
},
{
name: 'Statistics',
path: 'attributes.statistics',
},
{
name: 'Parameters',
path: 'attributes.parameters',
}
] ]
const service_fields = [ const service_fields = [
{'Service': 'id'}, {
{'Router': 'attributes.router'}, name: 'Service',
{'State': 'attributes.state'}, path: 'id',
{'Started At': 'attributes.started'}, },
{'Current Connections': 'attributes.connections'}, {
{'Total Connections': 'attributes.total_connections'}, name: 'Router',
{'Servers': 'relationships.servers.data[].id'}, path: 'attributes.router',
{'Parameters': 'attributes.parameters'}, },
{'Router Diagnostics': 'attributes.router_diagnostics'} {
name: 'State',
path: 'attributes.state',
},
{
name: 'Started At',
path: 'attributes.started',
},
{
name: 'Current Connections',
path: 'attributes.connections',
},
{
name: 'Total Connections',
path: 'attributes.total_connections',
},
{
name: 'Servers',
path: 'relationships.servers.data[].id',
},
{
name: 'Parameters',
path: 'attributes.parameters',
},
{
name: 'Router Diagnostics',
path: 'attributes.router_diagnostics',
}
] ]
const monitor_fields = [ const monitor_fields = [
{'Monitor': 'id'}, {
{'State': 'attributes.state'}, name: 'Monitor',
{'Servers': 'relationships.servers.data[].id'}, path: 'id',
{'Parameters': 'attributes.parameters'}, },
{'Monitor Diagnostics': 'attributes.monitor_diagnostics'} {
name: 'State',
path: 'attributes.state',
},
{
name: 'Servers',
path: 'relationships.servers.data[].id',
},
{
name: 'Parameters',
path: 'attributes.parameters',
},
{
name: 'Monitor Diagnostics',
path: 'attributes.monitor_diagnostics',}
] ]
const session_fields = [ const session_fields = [
{'Id': 'id'}, {
{'Service': 'relationships.services.data[].id'}, name: 'Id',
{'State': 'attributes.state'}, path: 'id',
{'User': 'attributes.user'}, },
{'Host': 'attributes.remote'}, {
{'Connected': 'attributes.connected'}, name: 'Service',
{'Idle': 'attributes.idle'}, path: 'relationships.services.data[].id',
{'Connections': 'attributes.connections[].server'}, },
{'Connection IDs': 'attributes.connections[].protocol_diagnostics.connection_id'}, {
{'Queries': 'attributes.queries[].statement'}, name: 'State',
{'Log': 'attributes.log'} path: 'attributes.state',
},
{
name: 'User',
path: 'attributes.user',
},
{
name: 'Host',
path: 'attributes.remote',
},
{
name: 'Connected',
path: 'attributes.connected',
},
{
name: 'Idle',
path: 'attributes.idle',
},
{
name: 'Connections',
path: 'attributes.connections[].server',
},
{
name: 'Connection IDs',
path: 'attributes.connections[].protocol_diagnostics.connection_id',
},
{
name: 'Queries',
path: 'attributes.queries[].statement',
},
{
name: 'Log',
path: 'attributes.log',
}
] ]
const filter_fields = [ const filter_fields = [
{'Filter': 'id'}, {
{'Module': 'attributes.module'}, name: 'Filter',
{'Services': 'relationships.services.data[].id'}, path: 'id',
{'Parameters': 'attributes.parameters'} },
{
name: 'Module',
path: 'attributes.module',
},
{
name: 'Services',
path: 'relationships.services.data[].id',
},
{
name: 'Parameters',
path: 'attributes.parameters',
}
] ]
const module_fields = [ const module_fields = [
{'Module': 'id'}, {
{'Type': 'attributes.module_type'}, name: 'Module',
{'Version': 'attributes.version'}, path: 'id',
{'Maturity': 'attributes.maturity'}, },
{'Description': 'attributes.description'}, {
{'Parameters': 'attributes.parameters'}, name: 'Type',
{'Commands': 'attributes.commands'} path: 'attributes.module_type',
},
{
name: 'Version',
path: 'attributes.version',
},
{
name: 'Maturity',
path: 'attributes.maturity',
},
{
name: 'Description',
path: 'attributes.description',
},
{
name: 'Parameters',
path: 'attributes.parameters',
},
{
name: 'Commands',
path: 'attributes.commands',
}
] ]
const thread_fields = [ const thread_fields = [
{'Id': 'id'}, {
{'Accepts': 'attributes.stats.accepts'}, name: 'Id',
{'Reads': 'attributes.stats.reads'}, path: 'id',
{'Writes': 'attributes.stats.writes'}, },
{'Hangups': 'attributes.stats.hangups'}, {
{'Errors': 'attributes.stats.errors'}, name: 'Accepts',
{'Blocking polls': 'attributes.stats.blocking_polls'}, path: 'attributes.stats.accepts',
{'Avg event queue length': 'attributes.stats.avg_event_queue_length'}, },
{'Max event queue length': 'attributes.stats.max_event_queue_length'}, {
{'Max exec time': 'attributes.stats.max_exec_time'}, name: 'Reads',
{'Max queue time': 'attributes.stats.max_queue_time'}, path: 'attributes.stats.reads',
{'Current FDs': 'attributes.stats.current_descriptors'}, },
{'Total FDs': 'attributes.stats.total_descriptors'}, {
{'Load (1s)': 'attributes.stats.load.last_second'}, name: 'Writes',
{'Load (1m)': 'attributes.stats.load.last_minute'}, path: 'attributes.stats.writes',
{'Load (1h)': 'attributes.stats.load.last_hour'}, },
{'QC cache size': 'attributes.stats.query_classifier_cache.size'}, {
{'QC cache inserts': 'attributes.stats.query_classifier_cache.inserts'}, name: 'Hangups',
{'QC cache hits': 'attributes.stats.query_classifier_cache.hits'}, path: 'attributes.stats.hangups',
{'QC cache misses': 'attributes.stats.query_classifier_cache.misses'}, },
{'QC cache evictions': 'attributes.stats.query_classifier_cache.evictions'}, {
name: 'Errors',
path: 'attributes.stats.errors',
},
{
name: 'Blocking polls',
path: 'attributes.stats.blocking_polls',
},
{
name: 'Avg event queue length',
path: 'attributes.stats.avg_event_queue_length',
},
{
name: 'Max event queue length',
path: 'attributes.stats.max_event_queue_length',
},
{
name: 'Max exec time',
path: 'attributes.stats.max_exec_time',
},
{
name: 'Max queue time',
path: 'attributes.stats.max_queue_time',
},
{
name: 'Current FDs',
path: 'attributes.stats.current_descriptors',
},
{
name: 'Total FDs',
path: 'attributes.stats.total_descriptors',
},
{
name: 'Load (1s)',
path: 'attributes.stats.load.last_second',
},
{
name: 'Load (1m)',
path: 'attributes.stats.load.last_minute',
},
{
name: 'Load (1h)',
path: 'attributes.stats.load.last_hour',
},
{
name: 'QC cache size',
path: 'attributes.stats.query_classifier_cache.size',
},
{
name: 'QC cache inserts',
path: 'attributes.stats.query_classifier_cache.inserts',
},
{
name: 'QC cache hits',
path: 'attributes.stats.query_classifier_cache.hits',
},
{
name: 'QC cache misses',
path: 'attributes.stats.query_classifier_cache.misses',
},
{
name: 'QC cache evictions',
path: 'attributes.stats.query_classifier_cache.evictions',
},
]
const show_maxscale_fields = [
{
name: 'Version',
path: 'attributes.version',
},
{
name: 'Commit',
path: 'attributes.commit',
},
{
name: 'Started At',
path: 'attributes.started_at',
},
{
name: 'Activated At',
path: 'attributes.activated_at',
},
{
name: 'Uptime',
path: 'attributes.uptime',
},
{
name: 'Parameters',
path: 'attributes.parameters',
}
]
const show_logging_fields = [
{
name: 'Current Log File',
path: 'attributes.log_file',
},
{
name: 'Enabled Log Levels',
path: 'attributes.log_priorities',
},
{
name: 'Parameters',
path: 'attributes.parameters',
}
]
const show_commands_fields = [
{
name: 'Command',
path: 'id',
},
{
name: 'Parameters',
path: 'attributes.parameters[].type',
},
{
name: 'Descriptions',
path: 'attributes.parameters[].description',
}
] ]
exports.command = 'show <command>' exports.command = 'show <command>'
@ -228,14 +494,7 @@ exports.builder = function(yargs) {
.usage('Usage: show maxscale') .usage('Usage: show maxscale')
}, function(argv) { }, function(argv) {
maxctrl(argv, function(host) { maxctrl(argv, function(host) {
return getResource(host, 'maxscale', [ return getResource(host, 'maxscale', show_maxscale_fields)
{'Version': 'attributes.version'},
{'Commit': 'attributes.commit'},
{'Started At': 'attributes.started_at'},
{'Activated At': 'attributes.activated_at'},
{'Uptime': 'attributes.uptime'},
{'Parameters': 'attributes.parameters'}
])
}) })
}) })
.command('thread <thread>', 'Show thread', function(yargs) { .command('thread <thread>', 'Show thread', function(yargs) {
@ -260,11 +519,7 @@ exports.builder = function(yargs) {
.usage('Usage: show logging') .usage('Usage: show logging')
}, function(argv) { }, function(argv) {
maxctrl(argv, function(host) { maxctrl(argv, function(host) {
return getResource(host, 'maxscale/logs', [ return getResource(host, 'maxscale/logs', show_logging_fields)
{'Current Log File': 'attributes.log_file'},
{'Enabled Log Levels': 'attributes.log_priorities'},
{'Parameters': 'attributes.parameters'}
])
}) })
}) })
.command('commands <module>', 'Show module commands of a module', function(yargs) { .command('commands <module>', 'Show module commands of a module', function(yargs) {
@ -273,11 +528,8 @@ exports.builder = function(yargs) {
.usage('Usage: show commands <module>') .usage('Usage: show commands <module>')
}, function(argv) { }, function(argv) {
maxctrl(argv, function(host) { maxctrl(argv, function(host) {
return getSubCollection(host, 'maxscale/modules/' + argv.module, 'attributes.commands', [ return getSubCollection(host, 'maxscale/modules/' + argv.module, 'attributes.commands',
{'Command': 'id'}, show_commands_fields)
{'Parameters': 'attributes.parameters[].type'},
{'Descriptions': 'attributes.parameters[].description'}
])
}) })
}) })
.usage('Usage: show <command>') .usage('Usage: show <command>')