|
8 | 8 | from typing import IO
|
9 | 9 |
|
10 | 10 | from cve_bin_tool.cvedb import CVEDB
|
| 11 | +from cve_bin_tool.log import LOGGER |
11 | 12 | from cve_bin_tool.util import CVEData, ProductInfo, VersionInfo
|
12 | 13 | from cve_bin_tool.version import VERSION
|
13 | 14 |
|
@@ -45,20 +46,50 @@ def db_entries_count():
|
45 | 46 | """
|
46 | 47 | Retrieves the count of CVE entries from the database grouped by data source.
|
47 | 48 |
|
| 49 | + The function fetches all data sources from the database and ensures that |
| 50 | + schema-required sources (NVD, OSV, GAD, REDHAT) are always included in the result, |
| 51 | + even if they don't exist in the database yet, while also including all other |
| 52 | + sources with counts initialized to 0. |
| 53 | +
|
48 | 54 | Returns:
|
49 | 55 | dict: A dictionary containing the count of CVE entries for each data source.
|
50 | 56 | """
|
51 | 57 | instance = CVEDB()
|
52 | 58 | cursor = instance.db_open_and_get_cursor()
|
53 |
| - cve_entries_check = "SELECT data_source, COUNT(*) as number FROM cve_severity GROUP BY data_source ORDER BY number DESC" |
54 |
| - cursor.execute(cve_entries_check) |
55 | 59 | data_entries = {}
|
56 |
| - rows = cursor.fetchall() |
57 |
| - for row in rows: |
58 |
| - source = row[0] |
59 |
| - entries = row[1] |
60 |
| - data_entries[source] = entries |
61 |
| - instance.db_close() |
| 60 | + |
| 61 | + # Always include the schema-required sources |
| 62 | + required_sources = ["NVD", "OSV", "GAD", "REDHAT"] |
| 63 | + for source in required_sources: |
| 64 | + data_entries[source] = 0 |
| 65 | + |
| 66 | + try: |
| 67 | + # Get all possible sources from the database |
| 68 | + sources_query = "SELECT DISTINCT data_source FROM cve_severity" |
| 69 | + cursor.execute(sources_query) |
| 70 | + all_sources = [row[0] for row in cursor.fetchall()] |
| 71 | + |
| 72 | + # Initialize counts to 0 for all sources not in required_sources |
| 73 | + for source in all_sources: |
| 74 | + if source not in data_entries: |
| 75 | + data_entries[source] = 0 |
| 76 | + |
| 77 | + # Get actual counts |
| 78 | + cve_entries_check = ( |
| 79 | + "SELECT data_source, COUNT(*) as number FROM cve_severity " |
| 80 | + "GROUP BY data_source ORDER BY number DESC" |
| 81 | + ) |
| 82 | + cursor.execute(cve_entries_check) |
| 83 | + rows = cursor.fetchall() |
| 84 | + for row in rows: |
| 85 | + source = row[0] |
| 86 | + entries = row[1] |
| 87 | + data_entries[source] = entries |
| 88 | + |
| 89 | + except Exception as e: |
| 90 | + LOGGER.error(f"Error in db_entries_count or cve_severity in db: {e}") |
| 91 | + finally: |
| 92 | + instance.db_close() |
62 | 93 | return data_entries
|
63 | 94 |
|
64 | 95 |
|
|
0 commit comments