commit 086d95180179b2d5e1433fec718a2f8d192ee90a Author: Arturo Filastò art@fuffa.org Date: Fri Dec 4 17:42:57 2015 +0100
Start implementing support for reporting using JSON
Working on #444 --- data/ooniprobe.conf.sample | 1 + ooni/reporter.py | 36 +++++++++++++++++++++++++----------- 2 files changed, 26 insertions(+), 11 deletions(-)
diff --git a/data/ooniprobe.conf.sample b/data/ooniprobe.conf.sample index 7aaacd7..a7d8d29 100644 --- a/data/ooniprobe.conf.sample +++ b/data/ooniprobe.conf.sample @@ -22,6 +22,7 @@ reports: # This is a prefix for each packet capture file (.pcap) per test: pcap: null collector: null + format: yaml advanced: geoip_data_dir: /usr/share/GeoIP debug: false diff --git a/ooni/reporter.py b/ooni/reporter.py index 42787fb..a8272f5 100644 --- a/ooni/reporter.py +++ b/ooni/reporter.py @@ -257,22 +257,36 @@ class OONIBReporter(OReporter): if not re.match(regexp, self.collectorAddress): raise errors.InvalidOONIBCollectorAddress
+ def serializeEntry(self, entry): + if config.report.format == "json": + if isinstance(entry, Measurement): + report_entry = entry.testInstance.report + elif isinstance(entry, Failure): + report_entry = entry.value + elif isinstance(entry, dict): + report_entry = entry + report_entry["record_type"] = "entry" + report_entry["report_id"] = self.reportID + content = json.dumps(report_entry, ensure_ascii=True) + "\n" + else: + content = '---\n' + if isinstance(entry, Measurement): + content += safe_dump(entry.testInstance.report) + elif isinstance(entry, Failure): + content += entry.value + elif isinstance(entry, dict): + content += safe_dump(entry) + content += '...\n' + return content + @defer.inlineCallbacks def writeReportEntry(self, entry): log.debug("Writing report with OONIB reporter") - content = '---\n' - if isinstance(entry, Measurement): - content += safe_dump(entry.testInstance.report) - elif isinstance(entry, Failure): - content += entry.value - elif isinstance(entry, dict): - content += safe_dump(entry) - content += '...\n'
- url = self.collectorAddress + '/report' + url = self.collectorAddress + '/report/' + self.reportID
- request = {'report_id': self.reportID, - 'content': content} + request = {'format': config.report.format, + 'content': self.serializeEntry(entry)}
log.debug("Updating report with id %s (%s)" % (self.reportID, url)) request_json = json.dumps(request)
tor-commits@lists.torproject.org