#compdef isi_statistics
# Copyright C 2008-2009 by Isilon Systems, Inc. All rights not expressly
# granted herein are reserved.
# Created in the United States of America.
# 
# This digital media is protected by U.S. and international copyright and
# intellectual property laws. Unless otherwise specified, all information
# and screens appearing as part of this digital medium, including
# software, services, documents, text, images, icons, and logos design;
# the selection, assembly, arrangement, and design thereof; and the code
# that enables its presentation, are the sole property of Isilon Systems,
# Inc.
# 
# Except as otherwise required by applicable law, no part of any
# copyrighted material may be reproduced, modified, translated,
# distributed, published, or transmitted by any means, electronic or
# mechanical, for any purpose, without the prior written consent of Isilon
# Systems. 
#  
# Isilon and Isilon Systems are registered trademarks of Isilon Systems,
# Inc.  Legion is a trademark of Isilon Systems, Inc.  All other
# trademarks and registered trademarks are the property of their
# respective holders.

_isi_statistics () {
  if (( CURRENT == 2 )); then
    _tags values && {compadd $@ protocol client system query history heat pstat list describe drive}
    _arguments \
      '--help[Print usage help and exit]'
  else
    local cmd=${words[2]}
    (( CURRENT-- ))
    shift words
    if [[ $cmd == protocol ]]; then
      curcontext="${curcontext%:*:*}:isi-statistics-protocol:"
      _isi_statistics_protocol
    elif [[ $cmd == client ]]; then
      curcontext="${curcontext%:*:*}:isi-statistics-client:"
      _isi_statistics_client
    elif [[ $cmd == 'heat' ]]; then
        curcontext="${curcontext%:*:*}:isi-statistics-heat:"
        _isi_statistics_heat
    elif [[ $cmd == system ]]; then
      curcontext="${curcontext%:*:*}:isi-statistics-system:"
      _isi_statistics_system
    elif [[ $cmd == query ]]; then
      curcontext="${curcontext%:*:*}:isi-statistics-query:"
      _isi_statistics_query
    elif [[ $cmd == 'history' ]]; then
        curcontext="${curcontext%:*:*}:isi-statistics-history:"
        _isi_statistics_history
    elif [[ $cmd == pstat ]]; then
      curcontext="${curcontext%:*:*}:isi-statistics-pstat:"
      _isi_statistics_pstat
    elif [[ $cmd == list ]]; then
      curcontext="${curcontext%:*:*}:isi-statistics-list:"
      _isi_statistics_list
    elif [[ $cmd == describe ]]; then
      curcontext="${curcontext%:*:*}:isi-statistics-describe:"
      _isi_statistics_describe
    elif [[ $cmd == drive ]]; then
      curcontext="${curcontext%:*:*}:isi-statistics-drive:"
      _isi_statistics_drive
    fi
  fi
}

(( $+functions[_isi_statistics_protocol] )) ||
_isi_statistics_protocol() {
  _arguments -s : \
		{-h,--help}'[print help and exit]' \
		{-c,--csv}'[Output data as comma-separated value list]' \
		{-C,--csvstring}'=[Output csv-style data with specified spacing string]' \
		'--noconversion[Do not dynamically convert units]' \
		'--noheader[Do not output column headers]' \
		{-t,--top}'[Enable top-style display]' \
		{-i,--interval}'=[Report data every NUM seconds]' \
		{-r,--repeat}'=[Run NUM times before quitting]' \
		{-d,--degraded}'[Continue to report if some nodes do not respond]' \
		{-o,--timeout}'=[Timeout remote commands after NUM seconds]' \
		{-n,--nodes}'=[specify nodes]:node_list:_isi_statistics_nodes' \
		'--protocols=[specify protocols]:protocol_list:_isi_statistics_protos' \
		'--classes=[specifiy classes]:class_list:_isi_statistics_classes' \
		'--operations=[specifiy operations]:operation_list:_isi_statistics_pops' \
		'--orderby=[sort by columns]:order_list:_isi_statistics_porder' \
		'--total[aggregate by filter]' \
		'--totalby=[specifiy aggregation]:field_list:_isi_statistics_pfields' \
		'--nooutput=[specify columns excluded from output]:column_list:_isi_statistics_pcols' \
		'--output=[specify output columns]:column_list:_isi_statistics_pcols' \
		'--long[show all columns]' \
		'--zero[do not suppress zero results]'
}

