Junior — Senior
Counting vulnerabilities for 2023 and 2024
livecode
Task condition
It is necessary to determine how many vulnerabilities have an identifier starting with CVE‑2023 and how many with CVE‑2024, fetching data page by page from the API. Below is an example solution in Python, which sequentially requests pages, aggregates the number of vulnerabilities for each device, and calculates the total sum.
from collections import defaultdict
import requests, json
def aggregate_page(data_page, counters):
page_total = 0
for entry in data_page:
dev_id = entry["device_id"]
vuln_count = len(entry["vulnerabilities"])
counters[dev_id] += vuln_count
page_total += vuln_count
return page_total
counters = defaultdict(int)
overall_total = 0
page_idx = 1
while True:
api_url = f"{base_url}?page_number={page_idx}"
response = requests.get(api_url, auth=(username, password)).json()
overall_total += aggregate_page(response["results"], counters)
if response["more"] is False:
break
page_idx += 1
print(json.dumps(dict(counters), indent=4))
print(overall_total)