Skip to content

Commit

Permalink
fix for world-map visualization
Browse files Browse the repository at this point in the history
  • Loading branch information
superstes committed Oct 18, 2024
1 parent 76777cb commit daa0739
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 14 deletions.
2 changes: 1 addition & 1 deletion visualization/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Use:
2. Generate the JSON file

```bash
python3 world_map_data.py -f ~/Downloads/risk_ip4_med.json -c ~/Downloads/country_asn.mmdb
python3 world_map_data.py -c ~/Downloads/country_asn.mmdb -4 ~/Downloads/risk_ip4_med.json -6 ~/Downloads/risk_ip6_med.json
```

3. For testing - run the minimal python3 webserver to enable JS to access the JSON file:
Expand Down
43 changes: 30 additions & 13 deletions visualization/world_map_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
'name': 'Reported abuse originating from this country',
'format': '{0}',
'thousandSeparator': '.',
'thresholdMax': 2_000,
'thresholdMin': 500
'thresholdMax': 2,
'thresholdMin': 1
},
'bot': {
'name': 'Reported bots',
Expand All @@ -46,6 +46,14 @@
'name': 'Reported crawlers',
'format': '{0}',
},
'ip4': {
'name': 'Reports from IPv4',
'format': '{0}',
},
'ip6': {
'name': 'Reports from IPv6',
'format': '{0}',
},
},
'values': {},
}
Expand All @@ -54,19 +62,27 @@

# pylint: disable=E0606
def main():
with open(args.file, 'r', encoding='utf-8') as f:
raw = json_loads(f.read())
with open(args.file_ip4, 'r', encoding='utf-8') as f:
raw4 = json_loads(f.read())

with open(args.file_ip6, 'r', encoding='utf-8') as f:
raw6 = json_loads(f.read())

with mmdb_database(args.country_db) as m:
for ips in raw.values():
for ip, reports in ips.items():
ip_md = m.get(ip)
if ip_md['country'] not in DATA['data']['values']:
DATA['data']['values'][ip_md['country']] = {c: 0 for c in CATEGORIES}
for ipv, ipv_db in {'ip4': raw4, 'ip6': raw6}.items():
for ips in ipv_db.values():
for ip, reports in ips.items():
ip_md = m.get(ip)
if ip_md['country'] not in DATA['data']['values']:
DATA['data']['values'][ip_md['country']] = {c: 0 for c in CATEGORIES}
DATA['data']['values'][ip_md['country']]['ip4'] = 0
DATA['data']['values'][ip_md['country']]['ip6'] = 0

for c in CATEGORIES:
if c in reports:
DATA['data']['values'][ip_md['country']][c] += reports[c]

for c in CATEGORIES:
if c in reports:
DATA['data']['values'][ip_md['country']][c] += reports[c]
DATA['data']['values'][ip_md['country']][ipv] += reports['all']

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

if __name__ == '__main__':
parser = ArgumentParser()
parser.add_argument('-f', '--file', help='JSON file to parse', default='risk_ip4_med.json')
parser.add_argument('-4', '--file-ip4', help='IPv4 JSON file to parse', default='risk_ip4_med.json')
parser.add_argument('-6', '--file-ip6', help='IPv6 JSON file to parse', default='risk_ip6_med.json')
parser.add_argument('-c', '--country-db', help='MMDB country data to use (IPInfo)', default='country_asn.mmdb')
args = parser.parse_args()
main()
Binary file modified visualization/world_map_example.webp
Binary file not shown.

0 comments on commit daa0739

Please sign in to comment.