(( $+functions[_isi_statistics_client] )) ||
_isi_statistics_client() {
  _arguments -s : \
		{-h,--help}'[print help and exit]' \
		{-c,--csv}'[Output data as comma-separated value list]' \
		{-C,--csvstring}'=[Output csv-style data with specified spacing string]' \
		'--noconversion[Do not dynamically convert units]' \
		'--noheader[Do not output column headers]' \
		{-t,--top}'[Enable top-style display]' \
		{-i,--interval}'=[Report data every NUM seconds]' \
		{-r,--repeat}'=[Run NUM times before quitting]' \
		{-d,--degraded}'[Continue to report if some nodes do not respond]' \
		{-o,--timeout}'=[Timeout remote commands after NUM seconds]' \
		{-n,--nodes}'=[specify nodes]:node_list:_isi_statistics_nodes' \
		'--protocols=[specify protocols]:protocol_list:_isi_statistics_protos' \
		'--classes=[specifiy classes]:class_list:_isi_statistics_classes' \
		'--orderby=[sort by columns]:order_list:_isi_statistics_corder' \
		'--total[aggregate by filter]' \
		'--totalby=[specifiy aggregation]:field_list:_isi_statistics_cfields' \
		'--nooutput=[specify columns excluded from output]:column_list:_isi_statistics_ccols' \
		'--output=[specify output columns]:column_list:_isi_statistics_ccols' \
		'--long[show all columns]' \
		'--zero[do not suppress zero results]' \
		'--wide[print resolved names with a wider column width]' \
		'--numeric[display numeric host and client identifiers]' \
		'--local_addrs=[specify local addresses]' \
		'--local_names=[specify local names]' \
		'--remote_addrs=[specify remote addresses]' \
		'--remote_names=[specify remote names]' \
		'--user_ids=[specify user ids]' \
		'--user_names=[specify user names]'
}

(( $+functions[_isi_statistics_heat] )) ||
_isi_statistics_heat() {
	_arguments -s : \
		{-h,--help}'[print help and exit]' \
		{-c,--csv}'[Output data as comma-separated value list]' \
		{-C,--csvstring}'=[Output csv-style data with specified spacing string]' \
		'--noconversion[Do not dynamically convert units]' \
		'--noheader[Do not output column headers]' \
		{-t,--top}'[Enable top-style display]' \
		{-i,--interval}'=[Report data every NUM seconds]' \
		{-r,--repeat}'=[Run NUM times before quitting]' \
		{-d,--degraded}'[Continue to report if some nodes do not respond]' \
		{-o,--timeout}'=[Timeout remote commands after NUM seconds]' \
		{-n,--nodes}'=[specify nodes]:node_list:_isi_statistics_nodes' \
		'--events=[specify events]:event_list:_isi_statistics_events' \
		'--classes=[specifiy classes]:class_list:_isi_statistics_classes' \
		'--orderby=[sort by columns]:order_list:_isi_statistics_horder' \
		'--totalby=[specifiy aggregation]:field_list:_isi_statistics_hfields' \
		'--output=[specify output columns]:column_list:_isi_statistics_hcols' \
		'--maxpath=[maximum length of path to lookup in filesystem]' \
		'--pathdepth=[squash paths to this directory depth]' \
		'--limit=[only show this many entries after totalling and ordering]' \
		'--long[show all columns]'
}

(( $+functions[_isi_statistics_system] )) ||
_isi_statistics_system() {
  _arguments -s : \
		{-h,--help}'[print help and exit]' \
		{-c,--csv}'[Output data as comma-separated value list]' \
		{-C,--csvstring}'=[Output csv-style data with specified spacing string]' \
		'--noconversion[Do not dynamically convert units]' \
		'--noheader[Do not output column headers]' \
		{-t,--top}'[Enable top-style display]' \
		{-i,--interval}'=[Report data every NUM seconds]' \
		{-r,--repeat}'=[Run NUM times before quitting]' \
		{-d,--degraded}'[Continue to report if some nodes do not respond]' \
		{-o,--timeout}'=[Timeout remote commands after NUM seconds]' \
		'--running=[Aggregate statistics over NUM iterations of tool]' \
		'--nodes[Output summary information for individual nodes]' \
		'--oprates[Output rate data in operations per second]' \
		'--timestamp[Display a column containing the POSIX time stamp]'
}

