Skip to content

Commit 55554fa

Browse files
committed
fix: unknown values in metrics cvedb
1 parent 8f9043a commit 55554fa

File tree

1 file changed

+17
-16
lines changed

1 file changed

+17
-16
lines changed

Diff for: cve_bin_tool/cvedb.py

+17-16
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,12 @@ class CVEDB:
218218
VALUES (?, ?)
219219
""",
220220
}
221+
METRICS = [
222+
(UNKNOWN_METRIC_ID, "UNKNOWN"),
223+
(EPSS_METRIC_ID, "EPSS"),
224+
(CVSS_2_METRIC_ID, "CVSS-2"),
225+
(CVSS_3_METRIC_ID, "CVSS-3"),
226+
]
221227

222228
def __init__(
223229
self,
@@ -368,7 +374,6 @@ def latest_schema(
368374

369375
# getting schema from command
370376
lines = table_schema.split("(")[1].split(",")
371-
372377
table_schema = [x.split("\n")[1].strip().split(" ")[0] for x in lines]
373378
table_schema.pop()
374379

@@ -378,13 +383,16 @@ def latest_schema(
378383
if table_schema == current_schema:
379384
schema_latest = True
380385

381-
# Check for metrics table schema
386+
# Check for metrics table data integrity
382387
if table_name == "metrics":
383-
result = cursor.execute(
384-
"SELECT * FROM metrics WHERE metrics_id=?", (UNKNOWN_METRIC_ID,)
385-
)
386-
if not result.fetchone():
387-
schema_latest = False
388+
for metric_id, metric_name in self.METRICS:
389+
result = cursor.execute(
390+
"SELECT * FROM metrics WHERE metrics_id=? AND metrics_name=?",
391+
(metric_id, metric_name),
392+
)
393+
if not result.fetchone():
394+
schema_latest = False
395+
break # Early exit if any metric is missing
388396

389397
return schema_latest
390398

@@ -640,16 +648,9 @@ def populate_affected(self, affected_data, cursor, data_source):
640648
def populate_metrics(self):
641649
"""Adding data to metric table."""
642650
cursor = self.db_open_and_get_cursor()
643-
# Insert a row without specifying cve_metrics_id
644651
insert_metrics = self.INSERT_QUERIES["insert_metrics"]
645-
data = [
646-
(UNKNOWN_METRIC_ID, "UNKNOWN"),
647-
(EPSS_METRIC_ID, "EPSS"),
648-
(CVSS_2_METRIC_ID, "CVSS-2"),
649-
(CVSS_3_METRIC_ID, "CVSS-3"),
650-
]
651-
# Execute the insert query for each row
652-
for row in data:
652+
# Use the METRICS constant to populate the table
653+
for row in self.METRICS:
653654
cursor.execute(insert_metrics, row)
654655
self.connection.commit()
655656
self.db_close()

0 commit comments

Comments
 (0)