blob: 3fec80c8786c51ec2e08b5ecd971ee84d80f5e40 [file] [log] [blame]
Richard Barnetted82f3782016-11-15 08:37:30 -08001#!/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 Barnette7efe0912017-04-10 12:17:27 -070016USAGE="usage: $(basename $0)"
17HELP="\
18$USAGE -p | -b | -v | -l <label>
19$USAGE -h
20
21Standard input to this command is the output of some variant of
22'atest host list'. The command line option selects a particular
23category of label to be counted:
24 -p: Count \"pool:\" label values.
25 -b: Count \"board:\" label values.
Prathmesh Prabhud4b9c8f2017-12-07 14:52:12 -080026 -m: Count \"model:\" label values.
Richard Barnette7efe0912017-04-10 12:17:27 -070027 -v: Count \"variant:\" label values.
28 -l <label>: Count values of labels named \"<label>:\"
29
30Exactly one label selection option must be supplied; there is no
31default, and multiple options aren't allowed.
32
33The comand reports the counts of the various values of the
34selected label.
35
36Example:
37 \$ atest host list -b board:daisy_skate | count_labels -p
38 9 bvt
39 14 suites
40 1 wificell
41"
42
43
44usage() {
45 if [ $# -ne 0 ]; then
46 echo "$@" >&2
47 echo >&2
48 fi
49 echo "$HELP" >&2
50 exit 1
51}
52
53COUNT=0
54ERR=0
Prathmesh Prabhud4b9c8f2017-12-07 14:52:12 -080055while getopts 'hpbmvl:' flag; do
Richard Barnetted82f3782016-11-15 08:37:30 -080056 case $flag in
57 p) LABEL=pool ;;
58 b) LABEL=board ;;
Prathmesh Prabhud4b9c8f2017-12-07 14:52:12 -080059 m) LABEL=model ;;
Richard Barnetted82f3782016-11-15 08:37:30 -080060 v) LABEL=variant ;;
Richard Barnette31a50ad2017-04-10 11:08:33 -070061 l) LABEL=$OPTARG ;;
Richard Barnette7efe0912017-04-10 12:17:27 -070062 h|\?) ERR=1 ;;
Richard Barnetted82f3782016-11-15 08:37:30 -080063 esac
Richard Barnette7efe0912017-04-10 12:17:27 -070064 COUNT=$(( COUNT + 1 ))
Richard Barnetted82f3782016-11-15 08:37:30 -080065done
66
Richard Barnette7efe0912017-04-10 12:17:27 -070067if [ $COUNT -ne 1 ]; then
68 usage "Must have exactly one label-specifying option" >&2
69fi
70
71if [ $ERR -ne 0 ]; then
72 usage
73fi
74
Richard Barnettee684e4f2018-08-28 13:59:49 -070075sed -e "/$LABEL:/ !d" -e "s=.*$LABEL:\([^,]*\).*=\1=" | sort |
76 uniq -c | awk '{sum += $1; print} END {printf "%7d total\n", sum}'