(( $+functions[_isi_statistics_drive] )) ||
_isi_statistics_drive() {
  _arguments -s : \
		{-h,--help}'[print help and exit]' \
		{-c,--csv}'[Output data as comma-separated value list]' \
		{-C,--csvstring}'=[Output csv-style data with specified spacing string]' \
		'--noconversion[Do not dynamically convert units]' \
		'--noheader[Do not output column headers]' \
		{-t,--top}'[Enable top-style display]' \
		{-i,--interval}'=[Report data every NUM seconds]' \
		{-r,--repeat}'=[Run NUM times before quitting]' \
		{-d,--degraded}'[Continue to report if some nodes do not respond]' \
		{-o,--timeout}'=[Timeout remote commands after NUM seconds]' \
		\*{-n,--nodes}'=[specify nodes]:nodes list:_isi_statistics_nodes' \
		'--long[show all columns]' \
		'--lnum[List drives by lnum]' \
		'--timestamp[Display a column containing the POSIX time stamp]' \
		'--type=[Specify drive type(s)]:type list:_isi_statistics_dtype' \
		'--orderby=[sort by columns]:order_list:_isi_statistics_dorder' \
		'--bay[List drives by bay]'
}

(( $+functions[_isi_statistics_query] )) ||
_isi_statistics_query() {
  _arguments -s : \
		{-h,--help}'[print help and exit]' \
		{-c,--csv}'[Output data as comma-separated value list]' \
		{-C,--csvstring}'=[Output csv-style data with specified spacing string]' \
		'--noconversion[Do not dynamically convert units]' \
		'--noheader[Do not output column headers]' \
		{-t,--top}'[Enable top-style display]' \
		{-i,--interval}'=[Report data every NUM seconds]' \
		{-r,--repeat}'=[Run NUM times before quitting]' \
		{-d,--degraded}'[Continue to report if some nodes do not respond]' \
		{-o,--timeout}'=[Timeout remote commands after NUM seconds]' \
		'--nofooter[Do not output footer row with aggregation]' \
		\*{-n,--nodes}'=[specify nodes]:nodes list:_isi_statistics_nodes' \
		\*{-s,--stats}'=[specify stats]:stats list:_isi_statistics_qstats'
}

(( $+functions[_isi_statistics_history] )) ||
_isi_statistics_history() {
	_arguments -s : \
		{-h,--help}'[print help and exit]' \
		{-c,--csv}'[Output data as comma-separated value list]' \
		{-C,--csvstring}'=[Output csv-style data with specified spacing string]' \
		'--noconversion[Do not dynamically convert units]' \
		'--noheader[Do not output column headers]' \
		{-t,--top}'[Enable top-style display]' \
		{-i,--interval}'=[Report data every NUM seconds]' \
		{-r,--repeat}'=[Run NUM times before quitting]' \
		{-d,--degraded}'[Continue to report if some nodes do not respond]' \
		{-o,--timeout}'=[Timeout remote commands after NUM seconds]' \
		'--nofooter[Do not output footer row with aggregation]' \
		\*{-n,--nodes}'=[specify nodes]:nodes list:_isi_statistics_nodes' \
		\*{-s,--stats}'=[specify stats]:stats list:_isi_statistics_qstats' \
		'--begin=[Begin time in epoch seconds]' \
		'--end=[End time in epoch seconds]' \
		'--resolution=[Minimum interval between series data points in seconds]' \
		{-1,--onecolumn}'[Do not produce a table for multiple stats]' \
		{-F,--formattime}'[Format stat times instead of using epoch seconds]' \
		'--zero[Show grid rows with no valid series points]'
}

