query_build_logs: Enable date range searches with --before, --after.

This allows restricting the search space using before, after, or both.

BUG=chromium:1012460
TEST=manual

Change-Id: Ic3dcf9a9dc69db1b98012f519d7d0c78c60871cf
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/chromite/+/2363340
Tested-by: Michael Mortensen <mmortensen@google.com>
Commit-Queue: Michael Mortensen <mmortensen@google.com>
Reviewed-by: Mike Frysinger <vapier@chromium.org>
diff --git a/contrib/query_build_logs b/contrib/query_build_logs
index 08b959c..24c1bd0 100755
--- a/contrib/query_build_logs
+++ b/contrib/query_build_logs
@@ -21,6 +21,8 @@
 string_data_array=()
 not_string_data_array=()
 sort_order="DESC"
+before_datestring=""
+after_datestring=""
 
 print_usage() {
   cat  <<HELP_USAGE
@@ -33,6 +35,8 @@
      [--asc]
      [--es|--ef]
      [--join <string1> <string2>]
+     [--before <datestring>]
+     [--after <datestring>]
      [-v|--verbose] [--usage|-u|--help|-h]
 
      If -c is specified, count the occurrences of the search string.
@@ -47,6 +51,9 @@
        step_execution_result are considered.
      If --ef is specified, then only steps with a failing (non-NULL)
        step_execution_result are considered.
+     The datestring for --before and --after is 'YYYY-MM-DD'.  Either
+       --before or --after (or both) can be specified, each with its
+       own datestring.
      Multiple -s arguments can be supplied, which allows an AND query,
      so that
        query_build_logs -s "String1" -s "String2" -s "String3"
@@ -97,6 +104,14 @@
       field_limit=$2
       shift 2
       ;;
+    --before)
+      before_datestring=$2
+      shift 2
+      ;;
+    --after)
+      after_datestring=$2
+      shift 2
+      ;;
     --only_stdout)
       only_stdout=1
       shift 1
@@ -215,6 +230,17 @@
   else
     query="$query LIKE \"%${join_2}%\""
   fi
+
+  if [[ -n ${before_datestring} ]]; then
+    query+=" AND DATETIME(TIMESTAMP_SECONDS(request_time.seconds))"
+    query+=" < '${before_datestring}'"
+  fi
+
+  if [[ -n ${after_datestring} ]]; then
+    query+=" AND DATETIME(TIMESTAMP_SECONDS(request_time.seconds))"
+    query+=" > '${after_datestring}'"
+  fi
+
   query="$query ORDER BY DateTime ${sort_order} LIMIT ${limit};"
   if [ "${verbose}" -eq 1 ]; then
     echo "QUERY: ${query}"
@@ -291,6 +317,16 @@
   query="${query} AND step_execution_result IS NULL"
 fi
 
+if [[ -n ${before_datestring} ]]; then
+  query+=" AND DATETIME(TIMESTAMP_SECONDS(request_time.seconds))"
+  query+=" < '${before_datestring}'"
+fi
+
+if [[ -n ${after_datestring} ]]; then
+  query+=" AND DATETIME(TIMESTAMP_SECONDS(request_time.seconds))"
+  query+=" > '${after_datestring}'"
+fi
+
 query="$query ORDER BY DateTime ${sort_order} LIMIT ${limit};"
 
 if [ "${verbose}" -eq 1 ]; then