blob: 22557718269503821d9e0e8883693d780ddf16d5 [file] [log] [blame]
#!/bin/bash
# Copyright 2019 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
# A helper to query build_log data contained in internal analysis_service
# events.
# Make sure we have prod access so we don't fail without getting any work done.
prodcertstatus 1>/dev/null 2>/dev/null || prodaccess
when="last3days"
string_data=""
count_data=""
limit=30
field_limit=80
verbose=0
only_stdout=0
print_usage() {
cat <<HELP_USAGE
Usage: $0 -c|--count <search_string> OR -s|--string <search_string>
[-w|-when] <when_string>
[-l|--limit] <limit_string> [--field_limit <field_limit>]
[-v|--verbose] [--usage|-u|--help|-h]
If -c is specified, count the occurrences of the search string.
If -s is specified, display the matching occurences based on the other
arguments (-l, --field_limit, etc).
search_string : What to search for in build_cmd logs.
when_string : Which dremel table, such as last7days, last1days, etc.
(default=last3days)
limit_string : Limits numbers of rows to this value (default=30).
field_limit : Show <field_limit> characters of stdout starting with
<search_string> (default=80).
only_stdout : Show only stdout and date, not buildId, stepName, etc.
verbose : Show dremel query before executing it.
HELP_USAGE
exit 0
}
while (( "$#" )); do
case "$1" in
-s|--string)
string_data=$2
shift 2
;;
-c|--count)
count_data=$2
shift 2
;;
-w|--when)
when=$2
shift 2
;;
-l|--limit)
limit=$2
shift 2
;;
--field_limit)
field_limit=$2
shift 2
;;
--only_stdout)
only_stdout=1
shift 1
;;
-v|--verbose)
verbose=1
shift
;;
--usage|-u|--help|-h)
print_usage
shift
;;
*)
echo "Unknown arg: $1"
print_usage
shift
esac
done
if [[ ! -z ${string_data} ]] && [[ ! -z ${count_data} ]]; then
echo "Specifying both -s and -c is not allowed."
print_usage
fi
# If a count was requested, build and execute the query. This code path is
# separate because it is much simpler than search queries.
if [[ ! -z ${count_data} ]]; then
query="SELECT count(stdout) AS Count FROM
chromeos_ci_eng.analysis_event_log.${when}
WHERE stdout LIKE \"%${count_data}%\";"
if [ "${verbose}" -eq 1 ]; then
echo "QUERY: ${query}"
echo "Executing..."
fi
echo "${query}" | dremel --min_completion_ratio 1
exit 0
fi
if [[ -z "${string_data}" ]]
then
echo "No search string specified. -s|--string <search_string> is required."
print_usage
fi
if [ -z "${field_limit}" ]
then
select_field="stdout"
else
select_field="SUBSTR(stdout, STRPOS(stdout, \"${string_data}\"),"
select_field="${select_field} ${field_limit})"
fi
if [ "${only_stdout}" -eq 1 ]; then
query="SELECT ${select_field} AS Stdout,
DATETIME(TIMESTAMP_SECONDS(request_time.seconds)) AS DateTime"
else
query="SELECT ${select_field} AS Stdout,
CONCAT('https://ci.chromium.org/b/', CAST(build_id AS STRING)) as BuildId,step_name,
DATETIME(TIMESTAMP_SECONDS(request_time.seconds)) AS DateTime"
fi
query="$query FROM chromeos_ci_eng.analysis_event_log.${when}
WHERE stdout LIKE \"%${string_data}%\" ORDER BY DateTime DESC LIMIT ${limit};"
if [ "${verbose}" -eq 1 ]; then
echo "QUERY: ${query}"
echo "Executing..."
fi
echo "${query}" | dremel --min_completion_ratio 1