(( $+functions[_isi_statistics_pstat] )) ||
_isi_statistics_pstat() {
  _arguments -s : \
		{-h,--help}'[print help and exit]' \
		{-c,--csv}'[Output data as comma-separated value list]' \
		{-C,--csvstring}'=[Output csv-style data with specified spacing string]' \
		'--noconversion[Do not dynamically convert units]' \
		'--noheader[Do not output column headers]' \
		{-t,--top}'[Enable top-style display]' \
		{-i,--interval}'=[Report data every NUM seconds]' \
		{-r,--repeat}'=[Run NUM times before quitting]' \
		{-d,--degraded}'[Continue to report if some nodes do not respond]' \
		{-o,--timeout}'=[Timeout remote commands after NUM seconds]' \
		'--protocol=[specify protocol]:protocol_list:_isi_statistics_protos_single'
}

(( $+functions[_isi_statistics_describe] )) ||
_isi_statistics_describe() {
  _arguments -s : \
		\*{-s,--stats}'=[specify stats]:stats list:_isi_statistics_qstats'
}

# XXX Tab completion currently tab-completes multiple sub-sub-commands after
# the 'isi statistics list' sub-command.  This could possibly be fixed
# with some 'CURRENT == ' positionality checking, but it might be more
# trouble than its worth.
(( $+functions[_isi_statistics_list] )) ||
_isi_statistics_list() {
  _tags values && {compadd $@ all classes nodes nooutput operations orderby output protocols stats totalby}
  _arguments \
    '--help[Print usage help and exit]'
}

# we don't want to cache this one in case the cluster changes
(( $+functions[_isi_statistics_nodes] )) ||
_isi_statistics_nodes() {
	_values -s , 'node filter' \
	$(isi statistics list nodes) \
	'local' \
	'all'
}

(( $+functions[_isi_statistics_protos] )) ||
_isi_statistics_protos() {
	if (( ! $+_isi_statistics_protos_values )); then
		typeset -g _isi_statistics_protos_values
		_isi_statistics_protos_values=(
			$(isi statistics list protocols) \
			'all' \
			'internal' \
			'external'
		)
	fi
	_values -s , 'protocol filter' $_isi_statistics_protos_values
}

(( $+functions[_isi_statistics_protos_single] )) ||
_isi_statistics_protos_single() {
	if (( ! $+_isi_statistics_protos_single_values )); then
		typeset -g _isi_statistics_protos_single_values
		_isi_statistics_protos_single_values=(
			$(isi statistics list protocols)
		)
	fi
	_values 'protocol' $_isi_statistics_protos_single_values
}

(( $+functions[_isi_statistics_classes] )) ||
_isi_statistics_classes() {
	if (( ! $+_isi_statistics_classes_values )); then
		typeset -ga _isi_statistics_classes_values
		_isi_statistics_classes_values=(
			$(isi statistics list classes) \
			'all'
		)
	fi
	_values -s , 'classes filter' $_isi_statistics_classes_values
}

(( $+functions[_isi_statistics_events] )) ||
_isi_statistics_events() {
	if (( ! $+_isi_statistics_events_values )); then
		typeset -ga _isi_statistics_events_values
		_isi_statistics_events_values=(
			$(isi statistics list events) \
			'all'
		)
	fi
	_values -s , 'events filter' $_isi_statistics_events_values
}

(( $+functions[_isi_statistics_pops] )) ||
_isi_statistics_pops() {
	if (( ! $+_isi_statistics_pops_values )); then
		typeset -ga _isi_statistics_pops_values
		_isi_statistics_pops_values=(
			$(isi statistics list operations) \
			'all'
		)
	fi
	_values -s , 'operations filter' $_isi_statistics_pops_values
}

(( $+functions[_isi_statistics_pfields] )) ||
_isi_statistics_pfields() {
	if (( ! $+_isi_statistics_pfields_values )); then
		typeset -ga _isi_statistics_pfields_values
		_isi_statistics_pfields_values=(
			$(isi statistics list totalby)
		)
	fi
	_values -s , 'aggregation buckets' $_isi_statistics_pfields_values
}

