Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(263)

Side by Side Diff: telemetry/telemetry/internal/results/csv_output_formatter.py

Issue 3003163002: Add CsvOutputFormatter to Telemetry. (Closed)
Patch Set: Created 3 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | telemetry/telemetry/internal/results/csv_output_formatter_unittest.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 # Copyright 2017 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
4
5 import collections
6 import csv
7 import json
8 import os
9 import tempfile
10
11 from telemetry.internal.results import output_formatter
12 from tracing.value import histograms_to_csv
13
14
15 def _ReadCsv(text):
16 dicts = []
17 header = None
18 for row in csv.reader(text.split('\n')):
19 if header is None:
20 header = row
21 elif row:
22 dicts.append(collections.OrderedDict(zip(header, row)))
23 return dicts
24
25
26 def _WriteCsv(dicts, fileobj):
27 header = []
28 for d in dicts:
29 for k in d.iterkeys():
30 if k not in header:
31 header.append(k)
32 rows = [header]
33 for d in dicts:
34 rows.append([d.get(k, '') for k in header])
35 csv.writer(fileobj).writerows(rows)
36
37
38 class CsvOutputFormatter(output_formatter.OutputFormatter):
39 def __init__(self, output_stream, reset_results=False):
40 super(CsvOutputFormatter, self).__init__(output_stream)
41 self._reset_results = reset_results
42
43 def Format(self, page_test_results):
44 histograms = page_test_results.AsHistogramDicts()
45 file_descriptor, json_path = tempfile.mkstemp()
46 os.close(file_descriptor)
47 json.dump(histograms, file(json_path, 'w'))
48 vinn_result = histograms_to_csv.HistogramsToCsv(json_path)
49 dicts = _ReadCsv(vinn_result.stdout)
50
51 self._output_stream.seek(0)
52 if not self._reset_results:
53 dicts += _ReadCsv(self._output_stream.read())
54 self._output_stream.seek(0)
55 _WriteCsv(dicts, self._output_stream)
56 self._output_stream.truncate()
OLDNEW
« no previous file with comments | « no previous file | telemetry/telemetry/internal/results/csv_output_formatter_unittest.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698