Skip to content

Commit 18256e7

Browse files
committed
Update for better user experience while importing
Signed-off-by: Abhra303 <chakrabortyabhradeep79@gmail.com>
1 parent 326422e commit 18256e7

2 files changed

Lines changed: 19 additions & 7 deletions

File tree

vulnerabilities/import_runner.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,13 @@
3030
from typing import Set
3131
from typing import Tuple
3232
from typing import Optional
33+
from tqdm import tqdm
3334

3435
import packageurl
3536
from django.db import DataError
3637
from django.core import serializers
3738

39+
from django.core.management.base import BaseCommand
3840
from vulnerabilities import models
3941
from vulnerabilities.data_source import Advisory, DataSource
4042
from vulnerabilities.data_source import PackageURL
@@ -78,7 +80,7 @@ def __init__(self, importer: models.Importer, batch_size: int):
7880
self.importer = importer
7981
self.batch_size = batch_size
8082

81-
def run(self, cutoff_date: datetime.datetime = None) -> None:
83+
def run(self, cutoff_date: datetime.datetime = None, command: BaseCommand = None) -> None:
8284
"""
8385
Create a data source for the given importer and store the data retrieved in the database.
8486
@@ -92,7 +94,7 @@ def run(self, cutoff_date: datetime.datetime = None) -> None:
9294
logger.info(f"Starting import for {self.importer.name}.")
9395
data_source = self.importer.make_data_source(self.batch_size, cutoff_date=cutoff_date)
9496
with data_source:
95-
process_advisories(data_source)
97+
process_advisories(data_source, command=command)
9698
self.importer.last_run = datetime.datetime.now(tz=datetime.timezone.utc)
9799
self.importer.data_source_cfg = dataclasses.asdict(data_source.config)
98100
self.importer.save()
@@ -113,13 +115,17 @@ def get_vuln_pkg_refs(vulnerability, package):
113115
)
114116

115117

116-
def process_advisories(data_source: DataSource) -> None:
118+
def process_advisories(data_source: DataSource, command: BaseCommand = None) -> None:
117119
bulk_create_vuln_pkg_refs = set()
118120
# Treat updated_advisories and added_advisories as same. Eventually
119121
# we want to refactor all data sources to provide advisories via a
120122
# single method.
121-
advisory_batches = chain(data_source.updated_advisories(), data_source.added_advisories())
122-
for batch in advisory_batches:
123+
if command:
124+
command.stdout.write("Collecting the data from the source ..")
125+
advisory_batches = list(chain(data_source.updated_advisories(), data_source.added_advisories()))
126+
if command:
127+
command.stdout.write("installing the data on database ...")
128+
for batch in tqdm(advisory_batches):
123129
for advisory in batch:
124130
try:
125131
vuln, vuln_created = _get_or_create_vulnerability(advisory)

vulnerabilities/management/commands/import.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,10 @@ def _import_data(self, importers, cutoff_date):
104104
failed_importers = []
105105

106106
for importer in importers:
107-
self.stdout.write(f"Importing data from {importer.name}")
107+
self.stdout.write(f"Importing data from {importer.name}...")
108108
batch_size = int(getattr(self, "batch_size", 10))
109109
try:
110-
ImportRunner(importer, batch_size).run(cutoff_date=cutoff_date)
110+
ImportRunner(importer, batch_size).run(cutoff_date=cutoff_date, command=self)
111111
self.stdout.write(
112112
self.style.SUCCESS(f"Successfully imported data from {importer.name}")
113113
)
@@ -119,3 +119,9 @@ def _import_data(self, importers, cutoff_date):
119119
)
120120
if failed_importers:
121121
raise CommandError(f"{len(failed_importers)} failed!: {','.join(failed_importers)}")
122+
else:
123+
self.stdout.write(
124+
self.style.SUCCESS(
125+
"\n\nAll done! vulnerability data has been successfully fetched."
126+
)
127+
)

0 commit comments

Comments
 (0)