From 5e39bb03e5464ad6eb4244461518854957f8ea93 Mon Sep 17 00:00:00 2001 From: Jyrki Keisala Date: Mon, 23 Dec 2024 11:01:08 +0200 Subject: [PATCH] refactor(ui): Remove obsolete code With new API endpoints, item count functions are no longer needed in the UI code, so remove them. Also, as `VulnerabilityRating` is finally exposed from back-end, no need to keep a copy of it in the UI code either. Further cleanup will be possible, once the `...runs/${runId}/vulnerabilities` endpoint also returns the overall vulnerability rating directly instead of calculating it in front-end. Signed-off-by: Jyrki Keisala --- ui/src/helpers/calc-overall-vulnerability.ts | 6 +- ui/src/helpers/get-status-class.ts | 9 +- ui/src/helpers/item-counts.ts | 163 ------------------- ui/src/helpers/sorting-functions.ts | 7 +- 4 files changed, 10 insertions(+), 175 deletions(-) delete mode 100644 ui/src/helpers/item-counts.ts diff --git a/ui/src/helpers/calc-overall-vulnerability.ts b/ui/src/helpers/calc-overall-vulnerability.ts index b5a25805f..7df1f7d58 100644 --- a/ui/src/helpers/calc-overall-vulnerability.ts +++ b/ui/src/helpers/calc-overall-vulnerability.ts @@ -17,10 +17,8 @@ * License-Filename: LICENSE */ -import { - VulnerabilityRating, - vulnerabilityRatings, -} from '@/helpers/get-status-class'; +import { VulnerabilityRating } from '@/api/requests'; +import { vulnerabilityRatings } from '@/helpers/get-status-class'; /** * Calculate the overall vulnerability rating based on the individual ratings. diff --git a/ui/src/helpers/get-status-class.ts b/ui/src/helpers/get-status-class.ts index cf130825b..6f37c961c 100644 --- a/ui/src/helpers/get-status-class.ts +++ b/ui/src/helpers/get-status-class.ts @@ -17,7 +17,12 @@ * License-Filename: LICENSE */ -import { JobStatus, OrtRunStatus, Severity } from '@/api/requests'; +import { + JobStatus, + OrtRunStatus, + Severity, + VulnerabilityRating, +} from '@/api/requests'; import { PackageManager } from '@/routes/organizations/$orgId/products/$productId/repositories/$repoId/-types'; // Combine statuses reported either by ORT Runs or the individual jobs within them. @@ -32,8 +37,6 @@ export const vulnerabilityRatings = { NONE: 0, } as const; -export type VulnerabilityRating = keyof typeof vulnerabilityRatings; - // TailwindCSS classes matching to statuses // // Note: all color classes need to be defined as they are here, as they diff --git a/ui/src/helpers/item-counts.ts b/ui/src/helpers/item-counts.ts deleted file mode 100644 index 6ee7149a9..000000000 --- a/ui/src/helpers/item-counts.ts +++ /dev/null @@ -1,163 +0,0 @@ -/* - * Copyright (C) 2024 The ORT Server Authors (See ) - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * SPDX-License-Identifier: Apache-2.0 - * License-Filename: LICENSE - */ - -import { - Issue, - Package, - RuleViolation, - Severity, - VulnerabilityWithIdentifier, -} from '@/api/requests'; -import { VulnerabilityWithRepositoryCount } from '@/hooks/use-vulnerabilities-by-product-suspense'; -import { calcOverallVulnerability } from './calc-overall-vulnerability'; -import { VulnerabilityRating } from './get-status-class'; - -/** - * Calculate the counts of vulnerabilities by their overall rating. - * - * @param vulnerabilities - * @returns Vulnerability counts sorted in decreasing order of rating. - */ -export const calcVulnerabilityRatingCounts = ( - vulnerabilities: - | VulnerabilityWithRepositoryCount[] - | VulnerabilityWithIdentifier[] -): { rating: VulnerabilityRating; count: number }[] => { - let criticalCount = 0; - let highCount = 0; - let mediumCount = 0; - let lowCount = 0; - let noneCount = 0; - for (const vulnerability of vulnerabilities) { - const ratings = vulnerability.vulnerability.references.map( - (reference) => reference.severity - ); - const overallRating = calcOverallVulnerability(ratings); - switch (overallRating) { - case 'CRITICAL': - criticalCount++; - break; - case 'HIGH': - highCount++; - break; - case 'MEDIUM': - mediumCount++; - break; - case 'LOW': - lowCount++; - break; - case 'NONE': - noneCount++; - break; - } - } - return [ - { rating: 'CRITICAL', count: criticalCount }, - { rating: 'HIGH', count: highCount }, - { rating: 'MEDIUM', count: mediumCount }, - { rating: 'LOW', count: lowCount }, - { rating: 'NONE', count: noneCount }, - ]; -}; - -/** - * Calculate the counts of issues by their severity. - * - * @param issues - * @returns Issue counts sorted in decreasing order of severity. - */ -export const calcIssueSeverityCounts = ( - issues: Issue[] -): { severity: Severity; count: number }[] => { - let errorCount = 0; - let warningCount = 0; - let hintCount = 0; - for (const issue of issues) { - switch (issue.severity) { - case 'ERROR': - errorCount++; - break; - case 'WARNING': - warningCount++; - break; - case 'HINT': - hintCount++; - break; - } - } - return [ - { severity: 'ERROR', count: errorCount }, - { severity: 'WARNING', count: warningCount }, - { severity: 'HINT', count: hintCount }, - ]; -}; - -/** - * Calculate the counts of rule violations by their severity. - * - * @param violations - * @returns Rule violation counts sorted in decreasing order of severity. - */ -export const calcRuleViolationSeverityCounts = ( - violations: RuleViolation[] -): { severity: Severity; count: number }[] => { - let errorCount = 0; - let warningCount = 0; - let hintCount = 0; - for (const violation of violations) { - switch (violation.severity) { - case 'ERROR': - errorCount++; - break; - case 'WARNING': - warningCount++; - break; - case 'HINT': - hintCount++; - break; - } - } - return [ - { severity: 'ERROR', count: errorCount }, - { severity: 'WARNING', count: warningCount }, - { severity: 'HINT', count: hintCount }, - ]; -}; - -/** - * Calculate the counts of packages by their ecosystem. - * - * @param packages - * @returns Package counts sorted by ecosystem. - */ -export const calcPackageEcosystemCounts = ( - packages: Package[] -): { ecosystem: string; count: number }[] => { - const ecosystemCounts = new Map(); - for (const pkg of packages) { - const ecosystem = pkg.identifier.type; - ecosystemCounts.set(ecosystem, (ecosystemCounts.get(ecosystem) || 0) + 1); - } - return Array.from(ecosystemCounts.entries()) - .map(([ecosystem, count]) => ({ - ecosystem: ecosystem, - count, - })) - .sort((a, b) => a.ecosystem.localeCompare(b.ecosystem)); -}; diff --git a/ui/src/helpers/sorting-functions.ts b/ui/src/helpers/sorting-functions.ts index 0a48bcb9e..477df5fde 100644 --- a/ui/src/helpers/sorting-functions.ts +++ b/ui/src/helpers/sorting-functions.ts @@ -17,11 +17,8 @@ * License-Filename: LICENSE */ -import { Severity } from '@/api/requests'; -import { - VulnerabilityRating, - vulnerabilityRatings, -} from '@/helpers/get-status-class'; +import { Severity, VulnerabilityRating } from '@/api/requests'; +import { vulnerabilityRatings } from '@/helpers/get-status-class'; /** * Compare two severities by their severity level. The severity levels are defined as follows: