devtools/bqerrors.sh: script to display error counts by date

This script makes it easy to see our progress in reducing errors
over time from the command line.

Change-Id: I8855925ecfd4370762de7d839823c9546b9603c9
Reviewed-on: https://go-review.googlesource.com/c/pkgsite-metrics/+/473167
Reviewed-by: Zvonimir Pavlinovic <zpavlinovic@google.com>
Run-TryBot: Jonathan Amsterdam <jba@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
diff --git a/devtools/bqerrors.sh b/devtools/bqerrors.sh
new file mode 100755
index 0000000..d06d083
--- /dev/null
+++ b/devtools/bqerrors.sh
@@ -0,0 +1,41 @@
+#!/bin/bash
+
+# Copyright 2023 The Go Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style
+# license that can be found in the LICENSE file.
+
+# List counts of errors by date in BigQuery tables.
+
+set -e
+
+source devtools/lib.sh || { echo "Are you at repo root?"; exit 1; }
+
+usage() {
+  die "usage: $0 DATASET"
+}
+
+bq_error_query() {
+  local -r table=$1
+  local q="
+        select date(created_at) as date, error_category, count(*) as count
+        from $table
+        group by 1, 2
+        order by 1 desc"
+  bq query $q
+}
+
+main() {
+  local dataset=$1
+  if [[ $dataset == '' ]]; then
+    usage
+  fi
+  local -r project=$(tfvar prod_project)
+  if [[ $project = '' ]]; then
+    die "missing TF_VAR_prod_project"
+  fi
+
+  bq_error_query $project.$dataset.vulncheck
+}
+
+
+main "$@"