(( $+functions[_isi_statistics_porder] )) ||
_isi_statistics_porder() {
	if (( ! $+_isi_statistics_porder_values )); then
		typeset -ga _isi_statistics_porder_values
		_isi_statistics_porder_values=(
			$(isi statistics list orderby)
		)
	fi
	_values -s , 'ordering buckets' $_isi_statistics_porder_values
}

(( $+functions[_isi_statistics_pcols] )) ||
_isi_statistics_pcols() {
	if (( ! $+_isi_statistics_pcols_values )); then
		typeset -ga _isi_statistics_pcols_values
		_isi_statistics_pcols_values=(
			$(isi statistics list output)
		)
	fi
	_values -s , 'column identifiers' $_isi_statistics_pcols_values
}

(( $+functions[_isi_statistics_cfields] )) ||
_isi_statistics_cfields() {
	if (( ! $+_isi_statistics_cfields_values )); then
		typeset -ga _isi_statistics_cfields_values
		_isi_statistics_cfields_values=(
			$(isi statistics list totalby --client)
		)
	fi
	_values -s , 'aggregation buckets' $_isi_statistics_cfields_values
}

(( $+functions[_isi_statistics_corder] )) ||
_isi_statistics_corder() {
	if (( ! $+_isi_statistics_corder_values )); then
		typeset -ga _isi_statistics_corder_values
		_isi_statistics_corder_values=(
			$(isi statistics list orderby --client)
		)
	fi
	_values -s , 'ordering buckets' $_isi_statistics_corder_values
}

(( $+functions[_isi_statistics_ccols] )) ||
_isi_statistics_ccols() {
	if (( ! $+_isi_statistics_ccols_values )); then
		typeset -ga _isi_statistics_ccols_values
		_isi_statistics_ccols_values=(
			$(isi statistics list output --client)
		)
	fi
	_values -s , 'column identifiers' $_isi_statistics_ccols_values
}

(( $+functions[_isi_statistics_hfields] )) ||
_isi_statistics_hfields() {
	if (( ! $+_isi_statistics_hfields_values )); then
		typeset -ga _isi_statistics_hfields_values
		_isi_statistics_hfields_values=(
			$(isi statistics list totalby --heat)
		)
	fi
	_values -s , 'aggregation buckets' $_isi_statistics_hfields_values
}

(( $+functions[_isi_statistics_horder] )) ||
_isi_statistics_horder() {
	if (( ! $+_isi_statistics_horder_values )); then
		typeset -ga _isi_statistics_horder_values
		_isi_statistics_horder_values=(
			$(isi statistics list orderby --heat)
		)
	fi
	_values -s , 'ordering buckets' $_isi_statistics_horder_values
}

(( $+functions[_isi_statistics_hcols] )) ||
_isi_statistics_hcols() {
	if (( ! $+_isi_statistics_hcols_values )); then
		typeset -ga _isi_statistics_hcols_values
		_isi_statistics_hcols_values=(
			$(isi statistics list output --heat)
		)
	fi
	_values -s , 'column identifiers' $_isi_statistics_hcols_values
}

(( $+functions[_isi_statistics_qstats] )) ||
_isi_statistics_qstats() {
	if (( ! $+_isi_statistics_qstats_values )); then
		typeset -ga _isi_statistics_qstats_values
		_isi_statistics_qstats_values=(
			$(isi statistics list stats)
		)
	fi
	_values -s , 'stats names' $_isi_statistics_qstats_values
}

(( $+functions[_isi_statistics_dtype] )) ||
_isi_statistics_dtype() {
	if (( ! $+_isi_statistics_drive_types )); then
		typeset -g _isi_statistics_drive_types
		_isi_statistics_drive_types=(
			'all' \
			'sata' \
			'sas' \
			'ssd'
		)
	fi
	_values -s , 'drive types' $_isi_statistics_drive_types
}

(( $+functions[_isi_statistics_dorder] )) ||
_isi_statistics_dorder() {
	if (( ! $+_isi_statistics_dorder_values )); then
		typeset -ga _isi_statistics_dorder_values
		_isi_statistics_dorder_values=(
			$(isi statistics list orderby --drive)
		)
	fi
	_values -s , 'ordering buckets' $_isi_statistics_dorder_values
}

_isi_statistics "$@"
