-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdiffsum.py
executable file
·173 lines (151 loc) · 5.29 KB
/
diffsum.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#!/usr/bin/env python3
import argparse
import logging
import sys
from typing import ( # noqa
Any,
Callable,
Iterable,
Iterator,
ItemsView,
List,
Set,
Sequence,
Tuple,
Union,
)
import dns.message
from respdiff import cli
from respdiff.database import LMDB
from respdiff.dataformat import DiffReport, Summary
from respdiff.dnsviz import DnsvizGrok
from respdiff.query import (
convert_queries,
get_printable_queries_format,
get_query_iterator,
qwire_to_msgid_qname_qtype,
)
def parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser(
description="create a summary report from gathered data stored in LMDB "
"and JSON datafile"
)
cli.add_arg_envdir(parser)
cli.add_arg_config(parser)
cli.add_arg_datafile(parser)
cli.add_arg_limit(parser)
cli.add_arg_stats_filename(parser, default="")
cli.add_arg_dnsviz(parser, default="")
parser.add_argument(
"--without-dnsviz-errors",
action="store_true",
help="omit domains that have any errors in DNSViz results",
)
parser.add_argument(
"--without-diffrepro",
action="store_true",
help="omit reproducibility data from summary",
)
parser.add_argument(
"--without-ref-unstable",
action="store_true",
help="omit unstable reference queries from summary",
)
parser.add_argument(
"--without-ref-failing",
action="store_true",
help="omit failing reference queries from summary",
)
return parser.parse_args()
def check_args(args: argparse.Namespace, report: DiffReport):
if (
args.without_ref_unstable or args.without_ref_failing
) and not args.stats_filename:
logging.critical("Statistics file must be provided as a reference.")
sys.exit(1)
if not report.total_answers:
logging.error("No answers in DB!")
sys.exit(1)
if report.target_disagreements is None:
logging.error(
"JSON report is missing diff data! Did you forget to run msgdiff?"
)
sys.exit(1)
def main():
cli.setup_logging()
args = parse_args()
datafile = cli.get_datafile(args)
report = DiffReport.from_json(datafile)
field_weights = args.cfg["report"]["field_weights"]
check_args(args, report)
ignore_qids = set()
if args.without_ref_unstable or args.without_ref_failing:
try:
stats = cli.read_stats(args.stats_filename)
except ValueError as exc:
logging.critical(str(exc))
sys.exit(1)
if args.without_ref_unstable:
ignore_qids.update(stats.queries.unstable)
if args.without_ref_failing:
ignore_qids.update(stats.queries.failing)
report = DiffReport.from_json(datafile)
report.summary = Summary.from_report(
report,
field_weights,
without_diffrepro=args.without_diffrepro,
ignore_qids=ignore_qids,
)
# dnsviz filter: by domain -> need to iterate over disagreements to get QIDs
if args.without_dnsviz_errors:
try:
dnsviz_grok = DnsvizGrok.from_json(args.dnsviz)
except (FileNotFoundError, RuntimeError) as exc:
logging.critical("Failed to load dnsviz data: %s", exc)
sys.exit(1)
error_domains = dnsviz_grok.error_domains()
with LMDB(args.envdir, readonly=True) as lmdb:
lmdb.open_db(LMDB.QUERIES)
# match domain, add QID to ignore
for qid, wire in get_query_iterator(lmdb, report.summary.keys()):
msg = dns.message.from_wire(wire)
if msg.question:
if any(
msg.question[0].name.is_subdomain(name)
for name in error_domains
):
ignore_qids.add(qid)
report.summary = Summary.from_report(
report,
field_weights,
without_diffrepro=args.without_diffrepro,
ignore_qids=ignore_qids,
)
cli.print_global_stats(report)
cli.print_differences_stats(report)
if report.summary: # when there are any differences to report
field_counters = report.summary.get_field_counters()
cli.print_fields_overview(field_counters, len(report.summary))
for field in field_weights:
if field in report.summary.field_labels:
cli.print_field_mismatch_stats(
field, field_counters[field], len(report.summary)
)
# query details
with LMDB(args.envdir, readonly=True) as lmdb:
lmdb.open_db(LMDB.QUERIES)
for field in field_weights:
if field in report.summary.field_labels:
for mismatch, qids in report.summary.get_field_mismatches(field):
queries = convert_queries(
get_query_iterator(lmdb, qids), qwire_to_msgid_qname_qtype
)
cli.print_mismatch_queries(
field,
mismatch,
get_printable_queries_format(queries),
args.limit,
)
report.export_json(datafile)
if __name__ == "__main__":
main()