133259e44c8229f70ffe0cf3bb5ca9375c4feb2f9John Reck# Copyright 2016 The Chromium Authors. All rights reserved. 233259e44c8229f70ffe0cf3bb5ca9375c4feb2f9John Reck# Use of this source code is governed by a BSD-style license that can be 333259e44c8229f70ffe0cf3bb5ca9375c4feb2f9John Reck# found in the LICENSE file. 433259e44c8229f70ffe0cf3bb5ca9375c4feb2f9John Reck 533259e44c8229f70ffe0cf3bb5ca9375c4feb2f9John Reckimport json 633259e44c8229f70ffe0cf3bb5ca9375c4feb2f9John Reckimport logging 733259e44c8229f70ffe0cf3bb5ca9375c4feb2f9John Reck 833259e44c8229f70ffe0cf3bb5ca9375c4feb2f9John Reckfrom telemetry.internal.results import output_formatter 933259e44c8229f70ffe0cf3bb5ca9375c4feb2f9John Reck 1033259e44c8229f70ffe0cf3bb5ca9375c4feb2f9John Reck 1133259e44c8229f70ffe0cf3bb5ca9375c4feb2f9John Reckclass HistogramSetJsonOutputFormatter(output_formatter.OutputFormatter): 1233259e44c8229f70ffe0cf3bb5ca9375c4feb2f9John Reck def __init__(self, output_stream, metadata, reset_results): 1333259e44c8229f70ffe0cf3bb5ca9375c4feb2f9John Reck super(HistogramSetJsonOutputFormatter, self).__init__(output_stream) 1433259e44c8229f70ffe0cf3bb5ca9375c4feb2f9John Reck self._metadata = metadata 1533259e44c8229f70ffe0cf3bb5ca9375c4feb2f9John Reck self._reset_results = reset_results 1633259e44c8229f70ffe0cf3bb5ca9375c4feb2f9John Reck 1733259e44c8229f70ffe0cf3bb5ca9375c4feb2f9John Reck def Format(self, page_test_results): 1833259e44c8229f70ffe0cf3bb5ca9375c4feb2f9John Reck histograms = page_test_results.AsHistogramDicts(self._metadata) 1933259e44c8229f70ffe0cf3bb5ca9375c4feb2f9John Reck self._output_stream.seek(0) 2033259e44c8229f70ffe0cf3bb5ca9375c4feb2f9John Reck if not self._reset_results: 2133259e44c8229f70ffe0cf3bb5ca9375c4feb2f9John Reck existing = self._output_stream.read() 2233259e44c8229f70ffe0cf3bb5ca9375c4feb2f9John Reck self._output_stream.seek(0) 2333259e44c8229f70ffe0cf3bb5ca9375c4feb2f9John Reck if existing: 2433259e44c8229f70ffe0cf3bb5ca9375c4feb2f9John Reck try: 2533259e44c8229f70ffe0cf3bb5ca9375c4feb2f9John Reck histograms += json.loads(existing) 2633259e44c8229f70ffe0cf3bb5ca9375c4feb2f9John Reck except ValueError: 2733259e44c8229f70ffe0cf3bb5ca9375c4feb2f9John Reck logging.warn('Found existing histograms json but failed to parse it.') 2833259e44c8229f70ffe0cf3bb5ca9375c4feb2f9John Reck json.dump(histograms, self._output_stream) 29