Skip to content

Commit 8cee434

Browse files
committed
Add progress bar to NVDImporter
Signed-off-by: Harsh Mishra <hmisraji07@gmail.com>
1 parent d21d2c1 commit 8cee434

3 files changed

Lines changed: 21 additions & 6 deletions

File tree

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,3 +120,4 @@ drf-spectacular==0.24.2
120120
coreapi==2.3.3
121121
coreschema==0.0.4
122122
itypes==1.2.0
123+
progress==1.6

vulnerabilities/importers/nvd.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import gzip
1111
import json
1212
from datetime import date
13+
from progress.bar import ChargingBar
1314

1415
import attr
1516
import requests
@@ -78,10 +79,18 @@ def fetch_cve_data_1_1(starting_year=2002):
7879
year since ``starting_year`` defaulting to 2002.
7980
"""
8081
current_year = date.today().year
82+
progress_bar_for_records_fetched = ChargingBar("\tRecords fetched", max=(current_year-starting_year)+1)
83+
progress_bar_for_records_fetched.start()
8184
# NVD json feeds start from 2002.
82-
for year in range(starting_year, current_year + 1):
83-
download_url = f"https://nvd.nist.gov/feeds/json/cve/1.1/nvdcve-1.1-{year}.json.gz"
84-
yield year, fetch(url=download_url)
85+
try:
86+
for year in range(starting_year, current_year + 1):
87+
try:
88+
download_url = f"https://nvd.nist.gov/feeds/json/cve/1.1/nvdcve-1.1-{year}.json.gz"
89+
yield year, fetch(url=download_url)
90+
finally:
91+
progress_bar_for_records_fetched.next()
92+
finally:
93+
progress_bar_for_records_fetched.finish()
8594

8695

8796
def to_advisories(cve_data):

vulnerabilities/management/commands/import.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@
88
#
99
import traceback
1010

11+
import progress
1112
from django.core.management.base import BaseCommand
1213
from django.core.management.base import CommandError
13-
14+
from progress.bar import IncrementalBar
1415
from vulnerabilities.import_runner import ImportRunner
1516
from vulnerabilities.importers import IMPORTERS_REGISTRY
1617

@@ -54,9 +55,10 @@ def import_data(self, importers):
5455
names for the importers.
5556
"""
5657
failed_importers = []
57-
58+
progress_bar_for_import = IncrementalBar("Fetching Data from Databases", max=len(importers))
59+
progress_bar_for_import.start()
5860
for importer in importers:
59-
self.stdout.write(f"Importing data using {importer.qualified_name}")
61+
self.stdout.write(f"\nImporting data using {importer.qualified_name}")
6062
try:
6163
ImportRunner(importer).run()
6264
self.stdout.write(
@@ -72,6 +74,9 @@ def import_data(self, importers):
7274
f"Failed to run importer {importer.qualified_name}. Continuing..."
7375
)
7476
)
77+
finally:
78+
progress_bar_for_import.next()
79+
progress_bar_for_import.finish()
7580

7681
if failed_importers:
7782
raise CommandError(f"{len(failed_importers)} failed!: {','.join(failed_importers)}")

0 commit comments

Comments
 (0)