|
| 1 | +# |
| 2 | +# Copyright (c) nexB Inc. and others. All rights reserved. |
| 3 | +# VulnerableCode is a trademark of nexB Inc. |
| 4 | +# SPDX-License-Identifier: Apache-2.0 |
| 5 | +# See http://www.apache.org/licenses/LICENSE-2.0 for the license text. |
| 6 | +# See https://github.com/aboutcode-org/vulnerablecode for support or download. |
| 7 | +# See https://aboutcode.org for more information about nexB OSS projects. |
| 8 | +# |
| 9 | + |
| 10 | + |
| 11 | +from aboutcode.pipeline import LoopProgress |
| 12 | + |
| 13 | +from vulnerabilities.models import AdvisoryV2 |
| 14 | +from vulnerabilities.pipelines import VulnerableCodePipeline |
| 15 | +from vulnerabilities.utils import compute_advisory_content |
| 16 | + |
| 17 | + |
| 18 | +class ComputeAdvisoryContentHash(VulnerableCodePipeline): |
| 19 | + """Compute Advisory Content Hash for Advisory.""" |
| 20 | + |
| 21 | + pipeline_id = "compute_advisory_content_hash_v2" |
| 22 | + |
| 23 | + @classmethod |
| 24 | + def steps(cls): |
| 25 | + return (cls.compute_advisory_content_hash,) |
| 26 | + |
| 27 | + def compute_advisory_content_hash(self): |
| 28 | + """Compute Advisory Content Hash for Advisory.""" |
| 29 | + |
| 30 | + advisories = AdvisoryV2.objects.filter(advisory_content_hash__isnull=True) |
| 31 | + |
| 32 | + advisories_count = advisories.count() |
| 33 | + |
| 34 | + progress = LoopProgress( |
| 35 | + total_iterations=advisories_count, |
| 36 | + logger=self.log, |
| 37 | + progress_step=1, |
| 38 | + ) |
| 39 | + |
| 40 | + to_update = [] |
| 41 | + batch_size = 5000 |
| 42 | + |
| 43 | + for advisory in progress.iter(advisories.iterator(chunk_size=batch_size)): |
| 44 | + advisory.advisory_content_hash = compute_advisory_content(advisory) |
| 45 | + to_update.append(advisory) |
| 46 | + |
| 47 | + if len(to_update) >= batch_size: |
| 48 | + AdvisoryV2.objects.bulk_update( |
| 49 | + to_update, |
| 50 | + ["advisory_content_hash"], |
| 51 | + batch_size=batch_size, |
| 52 | + ) |
| 53 | + to_update.clear() |
| 54 | + |
| 55 | + if to_update: |
| 56 | + AdvisoryV2.objects.bulk_update( |
| 57 | + to_update, |
| 58 | + ["advisory_content_hash"], |
| 59 | + batch_size=batch_size, |
| 60 | + ) |
| 61 | + |
| 62 | + self.log("Finished computing advisory_content_hash") |
0 commit comments