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
"""Compute pilot coverage of the exact two-reference DID intersection."""
def coverage(report, reference_hosts, pilot_hosts):
hosts = [row['host'] for row in report['collectors']]
if len(set(hosts)) != len(hosts) or len(reference_hosts) != 2:
raise ValueError('expected unique sources and two references')
refs = {hosts.index(host) for host in reference_hosts}
pilots = {host: hosts.index(host) for host in pilot_hosts}
denominator = 0
numerators = dict.fromkeys(pilots, 0)
seen = set()
for pattern in report['source_memberships']:
sources = pattern['sources']
key = frozenset(sources)
count = pattern['did_count']
if (not key or len(key) != len(sources) or key in seen or
any(type(index) is not int or not 0 <= index < len(hosts) for index in key) or
type(count) is not int or count <= 0):
raise ValueError('invalid source membership pattern')
seen.add(key)
if refs <= key:
denominator += count
for host, index in pilots.items():
if index in key:
numerators[host] += count
return {'reference_intersection_dids': denominator, 'pilots': {
host: {'reference_intersection_seen': count,
'coverage': count / denominator if denominator else None}
for host, count in numerators.items()}}