Partial revert: "Hand protos directly to histograms and fix summary."

This partially reverts commit 7427fc6560b0cdf67912863162c72cfde2ed0cd6.

Turns out proto importing is broken on the catapult side. A fix is
coming. Until then I'll have to use the old JSON way.

Bug: chromium:1029452
Change-Id: Ib5c43d721fe6c4e2639a0d518f4fa69b42b6c388
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/170230
Reviewed-by: Mirko Bonadei <mbonadei@webrtc.org>
Commit-Queue: Patrik Höglund <phoglund@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#30761}
diff --git a/tools_webrtc/perf/webrtc_dashboard_upload.py b/tools_webrtc/perf/webrtc_dashboard_upload.py
index a5c1c4c..7fc0f1a 100644
--- a/tools_webrtc/perf/webrtc_dashboard_upload.py
+++ b/tools_webrtc/perf/webrtc_dashboard_upload.py
@@ -46,6 +46,8 @@
 from tracing.value.diagnostics import generic_set
 from tracing.value.diagnostics import reserved_infos
 
+from google.protobuf import json_format
+
 
 def _GenerateOauthToken():
   args = ['luci-auth', 'token']
@@ -92,10 +94,25 @@
                                       'tracing', 'proto')
   sys.path.insert(0, histogram_proto_path)
 
-  hs = histogram_set.HistogramSet()
-  with options.input_results_file as f:
-    hs.ImportProto(f.read())
+  # TODO(https://crbug.com/1029452): Get rid of this import hack once we can
+  # just hand the contents of input_results_file straight to the histogram set.
+  try:
+    import histogram_pb2
+  except ImportError:
+    raise ImportError('Could not find histogram_pb2. You need to build the '
+                      'webrtc_dashboard_upload target before invoking this '
+                      'script. Expected to find '
+                      'histogram_pb2 in %s.' % histogram_proto_path)
 
+  with options.input_results_file as f:
+    histograms = histogram_pb2.HistogramSet()
+    histograms.ParseFromString(f.read())
+
+  # TODO(https://crbug.com/1029452): Don't convert to JSON as a middle step once
+  # there is a proto de-serializer ready in catapult.
+  json_data = json.loads(json_format.MessageToJson(histograms))
+  hs = histogram_set.HistogramSet()
+  hs.ImportDicts(json_data)
   return hs
 
 
@@ -114,6 +131,21 @@
         k.name, generic_set.GenericSet([v]))
 
 
+# TODO(https://crbug.com/1029452): Remove this once
+# https://chromium-review.googlesource.com/c/catapult/+/2094312 lands.
+def _HackSummaryOptions(histograms):
+  for histogram in histograms:
+    histogram.CustomizeSummaryOptions({
+      'avg': False,
+      'std': False,
+      'count': False,
+      'sum': False,
+      'min': False,
+      'max': False,
+      'nans': False,
+    })
+
+
 def _DumpOutput(histograms, output_file):
   with output_file:
     json.dump(histograms.AsDicts(), output_file, indent=4)
@@ -140,7 +172,7 @@
                       help='URL to the build page for this build.')
   parser.add_argument('--dashboard-url', required=True,
                       help='Which dashboard to use.')
-  parser.add_argument('--input-results-file', type=argparse.FileType('rb'),
+  parser.add_argument('--input-results-file', type=argparse.FileType(),
                       required=True,
                       help='A JSON file with output from WebRTC tests.')
   parser.add_argument('--output-json-file', type=argparse.FileType('w'),
@@ -150,21 +182,6 @@
   return parser
 
 
-# TODO(https://crbug.com/1029452): Remove this once
-# https://chromium-review.googlesource.com/c/catapult/+/2094312 lands.
-def _HackSummaryOptions(histograms):
-  for histogram in histograms:
-    histogram.CustomizeSummaryOptions({
-      'avg': False,
-      'std': False,
-      'count': False,
-      'sum': False,
-      'min': False,
-      'max': False,
-      'nans': False,
-    })
-
-
 def main(args):
   parser = _CreateParser()
   options = parser.parse_args(args)