Skip to content

Commit daa0739

Browse files
committed
fix for world-map visualization
1 parent 76777cb commit daa0739

File tree

3 files changed

+31
-14
lines changed

3 files changed

+31
-14
lines changed

visualization/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Use:
1111
2. Generate the JSON file
1212

1313
```bash
14-
python3 world_map_data.py -f ~/Downloads/risk_ip4_med.json -c ~/Downloads/country_asn.mmdb
14+
python3 world_map_data.py -c ~/Downloads/country_asn.mmdb -4 ~/Downloads/risk_ip4_med.json -6 ~/Downloads/risk_ip6_med.json
1515
```
1616

1717
3. For testing - run the minimal python3 webserver to enable JS to access the JSON file:

visualization/world_map_data.py

+30-13
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323
'name': 'Reported abuse originating from this country',
2424
'format': '{0}',
2525
'thousandSeparator': '.',
26-
'thresholdMax': 2_000,
27-
'thresholdMin': 500
26+
'thresholdMax': 2,
27+
'thresholdMin': 1
2828
},
2929
'bot': {
3030
'name': 'Reported bots',
@@ -46,6 +46,14 @@
4646
'name': 'Reported crawlers',
4747
'format': '{0}',
4848
},
49+
'ip4': {
50+
'name': 'Reports from IPv4',
51+
'format': '{0}',
52+
},
53+
'ip6': {
54+
'name': 'Reports from IPv6',
55+
'format': '{0}',
56+
},
4957
},
5058
'values': {},
5159
}
@@ -54,19 +62,27 @@
5462

5563
# pylint: disable=E0606
5664
def main():
57-
with open(args.file, 'r', encoding='utf-8') as f:
58-
raw = json_loads(f.read())
65+
with open(args.file_ip4, 'r', encoding='utf-8') as f:
66+
raw4 = json_loads(f.read())
67+
68+
with open(args.file_ip6, 'r', encoding='utf-8') as f:
69+
raw6 = json_loads(f.read())
5970

6071
with mmdb_database(args.country_db) as m:
61-
for ips in raw.values():
62-
for ip, reports in ips.items():
63-
ip_md = m.get(ip)
64-
if ip_md['country'] not in DATA['data']['values']:
65-
DATA['data']['values'][ip_md['country']] = {c: 0 for c in CATEGORIES}
72+
for ipv, ipv_db in {'ip4': raw4, 'ip6': raw6}.items():
73+
for ips in ipv_db.values():
74+
for ip, reports in ips.items():
75+
ip_md = m.get(ip)
76+
if ip_md['country'] not in DATA['data']['values']:
77+
DATA['data']['values'][ip_md['country']] = {c: 0 for c in CATEGORIES}
78+
DATA['data']['values'][ip_md['country']]['ip4'] = 0
79+
DATA['data']['values'][ip_md['country']]['ip6'] = 0
80+
81+
for c in CATEGORIES:
82+
if c in reports:
83+
DATA['data']['values'][ip_md['country']][c] += reports[c]
6684

67-
for c in CATEGORIES:
68-
if c in reports:
69-
DATA['data']['values'][ip_md['country']][c] += reports[c]
85+
DATA['data']['values'][ip_md['country']][ipv] += reports['all']
7086

7187
DATA['data']['values'] = dict(sorted(DATA['data']['values'].items(), key=lambda item: item[1]['all'], reverse=True))
7288
with open(SRC_PATH / 'world_map.json', 'w', encoding='utf-8') as f:
@@ -78,7 +94,8 @@ def main():
7894

7995
if __name__ == '__main__':
8096
parser = ArgumentParser()
81-
parser.add_argument('-f', '--file', help='JSON file to parse', default='risk_ip4_med.json')
97+
parser.add_argument('-4', '--file-ip4', help='IPv4 JSON file to parse', default='risk_ip4_med.json')
98+
parser.add_argument('-6', '--file-ip6', help='IPv6 JSON file to parse', default='risk_ip6_med.json')
8299
parser.add_argument('-c', '--country-db', help='MMDB country data to use (IPInfo)', default='country_asn.mmdb')
83100
args = parser.parse_args()
84101
main()

visualization/world_map_example.webp

208 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)