Richard Barnette | d82f378 | 2016-11-15 08:37:30 -0800 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | # |
| 3 | # Copyright 2014 The Chromium OS Authors. All rights reserved. |
| 4 | # Use of this source code is governed by a BSD-style license that can be |
| 5 | # found in the LICENSE file. |
| 6 | # |
| 7 | # count_labels: Print a summary of how many times a particular label |
| 8 | # value occurs in the output of an `atest host list` command. |
| 9 | # |
| 10 | # To find the sizes of the pools assigned to a board: |
| 11 | # atest host list -b board:$BOARD | count_labels -p |
| 12 | # |
| 13 | # To find how many of each board is assigned to a pool: |
| 14 | # atest host list -b pool:$POOL | count_labels -b |
| 15 | |
Richard Barnette | 7efe091 | 2017-04-10 12:17:27 -0700 | [diff] [blame] | 16 | USAGE="usage: $(basename $0)" |
| 17 | HELP="\ |
| 18 | $USAGE -p | -b | -v | -l <label> |
| 19 | $USAGE -h |
| 20 | |
| 21 | Standard input to this command is the output of some variant of |
| 22 | 'atest host list'. The command line option selects a particular |
| 23 | category of label to be counted: |
| 24 | -p: Count \"pool:\" label values. |
| 25 | -b: Count \"board:\" label values. |
Prathmesh Prabhu | d4b9c8f | 2017-12-07 14:52:12 -0800 | [diff] [blame] | 26 | -m: Count \"model:\" label values. |
Richard Barnette | 7efe091 | 2017-04-10 12:17:27 -0700 | [diff] [blame] | 27 | -v: Count \"variant:\" label values. |
| 28 | -l <label>: Count values of labels named \"<label>:\" |
| 29 | |
| 30 | Exactly one label selection option must be supplied; there is no |
| 31 | default, and multiple options aren't allowed. |
| 32 | |
| 33 | The comand reports the counts of the various values of the |
| 34 | selected label. |
| 35 | |
| 36 | Example: |
| 37 | \$ atest host list -b board:daisy_skate | count_labels -p |
| 38 | 9 bvt |
| 39 | 14 suites |
| 40 | 1 wificell |
| 41 | " |
| 42 | |
| 43 | |
| 44 | usage() { |
| 45 | if [ $# -ne 0 ]; then |
| 46 | echo "$@" >&2 |
| 47 | echo >&2 |
| 48 | fi |
| 49 | echo "$HELP" >&2 |
| 50 | exit 1 |
| 51 | } |
| 52 | |
| 53 | COUNT=0 |
| 54 | ERR=0 |
Prathmesh Prabhu | d4b9c8f | 2017-12-07 14:52:12 -0800 | [diff] [blame] | 55 | while getopts 'hpbmvl:' flag; do |
Richard Barnette | d82f378 | 2016-11-15 08:37:30 -0800 | [diff] [blame] | 56 | case $flag in |
| 57 | p) LABEL=pool ;; |
| 58 | b) LABEL=board ;; |
Prathmesh Prabhu | d4b9c8f | 2017-12-07 14:52:12 -0800 | [diff] [blame] | 59 | m) LABEL=model ;; |
Richard Barnette | d82f378 | 2016-11-15 08:37:30 -0800 | [diff] [blame] | 60 | v) LABEL=variant ;; |
Richard Barnette | 31a50ad | 2017-04-10 11:08:33 -0700 | [diff] [blame] | 61 | l) LABEL=$OPTARG ;; |
Richard Barnette | 7efe091 | 2017-04-10 12:17:27 -0700 | [diff] [blame] | 62 | h|\?) ERR=1 ;; |
Richard Barnette | d82f378 | 2016-11-15 08:37:30 -0800 | [diff] [blame] | 63 | esac |
Richard Barnette | 7efe091 | 2017-04-10 12:17:27 -0700 | [diff] [blame] | 64 | COUNT=$(( COUNT + 1 )) |
Richard Barnette | d82f378 | 2016-11-15 08:37:30 -0800 | [diff] [blame] | 65 | done |
| 66 | |
Richard Barnette | 7efe091 | 2017-04-10 12:17:27 -0700 | [diff] [blame] | 67 | if [ $COUNT -ne 1 ]; then |
| 68 | usage "Must have exactly one label-specifying option" >&2 |
| 69 | fi |
| 70 | |
| 71 | if [ $ERR -ne 0 ]; then |
| 72 | usage |
| 73 | fi |
| 74 | |
Richard Barnette | e684e4f | 2018-08-28 13:59:49 -0700 | [diff] [blame] | 75 | sed -e "/$LABEL:/ !d" -e "s=.*$LABEL:\([^,]*\).*=\1=" | sort | |
| 76 | uniq -c | awk '{sum += $1; print} END {printf "%7d total\n", sum}' |